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