DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:snmplive

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
plugin:snmplive [2008-09-18 16:12] 70.88.147.57plugin:snmplive [2017-11-25 21:16] (current) Aleksandr
Line 1: Line 1:
 +====== 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.
 +
 +  <snmplive ip="xxx.xxx.xxx.xxx" oid=".x.x. (...) x.x">
 +
 +==== 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''
 +
 +<code php>
 +<?php
 +/**
 + * Display SNMP values
 + 
 + * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 + * @author     Andreas Gohr <andi@splitbrain.org>
 + * @author     Michael Luggen <michael.luggen@unifr.ch>
 + */
 + 
 +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('<snmplive.*?>',$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 .= "<span id=\"".$data['ip'].$data['oid']."\">";
 +     $renderer->doc .= $this->_getSNMPdata($data['ip'], $data['oid'], $data['community']);
 +            $renderer->doc .= "</span>";
 +            $renderer->doc .= $this->_script($data['ip'],$data['oid'], $data['community']);
 +            return true;
 +        }
 +        return false;
 +    }
 + 
 +    function _script($ip,$oid,$community){
 +        $str  = '<script type="text/javascript" language="javascript">';
 +        $str .= "addEvent(window,'load',plugin_snmplive('".$ip."','".$oid."', '".$community."'));";
 +        $str .= '</script>';
 +        return $str;
 +    }
 + 
 +    /**
 +     * Returns the value of the "snmp variable"
 +     *
 +     * @author Michael Luggen <michael.luggen@unifr.ch>
 +     */
 +    function _getSNMPdata($ip,$oid,$community){
 +        return preg_replace('/(\w+)\: "?([^".]+)"?/','${2}',snmpget($ip, $community, $oid));
 +    }
 +}
 + 
 +//Setup VIM: ex: et ts=4 enc=utf-8 :
 +
 +
 +</code>
 +
 +==== ajax.php ====
 +
 +This is the backend to handle the AJAX requests. Put it into ''lib/plugins/snmplive/ajax.php''.
 +
 +<code php>
 +<?php
 +/**
 + * AJAX Backend Function for plugin_snmplive
 + *
 + * @author Andreas Gohr <andi@splitbrain.org>
 + * @author Michael Luggen <michael.luggen@unifr.ch>
 + */
 + 
 +//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);
 +?>
 +
 +</code>
 +
 +==== script.js ====
 +
 +The needed JavaScript goes into ''lib/plugins/snmplive/script.js''
 +
 +<code javascript>
 +/**
 + * Script for plugin_snmplive
 + *
 + * Fetches the new snmpvalues
 + *
 + * @author Andreas Gohr <andi@splitbrain.org>
 + * @author Michael Luggen <michael.luggen@unifr.ch>
 + */
 + 
 +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));
 +}
 +
 +</code>
 +
 +===== 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:
 +<code sh>
 +$ make install prefix=/usr/share/dokuwiki
 +</code>
 +
 +===== 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 <snmplive...> 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:
 +
 +<code>
 +<snmplive ip="localhost" oid="sysDescr.0">
 +
 +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
 +</code>
 +
 +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**|<snmplive ip="192.168.1.61" oid=".1.3.6.1.4.1.1718.3.2.2.1.12.1.1" community="public"><html>&nbsp;</html> watts|
 +  |**PDU2**|<snmplive ip="192.168.1.62" oid=".1.3.6.1.4.1.1718.3.2.2.1.12.2.1" community="public"><html>&nbsp;</html> watts|
 +  |**PDU3**|<snmplive ip="192.168.1.63" oid=".1.3.6.1.4.1.1718.3.2.2.1.12.1.1" community="public"><html>&nbsp;</html> watts|
 +  |**PDU4**|<snmplive ip="192.168.1.64" oid=".1.3.6.1.4.1.1718.3.2.2.1.12.2.1" community="public"><html>&nbsp;</html> watts|
 +  |**Total input feed:**| ~~=r0c1+r1c1+r2c1+r3c1~~<html>&nbsp;</html> 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//

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki