r2116 + process.php: don't fail silently
[racktables] / inc / snmp.php
1 <?php
2
3 function doSNMPmining ($object_id, $community)
4 {
5 // FIXME: switch to message log version 2
6 $log = array();
7 // IDs: http://cisco.com/en/US/products/sw/cscowork/ps2064/products_device_support_table09186a0080803bb4.html
8 // 2950: http://www.cisco.com/en/US/products/hw/switches/ps628/prod_models_home.html
9 // 2960: http://www.cisco.com/en/US/products/ps6406/prod_models_comparison.html
10 // 2970: http://cisco.com/en/US/products/hw/switches/ps5206/products_qanda_item09186a00801b1750.shtml
11 // 3500XL: http://cisco.com/en/US/products/hw/switches/ps637/products_eol_models.html
12 // 3560: http://cisco.com/en/US/products/hw/switches/ps5528/products_data_sheet09186a00801f3d7f.html
13 // 3750: http://cisco.com/en/US/products/hw/switches/ps5023/products_data_sheet09186a008016136f.html
14
15 // Cisco sysObjectID to model (not product number, i.e. image code is missing) decoder
16 $ciscomodel = array
17 (
18 283 => 'WS-C6509-E (9-slot system)',
19 # 694 => 'WS-C2960-24TC-L (24 Ethernet 10/100 ports and 2 dual-purpose uplinks)',
20 # 695 => 'WS-C2960-48TC-L (48 Ethernet 10/100 ports and 2 dual-purpose uplinks)',
21 696 => 'WS-C2960G-24TC-L (20 Ethernet 10/100/1000 ports and 4 dual-purpose uplinks)',
22 697 => 'WS-C2960G-48TC-L (44 Ethernet 10/100/1000 ports and 4 dual-purpose uplinks)',
23 716 => 'WS-C2960-24TT-L (24 Ethernet 10/100 ports and 2 10/100/1000 uplinks)',
24 717 => 'WS-C2960-48TT-L (48 Ethernet 10/100 ports and 2 10/100/1000 uplinks)',
25 527 => 'WS-C2970G-24T (24 Ethernet 10/100/1000 ports)',
26 561 => 'WS-C2970G-24TS (24 Ethernet 10/100/1000 ports and 4 10/100/1000 SFP uplinks)',
27 633 => 'WS-C3560-24TS (24 Ethernet 10/100 ports and 2 10/100/1000 SFP uplinks)',
28 634 => 'WS-C3560-48TS (48 Ethernet 10/100 ports and 4 10/100/1000 SFP uplinks)',
29 563 => 'WS-C3560-24PS (24 Ethernet 10/100 POE ports and 2 10/100/1000 SFP uplinks)',
30 564 => 'WS-C3560-48PS (48 Ethernet 10/100 POE ports and 4 10/100/1000 SFP uplinks)',
31 516 => 'WS-C3750-48PS (48 Ethernet 10/100 POE ports and 4 10/100/1000 SFP uplinks)',
32 614 => 'WS-C3560G-24PS (24 Ethernet 10/100/1000 POE ports and 4 10/100/1000 SFP uplinks)',
33 615 => 'WS-C3560G-24TS (24 Ethernet 10/100/1000 ports and 4 10/100/1000 SFP uplinks)',
34 616 => 'WS-C3560G-48PS (48 Ethernet 10/100/1000 POE ports and 4 10/100/1000 SFP uplinks)',
35 617 => 'WS-C3560G-48TS (48 Ethernet 10/100/1000 ports and 4 10/100/1000 SFP uplinks)',
36 58 => 'WS-C4503 (3-slot system)',
37 503 => '4503 (3-slot system)',
38 59 => 'WS-C4506 (6-slot system)',
39 502 => '4506 (6-slot system)',
40 626 => 'WS-C4948 (48 Ethernet 10/100/1000 ports and 4 10/100/1000 SFP uplinks)',
41 659 => 'WS-C4948-10GE (48 Ethernet 10/100/1000 ports and 2 10Gb X2 uplinks)',
42 428 => 'WS-C2950G-24 (24 Ethernet 10/100 ports and 2 1000 GBIC uplinks)',
43 429 => 'WS-C2950G-48 (48 Ethernet 10/100 ports and 2 1000 GBIC uplinks)',
44 559 => 'WS-C2950T-48 (48 Ethernet 10/100 ports and 2 10/100/1000 uplinks)',
45 );
46 // Cisco sysObjectID to Dictionary dict_key map
47 $hwtype = array
48 (
49 283 => 148,
50 696 => 167,
51 697 => 166,
52 527 => 210,
53 561 => 115,
54 633 => 169,
55 634 => 170,
56 563 => 171,
57 564 => 172,
58 614 => 175,
59 615 => 173,
60 616 => 176,
61 617 => 174,
62 58 => 145,
63 503 => 145,
64 59 => 156,
65 502 => 156,
66 626 => 147,
67 659 => 377,
68 428 => 389,
69 429 => 390,
70 559 => 387,
71 516 => 179,
72 716 => 164,
73 717 => 162,
74 );
75
76 $objectInfo = getObjectInfo ($object_id);
77 $endpoints = findAllEndpoints ($object_id, $objectInfo['name']);
78 $sysName = substr (snmpget ($endpoints[0], $community, 'sysName.0'), strlen ('STRING: '));
79 $sysDescr = snmpget ($endpoints[0], $community, 'sysDescr.0');
80 $sysChassi = snmpget ($endpoints[0], $community, '1.3.6.1.4.1.9.3.6.3.0');
81 // Strip the object type, it's always string here.
82 $sysDescr = substr ($sysDescr, strlen ('STRING: '));
83 $IOSversion = ereg_replace ('^.*, Version ([^ ]+), .*$', '\\1', $sysDescr);
84 $sysChassi = str_replace ('"', '', substr ($sysChassi, strlen ('STRING: ')));
85 if (strpos ($sysDescr, 'Cisco IOS Software') === 0 or strpos ($sysDescr, 'Cisco Internetwork Operating System Software') === 0)
86 $log[] = array ('code' => 'success', 'message' => 'Seems to be a Cisco box');
87 else
88 {
89 $log[] = array ('code' => 'error', 'message' => 'No idea how to handle ' . $sysDescr);
90 return $log;
91 }
92
93 // It's a Cisco box. Go on.
94 $attrs = getAttrValues ($object_id);
95 // Only fill in attribute values, if they are not set.
96 // FIXME: this is hardcoded
97
98 if (empty ($attrs[3]['value']) && !empty ($sysName)) // FQDN
99 {
100 $error = commitUpdateAttrValue ($object_id, 3, $sysName);
101 if ($error == TRUE)
102 $log[] = array ('code' => 'success', 'message' => 'FQDN set to ' . $sysName);
103 else
104 $log[] = array ('code' => 'error', 'message' => 'Failed settig FQDN: ' . $error);
105 }
106
107 if (empty ($attrs[5]['value']) and strlen ($IOSversion) > 0) // SW version
108 {
109 $error = commitUpdateAttrValue ($object_id, 5, $IOSversion);
110 if ($error == TRUE)
111 $log[] = array ('code' => 'success', 'message' => 'SW version set to ' . $IOSversion);
112 else
113 $log[] = array ('code' => 'error', 'message' => 'Failed settig SW version: ' . $error);
114 }
115
116 if (empty ($attrs[1]['value']) and strlen ($sysChassi) > 0) // OEM Serial #1
117 {
118 $error = commitUpdateAttrValue ($object_id, 1, $sysChassi);
119 if ($error == TRUE)
120 $log[] = array ('code' => 'success', 'message' => 'OEM S/N 1 set to ' . $sysChassi);
121 else
122 $log[] = array ('code' => 'error', 'message' => 'Failed settig OEM S/N 1: ' . $error);
123 }
124
125 if (empty ($attrs[4]['value'])) // switch OS type
126 {
127 switch (substr ($IOSversion, 0, 4))
128 {
129 case '12.2':
130 $error = commitUpdateAttrValue ($object_id, 4, 252);
131 break;
132 case '12.1':
133 $error = commitUpdateAttrValue ($object_id, 4, 251);
134 break;
135 case '12.0':
136 $error = commitUpdateAttrValue ($object_id, 4, 244);
137 break;
138 default:
139 $log[] = array ('code' => 'error', 'message' => "Unknown IOS version ${IOSversion}");
140 $error = TRUE;
141 break;
142 }
143 if ($error == TRUE)
144 $log[] = array ('code' => 'success', 'message' => 'Switch OS type set to Cisco IOS ' . substr ($IOSversion, 0, 4));
145 else
146 $log[] = array ('code' => 'error', 'message' => 'Failed setting Switch OS type');
147 }
148
149 $sysObjectID = snmpget ($endpoints[0], $community, 'sysObjectID.0');
150 // Transform OID
151 $sysObjectID = substr ($sysObjectID, strlen ('OID: SNMPv2-SMI::enterprises.9.1.'));
152 if (!isset ($ciscomodel[$sysObjectID]))
153 {
154 $log[] = array ('code' => 'error', 'message' => 'Could not guess exact HW model!');
155 return $log;
156 }
157 $log[] = array ('code' => 'success', 'message' => 'HW is ' . $ciscomodel[$sysObjectID]);
158 if (empty ($attrs[2]['value']) and isset ($hwtype[$sysObjectID])) // switch HW type
159 {
160 $error = commitUpdateAttrValue ($object_id, 2, $hwtype[$sysObjectID]);
161 if ($error == TRUE)
162 $log[] = array ('code' => 'success', 'message' => 'HW type updated Ok');
163 else
164 $log[] = array ('code' => 'error', 'message' => 'Failed settig HW type: ' . $error);
165 }
166 // Now fetch ifType, ifDescr and ifPhysAddr and let model-specific code sort the data out.
167 $ifType = snmpwalkoid ($endpoints[0], $community, 'ifType');
168 $ifDescr = snmpwalkoid ($endpoints[0], $community, 'ifdescr');
169 $ifPhysAddress = snmpwalkoid ($endpoints[0], $community, 'ifPhysAddress');
170 // Combine 3 tables into 1...
171 $ifList1 = array();
172 foreach ($ifType as $key => $val)
173 {
174 list ($dummy, $ifIndex) = explode ('.', $key);
175 list ($dummy, $type) = explode (' ', $val);
176 $ifList1[$ifIndex]['type'] = $type;
177 }
178 foreach ($ifDescr as $key => $val)
179 {
180 list ($dummy, $ifIndex) = explode ('.', $key);
181 list ($dummy, $descr) = explode (' ', $val);
182 $ifList1[$ifIndex]['descr'] = trim ($descr, '"');
183 }
184 foreach ($ifPhysAddress as $key => $val)
185 {
186 list ($dummy, $ifIndex) = explode ('.', $key);
187 list ($dummy, $addr) = explode (':', $val);
188 $addr = str_replace (' ', '', $addr);
189 $ifList1[$ifIndex]['phyad'] = $addr;
190 }
191 // ...and then reverse it inside out to make description the key.
192 $ifList2 = array();
193 foreach ($ifList1 as $ifIndex => $data)
194 {
195 $ifList2[$data['descr']]['type'] = $data['type'];
196 $ifList2[$data['descr']]['phyad'] = $data['phyad'];
197 $ifList2[$data['descr']]['idx'] = $ifIndex;
198 }
199 $newports = 0;
200 // Now we can directly pick necessary ports from the table accordingly
201 // to our known hardware model.
202 switch ($sysObjectID)
203 {
204 // FIXME: chassis edge switches often share a common naming scheme, so
205 // the sequences below have to be generalized. Let's have some duplicated
206 // code for the time being, as this is the first implementation ever.
207 case '697': // WS-C2960G-48TC-L
208 // 44 copper ports: 1X, 2X, 3X...
209 // 4 combo ports: 45, 46, 47, 48. Don't list SFP connectors atm, as it's not
210 // clear how to fit them into current Ports table structure.
211 for ($i = 1; $i <= 48; $i++)
212 {
213 $label = ($i >= 45) ? "${i}" : "${i}X";
214 $error = commitAddPort ($object_id, 'gi0/' . $i, 24, $label, $ifList2["GigabitEthernet0/${i}"]['phyad']);
215 if ($error == '')
216 $newports++;
217 else
218 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
219 }
220 break;
221 case '696': // WS-C2960G-24TC-L
222 // Quite similar to the above.
223 for ($i = 1; $i <= 24; $i++)
224 {
225 $label = ($i >= 21) ? "${i}" : "${i}X";
226 $error = commitAddPort ($object_id, 'gi0/' . $i, 24, $label, $ifList2["GigabitEthernet0/${i}"]['phyad']);
227 if ($error == '')
228 $newports++;
229 else
230 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
231 }
232 break;
233 case '716': // WS-C2960-24TT-L
234 case '563': // WS-C3560-24PS
235 case '633': // WS-C3560-24TS
236 case '428': // WS-C2950G-24
237 for ($i = 1; $i <= 24; $i++)
238 {
239 $label = "${i}X";
240 $error = commitAddPort ($object_id, 'fa0/' . $i, 19, $label, $ifList2["FastEthernet0/${i}"]['phyad']);
241 if ($error == '')
242 $newports++;
243 else
244 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
245 }
246 for ($i = 1; $i <= 2; $i++)
247 {
248 $label = "${i}";
249 $error = commitAddPort ($object_id, 'gi0/' . $i, 24, $label, $ifList2["GigabitEthernet0/${i}"]['phyad']);
250 if ($error == '')
251 $newports++;
252 else
253 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
254 }
255 break;
256 case '717': // WS-C2960-48TT-L
257 case '429': // WS-C2950G-48
258 case '559': // WS-C2950T-48
259 for ($i = 1; $i <= 48; $i++)
260 {
261 $label = "${i}X";
262 $error = commitAddPort ($object_id, 'fa0/' . $i, 19, $label, $ifList2["FastEthernet0/${i}"]['phyad']);
263 if ($error == '')
264 $newports++;
265 else
266 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
267 }
268 for ($i = 1; $i <= 2; $i++)
269 {
270 $label = "${i}";
271 $error = commitAddPort ($object_id, 'gi0/' . $i, 24, $label, $ifList2["GigabitEthernet0/${i}"]['phyad']);
272 if ($error == '')
273 $newports++;
274 else
275 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
276 }
277 break;
278 case '516': // WS-C3750-48PS
279 for ($i = 1; $i <= 48; $i++)
280 {
281 $label = "${i}X";
282 $error = commitAddPort ($object_id, 'fa1/0/' . $i, 19, $label, $ifList2["FastEthernet1/0/${i}"]['phyad']);
283 if ($error == '')
284 $newports++;
285 else
286 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
287 }
288 for ($i = 1; $i <= 4; $i++)
289 {
290 $label = "${i}";
291 $error = commitAddPort ($object_id, 'gi1/0/' . $i, 24, $label, $ifList2["GigabitEthernet1/0/${i}"]['phyad']);
292 if ($error == '')
293 $newports++;
294 else
295 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
296 }
297 break;
298 case '564': // WS-C3560-48PS
299 case '634': // WS-C3560-48TS
300 for ($i = 1; $i <= 48; $i++)
301 {
302 $label = "${i}X";
303 $error = commitAddPort ($object_id, 'fa0/' . $i, 19, $label, $ifList2["FastEthernet0/${i}"]['phyad']);
304 if ($error == '')
305 $newports++;
306 else
307 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
308 }
309 for ($i = 1; $i <= 4; $i++)
310 {
311 $label = "${i}";
312 $error = commitAddPort ($object_id, 'gi0/' . $i, 24, $label, $ifList2["GigabitEthernet0/${i}"]['phyad']);
313 if ($error == '')
314 $newports++;
315 else
316 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
317 }
318 break;
319 case '614': // WS-C3560G-24PS
320 case '615': // WS-C3560G-24TS
321 case '527': // WS-C2970G-24T
322 case '561': // WS-C2970G-24TS
323 for ($i = 1; $i <= 24; $i++)
324 {
325 $label = "${i}X";
326 $error = commitAddPort ($object_id, 'gi0/' . $i, 24, $label, $ifList2["GigabitEthernet0/${i}"]['phyad']);
327 if ($error == '')
328 $newports++;
329 else
330 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
331 }
332 break;
333 case '616': // WS-C3560G-48PS
334 case '617': // WS-C3560G-48TS
335 for ($i = 1; $i <= 48; $i++)
336 {
337 $label = "${i}X";
338 $error = commitAddPort ($object_id, 'gi0/' . $i, 24, $label, $ifList2["GigabitEthernet0/${i}"]['phyad']);
339 if ($error == '')
340 $newports++;
341 else
342 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
343 }
344 break;
345 case '626': // WS-C4948
346 case '659': // WS-C4948-10GE
347 for ($i = 1; $i <= 48; $i++)
348 {
349 $label = "${i}X";
350 $error = commitAddPort ($object_id, 'gi1/' . $i, 24, $label, $ifList2["GigabitEthernet1/${i}"]['phyad']);
351 if ($error == '')
352 $newports++;
353 else
354 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
355 }
356 break;
357 // For modular devices we don't iterate over all possible port names,
358 // but use the first list to pick everything that looks legitimate
359 // for this hardware. It would be correct to fetch the list of modules
360 // installed to generate lists of ports, but who is going to implement
361 // this?
362 case '503': // 4503
363 case '58': // WS-C4503
364 case '502': // 4506
365 case '59': // WS-C4506
366 case '283': // WS-C6509-E
367 foreach ($ifList1 as $port)
368 {
369 if ($port['type'] != 'ethernet-csmacd(6)')
370 continue;
371 // Copper Fa/Gi harvesting is relatively simple, while 10Gig ports can
372 // have random samples of transciever units.
373 if (strpos ($port['descr'], 'FastEthernet') === 0) // Fa
374 {
375 $prefix = 'fa';
376 $ptype = 19; // RJ-45/100Base-TX
377 list ($slotno, $portno) = explode ('/', substr ($port['descr'], strlen ('FastEthernet')));
378 }
379 elseif (strpos ($port['descr'], 'GigabitEthernet') === 0) // Gi
380 {
381 $prefix = 'gi';
382 $ptype = 24; // RJ-45/1000Base-T
383 list ($slotno, $portno) = explode ('/', substr ($port['descr'], strlen ('GigabitEthernet')));
384 }
385 else continue;
386 $label = "slot ${slotno} port ${portno}";
387 $pname = "${prefix}${slotno}/${portno}";
388 $error = commitAddPort ($object_id, $pname, $ptype, $label, $port['phyad']);
389 if ($error == '')
390 $newports++;
391 else
392 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $pname . ': ' . $error);
393 }
394 break;
395 default:
396 showError ("Unexpected sysObjectID '${sysObjectID}'", __FUNCTION__);
397 }
398 $error = commitAddPort ($object_id, 'con0', 29, 'console', '');
399 if ($error == '')
400 $newports++;
401 else
402 $log[] = array ('code' => 'error', 'message' => 'Failed to add console port : ' . $error);
403 if ($newports > 0)
404 $log[] = array ('code' => 'success', 'message' => "Added ${newports} new ports");
405 return $log;
406 }
407
408 ?>