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