r77184 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r77183‎ | r77184 | r77185 >
Date:21:10, 23 November 2010
Author:reedy
Status:deferred
Tags:
Comment:
Spaces, tabs, whitespace stylize
Modified paths:
  • /trunk/extensions/SNMPquery/SNMPquery.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SNMPquery/SNMPquery.php
@@ -17,93 +17,94 @@
1818 */
1919
2020 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' );
2222 }
2323
24 -
2524 $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+ );
3231
3332 # Define a setup function
3433 $wgHooks['ParserFirstCallInit'][] = 'SNMP_Setup';
3534
3635 # Add a hook to initialise the magic word
37 -$wgHooks['LanguageGetMagic'][] = 'SNMP_Magic';
 36+$wgHooks['LanguageGetMagic'][] = 'SNMP_Magic';
3837
3938 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;
4342 }
4443
4544 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;
5251 }
5352
5453 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;
6667 }
6768
6869 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 ) );
7576 }
7677
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;
8687 }
8788
8889 /* doesn't work well for templates, eg <snmp host="{{{ip}}}" /> */
8990 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+ }
110111 }
\ No newline at end of file

Status & tagging log