Skip to content

Instantly share code, notes, and snippets.

@MrXermon
Last active September 3, 2023 15:50
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save MrXermon/0b40d7b62bc67083529d01c8a33aa8be to your computer and use it in GitHub Desktop.
Save MrXermon/0b40d7b62bc67083529d01c8a33aa8be 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);
}
?>
@sthierolf
Copy link

Based on your PHP script, I created a similar one in Python.
If interested: netbox-librenms-graph.py

@MrXermon
Copy link
Author

Cool thing! I‘m actually not using the script anymore but hosting it in Python on the same server makes the whole thing more easier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment