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