Skip to content

Instantly share code, notes, and snippets.

@trogau

trogau/index.php

Last active Sep 23, 2020
Embed
What would you like to do?
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