From 98f75bd9ceecfbf0b02af36cc33be21198d55ee8 Mon Sep 17 00:00:00 2001 From: Mike Centola Date: Thu, 10 Jan 2019 00:36:37 -0500 Subject: [PATCH] Modify GPSD Application to support SNMP Extend. Update Docs (#9625) * Modify GPSD Application to support SNMP Extend. Update Docs * Fixed namespace and removed comments (#9625) --- doc/Extensions/Applications.md | 18 ++- includes/polling/applications/gpsd.inc.php | 132 +++++++++++++++++---- 2 files changed, 123 insertions(+), 27 deletions(-) mode change 100644 => 100755 includes/polling/applications/gpsd.inc.php diff --git a/doc/Extensions/Applications.md b/doc/Extensions/Applications.md index 8698213702..52c1c5be9a 100644 --- a/doc/Extensions/Applications.md +++ b/doc/Extensions/Applications.md @@ -48,7 +48,7 @@ The unix-agent does not have a discovery module, only a poller module. That poll 1. [FreeBSD NFS Server](#freebsd-nfs-server) - SNMP extend 1. [FreeRADIUS](#freeradius) - SNMP extend, Agent 1. [Freeswitch](#freeswitch) - SNMP extend, Agent -1. [GPSD](#gpsd) - Agent +1. [GPSD](#gpsd) - SNMP extend, Agent 1. [Mailscanner](#mailscanner) - SNMP extend 1. [Memcached](#memcached) - SNMP extend 1. [Munin](#munin) - Agent @@ -464,7 +464,21 @@ extend freeswitch /etc/snmp/freeswitch The application should be auto-discovered as described at the top of the page. If it is not, please follow the steps set out under `SNMP Extend` heading top of page. ### GPSD -A small shell script that reports GPSD status. + +##### SNMP Extend +1. Download the script onto the desired host. +``` +wget https://raw.githubusercontent.com/librenms/librenms-agent/master/snmp/gpsd -O /etc/snmp/gpsd +``` +2. Run `chmod +x /etc/snmp/gpsd` + +3. Edit your snmpd.conf file (usually `/etc/snmp/snmpd.conf`) and add: +``` +extend gpsd /etc/snmp/gpsd +``` +4. Restart snmpd on your host + +The application should be auto-discovered as described at the top of the page. If it is not, please follow the steps set out under `SNMP Extend` heading at the top of the page. ##### Agent [Install the agent](Agent-Setup.md) on this device if it isn't already and copy the `gpsd` script to `/usr/lib/check_mk_agent/local/` diff --git a/includes/polling/applications/gpsd.inc.php b/includes/polling/applications/gpsd.inc.php old mode 100644 new mode 100755 index b16dd5d5ee..cfa7828425 --- a/includes/polling/applications/gpsd.inc.php +++ b/includes/polling/applications/gpsd.inc.php @@ -24,20 +24,112 @@ * @subpackage Polling */ + /* + * GPSD Statistics + * @author Mike Centola + * @copyright 2019 Mike Centola, LibreNMS + * @license GPL + * @package LibreNMS + * @subpackage Polling + */ + + // TODO: Add Metrics for Lat/Long/Altitude (Additions commented out) + + /* Example Agent Data + hdop:X.XX + vdop:X.XX + satellites:XX + satellites_used:XX + */ + + /* + Example SNMP Extend Data + { + "data": { + "mode": "X", + "hdop": "X.XX", + "vdop": "X.XX", + "latitude": "XX.XXXXXXXX", + "longitude": "XX.XXXXXXXXX", + "altitude": "XXX.X", + "satellites": "XX", + "satellites_used": "XX" + }, + "error:"0", "errorString":"", "version":"X.XX-X" +} + */ + +use LibreNMS\Exceptions\JsonAppParsingFailedException; +use LibreNMS\Exceptions\JsonAppException; use LibreNMS\RRD\RrdDefinition; $name = 'gpsd'; $app_id = $app['app_id']; -if (!empty($agent_data['app'][$name]) && $app_id > 0) { - echo ' '.$name; - $gpsd = $agent_data['app'][$name]; - $gpsd_parsed = array(); - foreach (explode("\n", $gpsd) as $line) { - list ($field, $data) = explode(':', $line); - $gpsd_parsed[$field] = $data; +echo " $name\n"; + +if ($app_id > 0) { + if (!empty($agent_data['app'][$name])) { + $gpsd = $agent_data['app'][$name]; + + $gpsd_parsed = array(); + + foreach (explode("\n", $gpsd) as $line) { + list ($field, $data) = explode(':', $line); + $gpsd_parsed[$field] = $data; + } + + // Set Fields + + $check_fields = array( + 'mode', + 'hdop', + 'vdop', + 'satellites', + 'satellites_used', + ); + + $fields = array(); + + foreach ($check_fields as $field) { + if (!empty($gpsd_parsed[$field])) { + $fields[$field] = $gpsd_parsed[$field]; + } + } + } else { + // Use json_app_get to grab JSON formatted GPSD data + try { + $gpsd = json_app_get($device, $name); + } catch (JsonAppParsingFailedException $e) { + $legacy = $e->getOutput(); + + $gpsd = array( + data => array(), + ); + + list ($gpsd['data']['mode'], $gpsd['data']['hdop'], $gpsd['data']['vdop'], + $gpsd['data']['latitude'], $gpsd['data']['longitude'], $gpsd['data']['altitude'], + $gpsd['data']['satellites'], $gpsd['data']['satellites_used']) = explode("\n", $legacy); + } catch (JsonAppException $e) { + // Set Empty metrics and error message + + echo PHP_EOL . $name . ':' . $e->getCode() . ':' . $e->getMessage() . PHP_EOL; + update_application($app, $e->getCode() . ':' . $e->getMessage(), []); + return; + } + + // Set Fields + $fields = array( + 'mode' => $gpsd['data']['mode'], + 'hdop' => $gpsd['data']['hdop'], + 'vdop' => $gpsd['data']['vdop'], + 'satellites' => $gpsd['data']['satellites'], + 'satellites_used' => $gpsd['data']['satellites_used'], + ); } + // Generate RRD Def + $rrd_name = array('app', $name, $app_id); $rrd_def = RrdDefinition::make() ->addDataset('mode', 'GAUGE', 0, 4) @@ -45,24 +137,14 @@ if (!empty($agent_data['app'][$name]) && $app_id > 0) { ->addDataset('vdop', 'GAUGE', 0, 100) ->addDataset('satellites', 'GAUGE', 0, 40) ->addDataset('satellites_used', 'GAUGE', 0, 40); - - $check_fields = array( - 'mode', - 'hdop', - 'vdop', - 'satellites', - 'satellites_used', - ); - - $fields = array(); - - foreach ($check_fields as $field) { - if (!empty($gpsd_parsed[$field])) { - $fields[$field] = $gpsd_parsed[$field]; - } - } - + + // Update Application $tags = compact('name', 'app_id', 'rrd_name', 'rrd_def'); data_update($device, 'app', $tags, $fields); - update_application($app, $gpsd, $fields); + + if (!empty($agent_data['app'][$name])) { + update_application($app, $gpsd, $fields); + } else { + update_application($app, 'OK', $fields); + } }