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