Index: trunk/extensions/SNMPquery/SNMPquery.php |
— | — | @@ -17,93 +17,94 @@ |
18 | 18 | */ |
19 | 19 | |
20 | 20 | if ( !defined( 'MEDIAWIKI' ) ) { |
21 | | - die( 'This file is a MediaWiki extension, it is not a valid entry point' ); |
| 21 | + die( 'This file is a MediaWiki extension, it is not a valid entry point' ); |
22 | 22 | } |
23 | 23 | |
24 | | - |
25 | 24 | $wgExtensionCredits['parserhook'][] = array( |
26 | | - 'name' => 'SNMPquery', |
27 | | - 'author' => 'Rudy Rucker, Jr.', |
28 | | - 'url' => 'http://www.mediawiki.org/wiki/Extension:SNMPquery', |
29 | | - 'description' => 'Add SNMP queries to your wiki! Example: What is the uptime of your box?', |
30 | | - 'version' => '0.1', |
31 | | - ); |
| 25 | + 'name' => 'SNMPquery', |
| 26 | + 'author' => 'Rudy Rucker, Jr.', |
| 27 | + 'url' => 'http://www.mediawiki.org/wiki/Extension:SNMPquery', |
| 28 | + 'description' => 'Add SNMP queries to your wiki! Example: What is the uptime of your box?', |
| 29 | + 'version' => '0.1', |
| 30 | + ); |
32 | 31 | |
33 | 32 | # Define a setup function |
34 | 33 | $wgHooks['ParserFirstCallInit'][] = 'SNMP_Setup'; |
35 | 34 | |
36 | 35 | # Add a hook to initialise the magic word |
37 | | -$wgHooks['LanguageGetMagic'][] = 'SNMP_Magic'; |
| 36 | +$wgHooks['LanguageGetMagic'][] = 'SNMP_Magic'; |
38 | 37 | |
39 | 38 | function SNMP_Magic( &$magicWords, $langCode ) { |
40 | | - $magicWords['snmpget'] = array( 0, 'snmpget' ); |
41 | | - $magicWords['snmpwalk'] = array( 0, 'snmpwalk' ); |
42 | | - return true; |
| 39 | + $magicWords['snmpget'] = array( 0, 'snmpget' ); |
| 40 | + $magicWords['snmpwalk'] = array( 0, 'snmpwalk' ); |
| 41 | + return true; |
43 | 42 | } |
44 | 43 | |
45 | 44 | function SNMP_Setup() { |
46 | | - global $wgParser; |
47 | | - # Set a function hook associating the "example" magic word with our function |
48 | | - $wgParser->setFunctionHook( 'snmpget', 'SNMPget_Render' ); |
49 | | - $wgParser->setFunctionHook( 'snmpwalk', 'SNMPwalk_Render' ); |
50 | | - $wgParser->setHook( 'snmp', 'SNMP_Tag'); |
51 | | - return true; |
| 45 | + global $wgParser; |
| 46 | + # Set a function hook associating the "example" magic word with our function |
| 47 | + $wgParser->setFunctionHook( 'snmpget', 'SNMPget_Render' ); |
| 48 | + $wgParser->setFunctionHook( 'snmpwalk', 'SNMPwalk_Render' ); |
| 49 | + $wgParser->setHook( 'snmp', 'SNMP_Tag' ); |
| 50 | + return true; |
52 | 51 | } |
53 | 52 | |
54 | 53 | function SNMPwalk_Render( &$parser, $snmphost = '127.0.0.1', $com = 'public', |
55 | | - $snmpoid = 'SNMPv2-SMI::mib-2.1.6.0', $prefix, $suffix) { |
56 | | - $arr = snmpwalk($snmphost, $com, $snmpoid, 50000); |
57 | | - $result=''; |
58 | | - foreach ($arr as $value) { |
59 | | - if (isset($prefix)) |
60 | | - $result .= "$prefix "; |
61 | | - $result .= clean_Result($value) . " "; |
62 | | - if (isset($suffix)) |
63 | | - $result .= $suffix; |
64 | | - } |
65 | | - return $result; |
| 54 | + $snmpoid = 'SNMPv2-SMI::mib-2.1.6.0', $prefix, $suffix ) { |
| 55 | + $arr = snmpwalk( $snmphost, $com, $snmpoid, 50000 ); |
| 56 | + $result = ''; |
| 57 | + foreach ( $arr as $value ) { |
| 58 | + if ( isset( $prefix ) ) { |
| 59 | + $result .= "$prefix "; |
| 60 | + } |
| 61 | + $result .= clean_Result( $value ) . " "; |
| 62 | + if ( isset( $suffix ) ) { |
| 63 | + $result .= $suffix; |
| 64 | + } |
| 65 | + } |
| 66 | + return $result; |
66 | 67 | } |
67 | 68 | |
68 | 69 | function SNMPget_Render( &$parser, $snmphost = '127.0.0.1', $com = 'public', |
69 | | - $snmpoid = 'SNMPv2-SMI::mib-2.1.6.0', $prefix = '') { |
70 | | - $result = snmpget($snmphost, $com, $snmpoid, 50000); |
71 | | - if (! $result) { |
72 | | - return false; |
73 | | - } |
74 | | - return ("$prefix " . clean_Result($result)); |
| 70 | + $snmpoid = 'SNMPv2-SMI::mib-2.1.6.0', $prefix = '' ) { |
| 71 | + $result = snmpget( $snmphost, $com, $snmpoid, 50000 ); |
| 72 | + if ( !$result ) { |
| 73 | + return false; |
| 74 | + } |
| 75 | + return ( "$prefix " . clean_Result( $result ) ); |
75 | 76 | } |
76 | 77 | |
77 | | -function clean_Result($snmpvalue) { |
78 | | - if ($snmpvalue) { |
79 | | - $snmpvalue=preg_replace('/STRING: "(.*)"/', '${1}', $snmpvalue); |
80 | | - $snmpvalue=preg_replace('/Timeticks: \((\d+)\) (.*)/', '${2}', $snmpvalue); |
81 | | - $snmpvalue=preg_replace('/Gauge: (.*)$/', '${1}', $snmpvalue); |
82 | | - $snmpvalue=preg_replace('/INTEGER: (.*)$/', '${1}', $snmpvalue); |
83 | | - return $snmpvalue; |
84 | | - } |
85 | | - return false; |
| 78 | +function clean_Result( $snmpvalue ) { |
| 79 | + if ( $snmpvalue ) { |
| 80 | + $snmpvalue = preg_replace( '/STRING: "(.*)"/', '${1}', $snmpvalue ); |
| 81 | + $snmpvalue = preg_replace( '/Timeticks: \((\d+)\) (.*)/', '${2}', $snmpvalue ); |
| 82 | + $snmpvalue = preg_replace( '/Gauge: (.*)$/', '${1}', $snmpvalue ); |
| 83 | + $snmpvalue = preg_replace( '/INTEGER: (.*)$/', '${1}', $snmpvalue ); |
| 84 | + return $snmpvalue; |
| 85 | + } |
| 86 | + return false; |
86 | 87 | } |
87 | 88 | |
88 | 89 | /* doesn't work well for templates, eg <snmp host="{{{ip}}}" /> */ |
89 | 90 | function SNMP_Tag ( $text, $args, &$parser ) { |
90 | | - #$parser->disableCache(); |
91 | | - $attr = array(); |
92 | | - $snmphost = '127.0.0.1'; |
93 | | - $com = 'public'; |
94 | | - $snmpoid = 'system.SysContact.0'; |
95 | | - $mode = 'get'; |
96 | | - foreach( $args as $name => $value ) { |
97 | | - if ( $name == 'host') |
98 | | - $snmphost=$value; |
99 | | - elseif ( $name == 'oid') |
100 | | - $snmpoid=$value; |
101 | | - elseif ( $name == 'community') |
102 | | - $com=$value; |
103 | | - elseif ( $name == 'mode') |
104 | | - $mode=$value; |
105 | | - } |
106 | | - if ($mode == 'walk') |
107 | | - return SNMPwalk_Render(&$parser, $snmphost, $com, $snmpoid, $text); |
108 | | - else |
109 | | - return SNMPget_Render(&$parser, $snmphost, $com, $snmpoid, $text); |
| 91 | + $snmphost = '127.0.0.1'; |
| 92 | + $com = 'public'; |
| 93 | + $snmpoid = 'system.SysContact.0'; |
| 94 | + $mode = 'get'; |
| 95 | + foreach ( $args as $name => $value ) { |
| 96 | + if ( $name == 'host' ) { |
| 97 | + $snmphost = $value; |
| 98 | + } elseif ( $name == 'oid' ) { |
| 99 | + $snmpoid = $value; |
| 100 | + } elseif ( $name == 'community' ) { |
| 101 | + $com = $value; |
| 102 | + } elseif ( $name == 'mode' ) { |
| 103 | + $mode = $value; |
| 104 | + } |
| 105 | + } |
| 106 | + if ( $mode == 'walk' ) { |
| 107 | + return SNMPwalk_Render( &$parser, $snmphost, $com, $snmpoid, $text ); |
| 108 | + } else { |
| 109 | + return SNMPget_Render( &$parser, $snmphost, $com, $snmpoid, $text ); |
| 110 | + } |
110 | 111 | } |
\ No newline at end of file |