Extending Mediawiki for all your sysadmin needs

Date December 23, 2009

Way back in June of 2008, I mentioned that I had setup an internal wiki (using MediaWiki) here, for my own use, and for anyone else who wanted to use it. At the time, I lamented the fact that I hadn't figured out how to reliably manipulate the wiki database.

A year and a half later, that's still the truth, although I did spend four hours this morning working around that particular problem. I woke up a bit after 4am this morning and of all things, that old problem came to mind. I considered it, and I didn't necessarily want to populate the database, I really just wanted to display non-static information on the individual pages. I thought a little more and decided that there had to be plugins for MediaWiki, and I got up and started working.

Boy was I right. There are a TON of them. Too many, really. Ever walk into a specialty store looking for something, and it's a place the size of a warehouse full of whatever you're looking for, and you're overwhelmed, and can't pick? Sort of like that.

Not wanting to browse 1500 extensions, I figured that it couldn't be that difficult to make my own.

Years ago, I used to be a php programmer. Not a good one, to be sure, but I did sling some code. It was just a while back. It turns out that it's sort of like riding a bike. I found this tutorial, plus some MediaWiki docs, and hammered out some ugly looking code, but gloriously, it worked.

The extension that I wrote allows you to place the following code in your wikipage: <snmp>hostname,OID</snmp> and have the output of snmpwalk inserted into the text of the page. It's like magic, as far as I'm concerned :-)



<?php
// netSNMP.php
//
// The following source code is to be considered public domain.
// Please use it to build on
//
// Matt Simmons ([email protected])
// 20091223

if ( !defined ( 'MEDIAWIKI' ) ) { 
   echo "This is an extension to mediawiki, and cannot stand alone";
   die(1); 
}

$wgExtensionFunctions[] = "netSNMP"; 

function netSNMP() { 
   global $wgParser; 
   $wgParser->setHook( "snmp", "getNetSNMP" ); 
}

function getNetSNMP( $input ) { 
// process the input and produce the output
   $arrInput = explode(",", $input); 
   $data = retrData($arrInput); 
 
   $return = "\n" . $data . "\n"; 
   return $return; 
} // end renderFile()

function retrData($arrInput) { 
// Retrieves the data and returns  a string
   $community="COMMUNITYSTRING"; 

   $snmpHost = $arrInput[0]; 
   $snmpOID = $arrInput[1]; 

   // snmp_walk defaults to v1 
   $arrSNMPRes = snmp2_walk($snmpHost, $community, $snmpOID); 

   $result = implode($arrSNMPRes, "<br>"); 
 
   return $result; 
} // end function retrData()
?>

Yes, it's horribly insecure. Yes, anyone editing any page can query your SNMP-enabled devices (as long as the device has COMMUNITYSTRING as the public string), and it will probably kick your dog. But it's a proof of concept. It works, and it's not hard to write these extensions.

You just throw the script in $IP/extensions, modify your LocalSettings.php file to include it, and you're golden. I wish I would have checked into this much sooner, but now that I know, I can start coming up with good things to enable better wiki usage. I hope this helped.

  • michael

    Thanks for sharing...i am inspired to write my own :)

  • http://ryandlane.com Ryan Lane

    I love this idea. I may take this, and make it more secure by allowing hostnames to be limited to hosts in a MediaWiki: namespace page, and allow for the public string to be defined in the LocalSettings.php file, so that you don't need COMMINITYSTRING as the public string.

    What is the license on this? Can you release it GPL?

  • http://www.standalone-sysadmin.com Matt Simmons

    Just updated the source code. It should be considered public domain. Use it for whatever you'd like.

    Thanks!

  • http://blog.warll.com/ Warll

    You would be rusprised how hard it is to put something into the puplic domain, like really put it in the public domain. Of course there is not much to worry about so long as Matt isn't about to sue. It may not been very well known but the CC zero license is likely the closist thing to public domain possible wordwide.
    http://creativecommons.org/publicdomain/zero/1.0/
    Anecdote: Japan dosn't even have the concept of public domain, plus many other nations make some copyright rights inalienable. Blah, blah, ok so this almost petty at best.

    Speaking of plugins I'm writing one for wordpress, it is much easier than I thought it was going to be.

  • http://www.standalone-sysadmin.com Matt Simmons

    Mauahaha.

    No, I'm not suing anyone for using my code. :-)