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