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