====== snmplive Plugin ====== ---- plugin ---- description: Fetches SNMP values on the network and displays them live through Ajax author : Michael Luggen email : michael.luggen@unifr.ch type : syntax lastupdate : 2009-01-13 compatible : depends : conflicts : similar : xfortune tags : ajax, management, ip downloadurl: http://glen.alkohol.ee/pld/snmplive.tar.gz ---- //Based on the [[plugin:xfortune|xfortune-plugin]].// It gets the actual [[wp>Simple_Network_Management_Protocol|SNMP]] values out of any by the server reachable device (e.g. servers, printers, temp. sensors). I wrote this plugin to realize an easy to set-up server-admin dashboard. ===== Usage ===== You need the IP of the device and the exact oid... NEW: You can now also set the community. ==== Collection of interesting OID's ==== * http://www.oidview.com/mibs/0/Printer-MIB.html * .1.3.6.1.2.1.43.16.5.1.2.1.1 as example shows whats on the Display of a Brother HL-2700CN ===== History ===== * 2009-01-13: enhancement of community-flag by Alexandre Morel * 2007-03-08: first Version ===== Code ===== Create a directory called ''snmplive'' in the ''lib/plugins'' directory and place the following files in there. ==== syntax.php ==== Create this file in ''lib/plugins/snmplive/syntax.php'' * @author Michael Luggen */ if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); require_once(DOKU_PLUGIN.'syntax.php'); class syntax_plugin_snmplive extends DokuWiki_Syntax_Plugin { /** * return some info */ function getInfo(){ return array( 'author' => 'Michael Luggen', 'email' => 'michael.luggen@unifr.ch', 'date' => '2007-03-08', 'name' => 'SNMPlive Plugin', 'desc' => 'Updates various SNMP data live.', 'url' => 'http://www.dokuwiki.org/plugin:snmplive', ); } /** * What kind of syntax are we? */ function getType(){ return 'substition'; } /** * What about paragraphs? */ function getPType(){ return 'normal'; } /** * Where to sort in? */ function getSort(){ return 334; } /** * Connect pattern to lexer */ function connectTo($mode) { $this->Lexer->addSpecialPattern('',$mode,'plugin_snmplive'); } /** * Handle the match */ function handle($match, $state, $pos, &$handler){ $match = substr($match,9,-1); //strip markup from start and end //get values $param = array(); $values = array(); preg_match_all('/(\w*)="(.*?)"/us',$match,$param,PREG_SET_ORDER); foreach($param as $value) { list($m,$k,$v) = $value; $values[$k] = $v; } return $values; } /** * Create output */ function render($mode, &$renderer, $data) { if($mode == 'xhtml'){ $renderer->doc .= ""; $renderer->doc .= $this->_getSNMPdata($data['ip'], $data['oid'], $data['community']); $renderer->doc .= ""; $renderer->doc .= $this->_script($data['ip'],$data['oid'], $data['community']); return true; } return false; } function _script($ip,$oid,$community){ $str = ''; return $str; } /** * Returns the value of the "snmp variable" * * @author Michael Luggen */ function _getSNMPdata($ip,$oid,$community){ return preg_replace('/(\w+)\: "?([^".]+)"?/','${2}',snmpget($ip, $community, $oid)); } } //Setup VIM: ex: et ts=4 enc=utf-8 : ==== ajax.php ==== This is the backend to handle the AJAX requests. Put it into ''lib/plugins/snmplive/ajax.php''. * @author Michael Luggen */ //fix for Opera XMLHttpRequests if(!count($_POST) && $HTTP_RAW_POST_DATA){ parse_str($HTTP_RAW_POST_DATA, $_POST); } if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../').'/'); if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); require_once(DOKU_INC.'inc/init.php'); //close sesseion session_write_close(); require_once(DOKU_INC.'inc/pageutils.php'); require_once(DOKU_INC.'inc/utf8.php'); require_once(DOKU_PLUGIN.'snmplive/syntax.php'); header('Content-Type: text/html; charset=utf-8'); $SNMPip = cleanID($_POST['SNMPip']); $SNMPoid = cleanID($_POST['SNMPoid']); $SNMPcommunity = cleanID($_POST['SNMPcommunity']); print syntax_plugin_snmplive::_getSNMPdata($SNMPip,$SNMPoid,$SNMPcommunity); ?> ==== script.js ==== The needed JavaScript goes into ''lib/plugins/snmplive/script.js'' /** * Script for plugin_snmplive * * Fetches the new snmpvalues * * @author Andreas Gohr * @author Michael Luggen */ function plugin_snmplive(ip,oid,community){ var snmpId = ip + oid + community; if(!document.getElementById){ return; } var obj = document.getElementById(snmpId); if(obj === null){ return; } // We use SACK to do the AJAX requests var ajax = new sack(DOKU_BASE+'lib/plugins/snmplive/ajax.php'); ajax_qsearch.sack.AjaxFailedAlert = ''; ajax_qsearch.sack.encodeURIString = false; // define callback ajax.onCompletion = function(){ var data = this.response; if(data === ''){ return; } var out = document.getElementById(snmpId); out.style.visibility = 'hidden'; out.innerHTML = data; out.style.visibility = 'visible'; // restart timer window.setTimeout("plugin_snmplive('"+ip+"','"+oid+"','"+community+"')",1000); }; ajax.runAJAX('SNMPip='+encodeURI(ip)+'&SNMPoid='+encodeURI(oid)+'&SNMPcommunity='+encodeURI(community)); } ===== Install from tarball ===== There's also tarball for easy installation: http://glen.alkohol.ee/pld/snmplive.tar.gz. (version 2007-03-08) Just Download unpack it and run: $ make install prefix=/usr/share/dokuwiki ===== Known Issues ===== * it can not resolve hostnames (yet) * little problems with the ajax frame work (throws 2 errors at the beginning) * data is not cached (should cache like rss feeds are cached) ===== Discussion ===== I have just installed this plugin using the plugin manager.\\ If I insert any entry on a page, the page renders as //completely// blank - no headers, no footers, no content of any sort... I am using the monobook template (in case that's significant).\\ //[[m.mccarnm@aicr-dot-.org|Michael McCarn]] 2008-09-18// weird, hostnames are resolved for me, using php 5.2.3. however i noticed it first lowercases the oid names and secondly issues some warning: PHP Warning: snmpget(): Invalid object identifier: sysdescr.0 in /usr/share/dokuwiki/lib/plugins/snmplive/syntax.php on line 105, referer: http://localhost/dokuwiki/playground:snmplive however it outputs result ok to the page. --- //[[glen@alkohol.ee|Elan Ruusamäe]] 2007-08-29// A prerequisite for this plugin is the snmp package for php5. On a Debian system you could do apt-get install php5-snmp --- //[[p002748@googlemail.com|Robert S.]] 2011-08-30// Can anyone guide me how to sum several snmplive outputs in dokuwiki? --- //[[w*@yahoo.com|lolo]] 2015-03-14// I finally figured it out. You will need [[plugin:tablecalc|tablecalc plugin]] or something similar: ~~NOCACHE~~ |**PDU1**|  watts| |**PDU2**|  watts| |**PDU3**|  watts| |**PDU4**|  watts| |**Total input feed:**| ~~=r0c1+r1c1+r2c1+r3c1~~  watts| |**Annual cost estimate based on the total power consumption of the system at this load (11¢/kW):**|$~~=round(r4c1*24*0.365*0.11,2)~~/year| --- //[[w*@yahoo.com|lolo]] 2015-05-1// --- //[[na@googlemail.com|leeb]] 2015-06-17// I am struggling to get any output. If I run the command for SNMPGET from command line I get the expected result\\ But running in Dokuwiki I get\\ Warning: snmpget(): No response from xxxxx in /var/www/dokuwiki/lib/plugins/snmplive/syntax.php on line 104\\ --- //[[na@googlemail.com|leeb]] 2015-06-17//