MW:NagiosStatus

From ChekMate Security Group

Contents

Introduction

This extension allows the wiki server to pull the status page, the host summary or the service summary from your Nagios server. This extension was designed so that the Nagios server could be located on a remote server.

Stay tuned as I may add additional views from Nagios to MediaWiki.

Note: Most up to date version of this extension can be found at: http://www.chekmate.org/wiki/index.php/Projects

Change History

  • 23:46, 24 July 2006 (MDT) - NagiosStatus.php published (Smcnaught)

Maintainer

Shannon McNaught (smcnaught)

Homepage

ChekMate Technical Focus Group

License

NagiosStatus.php 
  - Adds Nagios views to MediaWiki

Copyright (C) 2006  Shannon McNaught

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Installation

Upload NagiosStatus.php to the extensions directory

Define NagiosStatus variables

Modify the following two variables:

 // ###### DEFINE VARIABLES TO Nagios Server ######

 # Root URL
 $Nagios_Web_Server = "http://YourNagiosServerHostName/";
 # Nagios CGI-BIN Directory
 $Nagios_CGI_URL = "http://YourNagiosServerHostName/nagios/cgi-bin/";

Add to LocalSettings.php

Add the following to the end of LocalSettings.php: include("extensions/NagiosStatus.php");

Configure Nagios's Web Server

The Wiki server must be able to pull the status page without any user authentication. This can be accomplished with apache by using "Allow from" and the "Satisfy Any" commands.

In my configuration, it is: (Note: Your configuration will likely be different)

ScriptAlias /nagios/cgi-bin "/usr/local/groundwork/apache2/cgi-bin/nagios"
<Directory "/usr/local/groundwork/apache2/cgi-bin/nagios">
    Allow from (IP Address of Wiki Server)
    Satisfy Any
    AuthType Basic
    require valid-user
    TKTAuthLoginURL http://NagiosServerIPAddress/monitor/login.php
    TKTAuthCookieName       nagios_auth_tkt
    PassEnv LD_LIBRARY_PATH
</Directory>

Modify /skins/monobook/main.css

Comment out backgound in the table style:

table {
/*      background: white; */
        font-size: 100%;
        color: black;
}

Usage

Add one of the three following to your wiki pages:

<NagiosStatus>status</NagiosStatus>

Image:MW-NagiosServicesView.png


<NagiosStatus>hosts</NagiosStatus>

Image:MW-NagiosHostsStatus.png

<NagiosStatus>services</NagiosStatus>

Image:MW-NagiosServicesStatus.png

NagiosStatus.php

<?php
# NagiosStatus Mediawiki extension
#
# original by smcnaught 24.07.2005
# Installation:
#  * put this file (NagiosStatus.php) into the extension directory of your mediawiki installation
#  * change the variables located in the renderNagiosStatus function below
#  * 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>
#


#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 ) {
  global $wgVersion, $wgTitle, $wgDBprefix;

  // ###### DEFINE VARIABLES TO Nagios Server ######

  # Root URL
  $Nagios_Web_Server = "http://YourNagiosServerHostName/";
  # Nagios CGI-BIN Directory
  $Nagios_CGI_URL = "http://YourNagiosServerHostName/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";
  $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 ######
  $aParams = explode("\n", $input);
  foreach($aParams as $sParam) {
    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)) {
              $inbody = 1;
            }
          }
        }
        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;
    }
  }

  return $output;
}

?>

Other sites using this extension