diff --git a/doc/Extensions/Applications.md b/doc/Extensions/Applications.md index 0f266eadc8..8698213702 100644 --- a/doc/Extensions/Applications.md +++ b/doc/Extensions/Applications.md @@ -1116,13 +1116,20 @@ The application should be auto-discovered as described at the top of the page. I A small shell script that exports apcacess ups status. ##### SNMP Extend -1. Copy the [ups apcups](https://github.com/librenms/librenms-agent/blob/master/snmp/ups-apcups.sh) to `/etc/snmp/` on your host. +1. Copy the [ups apcups](https://github.com/librenms/librenms-agent/blob/master/snmp/ups-apcups to `/etc/snmp/` on your host. -2. Run `chmod +x /etc/snmp/ups-apcups.sh` +2. Run `chmod +x /etc/snmp/ups-apcups` 3. Edit your snmpd.conf file (usually /etc/snmp/snmpd.conf) and add: ``` -extend ups-apcups /etc/snmp/ups-apcups.sh +extend ups-apcups /etc/snmp/ups-apcups +``` + +If 'apcaccess' is not in the PATH enviromental variable snmpd is +using, you may need to do something like below. + +``` +extend ups-apcups/usr/bin/env PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin /etc/snmp/ups-apcups ``` 4. Restart snmpd on your host diff --git a/includes/polling/applications/ups-apcups.inc.php b/includes/polling/applications/ups-apcups.inc.php index e127d1bd98..70ce28d624 100644 --- a/includes/polling/applications/ups-apcups.inc.php +++ b/includes/polling/applications/ups-apcups.inc.php @@ -19,17 +19,40 @@ * @author Cercel Valentin */ +use LibreNMS\Exceptions\JsonAppParsingFailedException; +use LibreNMS\Exceptions\JsonAppException; use LibreNMS\RRD\RrdDefinition; //NET-SNMP-EXTEND-MIB::nsExtendOutputFull."ups-apcups" $name = 'ups-apcups'; $app_id = $app['app_id']; -$oid = '.1.3.6.1.4.1.8072.1.3.2.3.1.2.10.117.112.115.45.97.112.99.117.112.115'; -$ups_apcups = trim(snmp_get($device, $oid, '-Oqv'), '"'); echo ' '.$name; -list ($line_volt, $load, $charge, $remaining, $bat_volt, $line_nominal, $bat_nominal) = explode("\n", $ups_apcups); +try { + $json_return=json_app_get($device, $name); +} catch (JsonAppParsingFailedException $e) { + // Legacy script, build compatible array + $legacy = trim($e->getOutput()); + + // pull apart the legacy info and create the basic required hash with it + list ($line_volt, $load, $charge, $remaining, $bat_volt, $line_nominal, $bat_nominal) = explode("\n", $legacy); + $json_return=array( + 'data' => array( + 'charge' => $charge, + 'time_remaining' => $remaining, + 'battery_nominal' => $bat_nominal, + 'battery_voltage' => $bat_volt, + 'input_voltage' => $line_volt, + 'nominal_voltage' => $line_nominal, + 'load' => $load + ) + ); +} catch (JsonAppException $e) { + echo PHP_EOL . $name . ':' .$e->getCode().':'. $e->getMessage() . PHP_EOL; + update_application($app, $e->getCode().':'.$e->getMessage(), []); // Set empty metrics and error message + return; +} $rrd_name = array('app', $name, $app_id); $rrd_def = RrdDefinition::make() @@ -41,16 +64,8 @@ $rrd_def = RrdDefinition::make() ->addDataset('nominal_voltage', 'GAUGE', 0) ->addDataset('load', 'GAUGE', 0, 100); -$fields = array( - 'charge' => $charge, - 'time_remaining' => $remaining, - 'battery_nominal' => $bat_nominal, - 'battery_voltage' => $bat_volt, - 'input_voltage' => $line_volt, - 'nominal_voltage' => $line_nominal, - 'load' => $load -); +$fields = $json_return{'data'}; $tags = compact('name', 'app_id', 'rrd_name', 'rrd_def'); data_update($device, 'app', $tags, $fields); -update_application($app, $ups_apcups, $fields); +update_application($app, 'OK', $fields); diff --git a/tests/data/linux_ups-apcups-legacy.json b/tests/data/linux_ups-apcups-legacy.json new file mode 100644 index 0000000000..fdfe897d6b --- /dev/null +++ b/tests/data/linux_ups-apcups-legacy.json @@ -0,0 +1,73 @@ +{ + "applications": { + "discovery": { + "applications": [ + { + "app_type": "ups-apcups", + "app_state": "UNKNOWN", + "discovered": "1", + "app_state_prev": null, + "app_status": "", + "app_instance": "" + } + ], + "application_metrics": [] + }, + "poller": { + "applications": [ + { + "app_type": "ups-apcups", + "app_state": "OK", + "discovered": "1", + "app_state_prev": "UNKNOWN", + "app_status": "", + "app_instance": "" + } + ], + "application_metrics": [ + { + "metric": "battery_nominal", + "value": "24.0", + "value_prev": null, + "app_type": "ups-apcups" + }, + { + "metric": "battery_voltage", + "value": "27.0", + "value_prev": null, + "app_type": "ups-apcups" + }, + { + "metric": "charge", + "value": "100.0", + "value_prev": null, + "app_type": "ups-apcups" + }, + { + "metric": "input_voltage", + "value": "117.0", + "value_prev": null, + "app_type": "ups-apcups" + }, + { + "metric": "load", + "value": "37.0", + "value_prev": null, + "app_type": "ups-apcups" + }, + { + "metric": "nominal_voltage", + "value": "120", + "value_prev": null, + "app_type": "ups-apcups" + }, + { + "metric": "time_remaining", + "value": "8.1", + "value_prev": null, + "app_type": "ups-apcups" + } + ] + } + } +} diff --git a/tests/data/linux_ups-apcups-v1.json b/tests/data/linux_ups-apcups-v1.json new file mode 100644 index 0000000000..fdfe897d6b --- /dev/null +++ b/tests/data/linux_ups-apcups-v1.json @@ -0,0 +1,73 @@ +{ + "applications": { + "discovery": { + "applications": [ + { + "app_type": "ups-apcups", + "app_state": "UNKNOWN", + "discovered": "1", + "app_state_prev": null, + "app_status": "", + "app_instance": "" + } + ], + "application_metrics": [] + }, + "poller": { + "applications": [ + { + "app_type": "ups-apcups", + "app_state": "OK", + "discovered": "1", + "app_state_prev": "UNKNOWN", + "app_status": "", + "app_instance": "" + } + ], + "application_metrics": [ + { + "metric": "battery_nominal", + "value": "24.0", + "value_prev": null, + "app_type": "ups-apcups" + }, + { + "metric": "battery_voltage", + "value": "27.0", + "value_prev": null, + "app_type": "ups-apcups" + }, + { + "metric": "charge", + "value": "100.0", + "value_prev": null, + "app_type": "ups-apcups" + }, + { + "metric": "input_voltage", + "value": "117.0", + "value_prev": null, + "app_type": "ups-apcups" + }, + { + "metric": "load", + "value": "37.0", + "value_prev": null, + "app_type": "ups-apcups" + }, + { + "metric": "nominal_voltage", + "value": "120", + "value_prev": null, + "app_type": "ups-apcups" + }, + { + "metric": "time_remaining", + "value": "8.1", + "value_prev": null, + "app_type": "ups-apcups" + } + ] + } + } +} diff --git a/tests/snmpsim/linux_ups-apcups-legacy.snmprec b/tests/snmpsim/linux_ups-apcups-legacy.snmprec new file mode 100644 index 0000000000..2ac2755584 --- /dev/null +++ b/tests/snmpsim/linux_ups-apcups-legacy.snmprec @@ -0,0 +1,10 @@ +1.3.6.1.2.1.1.1.0|4|Linux server 3.10.0-693.5.2.el7.x86_64 #1 SMP Fri Oct 20 20:32:50 UTC 2017 x86_64 +1.3.6.1.2.1.1.2.0|6|1.3.6.1.4.1.8072.3.2.10 +1.3.6.1.2.1.1.3.0|67|77550514 +1.3.6.1.2.1.1.4.0|4| +1.3.6.1.2.1.1.5.0|4| +1.3.6.1.2.1.1.6.0|4| +1.3.6.1.2.1.25.1.1.0|67|77552962 +1.3.6.1.4.1.8072.1.3.2.2.1.21.6.100.105.115.116.114.111|2|1 +1.3.6.1.4.1.8072.1.3.2.2.1.21.10.117.112.115.45.97.112.99.117.112.115|2|1 +1.3.6.1.4.1.8072.1.3.2.3.1.2.10.117.112.115.45.97.112.99.117.112.115|4x|3131372e300a33372e300a3130302e300a382e310a32372e300a3132300a32342e300a diff --git a/tests/snmpsim/linux_ups-apcups-v1.snmprec b/tests/snmpsim/linux_ups-apcups-v1.snmprec new file mode 100644 index 0000000000..01b6d1138c --- /dev/null +++ b/tests/snmpsim/linux_ups-apcups-v1.snmprec @@ -0,0 +1,10 @@ +1.3.6.1.2.1.1.1.0|4|Linux server 3.10.0-693.5.2.el7.x86_64 #1 SMP Fri Oct 20 20:32:50 UTC 2017 x86_64 +1.3.6.1.2.1.1.2.0|6|1.3.6.1.4.1.8072.3.2.10 +1.3.6.1.2.1.1.3.0|67|77550514 +1.3.6.1.2.1.1.4.0|4| +1.3.6.1.2.1.1.5.0|4| +1.3.6.1.2.1.1.6.0|4| +1.3.6.1.2.1.25.1.1.0|67|77552962 +1.3.6.1.4.1.8072.1.3.2.2.1.21.6.100.105.115.116.114.111|2|1 +1.3.6.1.4.1.8072.1.3.2.2.1.21.10.117.112.115.45.97.112.99.117.112.115|2|1 +1.3.6.1.4.1.8072.1.3.2.3.1.2.10.117.112.115.45.97.112.99.117.112.115|4x|7b0a202020226572726f72537472696e6722203a20226170636163636573732065786974656420776974682030222c0a202020226572726f7222203a202230222c0a202020226461746122203a207b0a202020202020226e6f6d696e616c5f766f6c7461676522203a2022313230222c0a2020202020202274696d655f72656d61696e696e6722203a2022382e31222c0a2020202020202263686172676522203a20223130302e30222c0a20202020202022626174746572795f6e6f6d696e616c22203a202232342e30222c0a20202020202022626174746572795f766f6c7461676522203a202232372e30222c0a202020202020226c6f616422203a202233372e30222c0a20202020202022696e7075745f766f6c7461676522203a20223131372e30220a2020207d2c0a2020202276657273696f6e22203a20310a7d0a