Created
July 8, 2014 20:43
-
-
Save foertel/02418192b501c05dfa1a to your computer and use it in GitHub Desktop.
Hacky PHP Script to push alfred data to graphite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
function run() { | |
$macMapping = array( | |
'ed:de:' => 'Swartz', | |
'99:81:' => 'Swartz', | |
); | |
exec('sudo alfred-json -r 158', $alfredJson); | |
$alfredJson = implode($alfredJson); | |
$data = json_decode($alfredJson); | |
foreach ($data as $routerMac => $routerData) { | |
$hostname = str_replace(array('.', ' '), '_', $routerData->hostname); | |
$macMapping[substr($routerMac, 3, 6)] = $hostname; | |
sendToGraphite('routers.' . $hostname . '.load.avg', $routerData->statistics->loadavg); | |
sendToGraphite('routers.' . $hostname . '.traffic.tx', ($routerData->statistics->traffic->tx->bytes + $routerData->statistics->traffic->mgmt_tx->bytes) * 8); | |
sendToGraphite('routers.' . $hostname . '.traffic.rx', ($routerData->statistics->traffic->rx->bytes + $routerData->statistics->traffic->mgmt_rx->bytes) * 8); | |
sendToGraphite('routers.' . $hostname . '.traffic.dropped', $routerData->statistics->traffic->tx->dropped); | |
} | |
exec('sudo batadv-vis -f jsondoc', $batmanJson); | |
$data = json_decode(implode($batmanJson)); | |
foreach ($data->vis as $clientCountData) { | |
$hostname = $macMapping[substr($clientCountData->primary, 3, 6)]; | |
sendToGraphite('routers.' . $hostname . '.clients.count', count($clientCountData->clients)); | |
} | |
} | |
function sendToGraphite($key, $value) { | |
$socketHandler = fsockopen('127.0.0.1', 2003); | |
fwrite($socketHandler, $key . ' ' . $value . ' ' . time() . PHP_EOL); | |
var_dump('wrote: ' . $key . ' ' . $value . ' ' . time()); | |
} | |
while (TRUE) { | |
run(); | |
echo date('Y-m-d H:i:s') . PHP_EOL; | |
sleep(59); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment