1 | 1 | simandl | <?php |
2 | | | // Pluggable datasource for PHP Weathermap 0.9 |
3 | | | // - return a live SNMP value |
4 | | | |
5 | | | // doesn't work well with large values like interface counters (I think this is a rounding problem) |
6 | | | // - also it doesn't calculate rates. Just fetches a value. |
7 | | | |
8 | | | // useful for absolute GAUGE-style values like DHCP Lease Counts, Wireless AP Associations, Firewall Sessions |
9 | | | // which you want to use to colour a NODE |
10 | | | |
11 | | | // You could also fetch interface states from IF-MIB with it. |
12 | | | |
13 | | | // TARGET snmp:public:hostname:1.3.6.1.4.1.3711.1.1:1.3.6.1.4.1.3711.1.2 |
14 | | | // (that is, TARGET snmp:community:host:in_oid:out_oid |
15 | | | |
16 | | | class WeatherMapDataSource_snmp extends WeatherMapDataSource { |
17 | | | |
18 | | | function Init(&$map) |
19 | | | { |
20 | 85 | simandl | // We can keep a list of unresponsive nodes, so we can give up earlier |
21 | | | $this->down_cache = array(); |
22 | | | |
23 | 1 | simandl | if(function_exists('snmpget')) { return(TRUE); } |
24 | | | debug("SNMP DS: snmpget() not found. Do you have the PHP SNMP module?\n"); |
25 | 85 | simandl | |
26 | 1 | simandl | return(FALSE); |
27 | | | } |
28 | | | |
29 | | | |
30 | | | function Recognise($targetstring) |
31 | | | { |
32 | | | if(preg_match("/^snmp:([^:]+):([^:]+):([^:]+):([^:]+)$/",$targetstring,$matches)) |
33 | | | { |
34 | | | return TRUE; |
35 | | | } |
36 | | | else |
37 | | | { |
38 | | | return FALSE; |
39 | | | } |
40 | | | } |
41 | | | |
42 | | | function ReadData($targetstring, &$map, &$item) |
43 | | | { |
44 | 85 | simandl | $data[IN] = NULL; |
45 | | | $data[OUT] = NULL; |
46 | | | $data_time = 0; |
47 | 1 | simandl | |
48 | 85 | simandl | $timeout = 1000000; |
49 | | | $retries = 2; |
50 | | | $abort_count = 0; |
51 | | | |
52 | | | $in_result = NULL; |
53 | | | $out_result = NULL; |
54 | | | |
55 | | | if($map->get_hint("snmp_timeout") != '') { |
56 | | | $timeout = intval($map->get_hint("snmp_timeout")); |
57 | | | debug("Timeout changed to ".$timeout." microseconds.\n"); |
58 | | | } |
59 | | | |
60 | | | if($map->get_hint("snmp_abort_count") != '') { |
61 | | | $abort_count = intval($map->get_hint("snmp_abort_count")); |
62 | | | debug("Will abort after $abort_count failures for a given host.\n"); |
63 | | | } |
64 | | | |
65 | | | if($map->get_hint("snmp_retries") != '') { |
66 | | | $retries = intval($map->get_hint("snmp_retries")); |
67 | | | debug("Number of retries changed to ".$retries.".\n"); |
68 | | | } |
69 | | | |
70 | 1 | simandl | if(preg_match("/^snmp:([^:]+):([^:]+):([^:]+):([^:]+)$/",$targetstring,$matches)) |
71 | | | { |
72 | | | $community = $matches[1]; |
73 | | | $host = $matches[2]; |
74 | | | $in_oid = $matches[3]; |
75 | | | $out_oid = $matches[4]; |
76 | | | |
77 | 85 | simandl | if( |
78 | | | ($abort_count == 0) |
79 | | | || ( |
80 | | | ( $abort_count>0 ) |
81 | | | && ( !isset($this->down_cache[$host]) || intval($this->down_cache[$host]) < $abort_count ) |
82 | | | ) |
83 | | | ) |
84 | | | { |
85 | | | if(function_exists("snmp_get_quick_print")) |
86 | | | { |
87 | | | $was = snmp_get_quick_print(); |
88 | | | snmp_set_quick_print(1); |
89 | | | } |
90 | | | if(function_exists("snmp_get_valueretrieval")) |
91 | | | { |
92 | | | $was2 = snmp_get_valueretrieval(); |
93 | | | } |
94 | | | |
95 | | | if(function_exists('snmp_set_oid_output_format')) |
96 | | | { |
97 | | | snmp_set_oid_output_format ( SNMP_OID_OUTPUT_NUMERIC ); |
98 | | | } |
99 | | | if(function_exists('snmp_set_valueretrieval')) |
100 | | | { |
101 | | | snmp_set_valueretrieval(SNMP_VALUE_PLAIN); |
102 | | | } |
103 | 1 | simandl | |
104 | 85 | simandl | if($in_oid != '-') |
105 | | | { |
106 | | | $in_result = snmpget($host,$community,$in_oid,$timeout,$retries); |
107 | | | if($in_result) |
108 | | | { |
109 | | | $data[IN] = floatval($in_result); |
110 | | | $item->add_hint("snmp_in_raw",$in_result); |
111 | | | } |
112 | | | else |
113 | | | { |
114 | | | $this->down_cache{$host}++; |
115 | | | } |
116 | | | } |
117 | | | if($out_oid != '-') |
118 | | | { |
119 | | | $out_result = snmpget($host,$community,$out_oid,$timeout,$retries); |
120 | | | if($out_result) |
121 | | | { |
122 | | | // use floatval() here to force the output to be *some* kind of number |
123 | | | // just in case the stupid formatting stuff doesn't stop net-snmp returning 'down' instead of 2 |
124 | | | $data[OUT] = floatval($out_result); |
125 | | | $item->add_hint("snmp_out_raw",$out_result); |
126 | | | } |
127 | | | else |
128 | | | { |
129 | | | $this->down_cache{$host}++; |
130 | | | } |
131 | | | } |
132 | | | |
133 | | | debug ("SNMP ReadData: Got $in_result and $out_result\n"); |
134 | | | |
135 | | | $data_time = time(); |
136 | 1 | simandl | |
137 | 85 | simandl | if(function_exists("snmp_set_quick_print")) |
138 | | | { |
139 | | | snmp_set_quick_print($was); |
140 | | | } |
141 | | | } |
142 | | | else |
143 | | | { |
144 | | | warn("SNMP for $host has reached $abort_count failures. Skipping. [WMSNMP01]"); |
145 | | | } |
146 | 1 | simandl | } |
147 | | | |
148 | 85 | simandl | debug ("SNMP ReadData: Returning (".($data[IN]===NULL?'NULL':$data[IN]).",".($data[OUT]===NULL?'NULL':$data[OUT]).",$data_time)\n"); |
149 | | | |
150 | | | return( array($data[IN], $data[OUT], $data_time) ); |
151 | 1 | simandl | } |
152 | | | } |
153 | | | |
154 | | | // vim:ts=4:sw=4: |
155 | | | ?> |