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