3 function doSNMPmining ($object_id, $community)
5 // FIXME: switch to message log version 2
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
15 // Cisco sysObjectID to model (not product number, i.e. image code is missing) decoder
18 278 => 'WS-C3548-XL (48 Ethernet 10/100 ports and 2 10/100/1000 uplinks)',
19 283 => 'WS-C6509-E (9-slot system)',
20 324 => 'WS-C2950-24 (24 Ethernet 10/100 ports)',
21 # 694 => 'WS-C2960-24TC-L (24 Ethernet 10/100 ports and 2 dual-purpose uplinks)',
22 # 695 => 'WS-C2960-48TC-L (48 Ethernet 10/100 ports and 2 dual-purpose uplinks)',
23 696 => 'WS-C2960G-24TC-L (20 Ethernet 10/100/1000 ports and 4 dual-purpose uplinks)',
24 697 => 'WS-C2960G-48TC-L (44 Ethernet 10/100/1000 ports and 4 dual-purpose uplinks)',
25 716 => 'WS-C2960-24TT-L (24 Ethernet 10/100 ports and 2 10/100/1000 uplinks)',
26 717 => 'WS-C2960-48TT-L (48 Ethernet 10/100 ports and 2 10/100/1000 uplinks)',
27 527 => 'WS-C2970G-24T (24 Ethernet 10/100/1000 ports)',
28 561 => 'WS-C2970G-24TS (24 Ethernet 10/100/1000 ports and 4 10/100/1000 SFP uplinks)',
29 633 => 'WS-C3560-24TS (24 Ethernet 10/100 ports and 2 10/100/1000 SFP uplinks)',
30 634 => 'WS-C3560-48TS (48 Ethernet 10/100 ports and 4 10/100/1000 SFP uplinks)',
31 563 => 'WS-C3560-24PS (24 Ethernet 10/100 POE ports and 2 10/100/1000 SFP uplinks)',
32 564 => 'WS-C3560-48PS (48 Ethernet 10/100 POE ports and 4 10/100/1000 SFP uplinks)',
33 516 => 'WS-C3750-XXPS (24 or 48 Ethernet 10/100 POE ports and 4 10/100/1000 SFP uplinks)',
34 614 => 'WS-C3560G-24PS (24 Ethernet 10/100/1000 POE ports and 4 10/100/1000 SFP uplinks)',
35 615 => 'WS-C3560G-24TS (24 Ethernet 10/100/1000 ports and 4 10/100/1000 SFP uplinks)',
36 616 => 'WS-C3560G-48PS (48 Ethernet 10/100/1000 POE ports and 4 10/100/1000 SFP uplinks)',
37 617 => 'WS-C3560G-48TS (48 Ethernet 10/100/1000 ports and 4 10/100/1000 SFP uplinks)',
38 624 => 'WS-C3750G-24TS (24 Ethernet 10/100/1000 ports and 4 10/100/1000 SFP uplinks)',
39 58 => 'WS-C4503 (3-slot system)',
40 503 => '4503 (3-slot system)',
41 59 => 'WS-C4506 (6-slot system)',
42 502 => '4506 (6-slot system)',
43 626 => 'WS-C4948 (48 Ethernet 10/100/1000 ports and 4 10/100/1000 SFP uplinks)',
44 659 => 'WS-C4948-10GE (48 Ethernet 10/100/1000 ports and 2 10Gb X2 uplinks)',
45 428 => 'WS-C2950G-24 (24 Ethernet 10/100 ports and 2 1000 GBIC uplinks)',
46 429 => 'WS-C2950G-48 (48 Ethernet 10/100 ports and 2 1000 GBIC uplinks)',
47 559 => 'WS-C2950T-48 (48 Ethernet 10/100 ports and 2 10/100/1000 uplinks)',
48 920 => 'WS-CBS3032-DEL-F (16 Ethernet 10/100/1000 and up to 8 10/100/1000 uplinks)',
50 // Cisco sysObjectID to Dictionary dict_key map
82 // Cisco portType to Dictionary dict_key map
85 18 => 19, // 10/100BaseT => RJ-45/100Base-T
86 28 => 25, // 1000BaseSX => SC/1000Base-SX
87 31 => 440, // No Transceiver => unknown
88 61 => 24, // 10/100/1000BaseT => RJ-45/1000Base-T
91 $objectInfo = getObjectInfo ($object_id);
92 $endpoints = findAllEndpoints ($object_id, $objectInfo['name']);
93 $sysName = substr (snmpget ($endpoints[0], $community, 'sysName.0'), strlen ('STRING: '));
94 $sysDescr = snmpget ($endpoints[0], $community, 'sysDescr.0');
95 $sysChassi = snmpget ($endpoints[0], $community, '1.3.6.1.4.1.9.3.6.3.0');
96 // Strip the object type, it's always string here.
97 $sysDescr = substr ($sysDescr, strlen ('STRING: '));
98 $IOSversion = ereg_replace ('^.*, Version ([^ ]+), .*$', '\\1', $sysDescr);
99 $sysChassi = str_replace ('"', '', substr ($sysChassi, strlen ('STRING: ')));
100 if (strpos ($sysDescr, 'Cisco IOS Software') === 0 or strpos ($sysDescr, 'Cisco Internetwork Operating System Software') === 0)
101 $log[] = array ('code' => 'success', 'message' => 'Seems to be a Cisco box');
104 $log[] = array ('code' => 'error', 'message' => 'No idea how to handle ' . $sysDescr);
108 // It's a Cisco box. Go on.
109 $attrs = getAttrValues ($object_id);
110 // Only fill in attribute values, if they are not set.
111 // FIXME: this is hardcoded
113 if (empty ($attrs[3]['value']) && !empty ($sysName)) // FQDN
115 $error = commitUpdateAttrValue ($object_id, 3, $sysName);
117 $log[] = array ('code' => 'success', 'message' => 'FQDN set to ' . $sysName);
119 $log[] = array ('code' => 'error', 'message' => 'Failed settig FQDN: ' . $error);
122 if (empty ($attrs[5]['value']) and strlen ($IOSversion) > 0) // SW version
124 $error = commitUpdateAttrValue ($object_id, 5, $IOSversion);
126 $log[] = array ('code' => 'success', 'message' => 'SW version set to ' . $IOSversion);
128 $log[] = array ('code' => 'error', 'message' => 'Failed settig SW version: ' . $error);
131 if (empty ($attrs[1]['value']) and strlen ($sysChassi) > 0) // OEM Serial #1
133 $error = commitUpdateAttrValue ($object_id, 1, $sysChassi);
135 $log[] = array ('code' => 'success', 'message' => 'OEM S/N 1 set to ' . $sysChassi);
137 $log[] = array ('code' => 'error', 'message' => 'Failed settig OEM S/N 1: ' . $error);
140 if (empty ($attrs[4]['value'])) // switch OS type
142 switch (substr ($IOSversion, 0, 4))
145 $error = commitUpdateAttrValue ($object_id, 4, 252);
148 $error = commitUpdateAttrValue ($object_id, 4, 251);
151 $error = commitUpdateAttrValue ($object_id, 4, 244);
154 $log[] = array ('code' => 'error', 'message' => "Unknown IOS version ${IOSversion}");
159 $log[] = array ('code' => 'success', 'message' => 'Switch OS type set to Cisco IOS ' . substr ($IOSversion, 0, 4));
161 $log[] = array ('code' => 'error', 'message' => 'Failed setting Switch OS type');
164 $sysObjectID = snmpget ($endpoints[0], $community, 'sysObjectID.0');
166 $sysObjectID = substr ($sysObjectID, strlen ('OID: SNMPv2-SMI::enterprises.9.1.'));
167 if (!isset ($ciscomodel[$sysObjectID]))
169 $log[] = array ('code' => 'error', 'message' => 'Could not guess exact HW model!');
172 $log[] = array ('code' => 'success', 'message' => 'HW is ' . $ciscomodel[$sysObjectID]);
173 if (empty ($attrs[2]['value']) and isset ($hwtype[$sysObjectID])) // switch HW type
175 $error = commitUpdateAttrValue ($object_id, 2, $hwtype[$sysObjectID]);
177 $log[] = array ('code' => 'success', 'message' => 'HW type updated Ok');
179 $log[] = array ('code' => 'error', 'message' => 'Failed settig HW type: ' . $error);
181 // Now fetch ifType, ifDescr and ifPhysAddr and let model-specific code sort the data out.
182 $ifType = snmpwalkoid ($endpoints[0], $community, 'ifType');
183 $ifDescr = snmpwalkoid ($endpoints[0], $community, 'ifdescr');
184 $ifPhysAddress = snmpwalkoid ($endpoints[0], $community, 'ifPhysAddress');
185 // Combine 3 tables into 1...
187 foreach ($ifType as $key => $val)
189 list ($dummy, $ifIndex) = explode ('.', $key);
190 list ($dummy, $type) = explode (' ', $val);
191 $ifList1[$ifIndex]['type'] = $type;
193 foreach ($ifDescr as $key => $val)
195 list ($dummy, $ifIndex) = explode ('.', $key);
196 list ($dummy, $descr) = explode (' ', $val);
197 $ifList1[$ifIndex]['descr'] = trim ($descr, '"');
199 foreach ($ifPhysAddress as $key => $val)
202 list ($dummy, $ifIndex) = explode ('.', $key);
203 // NET-SNMP may return MAC addresses in one of two (?) formats depending on
204 // DISPLAY-HINT internal database. The best we can do about it is to accept both.
205 // Bug originally reported by Walery Wysotsky against openSUSE 11.0.
206 if (preg_match ('/^string: /i', $val)) // STRING: x:yy:z:xx:y:zz
208 list ($dummy, $val) = explode (' ', $val);
209 $addrbytes = explode (':', $val);
210 foreach ($addrbytes as $bidx => $bytestr)
211 if (strlen ($bytestr) == 1)
212 $addrbytes[$bidx] = '0' . $bytestr;
214 elseif (preg_match ('/^hex-string: /i', $val)) // Hex-STRING: xx yy zz xx yy zz
215 $addrbytes = explode (' ', substr ($val, -17));
217 continue; // martian format
218 $ifList1[$ifIndex]['phyad'] = implode ('', $addrbytes);
220 // ...and then reverse it inside out to make description the key.
222 foreach ($ifList1 as $ifIndex => $data)
224 $ifList2[$data['descr']]['type'] = $data['type'];
225 $ifList2[$data['descr']]['phyad'] = $data['phyad'];
226 $ifList2[$data['descr']]['idx'] = $ifIndex;
229 // Now we can directly pick necessary ports from the table accordingly
230 // to our known hardware model.
231 switch ($sysObjectID)
233 // FIXME: chassis edge switches often share a common naming scheme, so
234 // the sequences below have to be generalized. Let's have some duplicated
235 // code for the time being, as this is the first implementation ever.
236 case '697': // WS-C2960G-48TC-L
237 // 44 copper ports: 1X, 2X, 3X...
238 // 4 combo ports: 45, 46, 47, 48. Don't list SFP connectors atm, as it's not
239 // clear how to fit them into current Ports table structure.
240 for ($i = 1; $i <= 48; $i++
)
242 $label = ($i >= 45) ?
"${i}" : "${i}X";
243 $error = commitAddPort ($object_id, 'gi0/' . $i, 24, $label, $ifList2["GigabitEthernet0/${i}"]['phyad']);
247 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
250 case '324': // WS-C2950-24
251 for ($i = 1; $i <= 24; $i++
)
254 $error = commitAddPort ($object_id, 'fa0/' . $i, 24, $label, $ifList2["FastEthernet0/${i}"]['phyad']);
258 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
261 case '624': // WS-C3750-24TS
262 case '696': // WS-C2960G-24TC-L
263 // Quite similar to the above.
264 for ($i = 1; $i <= 24; $i++
)
266 $label = ($i >= 21) ?
"${i}" : "${i}X";
267 $error = commitAddPort ($object_id, 'gi0/' . $i, 24, $label, $ifList2["GigabitEthernet0/${i}"]['phyad']);
271 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
274 case '716': // WS-C2960-24TT-L
275 case '563': // WS-C3560-24PS
276 case '633': // WS-C3560-24TS
277 case '428': // WS-C2950G-24
278 for ($i = 1; $i <= 24; $i++
)
281 $error = commitAddPort ($object_id, 'fa0/' . $i, 19, $label, $ifList2["FastEthernet0/${i}"]['phyad']);
285 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
287 for ($i = 1; $i <= 2; $i++
)
290 $error = commitAddPort ($object_id, 'gi0/' . $i, 24, $label, $ifList2["GigabitEthernet0/${i}"]['phyad']);
294 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
297 case '717': // WS-C2960-48TT-L
298 case '429': // WS-C2950G-48
299 case '559': // WS-C2950T-48
300 for ($i = 1; $i <= 48; $i++
)
303 $error = commitAddPort ($object_id, 'fa0/' . $i, 19, $label, $ifList2["FastEthernet0/${i}"]['phyad']);
307 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
309 for ($i = 1; $i <= 2; $i++
)
312 $error = commitAddPort ($object_id, 'gi0/' . $i, 24, $label, $ifList2["GigabitEthernet0/${i}"]['phyad']);
316 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
319 case '516': // WS-C3750G-24TS OR WS-C3750-48PS
320 // FIXME: only handles 2 models of 3750, make it handle all of them
321 // see if it has 24 or 48 ports
322 $numports = substr (snmpget ($endpoints[0], $community, '.1.3.6.1.4.1.9.5.1.3.1.1.14.1'), strlen('INTEGER: '));
324 if ($numports == 28) // has 24 ports (+4 SFP) meaning it's a WS-C3750G-24TS
326 for ($i = 1; $i <= 28; $i++
)
329 $error = commitAddPort ($object_id, 'gi1/0/' . $i, 24, $label, $ifList2["GigabitEthernet1/0/${i}"]['phyad']);
333 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
336 elseif ($numports == 52) // has 48 ports (+4 SFP) meaning it's a WS-C3750-48PS
338 for ($i = 1; $i <= 48; $i++
)
341 $error = commitAddPort ($object_id, 'fa1/0/' . $i, 19, $label, $ifList2["FastEthernet1/0/${i}"]['phyad']);
345 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
347 for ($i = 1; $i <= 4; $i++
)
350 $error = commitAddPort ($object_id, 'gi1/0/' . $i, 24, $label, $ifList2["GigabitEthernet1/0/${i}"]['phyad']);
354 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
358 case '564': // WS-C3560-48PS
359 case '634': // WS-C3560-48TS
360 for ($i = 1; $i <= 48; $i++
)
363 $error = commitAddPort ($object_id, 'fa0/' . $i, 19, $label, $ifList2["FastEthernet0/${i}"]['phyad']);
367 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
369 for ($i = 1; $i <= 4; $i++
)
372 $error = commitAddPort ($object_id, 'gi0/' . $i, 24, $label, $ifList2["GigabitEthernet0/${i}"]['phyad']);
376 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
379 case '614': // WS-C3560G-24PS
380 case '615': // WS-C3560G-24TS
381 case '527': // WS-C2970G-24T
382 case '561': // WS-C2970G-24TS
383 for ($i = 1; $i <= 24; $i++
)
386 $error = commitAddPort ($object_id, 'gi0/' . $i, 24, $label, $ifList2["GigabitEthernet0/${i}"]['phyad']);
390 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
393 case '616': // WS-C3560G-48PS
394 case '617': // WS-C3560G-48TS
395 for ($i = 1; $i <= 48; $i++
)
398 $error = commitAddPort ($object_id, 'gi0/' . $i, 24, $label, $ifList2["GigabitEthernet0/${i}"]['phyad']);
402 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
405 case '626': // WS-C4948
406 case '659': // WS-C4948-10GE
407 for ($i = 1; $i <= 48; $i++
)
410 $error = commitAddPort ($object_id, 'gi1/' . $i, 24, $label, $ifList2["GigabitEthernet1/${i}"]['phyad']);
414 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
417 // For modular devices we issue a separate SNMP query to determine port type,
418 // then extract blade & port numbers from the results.
419 case '58': // WS-C4503
420 case '503': // WS-C4503
421 case '59': // WS-C4506
422 case '502': // WS-C4506
423 case '283': // WS-C6509-E
424 // get slot #, port # and port type using Cisco's MIB
425 $portType = snmpwalkoid ($endpoints[0], $community, '.1.3.6.1.4.1.9.5.1.4.1.1.5');
428 foreach ($portType as $key => $val)
430 // slot = $portIndex[8]
431 // port = $portIndex[9]
432 $portIndex = explode('.', $key);
433 $ifList[$i]['slotno'] = $portIndex[8];
434 $ifList[$i]['portno'] = $portIndex[9];
436 // note the Cisco port type and corresponding RackTables port type
437 list ($dummy, $cptype) = explode (' ', $val);
438 $ifList[$i]['cptype'] = $cptype;
439 if (array_key_exists($cptype, $porttype))
440 $ifList[$i]['ptype'] = $porttype[$cptype];
442 $ifList[$i]['ptype'] = null;
446 // use Cisco's ifIndex attribute to map Cisco table to standard SNMP table
447 $ifIndex = snmpwalkoid ($endpoints[0], $community, '.1.3.6.1.4.1.9.5.1.4.1.1.11');
449 foreach ($ifIndex as $val)
451 if (is_null($ifList[$i]['ptype']))
453 $log[] = array ('code' => 'error', 'message' => 'Unknown port type: ' . $ifList[$i]['cptype']);
455 switch ($ifList[$i]['ptype'])
460 case 28: // 1000base-sx
464 default: // unknown, default to gig eth
467 $pname = "{$prefix}{$ifList[$i]['slotno']}/{$ifList[$i]['portno']}";
468 $label = "slot {$ifList[$i]['slotno']} port {$ifList[$i]['portno']}";
469 list($dummy, $index) = explode(' ', $val);
471 // if l2address already exists in DB, nullify value so new row gets added without error
472 if (!is_null(searchByl2address($ifList1[$index]['phyad']))) $ifList1[$index]['phyad'] = null;
474 $error = commitAddPort ($object_id, $pname, $ifList[$i]['ptype'], $label, $ifList1[$index]['phyad']);
478 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $pname . ': ' . $error);
483 case '278': // WS-C3548-XL
484 for ($i = 1; $i <= 48; $i++
)
487 $error = commitAddPort ($object_id, 'fa0/' . $i, 19, $label, $ifList2["FastEthernet0/${i}"]['phyad']);
491 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
493 for ($i = 1; $i <= 2; $i++
)
496 $error = commitAddPort ($object_id, 'gi0/' . $i, 24, $label, $ifList2["GigabitEthernet0/${i}"]['phyad']);
500 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
503 case '920': // WS-CBS3032-DEL-F
504 for ($i = 1; $i <= 24; $i++
)
507 $error = commitAddPort ($object_id, 'gi0/' . $i, 24, $label, $ifList2["GigabitEthernet0/${i}"]['phyad']);
511 $log[] = array ('code' => 'error', 'message' => 'Failed to add port ' . $label . ': ' . $error);
515 showError ("Unexpected sysObjectID '${sysObjectID}'", __FUNCTION__
);
517 $error = commitAddPort ($object_id, 'con0', 29, 'console', '');
521 $log[] = array ('code' => 'error', 'message' => 'Failed to add console port : ' . $error);
523 $log[] = array ('code' => 'success', 'message' => "Added ${newports} new ports");