add port type update
[racktables-contribs] / snmpgeneric.php
CommitLineData
9276dc97
ME
1<?php
2
3/********************************************
4 *
30bf198b 5 * RackTables 0.20.x snmpgeneric extension
9276dc97 6 *
30bf198b 7 * sync an RackTables object with an SNMP device.
9276dc97 8 *
30bf198b 9 * Should work with almost any SNMP capable device.
9276dc97 10 *
30bf198b 11 * reads SNMP tables:
12 * - system
13 * - ifTable
14 * - ifxTable
15 * - ipAddrTable (ipv4 only)
16 * - ipAddressTable (ipv4 + ipv6)
17 *
18 * Features:
19 * - update object attributes
20 * - create networks
21 * - create ports
22 * - add and bind ip addresses
7c2cfc75 23 * - create as new object
365fb193 24 * - save snmp settings per object (uses comment field)
30bf198b 25 *
26 * Known to work with:
27 * - Enterasys SecureStacks, S-Series
28 * - cisco 2620XM (thx to Rob)
29 * - hopefully many others
30 *
31 *
32 * Usage:
33 *
34 * 1. select "SNMP generic sync" tap
35 * 2. select your SNMP config (host, v1, v2c or v3, ...)
36 * 3. hit "Show List"
37 * 4. you will see a selection of all information that could be retrieved
38 * 5. select what should be updated and/or created
39 * 6. hit "Create" Button to make changes to RackTables
40 * 7. repeat step 1. to 6. as often as you like / need
9276dc97 41 *
9276dc97 42 *
9276dc97 43 * needs PHP 5
30bf198b 44 *
9276dc97 45 * TESTED on FreeBSD 9.0, nginx/1.0.12, php 5.3.10, NET-SNMP 5.7.1
30bf198b 46 * and RackTables <= 0.20.3
9276dc97 47 *
af78a786 48 * (c)2015 Maik Ehinger <m.ehinger@ltur.de>
9276dc97
ME
49 */
50
51/****
52 * INSTALL
02a702a1 53 * just place file in plugins directory
30bf198b 54 *
55 */
56
57/**
58 * The newest version of this plugin can be found at:
59 *
60 * https://github.com/github138/myRT-contribs/tree/develop-0.20.x
61 *
9276dc97
ME
62 */
63
64/* TODOs
30bf198b 65 *
9276dc97
ME
66 * - code cleanup
67 *
68 * - update visible label on ifAlias change !?
69 *
30bf198b 70 * - test if device supports mibs
9276dc97
ME
71 * - gethostbyaddr / gethostbyname host list
72 * - correct iif_name display if != 1
73 *
74 * - set more Object attributs / fields
9276dc97 75 *
365fb193 76 * - Input variables exceeded 1000
ccf830c6 77 * - update iftypes
365fb193 78 *
9276dc97
ME
79 */
80
1fc7fc7d 81/* RackTables Debug Mode */
82//$debug_mode=1;
83
30bf198b 84require_once('inc/snmp.php');
9276dc97
ME
85
86$tab['object']['snmpgeneric'] = 'SNMP Generic sync';
87$tabhandler['object']['snmpgeneric'] = 'snmpgeneric_tabhandler';
88//$trigger['object']['snmpgeneric'] = 'snmpgeneric_tabtrigger';
89
90$ophandler['object']['snmpgeneric']['create'] = 'snmpgeneric_opcreate';
91
92/* snmptranslate command */
93$sg_cmd_snmptranslate = '/usr/local/bin/snmptranslate';
94
95/* create ports without connector */
96$sg_create_noconnector_ports = FALSE;
97
98/* deselect add port for this snmp port types */
99$sg_ifType_ignore = array(
100 '1', /* other */
101 '24', /* softwareLoopback */
102 '23', /* ppp */
103 '33', /* rs232 */
30bf198b 104 '34', /* para */
9276dc97
ME
105 '53', /* propVirtual */
106 '77', /* lapd */
107 '131', /* tunnel */
108 '136', /* l3ipvlan */
109 '160', /* usb */
110 '161', /* ieee8023adLag */
111);
112
113/* ifType to RT oif_id mapping */
114$sg_ifType2oif_id = array(
115 /* 440 causes SQLSTATE[23000]: Integrity constraint violation:
30bf198b 116 * 1452 Cannot add or update a child row:
9276dc97
ME
117 * a foreign key constraint fails
118 */
119 // '1' => 440, /* other => unknown 440 */
120 '1' => 1469, /* other => virutal port 1469 */
30bf198b 121 '6' => 24, /* ethernetCsmacd => 1000BASE-T 24 */
9276dc97
ME
122 '24' => 1469, /* softwareLoopback => virtual port 1469 */
123 '33' => 1469, /* rs232 => RS-232 (DB-9) 681 */
30bf198b 124 '34' => 1469, /* para => virtual port 1469 */
9276dc97
ME
125 '53' => 1469, /* propVirtual => virtual port 1469 */
126 '62' => 1195, /* fastEther => 100BASE-FX 1195 */
127 '131' => 1469, /* tunnel => virtual port 1469 */
128 '136' => 1469, /* l3ipvlan => virtual port 1469 */
129 '160' => 1469, /* usb => virtual port 1469 */
130 '161' => 1469, /* ieee8023adLag => virtual port 1469 */
131);
132
133/* -------------------------------------------------- */
134
135/* snmp vendor list http://www.iana.org/assignments/enterprise-numbers */
136
137$sg_known_sysObjectIDs = array
138(
139 /* ------------ default ------------ */
140 'default' => array
141 (
142 // 'text' => 'default',
143 'pf' => array('snmpgeneric_pf_entitymib'),
144 'attr' => array
145 (
30bf198b 146 2 => array('pf' => 'snmpgeneric_pf_hwtype'), /* HW Typ*/
9276dc97
ME
147 3 => array('oid' => 'sysName.0'),
148 /* FQDN check only if regex matches */
149 //3 => array('oid' => 'sysName.0', 'regex' => '/^[^ .]+(\.[^ .]+)+\.?/', 'uncheck' => 'no FQDN'),
150 4 => array('pf' => 'snmpgeneric_pf_swtype', 'uncheck' => 'experimental'), /* SW type */
151 14 => array('oid' => 'sysContact.0'), /* Contact person */
152 // 1235 => array('value' => 'Constant'),
30bf198b 153 ),
9276dc97 154 'port' => array
30bf198b 155 (
9276dc97
ME
156 // 'AC-in' => array('porttypeid' => '1-16', 'uncheck' => 'uncheck reason/comment'),
157 // 'name' => array('porttypeid' => '1-24', 'ifDescr' => 'visible label'),
158 ),
159 ),
160
161 /* ------------ ciscoSystems --------------- */
162/* '9' => array
163 * (
164 * 'text' => 'ciscoSystems',
165 * ),
166 */
167 '9.1' => array
168 (
169 'text' => 'ciscoProducts',
170 'attr' => array(
171 4 => array('pf' => 'snmpgeneric_pf_catalyst'), /* SW type/version */
172 16 => array('pf' => 'snmpgeneric_pf_ciscoflash'), /* flash memory */
30bf198b 173
9276dc97 174 ),
30bf198b 175
9276dc97
ME
176 ),
177 /* ------------ Microsoft --------------- */
178 '311' => array
179 (
180 'text' => 'Microsoft',
181 'attr' => array(
182 4 => array('pf' => 'snmpgeneric_pf_swtype', 'oid' => 'sysDescr.0', 'regex' => '/.* Windows Version (.*?) .*/', 'replacement' => 'Windows \\1', 'uncheck' => 'TODO RT matching'), /*SW type */
183 ),
184 ),
185 /* ------------ Enterasys --------------- */
186 '5624' => array
187 (
188 'text' => 'Enterasys',
189 'attr' => array(
190 4 => array('pf' => 'snmpgeneric_pf_enterasys'), /* SW type/version */
191 ),
192 ),
193
194 /* Enterasys N3 */
195 '5624.2.1.53' => array
30bf198b 196 (
0ffff63b 197 'dict_key' => 2021,
198 'text' => 'Matrix N3',
30bf198b 199 ),
9276dc97
ME
200
201 '5624.2.2.284' => array
30bf198b 202 (
9276dc97 203 'dict_key' => 50002,
30bf198b 204 'text' => 'Securestack C2',
205 ),
9276dc97
ME
206
207 '5624.2.1.98' => array
30bf198b 208 (
9276dc97 209 'dict_key' => 50002,
30bf198b 210 'text' => 'Securestack C3',
211 ),
9276dc97
ME
212
213 '5624.2.1.100' => array
30bf198b 214 (
9276dc97 215 'dict_key' => 50002,
30bf198b 216 'text' => 'Securestack B3',
217 ),
9276dc97
ME
218
219 '5624.2.1.128' => array
30bf198b 220 (
0ffff63b 221 'dict_key' => 1970,
222 'text' => 'S-series SSA130',
30bf198b 223 ),
9276dc97
ME
224
225 '5624.2.1.129' => array
30bf198b 226 (
0ffff63b 227 'dict_key' => 1970,
228 'text' => 'S-series SSA150',
30bf198b 229 ),
9276dc97
ME
230
231 '5624.2.1.137' => array
30bf198b 232 (
0ffff63b 233 'dict_key' => 1987,
30bf198b 234 'text' => 'Securestack B5 POE',
235 ),
9276dc97
ME
236
237 /* S3 */
238 '5624.2.1.131' => array
239 (
0ffff63b 240 'dict_key' => 1974,
241 'text' => 'S-series S3',
9276dc97
ME
242 ),
243
244 /* S4 */
245 '5624.2.1.132' => array
246 (
0ffff63b 247 'dict_key' => 1975,
248 'text' => 'S-series S4'
9276dc97
ME
249 ),
250
251 /* S8 */
252 '5624.2.1.133' => array
253 (
0ffff63b 254 'dict_key' => 1977,
255 'text' => 'S-series S8'
256 ),
257
258 '5624.2.1.165' => array
259 (
260 'dict_key' => 1971,
261 'text' => 'S-series Bonded SSA',
9276dc97
ME
262 ),
263
264 /* ------------ net-snmp --------------- */
265 '8072' => array
266 (
30bf198b 267 'text' => 'net-snmp',
9276dc97
ME
268 'attr' => array(
269 4 => array('pf' => 'snmpgeneric_pf_swtype', 'oid' => 'sysDescr.0', 'regex' => '/(.*?) .*? (.*?) .*/', 'replacement' => '\\1 \\2', 'uncheck' => 'TODO RT matching'), /*SW type */
270 ),
271 ),
272
273 /* ------------ Frauenhofer FOKUS ------------ */
274 '12325' => array
275 (
276 'text' => 'Fraunhofer FOKUS',
277 'attr' => array(
278 4 => array('pf' => 'snmpgeneric_pf_swtype', 'oid' => 'sysDescr.0', 'regex' => '/.*? .*? (.*? .*).*/', 'replacement' => '\\1', 'uncheck' => 'TODO RT matching'), /*SW type */
279 ),
280 ),
281
282 '12325.1.1.2.1.1' => array
283 (
284 'dict_key' => 42, /* Server model noname/unknown */
285 'text' => 'BSNMP - mini SNMP daemon (bsnmpd)',
286 ),
287
288) + $known_switches;
289/* add snmp.php known_switches */
290
291/* ------------ Sample function --------------- */
292/*
293 * Sample Precessing Function (pf)
294 */
295function snmpgeneric_pf_sample(&$snmp, &$sysObjectID, $attr_id) {
296
297 $object = &$sysObjectID['object'];
298 $attr = &$sysObjectID['attr'][$attr_id];
299
300 if(!isset($attr['oid']))
301 return;
302
303 /* output success banner */
304 showSuccess('Found sysObjectID '.$sysObjectID['value']);
305
306 /* access attribute oid setting and do snmpget */
307 $oid = $attr['oid'];
308 $value = $snmp->get($oid);
309
310 /* set new attribute value */
311 $attr['value'] = $value;
312
313 /* do not check attribute per default */
314 $attr['uncheck'] = "comment";
315
316 /* set informal comment */
317 $attr['comment'] = "comment";
318
319 /* add additional ports */
320 // $sysObjectID['port']['name'] = array('porttypeid' => '1-24', 'ifPhysAddress' => '001122334455', 'ifDescr' => 'visible label', 'uncheck' => 'comment', 'disabled' => 'porttypeid select disabled');
321
322 /* set other attribute */
323// $sysObjectID['attr'][1234]['value'] = 'attribute value';
324
325} /* snmpgeneric_pf_sample */
326
327/* ------------ Enterasys --------------- */
328
329function snmpgeneric_pf_enterasys(&$snmp, &$sysObjectID, $attr_id) {
330
331 $attrs = &$sysObjectID['attr'];
332
333 //snmpgeneric_pf_entitymib($snmp, $sysObjectID, $attr_id);
334
335 /* TODO find correct way to get Bootroom and Firmware versions */
336
337 /* Model */
30bf198b 338 /*if(preg_match('/.*\.([^.]+)$/', $sysObjectID['value'], $matches)) {
9276dc97
ME
339 * showNotice('Device '.$matches[1]);
340 *}
341 */
342
343 /* TODO SW type */
344 //$attrs[4]['value'] = 'Enterasys'; /* SW type */
345
346 /* set SW version only if not already set by entitymib */
347 if(isset($attrs[5]['value']) && !empty($attrs[5]['value'])) {
30bf198b 348
9276dc97 349 /* SW version from sysDescr */
30bf198b 350 if(preg_match('/^Enterasys .* Inc\. (.+) [Rr]ev ([^ ]+) ?(.*)$/', $snmp->sysDescr, $matches)) {
9276dc97
ME
351
352 $attrs[5]['value'] = $matches[2]; /* SW version */
30bf198b 353
9276dc97
ME
354 // showSuccess("Found Enterasys Model ".$matches[1]);
355 }
356
357 } /* SW version */
358
359 /* add serial port */
30bf198b 360 //$sysObjectID['port']['console'] = array('porttypeid' => '1-29', 'ifDescr' => 'console', 'disabled' => 'disabled');
9276dc97
ME
361
362}
363
364/* ------------ Cisco --------------- */
365
366/* logic from snmp.php */
367function snmpgeneric_pf_catalyst(&$snmp, &$sysObjectID, $attr_id) {
368 $attrs = &$sysObjectID['attr'];
30bf198b 369 $ports = &$sysObjectID['port'];
9276dc97
ME
370
371 /* sysDescr multiline on C5200 */
372 if(preg_match ('/.*, Version ([^ ]+), .*/', $snmp->sysDescr, $matches)) {
373 $exact_release = $matches[1];
30bf198b 374 $major_line = preg_replace ('/^([[:digit:]]+\.[[:digit:]]+)[^[:digit:]].*/', '\\1', $exact_release);
9276dc97
ME
375
376 $ios_codes = array
30bf198b 377 (
378 '12.0' => 244,
379 '12.1' => 251,
380 '12.2' => 252,
381 );
382
9276dc97
ME
383 $attrs[5]['value'] = $exact_release;
384
30bf198b 385 if (array_key_exists ($major_line, $ios_codes))
9276dc97
ME
386 $attrs[4]['value'] = $ios_codes[$major_line];
387
388 } /* sw type / version */
389
390 $sysChassi = $snmp->get ('1.3.6.1.4.1.9.3.6.3.0');
391 if ($sysChassi !== FALSE or $sysChassi !== NULL)
392 $attrs[1]['value'] = str_replace ('"', '', $sysChassi);
393
30bf198b 394 $ports['con0'] = array('porttypeid' => '1-29', 'ifDescr' => 'console'); // RJ-45 RS-232 console
9276dc97
ME
395
396 if (preg_match ('/Cisco IOS Software, C2600/', $snmp->sysDescr))
30bf198b 397 $ports['aux0'] = array('porttypeid' => '1-29', 'ifDescr' => 'auxillary'); // RJ-45 RS-232 aux port
9276dc97
ME
398
399 // blade devices are powered through internal circuitry of chassis
400 if ($sysObjectID['value'] != '9.1.749' and $sysObjectID['value'] != '9.1.920')
401 {
30bf198b 402 $ports['AC-in'] = array('porttypeid' => '1-16');
9276dc97
ME
403 }
404
405} /* snmpgeneric_pf_catalyst */
406
407/* -------------------------------------------------- */
408function snmpgeneric_pf_ciscoflash(&$snmp, &$sysObjectID, $attr_id) {
30bf198b 409 /*
9276dc97
ME
410 * ciscoflashMIB = 1.3.6.1.4.1.9.9.10
411 */
412 /*
413 | 16 | uint | flash memory, MB |
414 */
415 $attrs = &$sysObjectID['attr'];
416
30bf198b 417 $ciscoflash = $snmp->walk('1.3.6.1.4.1.9.9.10.1.1.2'); /* ciscoFlashDeviceTable */
9276dc97
ME
418
419 $flash = array_keys($ciscoflash, 'flash');
420
421 foreach($flash as $oid) {
422 if(!preg_match('/(.*)?\.[^\.]+\.([^\.]+)$/',$oid,$matches))
423 continue;
424
425 $index = $matches[2];
426 $prefix = $matches[1];
427
428 showSuccess("Found Flash: ".$ciscoflash[$prefix.'.8.'.$index]." ".$ciscoflash[$prefix.'.2.'.$index]." bytes");
429
30bf198b 430 $attrs[16]['value'] = ceil($ciscoflash[$prefix.'.2.'.$index] / 1024 / 1024); /* ciscoFlashDeviceSize */
9276dc97
ME
431
432 }
433
434 /*
435 * ciscoMemoryPoolMIB = 1.3.6.1.4.1.9.9.48
436 * ciscoMemoryPoolUsed .1.1.1.5
437 * ciscoMemoryPoolFree .1.1.1.6
438 */
439
440 $ciscomem = $snmp->walk('1.3.6.1.4.1.9.9.48');
441
442 if(!empty($ciscomem)) {
443
444 $used = 0;
445 $free = 0;
446
447 foreach($ciscomem as $oid => $value) {
30bf198b 448
9276dc97
ME
449 switch(preg_replace('/.*?(\.1\.1\.1\.[^\.]+)\.[^\.]+$/','\\1',$oid)) {
450 case '.1.1.1.5':
451 $used += $value;
452 break;
453 case '.1.1.1.6':
454 $free += $value;
455 break;
456 }
457
458 }
459
30bf198b 460 $attrs[17]['value'] = ceil(($free + $used) / 1024 / 1024); /* RAM, MB */
9276dc97
ME
461 }
462
463} /* snmpgeneric_pf_ciscoflash */
464
465/* -------------------------------------------------- */
466/* -------------------------------------------------- */
467
468/* HW Type processor function */
469function snmpgeneric_pf_hwtype(&$snmp, &$sysObjectID, $attr_id) {
470
471 $attr = &$sysObjectID['attr'][$attr_id];
472
473 if (isset($sysObjectID['dict_key'])) {
474
475 $value = $sysObjectID['dict_key'];
476 showSuccess("Found HW type dict_key: $value");
30bf198b 477
9276dc97
ME
478 /* return array of attr_id => attr_value) */
479 $attr['value'] = $value;
480
481 } else {
482 showNotice("HW type dict_key not set - Unknown OID");
483 }
484
485} /* snmpgeneric_pf_hwtype */
486
487/* -------------------------------------------------- */
488
489/* SW type processor function */
490/* experimental */
491/* Find a way to match RT SW types !? */
492function snmpgeneric_pf_swtype(&$snmp, &$sysObjectID, $attr_id) {
493
494 /* 4 = SW type */
495
30bf198b 496 $attr = &$sysObjectID['attr'][$attr_id];
9276dc97
ME
497
498 $object = &$sysObjectID['object'];
499
500 $objtype_id = $object['objtype_id'];
501
502 if(isset($attr['oid']))
503 $oid = $attr['oid'];
504 else
30bf198b 505 $oid = 'sysDescr.0';
9276dc97
ME
506
507 $raw_value = $snmp->get($oid);
508
509 $replacement = '\\1';
510
511 if(isset($attr['regex'])) {
512 $regex = $attr['regex'];
513
514 if(isset($attr['replacement']))
515 $replacement = $attr['replacement'];
516
517 } else {
518 $list = array('bsd','linux','centos','suse','fedora','ubuntu','windows','solaris','vmware');
519
520 $regex = '/.* ([^ ]*('.implode($list,'|').')[^ ]*) .*/i';
521 $replacement = '\\1';
522 }
523
524 $value = preg_replace($regex, $replacement, $raw_value, -1, $count);
525 //$attr['value'] = $value;
526
527 if(!empty($value) && $count > 0) {
528 /* search dict_key for value in RT Dictionary */
529 /* depends on object type server(13)/switch(14)/router(15) */
30bf198b 530 $result = usePreparedSelectBlade
531 (
532 'SELECT dict_key,dict_value FROM Dictionary WHERE chapter_id in (13,14,15) and dict_value like ? order by dict_key desc limit 1',
533 array ('%'.$value.'%')
534 );
535 $row = $result->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_UNIQUE|PDO::FETCH_COLUMN);
9276dc97
ME
536
537 if(!empty($row)) {
538 $RTvalue = key($row);
539
540 if(isset($attr['comment']))
541 $attr['comment'] .= ", $value ($RTvalue) ".$row[$RTvalue];
542 else
543 $attr['comment'] = "$value ($RTvalue) ".$row[$RTvalue];
544
545 showSuccess("Found SW type: $value ($RTvalue) ".$row[$RTvalue]);
546 $value = $RTvalue;
547 }
548
549 /* set attr value */
550 $attr['value'] = $value;
551 // unset($attr['uncheck']);
552
553 }
554
555 if(isset($attr['comment']))
556 $attr['comment'] .= ' (experimental)';
557 else
558 $attr['comment'] = '(experimental)';
559
560} /* snmpgeneric_pf_swtype */
561
562/* -------------------------------------------------- */
30bf198b 563/* try to set SW version
9276dc97
ME
564 * and add some AC ports
565 *
566 */
567/* needs more testing */
568function snmpgeneric_pf_entitymib(&$snmp, &$sysObjectID, $attr_id) {
30bf198b 569
9276dc97
ME
570 /* $attr_id == NULL -> device pf */
571
572 $attrs = &$sysObjectID['attr'];
573 $ports = &$sysObjectID['port'];
574
575 $entPhysicalClass = $snmp->walk('.1.3.6.1.2.1.47.1.1.1.1.5'); /* entPhysicalClass */
576
577 if(empty($entPhysicalClass))
578 return;
579
580 showNotice("Found Entity Table (Experimental)");
581
30bf198b 582/* PhysicalClass
9276dc97
ME
583 * 1:other
584 * 2:unknown
585 * 3:chassis
586 * 4:backplane
587 * 5:container
588 * 6:powerSupply
589 * 7:fan
590 * 8:sensor
591 * 9:module
592 * 10:port
593 * 11:stack
594 * 12:cpu
595 */
596
597 /* chassis */
598
599 /* always index = 1 ??? */
600 $chassis = array_keys($entPhysicalClass, '3'); /* 3 chassis */
601
602 if(0)
603 if(!empty($chassis)) {
604 echo '<table>';
605
606 foreach($chassis as $key => $oid) {
607 /* get index */
30bf198b 608 if(!preg_match('/\.(\d+)$/',$oid, $matches))
9276dc97
ME
609 continue;
610
30bf198b 611 $index = $matches[1];
612
613 $name = $snmp->get(".1.3.6.1.2.1.47.1.1.1.1.7.$index");
614 $serialnum = $snmp->get(".1.3.6.1.2.1.47.1.1.1.1.11.$index");
615 $mfgname = $snmp->get(".1.3.6.1.2.1.47.1.1.1.1.12.$index");
616 $modelname = $snmp->get(".1.3.6.1.2.1.47.1.1.1.1.13.$index");
9276dc97 617
9276dc97
ME
618 //showNotice("$name $mfgname $modelname $serialnum");
619
620 echo("<tr><td>$name</td><td>$mfgname</td><td>$modelname</td><td>$serialnum</td>");
621 }
622 unset($key);
623 unset($oid);
624
625 echo '</table>';
626 } /* chassis */
627
628
629
630 /* modules */
631
632 $modules = array_keys($entPhysicalClass, '9'); /* 9 Modules */
633
634 if(!empty($modules)) {
635
636 echo '<br><br>Modules<br><table>';
637 echo("<tr><th>Name</th><th>MfgName</th><th>ModelName</th><th>HardwareRev</th><th>FirmwareRev</th><th>SoftwareRev</th><th>SerialNum</th>");
30bf198b 638
9276dc97
ME
639 foreach($modules as $key => $oid) {
640
641 /* get index */
30bf198b 642 if(!preg_match('/\.(\d+)$/',$oid, $matches))
9276dc97
ME
643 continue;
644
30bf198b 645 $index = $matches[1];
646
647 $name = $snmp->get(".1.3.6.1.2.1.47.1.1.1.1.7.$index");
648 $hardwarerev = $snmp->get(".1.3.6.1.2.1.47.1.1.1.1.8.$index");
649 $firmwarerev = $snmp->get(".1.3.6.1.2.1.47.1.1.1.1.9.$index");
650 $softwarerev = $snmp->get(".1.3.6.1.2.1.47.1.1.1.1.10.$index");
651 $serialnum = $snmp->get(".1.3.6.1.2.1.47.1.1.1.1.11.$index");
652 $mfgname = $snmp->get(".1.3.6.1.2.1.47.1.1.1.1.12.$index");
653 $modelname = $snmp->get(".1.3.6.1.2.1.47.1.1.1.1.13.$index");
9276dc97 654
9276dc97
ME
655 //showNotice("$name $mfgname $modelname $hardwarerev $firmwarerev $softwarerev $serialnum");
656
657 echo("<tr><td>".(empty($name) ? '-' : $name )."</td><td>$mfgname</td><td>$modelname</td><td>$hardwarerev</td><td>$firmwarerev</td><td>$softwarerev</td><td>$serialnum</td>");
658
659 /* set SW version to first module software version */
660 if($key == 0 ) {
661
662 $attrs[5]['value'] = $softwarerev; /* SW version */
663 $attrs[5]['comment'] = 'entity MIB';
664 }
665
666 }
667 unset($key);
668 unset($oid);
669
670 echo '</table>';
671 }
672
673
674 /* add AC ports */
675 $powersupply = array_keys($entPhysicalClass, '6'); /* 6 powerSupply */
676 $count = 1;
677 foreach($powersupply as $oid) {
678
679 /* get index */
30bf198b 680 if(!preg_match('/\.(\d+)$/',$oid, $matches))
9276dc97
ME
681 continue;
682
30bf198b 683 $index = $matches[1];
684 $descr = $snmp->get(".1.3.6.1.2.1.47.1.1.1.1.2.$index");
9276dc97 685
30bf198b 686 $ports['AC-'.$count] = array('porttypeid' => '1-16', 'ifDescr' => $descr, 'comment' => 'entity MIB', 'uncheck' => '');
9276dc97
ME
687 $count++;
688 }
689 unset($oid);
690}
691
692/* -------------------------------------------------- */
693
694/*
695 * regex processor function
696 * needs 'oid' and 'regex'
697 * uses first back reference as attribute value
698 */
699function snmpgeneric_pf_regex(&$snmp, &$sysObjectID, $attr_id) {
700
701 $attr = &$sysObjectID['attr'][$attr_id];
702
703 if (isset($attr['oid']) && isset($attr['regex'])) {
704
705 $oid = $attr['oid'];
706 $regex = $attr['regex'];
707
708 $raw_value = $snmp->get($oid);
709
710
711 if(isset($attr['replacement']))
712 $replace = $attr['replacement'];
713 else
714 $replace = '\\1';
715
716 $value = preg_replace($regex,$replace, $raw_value);
30bf198b 717
9276dc97
ME
718 /* return array of attr_id => attr_value) */
719 $attr['value'] = $value;
720
30bf198b 721 }
9276dc97
ME
722 // else Warning ??
723
724} /* snmpgeneric_pf_regex */
725
726/* -------------------------------------------------- */
727
728$sg_portiifoptions= getPortIIFOptions();
729$sg_portiifoptions[-1] = 'sfp'; /* generic sfp */
730
5130f8be 731$sg_portoifoptions= getPortOIFOptions();
9276dc97
ME
732
733/* -------------------------------------------------- */
734/* -------------------------------------------------- */
735
736function snmpgeneric_tabhandler($object_id) {
737
8188a17c 738// sg_var_dump_html($_POST);
739
740 if(isset($_POST['asnewobject']) && $_POST['asnewobject'] == "1")
741 {
742 $newobject_name = $_POST['object_name'];
743 $newobject_label = $_POST['object_label'];
744 $newobject_type_id = $_POST['object_type_id'];
745 $newobject_asset_no = $_POST['object_asset_no'];
746
747 if(sg_checkObjectNameUniqueness($newobject_name, $newobject_type_id))
748 {
749
750 $object_id = commitAddObject($newobject_name, $newobject_label, $newobject_type_id, $newobject_asset_no);
751
752 $_POST['asnewobject'] = "0";
753
754 parse_str($_SERVER['QUERY_STRING'],$query_string);
755
756 $query_string['object_id'] = $object_id;
757
758 $_SERVER['QUERY_STRING'] = http_build_query($query_string);
759
760 list($path, $qs) = explode('?',$_SERVER['REQUEST_URI'],2);
761 $_SERVER['REQUEST_URI'] = $path.'?'.$_SERVER['QUERY_STRING'];
762
763
764 // switch to new object
765 echo '<body>';
766 echo '<body onload="document.forms[\'newobject\'].submit();">';
767
768 echo '<form method=POST id=newobject action='.$_SERVER['REQUEST_URI'].'>';
769
770 foreach($_POST as $name => $value)
771 {
772 echo "<input type=hidden name=$name value=$value>";
773 }
774
775 echo '<input type=submit id="submitbutton" tabindex="1" value="Show List">';
776 echo '</from></body>';
777 exit;
30bf198b 778 }
8188a17c 779 else
780 {
781 showError("Object with name: \"$newobject_name\" already exists!!!");
782 $_POST['snmpconfig'] = "0";
783 }
784 }
785
365fb193 786 // save snmp settings
787 if(isset($_POST['save']) && $_POST['save'] == "1")
788 {
789 // TODO save only on success !!
790
791 $object = spotEntity('object', $object_id);
792
793 $snmpvalues[0] = 'SNMP';
794 $snmpnames = array('host', 'version', 'community');
795 if($_POST['version'] == "3")
796 $snmpnames = array_merge($snmpnames, array('sec_level','auth_protocol','auth_passphrase','priv_protocol','priv_passphrase'));
797
798 foreach($snmpnames as $key => $value)
799 {
800 if(isset($_POST[$value]))
801 {
802 switch($value)
803 {
804 case "auth_passphrase":
805 case "priv_passphrase":
806 $snmpvalues[$key + 1] = base64_encode($_POST[$value]);
807 break;
808
809 default: $snmpvalues[$key + 1] = $_POST[$value];
810 }
811 }
812 }
813
814 // sg_var_dump_html($snmpvalues);
815
816 $newsnmpstr = implode($snmpvalues,":");
817
818 $snmpstr = strtok($object['comment'],"\n\r");
819
820 $snmpstrarray = explode(':', $snmpstr);
821
822 $setcomment = "set";
823 if($snmpstrarray[0] == "SNMP")
824 {
825 if($newsnmpstr == $snmpstr)
826 $setcomment = "ok";
827 else
828 $setcomment = "update";
829 }
830
831 if($setcomment != "ok")
832 {
833
834 if($setcomment == "update")
835 $comment = str_replace($snmpstr,$newsnmpstr, $object['comment']);
836 else
837 $comment = "$newsnmpstr\n".$object['comment'];
838
839 // echo "$snmpnewstr ".$object['comment']." --> $comment";
840
841 commitUpdateObject($object_id, $object['name'], NULL, $object['has_problems'], NULL, $comment );
842 showNotice("$setcomment SNMP Settings: $newsnmpstr");
843
844 }
845
846 }
847
8188a17c 848 if(isset($_POST['snmpconfig']) && $_POST['snmpconfig'] == '1') {
849 snmpgeneric_list($object_id);
9276dc97
ME
850 } else {
851 snmpgeneric_snmpconfig($object_id);
852 }
853} /* snmpgeneric_tabhandler */
854
855/* -------------------------------------------------- */
856
857//function snmpgeneric_tabtrigger() {
858// return 'std';
859//} /* snmpgeneric_tabtrigger */
860
861/* -------------------------------------------------- */
862
863function snmpgeneric_snmpconfig($object_id) {
864
9276dc97
ME
865
866 $object = spotEntity ('object', $object_id);
867 //$object['attr'] = getAttrValues($object_id);
868 $endpoints = findAllEndpoints ($object_id, $object['name']);
869
870 addJS('function showsnmpv3(element) {
30bf198b 871 var style;
9276dc97
ME
872 if(element.value != \''.SNMPgeneric::VERSION_3.'\') {
873 style = \'none\';
874 document.getElementById(\'snmp_community_label\').style.display=\'\';
875 } else {
876 style = \'\';
877 document.getElementById(\'snmp_community_label\').style.display=\'none\';
878 }
879
30bf198b 880 var elements = document.getElementsByName(\'snmpv3\');
881 for(var i=0;i<elements.length;i++) {
9276dc97
ME
882 elements[i].style.display=style;
883 }
884 };',TRUE);
885
8188a17c 886 addJS('function shownewobject(element) {
887 var style;
888
889 if(element.checked) {
890 style = \'\';
891 } else {
892 style = \'none\';
893 }
894
895 var elements = document.getElementsByName(\'newobject\');
896 for(var i=0;i<elements.length;i++) {
897 elements[i].style.display=style;
898 }
899 };',TRUE);
900
30bf198b 901 addJS('function checkInput() {
902 var host = document.getElementById(\'host\');
903
904 if(host.value == "-1") {
905 var newvalue = prompt("Enter Hostname or IP Address","");
906 if(newvalue != "") {
907 host.options[host.options.length] = new Option(newvalue, newvalue);
908 host.value = newvalue;
909 }
910 }
911
912 if(host.value != "-1" && host.value != "")
913 return true;
914 else
915 return false;
916 };',TRUE);
917
8188a17c 918 echo '<body onload="document.getElementById(\'submitbutton\').focus(); showsnmpv3(document.getElementById(\'snmpversion\')); shownewobject(document.getElementById(\'asnewobject\'));">';
919
9276dc97
ME
920 foreach( $endpoints as $key => $value) {
921 $endpoints[$value] = $value;
922 unset($endpoints[$key]);
923 }
924 unset($key);
925 unset($value);
926
927 foreach( getObjectIPv4Allocations($object_id) as $ip => $value) {
928
30bf198b 929 $ip = ip_format($ip);
930
9276dc97
ME
931 if(!in_array($ip, $endpoints))
932 $endpoints[$ip] = $ip;
933 }
934 unset($ip);
935 unset($value);
936
937 foreach( getObjectIPv6Allocations($object_id) as $value) {
30bf198b 938 $ip = ip_format(ip_parse($value['addrinfo']['ip']));
9276dc97
ME
939
940 if(!in_array($ip, $endpoints))
941 $endpoints[$ip] = $ip;
942 }
943 unset($value);
944
30bf198b 945 /* ask for ip/host name on submit see js checkInput() */
946 $endpoints['-1'] = 'ask me';
947
365fb193 948 // saved snmp settings
949 $snmpstr = strtok($object['comment'],"\n\r");
950 $snmpstrarray = explode(':', $snmpstr);
951
952 if($snmpstrarray[0] == "SNMP")
953 {
954 $snmpnames = array('SNMP','host', 'version', 'community');
955 if($snmpstrarray[2] == "3")
956 $snmpnames = array_merge($snmpnames, array('sec_level','auth_protocol','auth_passphrase','priv_protocol','priv_passphrase'));
957
958 $snmpvalues = array();
959 foreach($snmpnames as $key => $value)
960 {
961 if(isset($snmpstrarray[$key]))
962 {
963 switch($key)
964 {
965 case 6:
966 case 8:
967 $snmpvalues[$value] = base64_decode($snmpstrarray[$key]);
968 break;
969
970 default: $snmpvalues[$value] = $snmpstrarray[$key];
971 }
972 }
973 }
974
975 unset($snmpvalues['SNMP']);
976
977 $snmpconfig = $snmpvalues;
978 }
979 else
980 $snmpconfig = array();
981
982 $snmpconfig += $_POST;
9276dc97
ME
983
984 if(!isset($snmpconfig['host'])) {
30bf198b 985 $snmpconfig['host'] = -1;
9276dc97
ME
986
987 /* try to find first FQDN or IP */
988 foreach($endpoints as $value) {
989 if(preg_match('/^[^ .]+(\.[^ .]+)+\.?/',$value)) {
990 $snmpconfig['host'] = $value;
991 break;
992 }
993 }
994 unset($value);
995 }
996
30bf198b 997// sg_var_dump_html($endpoints);
9276dc97 998
8188a17c 999 if(!isset($snmpconfig['version']))
9276dc97
ME
1000 $snmpconfig['version'] = mySNMP::SNMP_VERSION;
1001
1002 if(!isset($snmpconfig['community']))
1003 $snmpconfig['community'] = getConfigVar('DEFAULT_SNMP_COMMUNITY');
1004
1005 if(empty($snmpconfig['community']))
1006 $snmpconfig['community'] = mySNMP::SNMP_COMMUNITY;
1007
1008 if(!isset($snmpconfig['sec_level']))
1009 $snmpconfig['sec_level'] = NULL;
1010
1011 if(!isset($snmpconfig['auth_protocol']))
1012 $snmpconfig['auth_protocol'] = NULL;
1013
1014 if(!isset($snmpconfig['auth_passphrase']))
1015 $snmpconfig['auth_passphrase'] = NULL;
1016
1017 if(!isset($snmpconfig['priv_protocol']))
1018 $snmpconfig['priv_protocol'] = NULL;
1019
1020 if(!isset($snmpconfig['priv_passphrase']))
1021 $snmpconfig['priv_passphrase'] = NULL;
1022
8188a17c 1023 if(!isset($snmpconfig['asnewobject']))
1024 $snmpconfig['asnewobject'] = NULL;
1025
1026 if(!isset($snmpconfig['object_type_id']))
1027 $snmpconfig['object_type_id'] = '8';
1028
1029 if(!isset($snmpconfig['object_name']))
1030 $snmpconfig['object_name'] = NULL;
1031
1032 if(!isset($snmpconfig['object_label']))
1033 $snmpconfig['object_label'] = NULL;
1034
1035 if(!isset($snmpconfig['object_asset_no']))
1036 $snmpconfig['object_asset_no'] = NULL;
1037
365fb193 1038 if(!isset($snmpconfig['save']))
1039 $snmpconfig['save'] = true;
1040
8188a17c 1041// sg_var_dump_html($snmpconfig);
1042
1043// $snmpv3displaystyle = ($snmpconfig['version'] == "3" ? "style=\"\"" : "style=\"display:none;\"");
1044
9276dc97 1045 echo '<h1 align=center>SNMP Config</h1>';
30bf198b 1046 echo '<form method=post name="snmpconfig" onsubmit="return checkInput()" action='.$_SERVER['REQUEST_URI'].' />';
9276dc97
ME
1047
1048 echo '<table cellspacing=0 cellpadding=5 align=center class=widetable>
1049 <tr><th class=tdright>Host:</th><td>';
30bf198b 1050
365fb193 1051 //if($snmpconfig['asnewobject'] == '1' )
1052 if($snmpconfig['host'] != '-1' and !isset($endpoints[$snmpconfig['host']]))
8188a17c 1053 $endpoints[$snmpconfig['host']] = $snmpconfig['host'];
1054
30bf198b 1055 echo getSelect ($endpoints, array ('id' => 'host','name' => 'host'), $snmpconfig['host'], FALSE);
9276dc97
ME
1056
1057 echo'</td></tr>
30bf198b 1058 <tr>
9276dc97
ME
1059 <th class=tdright><label for=snmpversion>Version:</label></th>
1060 <td class=tdleft>';
1061
1062 echo getSelect (array(SNMPgeneric::VERSION_1 => 'v1', SNMPgeneric::VERSION_2C => 'v2c', SNMPgeneric::VERSION_3 => 'v3'),
1063 array ('name' => 'version', 'id' => 'snmpversion', 'onchange' => 'showsnmpv3(this)'),
1064 $snmpconfig['version'], FALSE);
1065
1066 echo '</td>
1067 </tr>
1068 <tr>
1069 <th id="snmp_community_label" class=tdright><label for=community>Community:</label></th>
8188a17c 1070 <th name="snmpv3" style="display:none;" class=tdright><label for=community>Security Name:</label></th>
9276dc97
ME
1071 <td class=tdleft><input type=text name=community value='.$snmpconfig['community'].' ></td>
1072 </tr>
1073 <tr name="snmpv3" style="display:none;">
1074 <th></th>
1075 </tr>
1076 <tr name="snmpv3" style="display:none;">
1077 <th class=tdright><label">Security Level:</label></th>
1078 <td class=tdleft>';
1079
1080 echo getSelect (array('noAuthNoPriv' => 'no Auth and no Priv', 'authNoPriv'=> 'auth without Priv', 'authPriv' => 'auth with Priv'),
1081 array ('name' => 'sec_level'),
1082 $snmpconfig['sec_level'], FALSE);
1083
1084 echo '</td></tr>
1085 <tr name="snmpv3" style="display:none;">
1086 <th class=tdright><label>Auth Type:</label></th>
1087 <td class=tdleft>
1088 <input name=auth_protocol type=radio value=MD5 '.($snmpconfig['auth_protocol'] == 'MD5' ? ' checked="checked"' : '').'/><label>MD5</label>
1089 <input name=auth_protocol type=radio value=SHA '.($snmpconfig['auth_protocol'] == 'SHA' ? ' checked="checked"' : '').'/><label>SHA</label>
1090 </td>
1091 </tr>
1092 <tr name="snmpv3" style="display:none;">
1093 <th class=tdright><label>Auth Key:</label></th>
1094 <td class=tdleft><input type=password id=auth_passphrase name=auth_passphrase value="'.$snmpconfig['auth_passphrase'].'"></td>
1095 </tr>
1096 <tr name="snmpv3" style="display:none;">
1097 <th class=tdright><label>Priv Type:</label></th>
1098 <td class=tdleft>
1099 <input name=priv_protocol type=radio value=DES '.($snmpconfig['priv_protocol'] == 'DES' ? ' checked="checked"' : '').'/><label>DES</label>
1100 <input name=priv_protocol type=radio value=AES '.($snmpconfig['priv_protocol'] == 'AES' ? ' checked="checked"' : '').'/><label>AES</label>
1101 </td>
1102 </tr>
1103 <tr name="snmpv3" style="display:none;">
1104 <th class=tdright><label>Priv Key</label></th>
1105 <td class=tdleft><input type=password name=priv_passphrase value="'.$snmpconfig['priv_passphrase'].'"></td>
1106 </tr>
1107 </tr>
8188a17c 1108
1109 <tr>
1110 <th></th>
1111 <td class=tdleft>
1112 <input name=asnewobject id=asnewobject type=checkbox value=1 onchange="shownewobject(this)"'.($snmpconfig['asnewobject'] == '1' ? ' checked="checked"' : '').'>
1113 <label>Create as new object</label></td>
1114 </tr>';
1115
1116// $newobjectdisplaystyle = ($snmpconfig['asnewobject'] == '1' ? "" : "style=\"display:none;\"");
1117
1118 echo '<tr name="newobject" style="display:none;">
1119 <th class=tdright>Type:</th><td class=tdleft>';
1120
1121 $typelist = withoutLocationTypes (readChapter (CHAP_OBJTYPE, 'o'));
1122 $typelist = cookOptgroups ($typelist);
1123
1124 printNiftySelect ($typelist, array ('name' => "object_type_id"), $snmpconfig['object_type_id']);
1125
1126 echo '</td></tr>
1127
1128 <tr name="newobject" style="display:none;">
1129 <th class=tdright>Common name:</th><td class=tdleft><input type=text name=object_name value='.$snmpconfig['object_name'].'></td></tr>
1130 <tr name="newobject" style="display:none;">
1131 <th class=tdright>Visible label:</th><td class=tdleft><input type=text name=object_label value='.$snmpconfig['object_label'].'></td></tr>
1132 <tr name="newobject" style="display:none;">
1133 <th class=tdright>Asset tag:</th><td class=tdleft><input type=text name=object_asset_no value='.$snmpconfig['object_asset_no'].'></td></tr>
1134
365fb193 1135 <tr>
1136 <th></th>
1137 <td class=tdleft>
1138 <input name=save id=save type=checkbox value=1'.($snmpconfig['save'] == '1' ? ' checked="checked"' : '').'>
1139 <label>Save SNMP settings for object</label></td>
1140 </tr>
9276dc97
ME
1141 <td colspan=2>
1142
1143 <input type=hidden name=snmpconfig value=1>
1144 <input type=submit id="submitbutton" tabindex="1" value="Show List"></td></tr>
1145
1146 </table></form>';
1147
1148} /* snmpgeneric_snmpconfig */
1149
1150function snmpgeneric_list($object_id) {
1151
30bf198b 1152 global $sg_create_noconnector_ports, $sg_known_sysObjectIDs, $sg_portoifoptions, $sg_ifType_ignore;
9276dc97
ME
1153
1154 if(isset($_POST['snmpconfig'])) {
30bf198b 1155 $snmpconfig = $_POST;
9276dc97
ME
1156 } else {
1157 showError("Missing SNMP Config");
1158 return;
1159 }
1160
8188a17c 1161// sg_var_dump_html($snmpconfig);
1162
9276dc97
ME
1163 echo '<body onload="document.getElementById(\'createbutton\').focus();">';
1164
1165 addJS('function setchecked(classname) { var boxes = document.getElementsByClassName(classname);
30bf198b 1166 var value = document.getElementById(classname).checked;
9276dc97
ME
1167 for(i=0;i<boxes.length;i++) {
1168 if(boxes[i].disabled == false)
1169 boxes[i].checked=value;
1170 }
1171 };', TRUE);
1172
1173 $object = spotEntity ('object', $object_id);
1174
1175 $object['attr'] = getAttrValues($object_id);
1176
1177 $snmpdev = new mySNMP($snmpconfig['version'], $snmpconfig['host'], $snmpconfig['community']);
1178
1179 if($snmpconfig['version'] == SNMPgeneric::VERSION_3 ) {
1180 $snmpdev->setSecurity( $snmpconfig['sec_level'],
1181 $snmpconfig['auth_protocol'],
1182 $snmpconfig['auth_passphrase'],
1183 $snmpconfig['priv_protocol'],
1184 $snmpconfig['priv_passphrase']
1185 );
1186 }
1187
1188 $snmpdev->init();
1189
1190 if($snmpdev->getErrno()) {
1191 showError($snmpdev->getError());
1192 return;
1193 }
1194
1195 /* SNMP connect successfull */
1196
1197 showSuccess("SNMP v".$snmpconfig['version']." connect to ${snmpconfig['host']} successfull");
1198
1199 echo '<form name=CreatePorts method=post action='.$_SERVER['REQUEST_URI'].'&module=redirect&op=create>';
1200
1201 echo "<strong>System Informations</strong>";
1202 echo "<table>";
1203// echo "<tr><th>OID</th><th>Value</th></tr>";
1204
1205 $systemoids = array('sysDescr', 'sysObjectID', 'sysUpTime', 'sysContact', 'sysName', 'sysLocation');
1206 foreach ($systemoids as $shortoid) {
1207
1208 $value = $snmpdev->{$shortoid};
1209
1210 if($shortoid == 'sysUpTime') {
1211 /* in hundredths of a second */
1212 $secs = (int)($value / 100);
1213 $days = (int)($secs / (60 * 60 * 24));
1214 $secs -= $days * 60 *60 * 24;
1215 $hours = (int)($secs / (60 * 60));
1216 $secs -= $hours * 60 * 60;
1217 $mins = (int)($secs / (60));
1218 $secs -= $mins * 60;
1219 $value = "$value ($days $hours:$mins:$secs)";
1220 }
1221
1222 echo "<tr><td title=\"".$snmpdev->lastgetoid."\" align=\"right\">$shortoid: </td><td>$value</td></tr>";
1223
1224 }
1225 unset($shortoid);
1226
1227 echo "</table>";
1228
1229 /* sysObjectID Attributes and Ports */
1230 $sysObjectID['object'] = &$object;
1231
1232 /* get sysObjectID */
1233 $sysObjectID['raw_value'] = $snmpdev->sysObjectID;
1234 //$sysObjectID['raw_value'] = 'NET-SNMP-MIB::netSnmpAgentOIDs.10';
1235
1236 $sysObjectID['value'] = preg_replace('/^.*enterprises\.([\.[:digit:]]+)$/','\\1', $sysObjectID['raw_value']);
1237
1238 /* try snmptranslate to numeric */
1239 if(preg_match('/[^\.0-9]+/',$sysObjectID['value'])) {
1240 $numeric_value = $snmpdev->translatetonumeric($sysObjectID['value']);
1241
1242 if(!empty($numeric_value)) {
1243 showSuccess("sysObjectID: ".$sysObjectID['value']." translated to $numeric_value");
1244 $sysObjectID['value'] = preg_replace('/^.1.3.6.1.4.1.([\.[:digit:]]+)$/','\\1', $numeric_value);
1245 }
1246 }
1247
1248 /* array_merge doesn't work with numeric keys !! */
1249 $sysObjectID['attr'] = array();
1250 $sysObjectID['port'] = array();
30bf198b 1251
9276dc97
ME
1252 $sysobjid = $sysObjectID['value'];
1253
1254 $count = 1;
1255
1256 while($count) {
1257
1258 if(isset($sg_known_sysObjectIDs[$sysobjid])) {
1259 $sysObjectID = $sysObjectID + $sg_known_sysObjectIDs[$sysobjid];
30bf198b 1260
9276dc97
ME
1261 if(isset($sg_known_sysObjectIDs[$sysobjid]['attr']))
1262 $sysObjectID['attr'] = $sysObjectID['attr'] + $sg_known_sysObjectIDs[$sysobjid]['attr'];
30bf198b 1263
9276dc97
ME
1264 if(isset($sg_known_sysObjectIDs[$sysobjid]['port']))
1265 $sysObjectID['port'] = $sysObjectID['port'] + $sg_known_sysObjectIDs[$sysobjid]['port'];
30bf198b 1266
9276dc97
ME
1267 if(isset($sg_known_sysObjectIDs[$sysobjid]['text'])) {
1268 showSuccess("found sysObjectID ($sysobjid) ".$sg_known_sysObjectIDs[$sysobjid]['text']);
1269 }
1270 }
1271
1272 $sysobjid = preg_replace('/\.[[:digit:]]+$/','',$sysobjid, 1, $count);
30bf198b 1273
9276dc97
ME
1274 /* add default sysobjectid */
1275 if($count == 0 && $sysobjid != 'default') {
1276 $sysobjid = 'default';
1277 $count = 1;
1278 }
1279 }
1280
1281 $sysObjectID['vendor_number'] = $sysobjid;
1282
1283 /* device pf */
1284 if(isset($sysObjectID['pf']))
1285 foreach($sysObjectID['pf'] as $function) {
1286 if(function_exists($function)) {
1287 /* call device pf */
1288 $function($snmpdev, $sysObjectID, NULL);
1289 } else {
1290 showWarning("Missing processor function ".$function." for device $sysobjid");
1291 }
1292 }
1293
1294
1295 /* sort attributes maintain numeric keys */
1296 ksort($sysObjectID['attr']);
1297
1298 /* DEBUG */
1299 //sg_var_dump_html($sysObjectID['attr'], "Before processing");
1300
1301 /* needs PHP >= 5 foreach call by reference */
1302 /* php 5.1.6 doesn't seem to work */
ccf830c6 1303 //foreach($sysObjectID['attr'] as $attr_id => &$attr)
9276dc97
ME
1304 foreach($sysObjectID['attr'] as $attr_id => $value) {
1305
1306 $attr = &$sysObjectID['attr'][$attr_id];
1307
1308 if(isset($object['attr'][$attr_id])) {
1309
1310 switch(TRUE) {
1311
1312 case isset($attr['pf']):
1313 if(function_exists($attr['pf'])) {
1314
1315 $attr['pf']($snmpdev, $sysObjectID, $attr_id);
1316
1317 } else {
1318 showWarning("Missing processor function ".$attr['pf']." for attribute $attr_id");
1319 }
1320
1321 break;
1322
1323 case isset($attr['oid']):
1324
1325 $attrvalue = $snmpdev->get($attr['oid']);
1326
1327 if(isset($attr['regex'])) {
1328 $regex = $attr['regex'];
1329
1330 if(isset($attr['replacement'])) {
1331 $replacement = $attr['replacement'];
1332 $attrvalue = preg_replace($regex, $replacement, $attrvalue);
1333 } else {
1334 if(!preg_match($regex, $attrvalue)) {
1335 if(!isset($attr['uncheck']))
1336 $attr['uncheck'] = "regex doesn't match";
30bf198b 1337 } else
9276dc97
ME
1338 unset($attr['uncheck']);
1339 }
1340 }
1341
30bf198b 1342 $attr['value'] = $attrvalue;
9276dc97
ME
1343
1344 break;
1345
1346 case isset($attr['value']):
1347 break;
1348
1349 default:
1350 showError("Error handling attribute id: $attr_id");
1351
1352 }
1353
1354 } else {
1355 showWarning("Object has no attribute id: $attr_id");
1356 unset($sysObjectID['attr'][$attr_id]);
1357 }
1358
1359 }
1360 unset($attr_id);
1361
1362 /* sort again in case there where attribs added ,maintain numeric keys */
1363 ksort($sysObjectID['attr']);
1364
1365 /* print attributes */
1366 echo '<br>Attributes<br><table>';
1367 echo '<tr><th><input type="checkbox" id="attribute" checked="checked" onclick="setchecked(this.id)"></td>';
1368 echo '<th>Name</th><th>Current Value</th><th>new value</th></tr>';
1369
1370 /* DEBUG */
1371 //sg_var_dump_html($sysObjectID['attr'], "After processing");
1372
30bf198b 1373 foreach($sysObjectID['attr'] as $attr_id => &$attr) {
9276dc97
ME
1374
1375 if(isset($object['attr'][$attr_id]) && isset($attr['value'])) {
1376
1377 if($attr['value'] == $object['attr'][$attr_id]['value'])
30bf198b 1378 $attr['uncheck'] = 'Current = new value';
9276dc97
ME
1379
1380 $value = $attr['value'];
1381
1382 $val_key = (isset($object['attr'][$attr_id]['key']) ? ' ('.$object['attr'][$attr_id]['key'].')' : '' );
1383 $comment = '';
1384
1385 if(isset($attr['comment'])) {
1386 if(!empty($attr['comment']))
1387 $comment = $attr['comment'];
1388 }
1389
1390 if(isset($attr['uncheck'])) {
1391 $checked = '';
1392 $comment .= ', '.$attr['uncheck'];
1393 } else {
1394 $checked = ' checked="checked"';
1395 }
1396
1397 $updateattrcheckbox = '<b style="background-color:#00ff00">'
1398 .'<input style="background-color:#00ff00" class="attribute" type="checkbox" name="updateattr['.$attr_id.']" value="'.$value.'"'
1399 .$checked.'></b>';
1400
1401 $comment = trim($comment,', ');
1402
1403 echo "<tr><td>$updateattrcheckbox</td><td title=\"id: $attr_id\">"
1404 .$object['attr'][$attr_id]['name'].'</td><td style="background-color:#d8d8d8">'
1405 .$object['attr'][$attr_id]['value'].$val_key.'</td><td>'.$value.'</td>'
1406 .'<td style="color:#888888">'.$comment.'</td></tr>';
1407 }
1408 }
1409 unset($attr_id);
1410
1411 echo '</table>';
1412
1413 /* ports */
1414
1415 /* get ports */
1416 amplifyCell($object);
1417
30bf198b 1418 /* set array key to port name */
9276dc97
ME
1419 foreach($object['ports'] as $key => $values) {
1420 $object['ports'][$values['name']] = $values;
1421 unset($object['ports'][$key]);
1422 }
1423
1424 $newporttypeoptions = getNewPortTypeOptions();
1425
1426// sg_var_dump_html($sysObjectID['port']);
1427
1428 if(!empty($sysObjectID['port'])) {
1429
1430 echo '<br>Vendor / Device specific ports<br>';
1431 echo '<table><tr><th><input type="checkbox" id="moreport" checked="checked" onclick="setchecked(this.id)"></th><th>ifName</th><th>porttypeid</th></tr>';
30bf198b 1432
9276dc97
ME
1433 foreach($sysObjectID['port'] as $name => $port) {
1434
1435 if(array_key_exists($name,$object['ports']))
1436 $disableport = TRUE;
1437 else
1438 $disableport = FALSE;
1439
1440 $comment = '';
1441
1442 if(isset($port['comment'])) {
1443 if(!empty($port['comment']))
1444 $comment = $port['comment'];
1445 }
1446 if(isset($port['uncheck'])) {
1447 $checked = '';
1448 $comment .= ', '.$port['uncheck'];
1449 } else {
1450 $checked = ' checked="checked"';
1451 }
1452
1453 $portcreatecheckbox = '<b style="background-color:'.($disableport ? '#ff0000' : '#00ff00')
1454 .'"><input style="background-color:'.($disableport ? '#ff0000' : '#00ff00').'" class="moreport" type="checkbox" name="portcreate['.$name.']" value="'.$name.'"'
1455 .($disableport ? ' disabled="disbaled"' : $checked ).'></b>';
1456
1457 $formfield = '<input type="hidden" name="ifName['.$name.']" value="'.$name.'">';
1458 echo "<tr>$formfield<td>$portcreatecheckbox</td><td>$name</td>";
1459
1460 if(isset($port['disabled'])) {
1461 $disabledselect = array('disabled' => "disabled");
1462 } else
1463 $disabledselect = array();
1464
1465
1466 foreach($port as $key => $value) {
1467
1468 if($key == 'uncheck' || $key == 'comment')
1469 continue;
30bf198b 1470
9276dc97
ME
1471 /* TODO iif_name */
1472 if($key == 'porttypeid')
1473 $displayvalue = getNiftySelect($newporttypeoptions,
1474 array('name' => "porttypeid[$name]") + $disabledselect, $value);
1475 /* disabled formfied won't be submitted ! */
30bf198b 1476 else
9276dc97
ME
1477 $displayvalue = $value;
1478
1479 $formfield = '<input type="hidden" name="'.$key.'['.$name.']" value="'.$value.'">';
1480 echo "$formfield<td>$displayvalue</td>";
1481 }
1482
1483 $comment = trim($comment,', ');
1484 echo "<td style=\"color:#888888\">$comment</td></tr>";
1485 }
1486 unset($name);
1487 unset($port);
30bf198b 1488
9276dc97
ME
1489 echo '</table>';
1490 }
1491
1492 /* snmp ports */
1493
1494 $ifsnmp = new ifSNMP($snmpdev);
1495
1496 /* ip spaces */
1497
1498 $ipspace = NULL;
1499 foreach($ifsnmp->ipaddress as $ifindex => $ipaddresses) {
1500
1501 foreach($ipaddresses as $ipaddr => $value) {
1502 $addrtype = $value['addrtype'];
1503 $netaddr = $value['net'];
1504 $maskbits = $value['maskbits'];
1505 $netid = NULL;
1506 $linklocal = FALSE;
1507
30bf198b 1508 //echo "<br> - DEBUG: ipspace $ipaddr - $netaddr - $addrtype - $maskbits<br>";
1509
9276dc97
ME
1510 /* check for ip space */
1511 switch($addrtype) {
1512 case 'ipv4':
1513 case 'ipv4z':
cc6eb4ee 1514 if($maskbits == 32)
1515 $netid = 'host';
1516 else
1517 $netid = getIPv4AddressNetworkId(ip_parse($ipaddr));
9276dc97 1518 break;
30bf198b 1519
9276dc97 1520 case 'ipv6':
30bf198b 1521
1522 /* format ipaddr for ip6_parse */
9276dc97
ME
1523 $ipaddr = preg_replace('/((..):(..))/','\\2\\3',$ipaddr);
1524 $ipaddr = preg_replace('/%.*$/','',$ipaddr);
1525
1fc7fc7d 1526 if(ip_checkparse($ipaddr) === false)
1527 continue(2); // 2 because of switch
1528
30bf198b 1529 $ip6_bin = ip6_parse($ipaddr);
1530 $ip6_addr = ip_format($ip6_bin);
1531 $netid = getIPv6AddressNetworkId($ip6_bin);
1532
1533 $node = constructIPRange($ip6_bin, $maskbits);
1534
1535 $netaddr = $node['ip'];
1536 $linklocal = substr($ip6_addr,0,5) == "fe80:";
1537
1538 //echo "<br> - DEBUG: ipspace $ipaddr - $addrtype - $maskbits - $netaddr - >$linklocal<<br>";
1539
9276dc97
ME
1540 break;
1541
1542 case 'ipv6z':
1543 /* link local */
1544 $netid = 'ignore';
1545 break;
1546 default:
1547 }
30bf198b 1548
9276dc97
ME
1549 if(empty($netid) && $netaddr != '::1' && $netaddr != '127.0.0.1' && $netaddr != '127.0.0.0' && $netaddr != '0.0.0.0' && !$linklocal) {
1550
1551 $netaddr .= "/$maskbits";
1552 $ipspace[$netaddr] = $addrtype;
1553 }
1554 }
1555 unset($ipaddr);
1556 unset($value);
1557 unset($addrtype);
1558 }
1559 unset($ifindex);
1560 unset($ipaddresses);
1561
1562 /* print ip spaces table */
1563 if(!empty($ipspace)) {
1564 echo '<br><br>Create IP Spaces';
1565 echo '<table><tr><th><input type="checkbox" id="ipspace" onclick="setchecked(this.id)" checked=\"checked\"></th>';
1566 echo '<th>Type</th><th>prefix</th><th>name</th><th width=150 title="reserve network and router addresses">reserve network / router addresses</th></tr>';
30bf198b 1567
9276dc97
ME
1568 $i = 1;
1569 foreach($ipspace as $prefix => $addrtype) {
30bf198b 1570
9276dc97
ME
1571 $netcreatecheckbox = '<b style="background-color:#00ff00">'
1572 .'<input class="ipspace" style="background-color:#00ff00" type="checkbox" name="netcreate['
1573 .$i.']" value="'.$addrtype.'" checked=\"checked\"></b>';
1574
1575 $netprefixfield = '<input type="text" size=50 name="netprefix['.$i.']" value="'.$prefix.'">';
1576
1577 $netnamefield = '<input type="text" name="netname['.$i.']">';
1578
1579 $netreservecheckbox = '<input type="checkbox" name="netreserve['.$i.']">';
1580
1581 echo "<tr><td>$netcreatecheckbox</td><td style=\"color:#888888\">$addrtype</td><td>$netprefixfield</td><td>$netnamefield</td><td>$netreservecheckbox</td></tr>";
1582
1583 $i++;
1584 }
1585 unset($prefix);
1586 unset($addrtype);
1587 unset($i);
1588
1589 echo '</table>';
1590 }
1591
1592
ccf830c6 1593 echo "<br><br>ifNumber: ".$ifsnmp->ifNumber."<br>indexcount: ".$ifsnmp->indexcount."<br><table><tbody valign=\"top\">";
9276dc97
ME
1594
1595 $portcompat = getPortInterfaceCompat();
1596
1597 $ipnets = array();
1598
cb15032d 1599 $ifsnmp->printifInfoTableHeader("<th>add ip</th><th>add port</th><th title=\"update mac\">upd mac</th><td>upd port type</th><th>porttypeid</th><th>comment</th></tr>");
9276dc97
ME
1600
1601 echo '<tr><td colspan="11"></td>
1602 <td><input type="checkbox" id="ipaddr" onclick="setchecked(this.id)">IPv4<br>
1603 <input type="checkbox" id="ipv6addr" onclick="setchecked(this.id)">IPv6</td>
1604 <td><input type="checkbox" id="ports" onclick="setchecked(this.id)"></td>
1605 <td><input type="checkbox" id="mac" onclick="setchecked(this.id)" checked="checked"></td></tr>';
1606
1607 foreach($ifsnmp as $if) {
1608
1609 $createport = TRUE;
1610 $disableport = FALSE;
1611 $ignoreport = FALSE;
1612 $port_info = NULL;
cb15032d 1613 $updateporttype = false;
9276dc97
ME
1614
1615 $updatemaccheckbox = '';
1616
1617 $hrefs = array();
1618
1619 $comment = "";
1620
1621 if(trim($ifsnmp->ifName($if)) == '') {
1622 $comment .= "no ifName";
1623 $createport = FALSE;
1624 } else {
1625
1626 if(array_key_exists($ifsnmp->ifName($if),$object['ports'])){
1627 $port_info = &$object['ports'][$ifsnmp->ifName($if)];
1628 $comment .= "Name exists";
1629 $createport = FALSE;
1630 $disableport = TRUE;
1631 }
1632 }
1633
1634 if($ifsnmp->ifPhysAddress($if) != '' ) {
1635
1636 $ifPhysAddress = $ifsnmp->ifPhysAddress($if);
1637
1638 $l2port = sg_checkL2Address($ifPhysAddress);
1639
9276dc97
ME
1640 if(!empty($l2port)) {
1641 $l2object_id = key($l2port);
1642
1643 $porthref = makeHref(array('page'=>'object', 'tab' => 'ports',
1644 'object_id' => $l2object_id, 'hl_port_id' => $l2port[$l2object_id]));
1645
1646 $comment .= ", L2Address exists";
1647 $hrefs['ifPhysAddress'] = $porthref;
1648 $createport = FALSE;
1649 // $disableport = TRUE;
1650 $updatemaccheckbox = '';
1651 }
1652
1653 $disablemac = true;
30bf198b 1654 if($disableport) {
9276dc97
ME
1655 if($port_info !== NULL) {
1656 if(str_replace(':','',$port_info['l2address']) != $ifPhysAddress)
1657 $disablemac = false;
1658 else
1659 $disablemac = true;
1660 }
1661 } else {
1662 /* port create always updates mac */
1663 $updatemaccheckbox = '<b style="background-color:#00ff00">'
1664 .'<input style="background-color:'
1665 .'#00ff00" type="checkbox"'
1666 .' checked="checked"'
1667 .' disabled=\"disabled\"></b>';
1668 }
1669
1670 if(!$disablemac)
1671 $updatemaccheckbox = '<b style="background-color:'.($disablemac ? '#ff0000' : '#00ff00').'">'
1672 .'<input class="mac" style="background-color:'
1673 .($disablemac ? '#ff0000' : '#00ff00').'" type="checkbox" name="updatemac['.$if.']" value="'
1674 .$object['ports'][$ifsnmp->ifName($if)]['id'].'" checked="checked"'
1675 .($disablemac ? ' disabled=\"disabled\"' : '' ).'></b>';
30bf198b 1676
9276dc97
ME
1677 }
1678
1679
1680 $porttypeid = guessRToif_id($ifsnmp->ifType($if), $ifsnmp->ifDescr($if));
1681
1682 if(in_array($ifsnmp->ifType($if),$sg_ifType_ignore)) {
1683 $comment .= ", ignore if type";
1684 $createport = FALSE;
1685 $ignoreport = TRUE;
1686 }
cb15032d 1687 else
1688 {
1689 if($port_info)
1690 {
1691 $ptid = $port_info['iif_id']."-".$port_info['oif_id'];
1692 if($porttypeid != $ptid)
1693 {
1694 $comment .= ", Update Type $ptid -> $porttypeid";
1695 $updateporttype = true;
1696 }
1697 }
1698 }
30bf198b 1699
9276dc97
ME
1700 /* ignore ports without an Connector */
1701 if(!$sg_create_noconnector_ports && ($ifsnmp->ifConnectorPresent($if) == 2)) {
1702 $comment .= ", no Connector";
1703 $createport = FALSE;
1704 }
1705
9276dc97
ME
1706 /* Allocate IPs ipv4 and ipv6 */
1707
1708 $ipaddresses = $ifsnmp->ipaddress($if);
30bf198b 1709
9276dc97
ME
1710 if(!empty($ipaddresses)) {
1711
1712 $ipaddrcell = '<table>';
1713
1714 foreach($ipaddresses as $ipaddr => $value) {
1715 $createipaddr = FALSE;
1716 $disableipaddr = FALSE;
1717 $ipaddrhref = '';
1718 $linklocal = FALSE;
30bf198b 1719
9276dc97
ME
1720 $addrtype = $value['addrtype'];
1721 $maskbits = $value['maskbits'];
1722 $bcast = $value['bcast'];
1723
30bf198b 1724 //echo "<br> - DEBUG: ip $ipaddr - $addrtype - $maskbits - $bcast<br>";
1725
9276dc97
ME
1726 switch($addrtype) {
1727 case 'ipv4z':
1728 case 'ipv4':
cc6eb4ee 1729 if($maskbits == 32)
1730 $bcast = "host";
1731
9276dc97
ME
1732 $inputname = 'ip';
1733 break;
30bf198b 1734
9276dc97
ME
1735 case 'ipv6z':
1736 $disableipaddr = TRUE;
1737 case 'ipv6':
1738 $inputname = 'ipv6';
1739
30bf198b 1740 /* format ipaddr for ip6_parse */
9276dc97
ME
1741 $ipaddr = preg_replace('/((..):(..))/','\\2\\3',$ipaddr);
1742 $ipaddr = preg_replace('/%.*$/','',$ipaddr);
30bf198b 1743
1fc7fc7d 1744 if(ip_checkparse($ipaddr) === false)
1745 continue(2); // 2 because of switch
1746
30bf198b 1747 /* ip_parse throws exception on parse errors */
1748 $ip6_bin = ip_parse($ipaddr);
1749 $ipaddr = ip_format($ip6_bin);
1750
1751 $node = constructIPRange($ip6_bin, $maskbits);
1752
1753 $linklocal = ($node['ip'] == 'fe80::');
9276dc97
ME
1754
1755 $createipaddr = FALSE;
1756 break;
1757
30bf198b 1758 } //switch
1759
1760 $address = getIPAddress(ip_parse($ipaddr));
9276dc97
ME
1761
1762 /* only if ip not already allocated */
1763 if(empty($address['allocs'])) {
30bf198b 1764 if(!$ignoreport) {
9276dc97 1765 $createipaddr = TRUE;
30bf198b 1766 }
9276dc97
ME
1767 } else {
1768 $disableipaddr = TRUE;
30bf198b 1769
9276dc97 1770 $ipobject_id = $address['allocs'][0]['object_id'];
30bf198b 1771
9276dc97
ME
1772 $ipaddrhref = makeHref(array('page'=>'object',
1773 'object_id' => $ipobject_id, 'hl_ipv4_addr' => $ipaddr));
30bf198b 1774
9276dc97 1775 }
30bf198b 1776
9276dc97
ME
1777 /* reserved addresses */
1778 if($address['reserved'] == 'yes') {
1779 $comment .= ', '.$address['ip'].' reserved '.$address['name'];
1780 $createipaddr = FALSE;
1781 // $disableipaddr = TRUE;
1782 }
1783
1784 if($ipaddr == '127.0.0.1' || $ipaddr == '0.0.0.0' || $ipaddr == '::1' || $ipaddr == '::' || $linklocal) {
1785 $createipaddr = FALSE;
1786 $disableipaddr = TRUE;
1787 }
1788
1789 if($ipaddr === $bcast) {
1790 $comment .= ", $ipaddr broadcast";
1791 $createipaddr = FALSE;
1792 $disableipaddr = TRUE;
1793 }
1794
30bf198b 1795 if(!$disableipaddr) {
1796 $ipaddrcheckbox = '<b style="background-color:'.($disableipaddr ? '#ff0000' : '#00ff00')
1797 .'"><input class="'.$inputname.'addr" style="background-color:'
1798 .($disableipaddr ? '#ff0000' : '#00ff00')
1799 .'" type="checkbox" name="'.$inputname.'addrcreate['.$ipaddr.']" value="'.$if.'"'
1800 .($disableipaddr ? ' disabled="disabled"' : '')
1801 .($createipaddr ? ' checked="checked"' : '').'></b>';
1802 } else {
1803 $ipaddrcheckbox = '';
1804 }
9276dc97
ME
1805
1806 $ipaddrcell .= "<tr><td>$ipaddrcheckbox</td>";
1807
30bf198b 1808 if(!empty($ipaddrhref)) {
9276dc97 1809 $ipaddrcell .= "<td><a href=$ipaddrhref>$ipaddr/$maskbits</a></td></tr>";
30bf198b 1810 } else {
9276dc97 1811 $ipaddrcell .= "<td>$ipaddr/$maskbits</td></tr>";
30bf198b 1812 }
9276dc97 1813
30bf198b 1814 } // foreach
9276dc97
ME
1815 unset($ipaddr);
1816 unset($value);
30bf198b 1817
9276dc97 1818 $ipaddrcell .= '</table>';
30bf198b 1819
1820 // if(!empty($ipaddresses))
9276dc97
ME
1821 } else {
1822 $ipaddrcreatecheckbox = '';
1823 $ipaddrcell = '';
9276dc97
ME
1824 }
1825
9276dc97 1826 /* checkboxes for add port and add ip */
30bf198b 1827 /* FireFox needs <b style=..>, IE and Opera work with <td style=..> */
9276dc97
ME
1828 if(!$disableport)
1829 $portcreatecheckbox = '<b style="background-color:'.($disableport ? '#ff0000' : '#00ff00')
1830 .'"><input class="ports" style="background-color:'.($disableport ? '#ff0000' : '#00ff00')
1831 .'" type="checkbox" name="portcreate['.$if.']" value="'.$if.'"'
1832 .($disableport ? ' disabled="disbaled"' : '').($createport ? ' checked="checked"' : '').'></b>';
1833 else
1834 $portcreatecheckbox = '';
30bf198b 1835
9276dc97
ME
1836 /* port type id */
1837 /* add port type to newporttypeoptions if missing */
1838 if(strpos(serialize($newporttypeoptions),$porttypeid) === FALSE) {
1839
1840 $portids = explode('-',$porttypeid);
1841 $oif_name = $sg_portoifoptions[$portids[1]];
1842
30bf198b 1843 $newporttypeoptions['auto'] = array($porttypeid => "*$oif_name");
9276dc97
ME
1844 }
1845
1846 $selectoptions = array('name' => "porttypeid[$if]");
1847
cb15032d 1848 if($disableport && !$updateporttype)
9276dc97
ME
1849 $selectoptions['disabled'] = "disabled";
1850
cb15032d 1851 $updateporttypecheckbox = "";
1852
1853
1854 if($updateporttype)
1855 $updateporttypecheckbox = '<b style="background-color:#00ff00;">'
1856 .'<input class="porttype" style="background-color:#00ff00;" type="checkbox" name="updateporttype['.$if.']" value="'
1857 .$port_info['id'].($updateporttype ? '" checked="checked"' : '' ).'></b>';
1858
9276dc97
ME
1859 $porttypeidselect = getNiftySelect($newporttypeoptions, $selectoptions, $porttypeid);
1860
1861 $comment = trim($comment,', ');
1862
cb15032d 1863 $ifsnmp->printifInfoTableRow($if,"<td>$ipaddrcell</td><td>$portcreatecheckbox</td><td>$updatemaccheckbox</td><td>$updateporttypecheckbox</td><td>$porttypeidselect</td><td nowrap=\"nowrap\">$comment</td>", $hrefs);
9276dc97
ME
1864
1865 }
1866 unset($if);
1867
1868 /* preserve snmpconfig */
1869 foreach($_POST as $key => $value) {
1870 echo '<input type=hidden name='.$key.' value='.$value.' />';
1871 }
1872 unset($key);
1873 unset($value);
1874
1875 echo '<tr><td colspan=15 align="right"><p><input id="createbutton" type=submit value="Create Ports and IPs" onclick="return confirm(\'Create selected items?\')"></p></td></tr></tbody></table></form>';
1876
ccf830c6 1877} // END function snmpgeneric_list
9276dc97
ME
1878
1879/* -------------------------------------------------- */
1880function snmpgeneric_opcreate() {
1881
1882 $object_id = $_REQUEST['object_id'];
1883 $attr = getAttrValues($object_id);
1884
1885// sg_var_dump_html($_REQUEST);
1886// sg_var_dump_html($attr);
1887
1888 /* commitUpdateAttrValue ($object_id, $attr_id, $new_value); */
1889 if(isset($_POST['updateattr'])) {
1890 foreach($_POST['updateattr'] as $attr_id => $value) {
1891 // if(empty($attr[$attr_id]['value']))
1892 if(!empty($value)) {
1893 commitUpdateAttrValue ($object_id, $attr_id, $value);
1894 showSuccess("Attribute ".$attr[$attr_id]['name']." set to $value");
1895 }
1896 }
1897 unset($attr_id);
1898 unset($value);
1899 }
1900 /* updateattr */
1901
1902 /* create ports */
1903 if(isset($_POST['portcreate'])) {
1904 foreach($_POST['portcreate'] as $if => $value) {
1905
1906 $ifName = (isset($_POST['ifName'][$if]) ? trim($_POST['ifName'][$if]) : '' );
1907 $ifPhysAddress = (isset($_POST['ifPhysAddress'][$if]) ? trim($_POST['ifPhysAddress'][$if]) : '' );
1908 $ifAlias = (isset($_POST['ifAlias'][$if]) ? trim($_POST['ifAlias'][$if]) : '' );
1909 $ifDescr = (isset($_POST['ifDescr'][$if]) ? trim($_POST['ifDescr'][$if]) : '' );
1910
1911 $visible_label = (empty($ifAlias) ? '' : $ifAlias.'; ').$ifDescr;
1912
1913 if(empty($ifName)) {
1914 showError('Port without ifName '.$_POST['porttypeid'][$if].', '.$visible_label.', '.$ifPhysAddress);
1915 } else {
30bf198b 1916 commitAddPort ($object_id, $ifName, $_POST['porttypeid'][$if], $visible_label, $ifPhysAddress);
9276dc97
ME
1917 showSuccess('Port created '.$ifName.', '.$_POST['porttypeid'][$if].', '.$visible_label.', '.$ifPhysAddress);
1918 }
1919 }
1920 unset($if);
1921 unset($value);
1922 }
1923 /* portcreate */
1924
1925 /* net create */
1926 if(isset($_POST['netcreate'])) {
1927 foreach($_POST['netcreate'] as $id => $addrtype) {
1928 $range = $_POST['netprefix'][$id];
1929 $name = $_POST['netname'][$id];
1930 $is_reserved = isset($_POST['netreserve'][$id]);
1931
1932 if($addrtype == 'ipv4' || $addrtype == 'ipv4z')
1933 createIPv4Prefix($range, $name, $is_reserved);
1934 else
1935 createIPv6Prefix($range, $name, $is_reserved);
1936
1937 showSuccess("$range $name created");
1938
1939 }
1940 unset($id);
1941 unset($addrtype);
1942 }
1943 /* netcreate */
1944
1945 /* allocate ipv6 adresses */
1946 if(isset($_POST['ipv6addrcreate'])) {
30bf198b 1947 foreach($_POST['ipv6addrcreate'] as $ipaddr => $if) {
1948
1949 bindIPv6ToObject(ip6_parse($ipaddr), $object_id,$_POST['ifName'][$if], 1); /* connected */
1950 showSuccess("$ipaddr allocated");
9276dc97
ME
1951 }
1952 unset($ipaddr);
1953 unset($if);
1954 }
1955 /* allocate ip adresses */
1956 if(isset($_POST['ipaddrcreate'])) {
30bf198b 1957 foreach($_POST['ipaddrcreate'] as $ipaddr => $if) {
9276dc97 1958
30bf198b 1959 bindIPToObject(ip_parse($ipaddr), $object_id,$_POST['ifName'][$if], 1); /* connected */
1960 showSuccess("$ipaddr allocated");
9276dc97
ME
1961 }
1962 unset($ipaddr);
1963 unset($if);
1964 }
1965 /* ipaddrecreate */
1966
1967 /* update mac addresses only */
1968 if(isset($_POST['updatemac'])) {
1969 foreach($_POST['updatemac'] as $if => $port_id) {
1970
1971 $ifPhysAddress = (isset($_POST['ifPhysAddress'][$if]) ? trim($_POST['ifPhysAddress'][$if]) : '' );
1972
1973 sg_commitUpdatePortl2address($object_id, $port_id, $ifPhysAddress);
1974
1975 $ifName = (isset($_POST['ifName'][$if]) ? trim($_POST['ifName'][$if]) : '' );
1976 showSuccess("l2address updated on $ifName to $ifPhysAddress");
1977 }
1978 unset($if);
1979 unset($port_id);
1980 }
1981 /* updatemac */
1982
cb15032d 1983 /* update port type */
1984 if(isset($_POST['updateporttype'])) {
1985 foreach($_POST['updateporttype'] as $if => $port_id) {
1986
1987 $porttypeid = (isset($_POST['porttypeid'][$if]) ? trim($_POST['porttypeid'][$if]) : '' );
1988
1989 sg_commitUpdatePortType($object_id, $port_id, $porttypeid);
1990
1991 $ifName = (isset($_POST['ifName'][$if]) ? trim($_POST['ifName'][$if]) : '' );
1992 showSuccess("port type updated on $ifName");
1993 }
1994 unset($if);
1995 unset($port_id);
1996 }
1997 /* updateporttype */
9276dc97
ME
1998} /* snmpgeneric_opcreate */
1999
2000/* -------------------------------------------------- */
2001
2002/* returns RT interface type depending on ifType, ifDescr, .. */
2003function guessRToif_id($ifType,$ifDescr = NULL) {
2004 global $sg_ifType2oif_id;
2005 global $sg_portiifoptions;
2006 global $sg_portoifoptions;
2007
2008 /* default value */
2009 $retval = '24'; /* 1000BASE-T */
2010
2011 if(isset($sg_ifType2oif_id[$ifType])) {
2012 $retval = $sg_ifType2oif_id[$ifType];
2013 }
2014
2015 if(strpos($retval,'-') === FALSE)
2016 $retval = "1-$retval";
2017
2018 /* no ethernetCsmacd */
30bf198b 2019 if($ifType != 6)
9276dc97
ME
2020 return $retval;
2021
2022
2023 /* try to identify outer and inner interface type from ifDescr */
2024
2025 /**********************
2026 * ifDescr samples
2027 *
2028 * Enterasys C3
2029 *
2030 * Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 45 - no sfp inserted
2031 * Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 47 - sfp 1000BASE-SX inserted
2032 *
2033 *
2034 * Enterasys S4
2035 *
2036 * Enterasys Networks, Inc. 1000BASE Gigabit Ethernet Port; No GBIC/MGBIC Inserted
2037 * Enterasys Networks, Inc. 1000BASE-SX Mini GBIC w/LC connector
2038 * Enterasys Networks, Inc. 10GBASE SFP+ 10-Gigabit Ethernet Port; No SFP+ Inserted
2039 * Enterasys Networks, Inc. 10GBASE-SR SFP+ 10-Gigabit Ethernet Port (850nm Short Wavelength, 33/82m MMF, LC)
2040 * Enterasys Networks, Inc. 1000BASE Gigabit Ethernet Port; Unknown GBIC/MGBIC Inserted
30bf198b 2041 *
9276dc97
ME
2042 */
2043
2044 foreach($sg_portiifoptions as $iif_id => $iif_type) {
30bf198b 2045
9276dc97
ME
2046 /* TODO better matching */
2047
2048
2049 /* find iif_type */
2050 if(preg_match('/(.*?)('.preg_quote($iif_type).')(.*)/i',$ifDescr,$matches)) {
2051
2052 $oif_type = "empty ".$iif_type;
2053
2054 $no = preg_match('/ no $/i', $matches[1]);
2055
2056 if(preg_match('/(\d+[G]?)BASE[^ ]+/i', $matches[1], $basematch)) {
2057 $oif_type=$basematch[0];
2058 } else {
2059 if(preg_match('/(\d+[G]?)BASE[^ ]+/i', $matches[3], $basematch)) {
2060 $oif_type=$basematch[0];
2061 }
2062 }
2063
2064 if($iif_id == -1) {
2065 /* 2 => SFP-100 or 4 => SFP-1000 */
2066
2067 if(isset($basematch[1])) {
2068 switch($basematch[1]) {
2069 case '100' :
2070 $iif_id = 2;
2071 $iif_type = "SFP-100";
2072 break;
2073 default:
2074 case '1000' :
30bf198b 2075 $iif_id = 4;
9276dc97
ME
2076 $iif_type = "SFP-1000";
2077 break;
2078 }
2079 }
30bf198b 2080
3049413e 2081 if(preg_match('/sfp 1000-sx/i',$ifDescr))
2082 $oif_type = '1000BASE-SX';
2083
2084 if(preg_match('/sfp 1000-lx/i',$ifDescr))
2085 $oif_type = '1000BASE-LX';
2086
9276dc97
ME
2087 }
2088
2089 if($no) {
2090 $oif_type = "empty ".$iif_type;
2091 }
2092
2093 $oif_type = preg_replace('/BASE/',"Base",$oif_type);
2094
2095 $oif_id = array_search($oif_type,$sg_portoifoptions);
2096
2097 if($oif_id != '') {
2098 $retval = "$iif_id-$oif_id";
2099 }
2100
2101 /* TODO check port compat */
2102
2103 /* stop foreach */
2104 break;
2105 }
2106 }
2107 unset($iif_id);
2108 unset($iif_type);
2109
2110 if(strpos($retval,'-') === FALSE)
2111 $retval = "1-$retval";
2112
2113 return $retval;
2114
2115}
2116
2117/* --------------------------------------------------- */
2118
2119function sg_commitUpdatePortl2address($object_id, $port_id, $port_l2address)
2120{
2121 $db_l2address = l2addressForDatabase ($port_l2address);
2122
2123 global $dbxlink;
2124 $dbxlink->exec ('LOCK TABLES Port WRITE');
2125 if (alreadyUsedL2Address ($db_l2address, $object_id))
2126 {
2127 $dbxlink->exec ('UNLOCK TABLES');
2128 // FIXME: it is more correct to throw InvalidArgException here
2129 // and convert it to InvalidRequestArgException at upper level,
2130 // when there is a mean to do that.
2131 throw new InvalidRequestArgException ('port_l2address', $db_l2address, 'address belongs to another object');
2132 }
2133 usePreparedUpdateBlade
2134 (
2135 'Port',
2136 array
2137 (
2138 'l2address' => ($db_l2address === '') ? NULL : $db_l2address,
2139 ),
2140 array
2141 (
2142 'id' => $port_id,
2143 'object_id' => $object_id
2144 )
2145 );
2146 $dbxlink->exec ('UNLOCK TABLES');
2147} /* sg_commitUpdatePortl2address */
2148
cb15032d 2149/* --------------------------------------------------- */
2150
2151function sg_commitUpdatePortType($object_id, $port_id, $porttypeid)
2152{
2153 global $dbxlink;
2154
2155 list($iif_id, $type) = explode("-",$porttypeid);
2156
2157 $dbxlink->exec ('LOCK TABLES Port WRITE');
2158 usePreparedUpdateBlade
2159 (
2160 'Port',
2161 array
2162 (
2163 'iif_id' => ($iif_id === '') ? NULL : $iif_id,
2164 'type' => ($type === '') ? NULL : $type
2165 ),
2166 array
2167 (
2168 'id' => $port_id,
2169 'object_id' => $object_id
2170 )
2171 );
2172 $dbxlink->exec ('UNLOCK TABLES');
2173} /* sg_commitUpdatePortType */
9276dc97
ME
2174/* ----------------------------------------------------- */
2175
2176/* returns object_id and port_id to a given l2address */
2177function sg_checkL2Address ($address)
2178{
2179 $result = usePreparedSelectBlade
2180 (
2181 'SELECT object_id,id FROM Port WHERE BINARY l2address = ?',
2182 array ($address)
2183 );
2184 $row = $result->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_UNIQUE|PDO::FETCH_COLUMN);
2185 return $row;
2186}
2187
2188/* returns oi_id and name */
2189function getPortOIOptions()
2190{
2191 $result = usePreparedSelectBlade
2192 (
2193 'SELECT dict_key,dict_value from Dictionary where chapter_id = 2',
2194 array ()
2195 );
2196 $row = $result->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_UNIQUE|PDO::FETCH_COLUMN);
2197 return $row;
2198}
2199
8188a17c 2200function sg_checkObjectNameUniqueness ($name, $type_id, $object_id = 0)
2201{
2202 // Some object types do not need unique names
2203 // 1560 - Rack
2204 // 1561 - Row
2205 $dupes_allowed = array (1560, 1561);
2206 if (in_array ($type_id, $dupes_allowed))
2207 return;
2208
2209 $result = usePreparedSelectBlade
2210 (
2211 'SELECT COUNT(*) FROM Object WHERE name = ? AND id != ?',
2212 array ($name, $object_id)
2213 );
2214 $row = $result->fetch (PDO::FETCH_NUM);
2215 if ($row[0] != 0)
2216 return false;
2217 else
2218 return true;
2219}
2220
2221
9276dc97
ME
2222/* ------------------------------------------------------- */
2223class SNMPgeneric {
2224
2225 protected $host;
2226 protected $version;
2227
2228 /* SNMPv1 and SNMPv2c */
2229 protected $community;
2230
2231 /* SNMPv3 */
2232 protected $sec_level;
2233 protected $auth_protocol;
2234 protected $auth_passphrase;
2235 protected $priv_protocol;
2236 protected $priv_passphrase;
2237// protected $contextName;
2238// protected $contextEngineID;
2239
2240 const VERSION_1 = 1;
2241 const VERSION_2C = 2;
2242 const VERSION_3 = 3;
2243
2244 protected $result;
2245
2246 function __construct($version, $host, $community) {
30bf198b 2247
9276dc97
ME
2248 $this->host = $host;
2249 $this->version = $version;
30bf198b 2250 $this->community = $community;
2251
2252 set_error_handler(array($this,'ErrorHandler'), E_WARNING);
9276dc97
ME
2253 }
2254
2255 function setSecurity($sec_level, $auth_protocol = 'md5', $auth_passphrase = '', $priv_protocol = 'des', $priv_passphrase = '') {
2256 $this->sec_level = $sec_level;
2257 $this->auth_protocol = $auth_protocol;
2258 $this->auth_passphrase = $auth_passphrase;
2259 $this->priv_protocol = $priv_protocol;
2260 $this->priv_passphrase = $priv_passphrase;
2261 }
2262
2263 function walk( $oid, $suffix_as_key = FALSE) {
2264
2265 switch($this->version) {
2266 case self::VERSION_1:
2267 if($suffix_as_key){
2268 $this->result = snmpwalk($this->host,$this->community,$oid);
2269 } else {
2270 $this->result = snmprealwalk($this->host,$this->community,$oid);
2271 }
2272 break;
2273
2274 case self::VERSION_2C:
2275 if($suffix_as_key){
2276 $this->result = snmp2_walk($this->host,$this->community,$oid);
2277 } else {
2278 $this->result = snmp2_real_walk($this->host,$this->community,$oid);
2279 }
2280 break;
2281
2282 case self::VERSION_3:
2283 if($suffix_as_key){
2284 $this->result = snmp3_walk($this->host,$this->community, $this->sec_level, $this->auth_protocol, $this->auth_passphrase, $this->priv_protocol, $this->priv_passphrase,$oid);
2285 } else {
2286 $this->result = snmp3_real_walk($this->host,$this->community, $this->sec_level, $this->auth_protocol, $this->auth_passphrase, $this->priv_protocol, $this->priv_passphrase,$oid);
2287 }
2288 break;
2289 }
2290
2291 return $this->result;
2292
2293 }
2294
2295 private function __snmpget($object_id) {
2296
2297 $retval = FALSE;
30bf198b 2298
9276dc97
ME
2299 switch($this->version) {
2300 case self::VERSION_1:
2301 $retval = snmpget($this->host,$this->community,$object_id);
2302 break;
2303
2304 case self::VERSION_2C:
2305 $retval = snmp2_get($this->host,$this->community,$object_id);
2306 break;
2307
2308 case self::VERSION_3:
2309 $retval = snmp3_get($this->host,$this->community, $this->sec_level, $this->auth_protocol, $this->auth_passphrase, $this->priv_protocol, $this->priv_passphrase,$object_id);
2310 break;
2311 }
2312
2313 return $retval;
2314 }
2315
2316 function get($object_id, $preserve_keys = false) {
2317
2318 if(is_array($object_id)) {
2319
2320 if( $preserve_keys ) {
2321 foreach($object_id as $oid) {
2322 $this->result[$oid] = $this->__snmpget($oid);
2323 }
2324 unset($oid);
2325 } else {
2326 foreach($object_id as $oid) {
2327 $result_oid = preg_replace('/.\d$/','',$oid);
2328 $this->result[$result_oid] = $this->__snmpget($oid);
2329 }
2330 unset($oid);
2331 }
2332 } else {
2333 $this->result = $this->__snmpget($object_id);
2334 }
2335
2336 return $this->result;
30bf198b 2337
9276dc97
ME
2338 }
2339
2340 function close() {
2341 }
2342
2343 function getErrno() {
2344 return ($this->result === FALSE);
2345 }
2346
2347 function getError() {
2348 $var = error_get_last();
2349 return $var['message'];
2350 }
30bf198b 2351
2352 function Errorhandler($errno, $errstr, $errfile, $errline) {
2353 switch(TRUE) {
2354 case (False !== strpos($errstr,'No Such Object available on this agent at this OID')):
2355 /* no further error processing */
2356 return true;
2357 break;
2358 }
2359
2360 /* proceed with default error handling */
2361 return false;
2362 }
9276dc97
ME
2363} /* SNMPgeneric */
2364
2365/* ------------------------------------------------------- */
2366/*
30bf198b 2367 * SNMP with system OIDs
9276dc97
ME
2368 */
2369class mySNMP extends SNMPgeneric implements Iterator {
2370
2371 const SNMP_VERSION = SNMPgeneric::VERSION_2C;
2372 const SNMP_COMMUNITY = 'public';
2373
2374 public $lastgetoid;
2375
2376 //private $sysInfo;
2377 private $system;
2378
2379 /* is system table available ? */
2380 private $systemerror = TRUE;
2381
2382 function __construct($version, $host, $community) {
2383 parent::__construct($version, $host, $community);
2384
2385 //snmp_set_valueretrieval(SNMP_VALUE_LIBRARY);
2386
2387 /* Return values without SNMP type hint */
2388 snmp_set_valueretrieval(SNMP_VALUE_PLAIN);
2389
2390 /* needs php >= 5.2.0 */
2391 // snmp_set_oid_output_format(SNMP_OID_OUTPUT_FULL);
30bf198b 2392
9276dc97
ME
2393 // snmp_set_quick_print(1);
2394
2395 } /* __construct */
2396
2397 function init() {
2398 /* .iso.org.dod.internet.mgmt.mib-2.system */
2399 $this->system = $this->walk(".1.3.6.1.2.1.1");
2400
2401 $this->systemerror = $this->getErrno() || empty($this->system);
2402 } /* init() */
2403
2404 /* get value from system cache */
2405 private function _getvalue($object_id) {
2406
2407 /* TODO better matching */
2408
2409 if( isset($this->system["SNMPv2-MIB::$object_id"])) {
2410 $this->lastgetoid = "SNMPv2-MIB::$object_id";
2411 return $this->system["SNMPv2-MIB::$object_id"];
2412 } else {
2413 if( isset($this->system[".iso.org.dod.internet.mgmt.mib-2.system.$object_id"])) {
2414 $this->lastgetoid = ".iso.org.dod.internet.mgmt.mib-2.system.$object_id";
2415 return $this->system[".iso.org.dod.internet.mgmt.mib-2.system.$object_id"];
2416 } else {
2417 if( isset($this->system[$object_id])) {
2418 $this->lastgetoid = $object_id;
2419 return $this->system[$object_id];
2420 } else {
2421 foreach($this->system as $key => $value) {
2422 if(strpos($key, $object_id)) {
2423 $this->lastgetoid = $key;
2424 return $value;
2425 }
2426 }
2427 unset($key);
2428 unset($value);
2429 }
2430 }
2431 }
2432
2433 return NULL;
2434 }
2435
2436 function get($object_id, $preserve_keys = false) {
2437
2438 if(!$this->systemerror)
2439 $retval = $this->_getvalue($object_id);
2440 else
2441 $retval = NULL;
2442
2443 if($retval === NULL) {
2444 $this->lastgetoid = $object_id;
2445 $retval = parent::get($object_id,$preserve_keys);
2446 }
2447
2448 return $retval;
2449
2450 } /* get */
2451
2452 function translatetonumeric($oid) {
2453 global $sg_cmd_snmptranslate;
2454
2455 $val = exec(escapeshellcmd($sg_cmd_snmptranslate).' -On '.escapeshellarg($oid), $output, $retval);
2456
2457 if($retval == 0)
2458 return $val;
2459
2460 return FALSE;
2461
2462 } /* translatetonumeric */
2463/*
2464 function get_new($object_id, $preserve_keys = false) {
2465 $result = parent::get($object_id,$preserve_keys);
2466 return $this->removeDatatype($result);
2467 }
2468
2469 function walk_new($object_id) {
2470 $result = parent::walk($object_id);
2471 return $this->removeDatatype($result);
2472 }
2473
2474*/
2475 /* use snmp_set_valueretrieval(SNMP_VALUE_PLAIN) instead */
2476/* function removeDatatype($val) {
2477 return preg_replace('/^\w+: /','',$val);
2478 }
2479*/
2480 /* make something like $class->sysDescr work */
2481 function __get($name) {
2482 if($this->systemerror) {
2483 return;
2484 }
30bf198b 2485
9276dc97
ME
2486 $retval = $this->_getvalue($name);
2487
2488 if($retval === NULL) {
2489
2490 $trace = debug_backtrace();
30bf198b 2491 trigger_error(
2492