DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:snmplive

snmplive Plugin

Compatible with DokuWiki

No compatibility info given!

plugin Fetches SNMP values on the network and displays them live through Ajax

Last updated on
2009-01-13
Provides
Syntax

This extension has not been updated in over 2 years. It may no longer be maintained or supported and may have compatibility issues.

Similar to xfortune

Tagged with ajax, ip, management

Based on the xfortune-plugin.

It gets the actual 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

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

<?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 :

ajax.php

This is the backend to handle the AJAX requests. Put it into lib/plugins/snmplive/ajax.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);
?>

script.js

The needed JavaScript goes into lib/plugins/snmplive/script.js

/**
 * 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));
}

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 <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).
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:

<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

however it outputs result ok to the page.

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

Robert S. 2011-08-30

Can anyone guide me how to sum several snmplive outputs in dokuwiki?

lolo 2015-03-14

I finally figured it out. You will need 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|

lolo 2015-05-1

  1. 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

  1. leeb 2015-06-17
plugin/snmplive.txt · Last modified: 2017-11-25 21:16 by Aleksandr

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