DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:snmplive

This is an old revision of the document!


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

The missing download url means that this extension cannot be installed via the Extension Manager. Please see Publishing a Plugin on dokuwiki.org. Recommended are public repository hosts like GitHub, GitLab or Bitbucket.

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, snmp

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:gallery',
        );
    }
 
    /**
     * 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

plugin/snmplive.1231859451.txt.gz · Last modified: 2009-10-08 23:15 (external edit)

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