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