Index: trunk/extensions/SNMPquery/SNMPquery.php |
— | — | @@ -0,0 +1,109 @@ |
| 2 | +<?php |
| 3 | +/* SNMPquery.php - Fri Dec 4 00:28:58 PST 2009, rururudy |
| 4 | + * Beerware (2009) |
| 5 | + * |
| 6 | + * Example inline: |
| 7 | + * <snmp mode=get host=4.2.2.1 community=public oid=.1.3.6.1.2.1.1.3.0>Uptime: </snmp> |
| 8 | + * |
| 9 | + * Example in a template where the IP is a variable: |
| 10 | + * {{ #snmpget: {{{ip}}}|{{{community}}}|.1.3.6.1.2.1.1.3.0| '''Uptime:''' }} |
| 11 | + * {{ #snmpwalk: {{{ip}}}|{{{community}}}|.1.3.6.1.4.1.14988.1.1.1.1.1.4|<br>'''Signal Strength:'''|dBm}} |
| 12 | + * |
| 13 | + * Note: All my queries are on a fast network, so I set the time out to 50ms... if you have |
| 14 | + * a bunch of queries on one page and one SNMPd server is down, the page will take forever to |
| 15 | + * load if you don't keep that timeout low.... I found 20ms was too short for some servers. |
| 16 | + * |
| 17 | + * Developed for MonkeyBrains.net |
| 18 | + */ |
| 19 | + |
| 20 | +if ( !defined( 'MEDIAWIKI' ) ) { |
| 21 | + die( 'This file is a MediaWiki extension, it is not a valid entry point' ); |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +$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 | + ); |
| 32 | + |
| 33 | +# Define a setup function |
| 34 | +$wgHooks['ParserFirstCallInit'][] = 'SNMP_Setup'; |
| 35 | + |
| 36 | +# Add a hook to initialise the magic word |
| 37 | +$wgHooks['LanguageGetMagic'][] = 'SNMP_Magic'; |
| 38 | + |
| 39 | +function SNMP_Magic( &$magicWords, $langCode ) { |
| 40 | + $magicWords['snmpget'] = array( 0, 'snmpget' ); |
| 41 | + $magicWords['snmpwalk'] = array( 0, 'snmpwalk' ); |
| 42 | + return true; |
| 43 | +} |
| 44 | + |
| 45 | +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; |
| 52 | +} |
| 53 | + |
| 54 | +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; |
| 66 | +} |
| 67 | + |
| 68 | +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)); |
| 75 | +} |
| 76 | + |
| 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; |
| 86 | +} |
| 87 | + |
| 88 | +/* doesn't work well for templates, eg <snmp host="{{{ip}}}" /> */ |
| 89 | +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); |
| 110 | +} |
\ No newline at end of file |
Property changes on: trunk/extensions/SNMPquery/SNMPquery.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 111 | + native |