1<?php
2/*
3 * LibreNMS
4 *
5 * Copyright (c) 2016 Søren Friis Rosiak <sorenrosiak@gmail.com>
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.  Please see LICENSE.txt at the top level of
10 * the source code distribution for details.
11 */
12
13$temp = snmp_get($device, 'upsAdvBatteryReplaceIndicator.0', '-Ovqe', 'PowerNet-MIB');
14$cur_oid = '.1.3.6.1.4.1.318.1.1.1.2.2.4.0';
15$index = '0';
16
17if (is_numeric($temp)) {
18    //Create State Index
19    $state_name = 'upsAdvBatteryReplaceIndicator';
20    $states = [
21        ['value' => 1, 'generic' => 0, 'graph' => 0, 'descr' => 'noBatteryNeedsReplacing'],
22        ['value' => 2, 'generic' => 2, 'graph' => 0, 'descr' => 'batteryNeedsReplacing'],
23    ];
24    create_state_index($state_name, $states);
25
26    $descr = 'UPS Battery Replacement Status';
27    //Discover Sensors
28    discover_sensor($valid['sensor'], 'state', $device, $cur_oid, $index, $state_name, $descr, 1, 1, null, null, null, null, $temp, 'snmp', $index);
29
30    //Create Sensor To State Index
31    create_sensor_to_state_index($device, $state_name, $index);
32}
33
34$cooling_status = snmpwalk_cache_oid($device, 'coolingUnitStatusDiscreteEntry', [], 'PowerNet-MIB');
35foreach ($cooling_status as $index => $data) {
36    $cur_oid = '.1.3.6.1.4.1.318.1.1.27.1.4.2.2.1.4.' . $index;
37    $state_name = $data['coolingUnitStatusDiscreteDescription'];
38
39    $tmp_states = explode(',', $data['coolingUnitStatusDiscreteIntegerReferenceKey']);
40    $states = [];
41    foreach ($tmp_states as $k => $ref) {
42        preg_match('/([\w]+)\\(([\d]+)\\)/', $ref, $matches);
43        $nagios_state = get_nagios_state($matches[1]);
44        $states[] = ['value' => 0, 'generic' => $nagios_state, 'graph' => 0, $matches[2], 'descr' => $matches[1]];
45    }
46    create_state_index($state_name, $states);
47
48    discover_sensor($valid['sensor'], 'state', $device, $cur_oid, $cur_oid, 'apc', $state_name, 1, 1, null, null, null, null, $data['coolingUnitStatusDiscreteValueAsInteger']);
49    create_sensor_to_state_index($device, $state_name, $index);
50}
51
52unset($cooling_status);
53
54$cooling_unit = snmpwalk_cache_oid($device, 'coolingUnitExtendedDiscreteEntry', [], 'PowerNet-MIB');
55foreach ($cooling_unit as $index => $data) {
56    $cur_oid = '.1.3.6.1.4.1.318.1.1.27.1.6.2.2.1.4.' . $index;
57    $state_name = $data['coolingUnitExtendedDiscreteDescription'];
58
59    $tmp_states = explode(',', $data['coolingUnitExtendedDiscreteIntegerReferenceKey']);
60    $states = [];
61    foreach ($tmp_states as $k => $ref) {
62        preg_match('/([\w]+)\\(([\d]+)\\)/', $ref, $matches);
63        $nagios_state = get_nagios_state($matches[1]);
64        $states[] = ['value' => 0, 'generic' => $nagios_state, 'graph' => 0, $matches[2], 'descr' => $matches[1]];
65    }
66    create_state_index($state_name, $states);
67
68    discover_sensor($valid['sensor'], 'state', $device, $cur_oid, $cur_oid, 'apc', $state_name, 1, 1, null, null, null, null, $data['coolingUnitExtendedDiscreteValueAsInteger']);
69    create_sensor_to_state_index($device, $state_name, $index);
70}
71
72unset($cooling_unit);
73
74$relays = snmpwalk_cache_oid($device, 'emsOutputRelayControlEntry', [], 'PowerNet-MIB');
75foreach ($relays as $index => $data) {
76    $cur_oid = '.1.3.6.1.4.1.318.1.1.10.3.2.1.1.3.' . $index;
77    $state_name = $data['emsOutputRelayControlOutputRelayName'];
78    $states = [
79        ['value' => 1, 'generic' => 2, 'graph' => 0, 'descr' => 'immediateCloseEMS'],
80        ['value' => 2, 'generic' => 0, 'graph' => 0, 'descr' => 'immediateOpenEMS'],
81    ];
82    create_state_index($state_name, $states);
83
84    $current = apc_relay_state($data['emsOutputRelayControlOutputRelayCommand']);
85    if (is_numeric($current)) {
86        discover_sensor($valid['sensor'], 'state', $device, $cur_oid, $cur_oid, $state_name, $state_name, 1, 1, null, null, null, null, $current);
87        create_sensor_to_state_index($device, $state_name, $cur_oid);
88    }
89}
90unset(
91    $relays,
92    $index,
93    $data
94);
95
96$switched = snmpwalk_cache_oid($device, 'emsOutletControlEntry', [], 'PowerNet-MIB');
97foreach ($switched as $index => $data) {
98    $cur_oid = '.1.3.6.1.4.1.318.1.1.10.3.3.1.1.3.' . $index;
99    $state_name = $data['emsOutletControlOutletName'];
100    $states = [
101        ['value' => 1, 'generic' => 2, 'graph' => 0, 'descr' => 'immediateOnEMS'],
102        ['value' => 2, 'generic' => 0, 'graph' => 0, 'descr' => 'immediateOffEMS'],
103    ];
104    create_state_index($state_name, $states);
105
106    $current = apc_relay_state($data['emsOutletControlOutletCommand']);
107    if (is_numeric($current)) {
108        discover_sensor($valid['sensor'], 'state', $device, $cur_oid, $cur_oid, $state_name, $state_name, 1, 1, null, null, null, null, $current);
109        create_sensor_to_state_index($device, $state_name, $cur_oid);
110    }
111}
112unset(
113    $switched,
114    $index,
115    $data
116);
117
118foreach ($pre_cache['mem_sensors_status'] as $index => $data) {
119    if ($data['memSensorsCommStatus']) {
120        $cur_oid = '.1.3.6.1.4.1.318.1.1.10.4.2.3.1.7.' . $index;
121        $state_name = 'memSensorsCommStatus';
122        $states = [
123            ['value' => 1, 'generic' => 1, 'graph' => 0, 'descr' => 'notInstalled'],
124            ['value' => 2, 'generic' => 0, 'graph' => 0, 'descr' => 'commsOK'],
125            ['value' => 3, 'generic' => 2, 'graph' => 0, 'descr' => 'commsLost'],
126        ];
127        create_state_index($state_name, $states);
128
129        $current = $data['memSensorsCommStatus'];
130    }
131    $descr = $data['memSensorsStatusSensorName'] . ' - ' . $data['memSensorsStatusSensorLocation'];
132    $divisor = 1;
133    $multiplier = 1;
134    if (is_numeric($current)) {
135        discover_sensor($valid['sensor'], 'state', $device, $cur_oid, $state_name . '.' . $index, $state_name, $state_name, 1, 1, null, null, null, null, $current);
136        create_sensor_to_state_index($device, $state_name, $state_name . '.' . $index);
137    }
138
139    if ($data['memSensorsAlarmStatus']) {
140        $cur_oid = '.1.3.6.1.4.1.318.1.1.10.4.2.3.1.8.' . $index;
141        $state_name = 'memSensorsAlarmStatus';
142        $states = [
143            ['value' => 1, 'generic' => 0, 'graph' => 0, 'descr' => 'memNormal'],
144            ['value' => 2, 'generic' => 1, 'graph' => 0, 'descr' => 'memWarning'],
145            ['value' => 3, 'generic' => 2, 'graph' => 0, 'descr' => 'memCritical'],
146        ];
147        create_state_index($state_name, $states);
148
149        $current = $data['memSensorsAlarmStatus'];
150    }
151    $descr = $data['memSensorsStatusSensorName'] . ' - ' . $data['memSensorsStatusSensorLocation'];
152    $divisor = 1;
153    $multiplier = 1;
154    if (is_numeric($current)) {
155        discover_sensor($valid['sensor'], 'state', $device, $cur_oid, $state_name . '.' . $index, $state_name, $state_name, 1, 1, null, null, null, null, $current);
156        create_sensor_to_state_index($device, $state_name, $state_name . '.' . $index);
157    }
158}
159
160// Monitor contact switches via the UIO ports.
161$apcContactData = snmpwalk_cache_oid($device, 'iemConfigContactsTable', [], 'PowerNet-MIB', null, '-OQUse');
162$apcContactData = snmpwalk_cache_oid($device, 'iemStatusContactsTable', $apcContactData, 'PowerNet-MIB', null, '-OQUse');
163
164foreach (array_keys($apcContactData) as $index) {
165    // APC disabled (1), enabled (2)
166    if ($apcContactData[$index]['iemConfigContactEnable'] == 2) {
167        $current = $apcContactData[$index]['iemStatusContactStatus'];
168        $sensorType = 'apc';
169        $cur_oid = '.1.3.6.1.4.1.318.1.1.10.2.3.4.1.3.' . $index;
170        $severity = $apcContactData[$index]['iemConfigContactSeverity'];
171
172        // APC critical (1), warning (2)
173        // LibreNMS warning (1), critical (2)
174        $faultGeneric = 1;
175        if ($severity == 1) {
176            $faultGeneric = 2;
177        } elseif ($severity == 2) {
178            $faultGeneric = 1;
179        }
180
181        $state_name = $apcContactData[$index]['iemConfigContactName'];
182        $states = [
183            ['value' => 1, 'generic' => 0, 'graph' => 0, 'descr' => 'noFault'],
184            ['value' => 2, 'generic' => $faultGeneric, 'graph' => 1, 'descr' => 'fault'],
185            ['value' => 3, 'generic' => 0, 'graph' => 0, 'descr' => 'disabled'],
186        ];
187        create_state_index($state_name, $states);
188
189        discover_sensor($valid['sensor'], 'state', $device, $cur_oid, $state_name . '.' . $index, $state_name, $state_name, 1, 1, null, null, null, null, $current);
190        create_sensor_to_state_index($device, $state_name, $state_name . '.' . $index);
191    }
192}
193