Skip to content

Instantly share code, notes, and snippets.

@ithoq
Forked from MrXermon/netbox-graph.php
Created September 3, 2023 15:50
Show Gist options
  • Save ithoq/2636337d1e522f09184b94fa5d9880ad to your computer and use it in GitHub Desktop.
Save ithoq/2636337d1e522f09184b94fa5d9880ad to your computer and use it in GitHub Desktop.
Display interface graphs in Netbox fetched from LibreNMS.
<?php
/*
* Display LibreNMS interface graphs in Netbox
*
* 1. Upload this file into the plugin folder of LibreNMS.
* 2. Generate an API key in LibreNMS and insert the connection details below.
* 3. Create graphs in Netbox using the following URL scheme.
* https://nms.level66.network/plugins/netbox-graph.php?device={{ obj.device.name }}&interface={{ obj.name }}&duration=8h
* 4. Save time in your daily work!
*
*/
$lnms = Array(
'token' => '',
'url' => 'https://nms.level66.network/api/v0',
);
/*
* Set Content-Type to match image type.
*
*/
header('Content-Type: image/png');
/*
* Check if all variables are set.
*
*/
if(isset($_GET['device'], $_GET['interface'], $_GET['duration'])){
/*
* Calculate beginning of the graph.
*/
switch($_GET['duration']){
case '8h':
$from = time() - 28800;
break;
case '12h':
$from = time() - 43200;
break;
case '24h':
$from = time() - 86400;
break;
case '1w':
$from = time() - 604800;
break;
default:
$from = time() - 28800;
}
/*
* Build URL to fetch data from LibreNMS.
*
*/
$_GET['device'] = str_replace(Array(':1', ':2', ':3', ':4', ':5', ':6', ':7', ':8'), '', $_GET['device']);
$url = $lnms['url'] . '/devices/' . urlencode($_GET['device']) . '/ports/' . urlencode($_GET['interface']) . '/port_bits?width=780&height=200&from=' . $from;
/*
* Fetch data using LibreNMS api key.
*
*/
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, Array('X-Auth-Token: ' . $lnms['token']));
$response = curl_exec($curl);
/*
* Validate response and show graph.
*
*/
if(curl_getinfo($curl, CURLINFO_HTTP_CODE) == 200){
print($response);
}
/*
* Cleanup stuff.
*
*/
unset($url, $response, $curl, $lnms, $from);
curl_close($curl);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment