Side Projects:

Facedetection
Source Magic
Intrusion Detection System
Issue Tracker
Searchengine
RemindMe
Open API

Impressum
Andreas Beder 2012


Realtime bandwidth meter with php and jquery

 

Download

SourceCode of data.php

Code

<?
        
    $int
="eth0";
    
session_start();
    
    
$rx[] = @file_get_contents("/sys/class/net/$int/statistics/rx_bytes");
    
$tx[] = @file_get_contents("/sys/class/net/$int/statistics/tx_bytes");
    
sleep(1);
    
$rx[] = @file_get_contents("/sys/class/net/$int/statistics/rx_bytes");
    
$tx[] = @file_get_contents("/sys/class/net/$int/statistics/tx_bytes");
    
    
$tbps $tx[1] - $tx[0];
    
$rbps $rx[1] - $rx[0];
    
    
$round_rx=round($rbps/10242);
    
$round_tx=round($tbps/10242);
    
    
$time=date("U")."000";
    
$_SESSION['rx'][] = "[$time$round_rx]";
    
$_SESSION['tx'][] = "[$time$round_tx]";
    
$data['label'] = $int;
    
$data['data'] = $_SESSION['rx'];
    
# to make sure that the graph shows only the
    # last minute (saves some bandwitch to)
    
if (count($_SESSION['rx'])>60)
    {
    
    $x min(array_keys($_SESSION['rx']));
    
    unset($_SESSION['rx'][$x]);
    }
    
    
# json_encode didnt work, if you found a workarround pls write me
    # echo json_encode($data, JSON_FORCE_OBJECT);
    
    
echo '
    {"label":"'
.$int.'","data":['.implode($_SESSION['rx'], ",").']}
    '
;
    
?>

Source of index.php

Code

<?php
    session_start
();
    
session_destroy();
    
session_start();
    
?>
    <html>
        <head>
            <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
            <script type="text/javascript" src="js/jquery.flot.js"></script>
            
            <script id="source" language="javascript" type="text/javascript">
            $(document).ready(function() {
                var options = {
                    lines: { show: true },
                    points: { show: true },
                    xaxis: { mode: "time" }
                };
                var data = [];
                var placeholder = $("#placeholder");
    
                $.plot(placeholder, data, options);
    
                var iteration = 0;
    
                function fetchData() {
                    ++iteration;
    
                    function onDataReceived(series) {
                        // we get all the data in one go, if we only got partial
                        // data, we could merge it with what we already got
                        data = [ series ];
                        
                        $.plot($("#placeholder"), data, options);
                        fetchData();
                    }
    
                    $.ajax({
                        url: "data.php",
                        method: 'GET',
                        dataType: 'json',
                        success: onDataReceived
                    });
                    
                }
    
                setTimeout(fetchData, 1000);
            });
    
        </script>
        </head>
        <body>
        <div id="placeholder" style="width:600px;height:300px;"></div>
        </body>
    </html>



Name
nfo
Unfortunately most vds provider don t allow you to change the permissions outside you document root.
You can ask your ISP if he would be so kind to give you read permissions on the specific files.
/sys/class/net/eth0/statistics/rx_byte
/sys/class/net/eth0/statistics/tx_byte

good luck anyway

andreas

Name
dynamind
how can I set this permissions, I have a VDS?

Name
nfo
maybe your webserver (www-data or nobody) have no permissions to read /sys/class/net/eth0/statistics/rx_byte ?!

Name
dynamind
looks great on your webserver but keeps blank on mine, which options should be enabled to get the data from eth0?

Name
micha
danke für die info.
ich werds mir speichern, wer weiß wofür ich es mal gebrauchen kann, jedenfalls gefällt mir die animation.

like always, nice work!

Name
andreas
Hi Micha,

das file_get_contents liesst an sich nur den inhalt einer datei ...
in dem fall:
/sys/class/net/$int/statistics/rx_bytes

das ganze funktioniert d.h. nicht unter windows sondern nur auf linux/unix systemen...

Name
micha
hi andreas,

kannst du mir auch verraten was es mit file_get_contents() auf sich hat? denn bei mir tut sich nichts.

grüße ausm siegerland^^