Extension talk:NagiosStatus

From mediawiki.org
Latest comment: 16 years ago by SeanFromIT
  1. Does anybody want to help me fix this? $output seems to be null.
The following discussion has been transferred from Meta-Wiki.
Any user names refer to users of that site, who are not necessarily users of MediaWiki.org (even if they share the same username).
Example: <NagiosStatus>host=10.0.0.1</NagiosStatus>
<?php
# NagiosStatus Mediawiki extension
#
# original by smcnaught 24.07.2005  - I am also available on irc.chekmate.org #MediaWiki
# Installation:
#  * put this file (NagiosStatus.php) into the extension directory of your mediawiki installation
#  * add the following to the end of LocalSettings.php: include("extensions/NagiosStatus.php");
#  * configure Nagios's web server to allow wiki server to access without username/password
#  * remove table's background parameter from skin/monobook/main.css style sheet.
#
# Usage:
#  Use one section between <NagiosStatus>-tags for each feed.
#
# Example:
#    <NagiosStatus>status</NagiosStatus>
#    <NagiosStatus>hosts</NagiosStatus>
#    <NagiosStatus>services</NagiosStatus>
#    <NagiosStatus>host=10.0.0.1</NagiosStatus>
#
 

#install extension hook
$wgExtensionFunctions[] = "wfNagiosStatusExtension";
 
#extension hook callback function
function wfNagiosStatusExtension() {
  global $wgParser;
  #install parser hook for <NagiosStatus> tags
  $wgParser->setHook( "NagiosStatus", "renderNagiosStatus" );
}
 
#parser hook callback function
function renderNagiosStatus( $input, $argv, &$parser ) {
  global $wgVersion, $wgTitle, $wgDBprefix, $wgOut, $wgUser, $wgRequest;
 
  // ###### DEFINE VARIABLES TO Nagios Server ######
 
  # Root URL
  $Nagios_Web_Server = "https://nagios/";
  # Nagios CGI-BIN Directory
  $Nagios_CGI_URL = "https://nagios/nagios/cgi-bin/";
 
  // ###### INVALIDATE CACHE ######
 
  $ts = mktime();
  $now = gmdate("YmdHis", $ts + 120);
  $ns = $wgTitle->getNamespace();
  $ti = wfStrencode($wgTitle->getDBkey());
  $version = preg_replace("/^([1-9]).([1-9]).*/", "\\1\\2", $wgVersion);
  if ($version>14) $sql = "UPDATE $wgDBprefix"."page SET page_touched='$now' WHERE page_namespace=$ns AND page_title='$ti'";
  else             $sql = "UPDATE $wgDBprefix"."cur SET cur_touched='$now' WHERE cur_namespace=$ns AND cur_title='$ti'";
  wfQuery($sql, DB_WRITE, "");
 
  // ###### LOAD PAGE ######
 
  $URL = $Nagios_CGI_URL . "status.cgi";
 
  $aParams = explode("\n", $input);
  if (preg_match("/host=/i",$aParams[0])) {
    $URL = $URL . "?" . $aParams[0];
  }
  $loadpage =  file_get_contents($URL);
  $loadpage = preg_replace("/<A /","<A target=_new ",$loadpage);
  $loadpage = preg_replace("/<a /","<a target=_new ",$loadpage);
  $loadpage = preg_replace("/HREF='http:\/\/www.nagios.org/","autoref='http://www.nagios.org",$loadpage);
  $loadpage = preg_replace("/IMG SRC='\//","IMG SRC='$Nagios_Web_Server",$loadpage);
  $loadpage = preg_replace("/img src='\//","IMG SRC='$Nagios_Web_Server",$loadpage);
  $loadpage = preg_replace("/href='\//","autoref='$Nagios_Web_Server",$loadpage);
  $loadpage = preg_replace("/href='/","autoref='$Nagios_CGI_URL",$loadpage);
  $loadpage = preg_replace("/HREF='\//","autoref='$Nagios_Web_Server",$loadpage);
  $loadpage = preg_replace("/HREF='/","autoref='$Nagios_CGI_URL",$loadpage);
  $loadpage = preg_replace("/autoref/","HREF",$loadpage);
  $loadpage = preg_replace("/<\/head>/","",$loadpage);
  $loadpage = preg_replace("/\(null\)/","all",$loadpage);
 
  $elements = explode("\n",$loadpage);
  $inbody = 0;
 

  // ###### PARSE PARAMETERS ######
  foreach($aParams as $sParam) {
  if (preg_match("/host=/i",$sParam)) {
    $sParam = "status";
  }
    switch ($sParam) {
      case 'status':
        foreach ($elements as $element) {
          if (preg_match("/LINK REL='stylesheet'/",$element)) {
            $output = $output . $element . "\n";
          }
          if ($inbody) {
            if (preg_match("/<\/body>/",$element)) {
              $inbody = 0;
            } else {
              $output = $output . $element . "\n";
            }
          } else {
#           if (preg_match("/<body CLASS='status'>/",$element)) {
            if (preg_match("/Service Status Details/",$element)) {
              $inbody = 1;
              $output = $output . $element . "\n";
            }
          }
        }
        break;
      case 'hosts':
        foreach ($elements as $element) {
          if (preg_match("/LINK REL='stylesheet'/",$element)) {
            $output = $output . $element . "\n";
          }
          if ($inbody) {
            if (preg_match("/<\/DIV>/",$element)) {
              $inbody = 0;
            } else {
              $output = $output . $element . "\n";
            }
          } else {
            if (preg_match("/<DIV CLASS='hostTotals'>Host Status Totals<\/DIV>/",$element)) {
              $inbody = 1;
            }
          }
        }
        break;
      case 'services':
        foreach ($elements as $element) {
          if (preg_match("/LINK REL='stylesheet'/",$element)) {
            $output = $output . $element . "\n";
          }
          if ($inbody) {
            if (preg_match("/<\/DIV/",$element)) {
              $inbody = 0;
            } else {
              $output = $output . $element . "\n";
            }
          } else {
            if (preg_match("/<DIV CLASS='serviceTotals'>Service Status Totals<\/DIV>/",$element)) {
              $inbody = 1;
            }
          }
        }
        break;
    }
  }
  $output = "<span class=\"plainlinks\">" . $output. "</span>"; # Remove Pad Locks
 

#  $localParser = new Parser();
#  $output = $localParser->parse("Hello", $parser->mTitle, $parser->mOptions,true,false);
#  $text = $output->getText();
  return $output;
 
}
 
?>


If you have Nagios configured with authentication[edit]

I was able to get partial success with following change in NagiosStatus.php:

# Nagios CGI-BIN Directory
$Nagios_CGI_URL = "http://username:password@server/nagios/cgi-bin/";

Firefox asks for a user and a password, but page is displayed correctly. I get one error in the http log file for each page access. Maybe someone has an idea.

Here is the log error:

Dec 12 11:20:50 2006] [error] [client 171.30.9.14] PHP Notice:  Undefined variable: output 
in /var/www/mwiki/extensions/NagiosStatus.php on line 89, referer: http://server/mwiki/index.php/Main_Page

My Nagios http config file just for referance:

<Directory /usr/lib/nagios/cgi/>
  Options ExecCGI
  AllowOverride None
  order deny,allow
  allow from all
  AuthType Basic
  AuthUserFile /etc/nagios/htpasswd.users
  AuthName "Nagios Access"
  Require valid-user
</Directory>

Ivica 02:59, 12 December 2006 (UTC)Reply