Skip to content

Instantly share code, notes, and snippets.

@jrha
Last active November 17, 2023 12:43
Show Gist options
  • Save jrha/1960cefcf6bcf623ee5ae327dd5fe0bb to your computer and use it in GitHub Desktop.
Save jrha/1960cefcf6bcf623ee5ae327dd5fe0bb to your computer and use it in GitHub Desktop.
wLCG Network Monitoring for LibreNMS
// Add the following structure to librenms/config.php
// Array of arrays of LibreNMS port IDs per sub-site
$config['wlcg']['site_name'] = 'EXAMPLE';
$config['wlcg']['sub_sites'] = Array(
"EXAMPLE-LEGACY" => Array(
1234,
1235,
),
"EXAMPLE-LHCOPN" => Array(
42112,
42113,
),
);
#!/usr/bin/env php
<?php
// Script for LibreNMS to provide wLCG network monitoring in JSON format
// Place this file in librenms/scripts/
use Illuminate\Support\Str;
use LibreNMS\Config;
use LibreNMS\Util\Debug;
$install_dir = realpath(__DIR__ . '/..');
chdir($install_dir);
$init_modules = [];
require $install_dir . '/includes/init.php';
$options = getopt('', ['help']);
function print_help() {
echo "\n";
}
if (isset($options['help'])) {
print_help();
exit(0);
}
// Returns the timestamp of the poller with the most recent update
function get_poller_last_update() {
$pollers = dbFetchRows(
"select last_polled ".
"from pollers ".
"order by last_polled desc ".
"limit 1"
);
return $pollers[0]["last_polled"];
}
// Returns the highest frequency of a poller that is actually monitoring devices
function get_poller_frequency() {
$pollers = dbFetchRows(
"select frequency ".
"from poller_cluster_stats ".
"where poller_type = 'poller' ".
"and devices > 0 ".
"order by frequency asc ".
"limit 1"
);
return $pollers[0]["frequency"];
}
// Given an array of ports and a site name, returns an array of data in wLCG networking monitoring format
function get_port_info($site_ports, $site_name) {
$ports = dbFetchRows(
"select concat(substring_index(sysName, '.', 1), '_', ifName) as name, ifInOctets_rate, ifOutOctets_rate ".
"from ports ".
"join devices on devices.device_id = ports.device_id ".
"where port_id in (".implode(",", $site_ports).")"
);
$interface_names = Array();
$total_in = 0;
$total_out = 0;
foreach ($ports as $port) {
$interface_names[] = $port["name"];
$total_in += $port["ifInOctets_rate"];
$total_out += $port["ifOutOctets_rate"];
}
$results = Array(
"Description" => "Network statistics for $site_name",
"UpdatedLast" => get_poller_last_update(),
"InBytesPerSec" => $total_in,
"OutBytesPerSec" => $total_out,
"UpdateInterval" => sprintf("%s seconds", get_poller_frequency()),
"MonitoredInterfaces" => $interface_names,
);
return $results;
}
$all_ports = Array();
foreach (\LibreNMS\Config::get('wlcg')['sub_sites'] as $sub_site => $ports) {
file_put_contents("html/reports/$sub_site.json", json_encode(get_port_info($ports, $sub_site)), LOCK_EX);
$all_ports = array_merge($all_ports, $ports);
}
$site_name = \LibreNMS\Config::get('wlcg')['site_name'];
file_put_contents("html/reports/$site_name.json", json_encode(get_port_info($all_ports, $site_name)), LOCK_EX);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment