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