Skip to content

Instantly share code, notes, and snippets.

@trogau
Last active September 23, 2020 03:30
Show Gist options
  • Save trogau/f5647818a86e54106333c0ad9cfc0cb5 to your computer and use it in GitHub Desktop.
Save trogau/f5647818a86e54106333c0ad9cfc0cb5 to your computer and use it in GitHub Desktop.
Some scripts for BusyBox to parse out network interface stats and send them to a simple PHP setup for collection & use with Prometheus.
<?php
header("Content-Type: text/plain");
$data = file("data.txt");
$bytesRx = $data[0];
$bytesTx = $data[1];
?># TYPE modem_stats_bytesrx counter
modem_stats_bytesrx <?= $bytesRx ?>
# TYPE modem_stats_bytestx counter
modem_stats_bytestx <?= $bytesTx ?>
while true; do curl -k "https://example.com/stats/update.php?bytesRx=`cat /sys/class/net/eth0/statistics/rx_bytes | tr -d '\n'`&bytesTx=`cat /sys/class/net/eth0/statistics/tx_bytes | tr -d '\n'`"; sleep 10; done
<?php
// Accepts input like: example.com/update.php?bytesRx=10481489&bytesTx=51757
$bytesTx = intval($_GET['bytesTx']);
$bytesRx = intval($_GET['bytesRx']);
// Display the numbers so the calling instance can see if it's working
print "Got RX: ".number_format($bytesRx)." / Got TX: ".number_format($bytesTx)."\n";
// DANGER: requires the web server user can write to this file.
$fp = fopen("data.txt", "w");
fputs($fp, "$bytesRx\n$bytesTx");
fclose($fp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment