Skip to content

Commit

Permalink
Dymamic creation of PNP templates works prototypically
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasKettner committed Feb 3, 2015
1 parent 3320d10 commit 5cf466e
Show file tree
Hide file tree
Showing 12 changed files with 396 additions and 255 deletions.
2 changes: 1 addition & 1 deletion pnp-templates/.f12
@@ -1,7 +1,7 @@
#!/bin/bash
SITE=${SITE:-$(cat ../.site || true)}
SITE=${SITE:-$(omd sites --bare | head -n 1)}
sudo rsync -varx * /omd/sites/$SITE/share/check_mk/pnp-templates
sudo rsync --delete -varx ./ /omd/sites/$SITE/share/check_mk/pnp-templates/

# do nc to port 32000 only if there is someone listening
# (otherwise complete f12 script breaks if no listener on 32000)
Expand Down
179 changes: 0 additions & 179 deletions pnp-templates/check_mk-df.php

This file was deleted.

125 changes: 125 additions & 0 deletions pnp-templates/default.php
@@ -0,0 +1,125 @@
<?php

# Perfdaten plus Hostname, servicedesc, etc. zu
# lokalem Multisite-Webservice senden. Antwort ist
# die Liste von allen Graphen, mit jeweils Kommandzeile und Graph-Befehl
# $opt[], $def[]. Oder None -> Kein Graph vorhanden. Fallback
# auf PNP-Template. Auch bei HTTP-Fehler Fallbacken.
# URL, die von localhost
# bei OMD: /site/....
# bei Handinstallation localhost:/check_mk/
#
# Copyright (c) 2006-2010 Joerg Linge (http://www.pnp4nagios.org)
# Default Template used if no other template is found.
# Don`t delete this file !
#
# Define some colors ..
#

$omd_site = getenv("OMD_SITE");
if ($omd_site)
$url = "http://localhost/$omd_site/check_mk/";
else
$url = "http://localhost/check_mk/";

# TODO: Timeout handling.
$fd = @fopen($url . "pnp_template.py"
. "?host=" . urlencode($hostname)
. "&service=" . urlencode($servicedesc)
. "&perfdata=" . urlencode($NAGIOS_PERFDATA)
. "&check_command=" . urlencode($NAGIOS_CHECK_COMMAND), "r");
if ($fd) {
while (!feof($fd)) {
$opt[] = trim(fgets($fd));
$def[] = trim(fgets($fd));
}
}
else
{

$_WARNRULE = '#FFFF00';
$_CRITRULE = '#FF0000';
$_AREA = '#256aef';
$_LINE = '#000000';
#
# Initial Logic ...
#

foreach ($this->DS as $KEY=>$VAL) {

$maximum = "";
$minimum = "";
$critical = "";
$crit_min = "";
$crit_max = "";
$warning = "";
$warn_max = "";
$warn_min = "";
$vlabel = " ";
$lower = "";
$upper = "";

if ($VAL['WARN'] != "" && is_numeric($VAL['WARN']) ){
$warning = $VAL['WARN'];
}
if ($VAL['WARN_MAX'] != "" && is_numeric($VAL['WARN_MAX']) ) {
$warn_max = $VAL['WARN_MAX'];
}
if ( $VAL['WARN_MIN'] != "" && is_numeric($VAL['WARN_MIN']) ) {
$warn_min = $VAL['WARN_MIN'];
}
if ( $VAL['CRIT'] != "" && is_numeric($VAL['CRIT']) ) {
$critical = $VAL['CRIT'];
}
if ( $VAL['CRIT_MAX'] != "" && is_numeric($VAL['CRIT_MAX']) ) {
$crit_max = $VAL['CRIT_MAX'];
}
if ( $VAL['CRIT_MIN'] != "" && is_numeric($VAL['CRIT_MIN']) ) {
$crit_min = $VAL['CRIT_MIN'];
}
if ( $VAL['MIN'] != "" && is_numeric($VAL['MIN']) ) {
$lower = " --lower=" . $VAL['MIN'];
$minimum = $VAL['MIN'];
}
if ( $VAL['MAX'] != "" && is_numeric($VAL['MAX']) ) {
$maximum = $VAL['MAX'];
}
if ($VAL['UNIT'] == "%%") {
$vlabel = "%";
$upper = " --upper=101 ";
$lower = " --lower=0 ";
}
else {
$vlabel = $VAL['UNIT'];
}

$opt[$KEY] = '--vertical-label "' . $titel . $vlabel . '" --title "' . $this->MACRO['DISP_HOSTNAME'] . ' / ' . $this->MACRO['DISP_SERVICEDESC'] . '"' . $upper . $lower;
$ds_name[$KEY] = $VAL['LABEL'];
$def[$KEY] = rrd::def ("var1", $VAL['RRDFILE'], $VAL['DS'], "AVERAGE");
$def[$KEY] .= rrd::gradient("var1", "3152A5", "BDC6DE", rrd::cut($VAL['NAME'],16), 20);
$def[$KEY] .= rrd::line1 ("var1", $_LINE );
$def[$KEY] .= rrd::gprint ("var1", array("LAST","MAX","AVERAGE"), "%3.4lf %S".$VAL['UNIT']);
if ($warning != "") {
$def[$KEY] .= rrd::hrule($warning, $_WARNRULE, "Warning $warning \\n");
}
if ($warn_min != "") {
$def[$KEY] .= rrd::hrule($warn_min, $_WARNRULE, "Warning (min) $warn_min \\n");
}
if ($warn_max != "") {
$def[$KEY] .= rrd::hrule($warn_max, $_WARNRULE, "Warning (max) $warn_max \\n");
}
if ($critical != "") {
$def[$KEY] .= rrd::hrule($critical, $_CRITRULE, "Critical $critical \\n");
}
if ($crit_min != "") {
$def[$KEY] .= rrd::hrule($crit_min, $_CRITRULE, "Critical (min) $crit_min \\n");
}
if ($crit_max != "") {
$def[$KEY] .= rrd::hrule($crit_max, $_CRITRULE, "Critical (max) $crit_max \\n");
}
$def[$KEY] .= rrd::comment("Default Template\\r");
$def[$KEY] .= rrd::comment("Command " . $VAL['TEMPLATE'] . "\\r");
}
}

?>
8 changes: 8 additions & 0 deletions scripts/setup.sh
Expand Up @@ -964,6 +964,14 @@ and change the path there. Restart Apache afterwards."
Satisfy any
</Location>
# Webservice for dynamic creation of PNP templates
<Location "${url_prefix}check_mk/pnp_template.py">
Order deny,allow
Deny from all
Allow from 127.0.0.1
Satisfy any
</Location>
</IfModule>
Expand Down
24 changes: 13 additions & 11 deletions web/htdocs/index.py
Expand Up @@ -158,17 +158,19 @@ def handler(req, fields = None, profiling = True):
# during automation action processing (e.g. hooks triggered by restart)
default_permissions.load()

# Special handling for automation.py. Sorry, this must be hardcoded
# here. Automation calls bybass the normal authentication stuff
if html.myfile in [ "automation", "run_cron" ]:
try:
handler()
except Exception, e:
html.write(str(e))
if config.debug:
html.write(html.attrencode(format_exception()))
release_all_locks()
return apache.OK
# Some pages do skip authentication. This is done by adding
# noauth: to the page hander, e.g. "noauth:run_cron" : ...
if handler == page_not_found:
handler = pagehandlers.get("noauth:" + html.myfile, page_not_found)
if handler != page_not_found:
try:
handler()
except Exception, e:
html.write(str(e))
if config.debug:
html.write(html.attrencode(format_exception()))
release_all_locks()
return apache.OK

# Prepare output format
output_format = html.var("output_format", "html")
Expand Down
4 changes: 4 additions & 0 deletions web/htdocs/lib.py
Expand Up @@ -288,6 +288,10 @@ def pnp_cleanup(s):
.replace('/', '_') \
.replace('\\', '_')

# Quote string for use as arguments on the shell
def quote_shell_string(s):
return "'" + s.replace("'", "'\"'\"'") + "'"

ok_marker = '<b class="stmark state0">OK</b>'
warn_marker = '<b class="stmark state1">WARN</b>'
crit_marker = '<b class="stmark state2">CRIT</b>'
Expand Down

0 comments on commit 5cf466e

Please sign in to comment.