Imported Upstream version 0.9.6
This commit is contained in:
30
lib/Makefile.in
Normal file
30
lib/Makefile.in
Normal file
@@ -0,0 +1,30 @@
|
||||
prefix=@prefix@
|
||||
DATAROOTDIR=@datarootdir@
|
||||
exec_prefix=@exec_prefix@
|
||||
LOGDIR=@localstatedir@
|
||||
CFGDIR=@sysconfdir@
|
||||
BINDIR=@bindir@
|
||||
CGIDIR=@sbindir@
|
||||
LIBDIR=@libdir@
|
||||
|
||||
INSTALL=@INSTALL@
|
||||
INSTALL_OPTS=@INSTALL_OPTS@
|
||||
|
||||
CP=@CP@
|
||||
|
||||
all html:
|
||||
|
||||
clean:
|
||||
-rm nagiosBp.pm
|
||||
-rm ndodb.pm
|
||||
-rm bsutils.pm
|
||||
-rm settings.pm
|
||||
|
||||
distclean: clean
|
||||
-rm Makefile
|
||||
|
||||
devclean: distclean
|
||||
|
||||
install:
|
||||
$(INSTALL) -m 775 $(INSTALL_OPTS) -d $(DESTDIR)$(LIBDIR)
|
||||
$(INSTALL) -m 664 $(INSTALL_OPTS) *.pm $(DESTDIR)$(LIBDIR)
|
||||
290
lib/bsutils.pm.in
Normal file
290
lib/bsutils.pm.in
Normal file
@@ -0,0 +1,290 @@
|
||||
|
||||
|
||||
# Nagios Business Process View and Nagios Business Process Analysis
|
||||
# Copyright (C) 2003-2010 Sparda-Datenverarbeitung eG, Nuernberg, Germany
|
||||
# Bernd Stroessreuther <berny1@users.sourceforge.net>
|
||||
#
|
||||
# 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; version 2 of the License.
|
||||
#
|
||||
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
|
||||
package bsutils;
|
||||
|
||||
use Exporter;
|
||||
@ISA = qw(Exporter);
|
||||
@EXPORT = qw(printArray printHash fixedLen cutOffSpaces getHostnameFromUrl getProtocolFromUrl);
|
||||
|
||||
|
||||
=head1 NAME
|
||||
|
||||
bsutils - Some functions quite often used (I do not want to rewrite every time)
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
use bsutils;
|
||||
|
||||
printArray(\@a);
|
||||
printArray(\@a, "DEBUG: ");
|
||||
|
||||
printHash(\%h);
|
||||
printHash(\%h, " "); # e. g. if you want to print intended
|
||||
|
||||
print fixedLen("aVeryLongStringWithoutAnySpace", 20, "right") . "\n";
|
||||
# prints out: aVeryLongStringWitho
|
||||
|
||||
$c = "short";
|
||||
$c = fixedLen($c, 20, undef, ".");
|
||||
# $c is now: short...............
|
||||
|
||||
$string = cutOffSpaces("foo ");
|
||||
# $string = "foo"
|
||||
|
||||
$hostname = getHostnameFromUrl("http://www.example.com:80/foo/");
|
||||
# $hostname = "www.example.com";
|
||||
$hostname = getHostnameFromUrl("http://myworkstation.example.com", "s");
|
||||
# $hostname = "myworkstation";
|
||||
|
||||
print getProtocolFromUrl("https://www.example.com/test/") ."\n";
|
||||
# https
|
||||
$p = getProtocolFromUrl("www.example.com");
|
||||
# $p = "http";
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
=head2 bsutils::printArray
|
||||
|
||||
bsutils::printArray(\@array [, $prefix])
|
||||
|
||||
prints out the content of an array in a structured way
|
||||
|
||||
parameter 1: reference to an array
|
||||
parameter 2: prefix for every line of output
|
||||
returns: nothing of value
|
||||
|
||||
=cut
|
||||
|
||||
sub printArray
|
||||
{
|
||||
my $array = shift;
|
||||
my $prefix = shift;
|
||||
my ($i, $len);
|
||||
|
||||
$len=length(@$array - 1);
|
||||
#print "len: $len\n";
|
||||
for ($i=0; $i<@$array; $i++)
|
||||
{
|
||||
$i = sprintf("%0${len}d", $i);
|
||||
print "${prefix}[$i]: $array->[$i]\n";
|
||||
}
|
||||
}
|
||||
|
||||
=head2 bsutils::printHash
|
||||
|
||||
bsutils::printHash(\%hash [, $prefix])
|
||||
|
||||
prints out the content of a hash in a structured way
|
||||
|
||||
parameter 1: reference to a hash
|
||||
parameter 2: prefix for every line of output
|
||||
returns: nothing of value
|
||||
|
||||
=cut
|
||||
|
||||
sub printHash
|
||||
{
|
||||
my $hash = shift;
|
||||
my $prefix = shift || "";
|
||||
my ($key);
|
||||
my $maxlen = 0;
|
||||
|
||||
foreach $key (keys %$hash)
|
||||
{
|
||||
if (length($key) > $maxlen)
|
||||
{
|
||||
$maxlen = length($key);
|
||||
}
|
||||
}
|
||||
#print "max: $maxlen\n";
|
||||
|
||||
foreach $key (keys %$hash)
|
||||
{
|
||||
print "${prefix}" . fixedLen("[$key]", $maxlen+2, "left") . " => $hash->{$key}\n";
|
||||
# print "${prefix}[$key] => $hash->{$key}\n";
|
||||
}
|
||||
}
|
||||
|
||||
=head2 bsutils::fixedLen
|
||||
|
||||
bsutils::fixedLen($string [, $len [, "left"|"right" [, $fillchar]]])
|
||||
|
||||
brings a given string to a fixed length and returns the string afterwards
|
||||
no matter if it is shorter or longer before
|
||||
|
||||
parameter 1: the string
|
||||
parameter 2: the desired length (integer), defaults to 10 if omitted
|
||||
parameter 3: "left" or "right": tells on which side blanks are appended or characters are cut off
|
||||
parameter 4: fillcharacter: 1 character, which should be used to fill up short strings
|
||||
defaults to blank " "
|
||||
returns: the resulting string
|
||||
|
||||
=cut
|
||||
|
||||
sub fixedLen
|
||||
{
|
||||
my $string = shift;
|
||||
my $len = shift || 10;
|
||||
my $side = shift || "right";
|
||||
my $fillchar = shift || " ";
|
||||
my $fillstring;
|
||||
|
||||
if (length($string) > $len)
|
||||
{
|
||||
if ($side eq "left")
|
||||
{
|
||||
$string = substr($string, $len*(-1));
|
||||
}
|
||||
else
|
||||
{
|
||||
$string = substr($string, 0, $len);
|
||||
}
|
||||
}
|
||||
if (length($string) < $len)
|
||||
{
|
||||
$fillchar = substr($fillchar, 0, 1);
|
||||
$fillstring = $fillchar x ($len-length($string));
|
||||
if ($side eq "left")
|
||||
{
|
||||
$string = $fillstring . $string;
|
||||
}
|
||||
else
|
||||
{
|
||||
$string .= $fillstring;
|
||||
}
|
||||
}
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
=head2 bsutils::cutOffSpaces
|
||||
|
||||
bsutils::cutOffSpaces($string)
|
||||
|
||||
cuts of leading and trailing whitespace characters of a given string
|
||||
|
||||
parameter 1: the string
|
||||
returns: the resulting string
|
||||
|
||||
=cut
|
||||
|
||||
sub cutOffSpaces
|
||||
{
|
||||
my $string = shift;
|
||||
$string =~ s/^\s*//;
|
||||
$string =~ s/\s*$//;
|
||||
# does the same as the two lines above, but takes twice as long
|
||||
#$string =~ s/^\s*(.*?)\s*$/$1/;
|
||||
return ($string);
|
||||
}
|
||||
|
||||
=head2 bsutils::getHostnameFromUrl
|
||||
|
||||
bsutils::getHostnameFromUrl($URL [, "s"|"l"])
|
||||
|
||||
from a given URL, we extract the hostname
|
||||
give "s" as second parameter to get the short hostname (everything before the first dot)
|
||||
give "l" or leave empty, to get the full qualified hostname, if it is in the URL as full qualified name
|
||||
|
||||
parameter 1: the URL
|
||||
parameter 2: the return modifier
|
||||
returns: the hostname as string
|
||||
|
||||
=cut
|
||||
|
||||
sub getHostnameFromUrl
|
||||
{
|
||||
my $url = shift;
|
||||
my $switch = shift;
|
||||
|
||||
if ($switch eq "s")
|
||||
{
|
||||
# if an IP is used instead a hostname there is no sense in cutting after the first dot
|
||||
if ($url =~ m/^(.+:\/\/)?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/)
|
||||
{
|
||||
if (defined($2))
|
||||
{
|
||||
return($2);
|
||||
}
|
||||
else
|
||||
{
|
||||
return($1);
|
||||
}
|
||||
}
|
||||
|
||||
$url =~ m/^(.+:\/\/)?([^.:\/]+)/;
|
||||
if (defined($2))
|
||||
{
|
||||
return($2);
|
||||
}
|
||||
else
|
||||
{
|
||||
return($1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$url =~ m/^(.+:\/\/)?([^:\/]+)/;
|
||||
if (defined($2))
|
||||
{
|
||||
return($2);
|
||||
}
|
||||
else
|
||||
{
|
||||
return($1);
|
||||
}
|
||||
}
|
||||
return(undef);
|
||||
}
|
||||
|
||||
=head2 bsutils::getProtocolFromUrl
|
||||
|
||||
bsutils::getProtocolFromUrl($URL)
|
||||
|
||||
from a given URL, we extract the protocol
|
||||
|
||||
parameter 1: the URL
|
||||
returns: the protocol as string
|
||||
|
||||
=cut
|
||||
|
||||
sub getProtocolFromUrl
|
||||
{
|
||||
my $url = shift;
|
||||
$url =~ m/^(.+):\/\//;
|
||||
if (defined($1))
|
||||
{
|
||||
return($1);
|
||||
}
|
||||
else
|
||||
{
|
||||
return("http");
|
||||
}
|
||||
}
|
||||
|
||||
=head1 AUTHOR
|
||||
|
||||
Bernd Stroessreuther <berny1@users.sourceforge.net>
|
||||
|
||||
=cut
|
||||
|
||||
|
||||
return (1);
|
||||
434
lib/nagiosBp.pm.in
Normal file
434
lib/nagiosBp.pm.in
Normal file
@@ -0,0 +1,434 @@
|
||||
|
||||
|
||||
# Nagios Business Process View and Nagios Business Process Analysis
|
||||
# Copyright (C) 2003-2010 Sparda-Datenverarbeitung eG, Nuernberg, Germany
|
||||
# Bernd Stroessreuther <berny1@users.sourceforge.net>
|
||||
#
|
||||
# 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; version 2 of the License.
|
||||
#
|
||||
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
|
||||
package nagiosBp;
|
||||
|
||||
use lib ('@libdir@');
|
||||
use Exporter;
|
||||
use strict;
|
||||
use bsutils;
|
||||
use settings;
|
||||
our $settings = getSettings();
|
||||
our %i18n;
|
||||
our @ISA = qw(Exporter);
|
||||
our @EXPORT = qw(getBPs read_language_file get_lang_string getAvaiableLanguages and listAllComponentsOf);
|
||||
|
||||
|
||||
#parse nagios-bp.conf (our own config file)
|
||||
# parameter 1: the path of nagios-bp.conf file to be used
|
||||
# parameter 2: a reference to the hardstates hash
|
||||
# this hash is extended by this function (states of business processes are added)
|
||||
# parameter 3: "true" or "false", should external scripts be executed
|
||||
# defaults to "true"
|
||||
# returns: hash reference with descriptions of all business processes
|
||||
# returns: hash reference with priorities of all business processes
|
||||
# returns: hash reference with the outputs of all external-info scripts
|
||||
# empty if parameter 3 is "false"
|
||||
# returns: hash reference with all info-urls
|
||||
# returns: hash reference with the formula for each business process
|
||||
sub getBPs()
|
||||
{
|
||||
my $nagios_bp_conf = shift;
|
||||
my $hardstates = shift;
|
||||
my $execute_external_scripts = shift || "true";
|
||||
|
||||
my (@fields, @fields_state, $in, $formula, $num_of_operators, $result, %display_status, %display, %script_out, %info_url, %components, $description, $i, $min_ok, $name_ext, $name, $script, $status, $url, $var);
|
||||
|
||||
open (IN, "<$nagios_bp_conf") or nagdie("unable to read $nagios_bp_conf");
|
||||
while ($in = <IN>)
|
||||
{
|
||||
# filter comments (starting with #) and blank lines
|
||||
if ($in !~ m/^#/ && $in !~ m/^ *$/)
|
||||
{
|
||||
#print "$in";
|
||||
|
||||
# for all display definitions (lines starting with "display")
|
||||
if ($in =~ m/^display/)
|
||||
{
|
||||
$in = substr($in, 8);
|
||||
($status, $name, $description) = split(/;/, $in);
|
||||
chomp($description);
|
||||
$display{$name} = $description;
|
||||
$display_status{$name} = $status;
|
||||
#print "name: $name description: $description\n";
|
||||
}
|
||||
|
||||
# for all external_info definitions (lines starting with "external_info")
|
||||
elsif ($in =~ m/^external_info/)
|
||||
{
|
||||
if ($execute_external_scripts ne "false")
|
||||
{
|
||||
$in = substr($in, 14);
|
||||
($name_ext, $script) = split(/;/, $in);
|
||||
chomp($script);
|
||||
open(SCRIPT, "$script |") or die "unable to execute script $script";
|
||||
$script_out{$name_ext} = <SCRIPT>;
|
||||
close(SCRIPT);
|
||||
#print "name: $name_ext out: $script_out{$name_ext}\n";
|
||||
}
|
||||
}
|
||||
|
||||
# for all info_url definitions (lines starting with "info_url")
|
||||
elsif ($in =~ m/^info_url/)
|
||||
{
|
||||
$in = substr($in, 9);
|
||||
($name_ext, $url) = split(/;/, $in);
|
||||
chomp($url);
|
||||
$info_url{$name_ext} = $url;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
# for all variable definitions (containing a =)
|
||||
if ($in =~ m/=/)
|
||||
{
|
||||
@fields = split(/ *= */, $in);
|
||||
$var = cutOffSpaces($fields[0]);
|
||||
if ($var =~ m/;/ ) { nagdie("variable names are not allowed to contain semicolon") }
|
||||
chomp($fields[1]);
|
||||
$formula = cutOffSpaces($fields[1]);
|
||||
|
||||
$num_of_operators=0;
|
||||
if ($formula =~ m/\|/) { $num_of_operators++ };
|
||||
if ($formula =~ m/\+/) { $num_of_operators++ };
|
||||
if ($formula =~ m/&/) { $num_of_operators++ };
|
||||
if ($num_of_operators > 1) { nagdie("no formulas mixing up the different operators") }
|
||||
# formulas containig only one element are used the same way as "and" formulas
|
||||
if ($formula !~ m/\|/ && $formula !~ m/&/ && $formula !~ m/\+/) { $formula .= " &" }
|
||||
#remember every single variable definition for later reverse lookup
|
||||
$components{$var} = $formula;
|
||||
|
||||
# for formulas with "or"
|
||||
if ($formula =~ m/\|/)
|
||||
{
|
||||
@fields = split(/ *\| */, $formula);
|
||||
@fields_state = ();
|
||||
for ($i=0; $i<@fields; $i++)
|
||||
{
|
||||
@fields_state[$i] = $hardstates->{$fields[$i]};
|
||||
#print "$i: $fields[$i]: $hardstates{$fields[$i]}\n";
|
||||
}
|
||||
$result = &or(@fields_state);
|
||||
#print "$var $result\n";
|
||||
$hardstates->{$var} = $result;
|
||||
}
|
||||
|
||||
# for formulas with "and"
|
||||
if ($formula =~ m/&/)
|
||||
{
|
||||
@fields = split(/ *& */, $formula);
|
||||
@fields_state = ();
|
||||
for ($i=0; $i<@fields; $i++)
|
||||
{
|
||||
@fields_state[$i] = $hardstates->{$fields[$i]};
|
||||
#print "$i: $fields[$i]: $hardstates{$fields[$i]}\n";
|
||||
}
|
||||
$result = &and(@fields_state);
|
||||
#print "$var $result\n";
|
||||
$hardstates->{$var} = $result;
|
||||
}
|
||||
|
||||
# for formulas "x of y" (symbol +)
|
||||
if ($formula =~ m/\+/)
|
||||
{
|
||||
if ($formula =~ m/^(\d+) *of: *(.+)/)
|
||||
{
|
||||
$formula = $2;
|
||||
$min_ok = $1;
|
||||
@fields = split(/ *\+ */, $formula);
|
||||
@fields_state = ();
|
||||
for ($i=0; $i<@fields; $i++)
|
||||
{
|
||||
@fields_state[$i] = $hardstates->{$fields[$i]};
|
||||
#print "$i: $fields[$i]: $hardstates{$fields[$i]}\n";
|
||||
}
|
||||
$result = &x_of_y($min_ok, @fields_state);
|
||||
#print "debug: $var $result\n";
|
||||
$hardstates->{$var} = $result;
|
||||
}
|
||||
else
|
||||
{
|
||||
nagdie('syntax must be: <var> = <num> of: <var1> + <var2> [+ <varn>]*');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
close(IN);
|
||||
#&printHash(\%components, "DEBUG components: ");
|
||||
return(\%display, \%display_status, \%script_out, \%info_url, \%components);
|
||||
}
|
||||
|
||||
|
||||
# making an "and" conjuctions of states
|
||||
sub and()
|
||||
{
|
||||
my @params = @_;
|
||||
my %states = ( 0 => "UNKNOWN", 1 => "OK", 2 => "UNKNOWN", 3 => "WARNING", 4 => "CRITICAL" );
|
||||
my %statesrev = ( "OK" => 1, "UNKNOWN" => 2, "WARNING" => 3, "CRITICAL" => 4);
|
||||
my $i;
|
||||
my $max = 0;
|
||||
my $value;
|
||||
|
||||
for ($i=0; $i<@params; $i++)
|
||||
{
|
||||
$value = $statesrev{$params[$i]};
|
||||
if ($value eq "") {$value = 2}
|
||||
if ($value > $max) { $max = $value }
|
||||
#print "$params[$i]: $value\n";
|
||||
}
|
||||
|
||||
#return
|
||||
$states{$max};
|
||||
}
|
||||
|
||||
# making an "or" conjuctions of states
|
||||
sub or()
|
||||
{
|
||||
my @params = @_;
|
||||
my %states = ( 1 => "OK", 2 => "UNKNOWN", 3 => "WARNING", 4 => "CRITICAL", 5 => "UNKNOWN" );
|
||||
my %statesrev = ( "OK" => 1, "UNKNOWN" => 2, "WARNING" => 3, "CRITICAL" => 4);
|
||||
my $i;
|
||||
my $min = 5;
|
||||
my $value;
|
||||
|
||||
for ($i=0; $i<@params; $i++)
|
||||
{
|
||||
$value = $statesrev{$params[$i]};
|
||||
if ($value eq "") {$value = 2}
|
||||
if ($value < $min) { $min = $value }
|
||||
#print "$params[$i]: $value\n";
|
||||
}
|
||||
|
||||
#return
|
||||
$states{$min};
|
||||
}
|
||||
|
||||
# making an "x_of_y" conjuctions of states
|
||||
sub x_of_y()
|
||||
{
|
||||
my @params = @_;
|
||||
my $i;
|
||||
my %state_counts;
|
||||
my $return = "UNKNOWN";
|
||||
my $min_ok = shift(@params);
|
||||
#print "min_ok: $min_ok\n";
|
||||
|
||||
for ($i=0; $i<@params; $i++)
|
||||
{
|
||||
$state_counts{$params[$i]}++;
|
||||
#print "parm $i: \"$params[$i]\"\n";
|
||||
}
|
||||
|
||||
if ($state_counts{"OK"} >= $min_ok) { $return="OK" }
|
||||
elsif ($state_counts{"OK"} + $state_counts{"WARNING"} >= $min_ok) { $return="WARNING" }
|
||||
else { $return="CRITICAL" }
|
||||
|
||||
#return
|
||||
$return;
|
||||
}
|
||||
|
||||
# internationalization: read the different output strings in a given language
|
||||
# and store the strings in global hash i18n
|
||||
# there it can be accessed by get_lang_string()
|
||||
# param 1: the language, can be a single language abreviation like "de", "en",...
|
||||
# or a string like a Accept-Language HTTP Header
|
||||
# HTTP_ACCEPT_LANGUAGE='en,en-us;q=0.8,de-de;q=0.5,de;q=0.3'
|
||||
# param 2: default language e. g. "en"
|
||||
# if the given language is unavailable
|
||||
sub read_language_file()
|
||||
{
|
||||
my $lang = shift;
|
||||
my $default_lang = shift;
|
||||
|
||||
my ($in, $name, $value, $filename, @languagepriority, $i, @searchresult, $available_lang);
|
||||
|
||||
die "default_lang is not valid\n" if ($default_lang !~ m/^[a-z][a-z]$/);
|
||||
|
||||
chomp($lang);
|
||||
|
||||
$available_lang = &getAvaiableLanguages();
|
||||
|
||||
#print "lang: $lang\n";
|
||||
#print "default_lang: $default_lang\n";
|
||||
#print "available_lang: " . join(", ", @$available_lang) . "\n";
|
||||
|
||||
# extract language out of accept language header
|
||||
if ($lang !~ m/^[a-z][a-z]$/)
|
||||
{
|
||||
#print "lang: $lang\n";
|
||||
@languagepriority = split(/[,;]/, $lang);
|
||||
for ($i=0; $i<@languagepriority; $i++)
|
||||
{
|
||||
next if ($languagepriority[$i] =~ m/^q=/);
|
||||
$languagepriority[$i] =~ s/-[a-z][a-z]//;
|
||||
next if ($languagepriority[$i] !~ m/^[a-z][a-z]$/);
|
||||
#print "$languagepriority[$i]\n";
|
||||
@searchresult = grep(/$languagepriority[$i]/, @$available_lang);
|
||||
#print scalar @searchresult . "\n";
|
||||
if (@searchresult > 0)
|
||||
{
|
||||
$lang = $languagepriority[$i];
|
||||
last;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($lang !~ m/^[a-z][a-z]$/)
|
||||
{
|
||||
$lang = $default_lang;
|
||||
}
|
||||
#print "lang: $lang\n";
|
||||
|
||||
# load the best matching language file
|
||||
$filename = "$settings->{'NAGIOSBP_LANG'}/i18n_$lang.txt";
|
||||
|
||||
if ( ! -r $filename )
|
||||
{
|
||||
$lang = $default_lang;
|
||||
$filename = "$settings->{'NAGIOSBP_LANG'}/i18n_$lang.txt";
|
||||
}
|
||||
|
||||
open(IN, "<$filename") or die "unable to read language file $filename\n";
|
||||
while($in = <IN>)
|
||||
{
|
||||
if ($in !~ m/^\s*#/ && $in !~ m/^\s*$/)
|
||||
{
|
||||
#print $in;
|
||||
chomp($in);
|
||||
($name, $value) = split(/\s*=\s*/, $in);
|
||||
#print "name: $name, value: $value\n";
|
||||
$i18n{$name} = $value;
|
||||
}
|
||||
}
|
||||
close(IN);
|
||||
|
||||
return(1);
|
||||
}
|
||||
|
||||
sub get_lang_string()
|
||||
{
|
||||
my $string = shift;
|
||||
my $substituted = "";
|
||||
my $i=1;
|
||||
my $var="";
|
||||
|
||||
if (defined $i18n{$string})
|
||||
{
|
||||
# allow variable susbstitution
|
||||
$substituted = $i18n{$string};
|
||||
while ($var = shift)
|
||||
{
|
||||
$substituted =~ s/__var${i}__/$var/g;
|
||||
$i++;
|
||||
}
|
||||
|
||||
return($substituted);
|
||||
}
|
||||
else
|
||||
{
|
||||
return($string);
|
||||
}
|
||||
}
|
||||
|
||||
# build a list of available languages
|
||||
sub getAvaiableLanguages()
|
||||
{
|
||||
my (@available_lang, $fname);
|
||||
|
||||
opendir(DIR, $settings->{'NAGIOSBP_LANG'}) or die "unable to open directory for language files $settings->{'NAGIOSBP_LANG'}\n";
|
||||
while ($fname = readdir(DIR))
|
||||
{
|
||||
if (-f "$settings->{'NAGIOSBP_LANG'}/$fname" && $fname =~ m/i18n_([a-z][a-z]).txt$/)
|
||||
{
|
||||
#print "available_lang: $1\n";
|
||||
push(@available_lang, $1);
|
||||
}
|
||||
}
|
||||
closedir(DIR);
|
||||
|
||||
return(\@available_lang);
|
||||
}
|
||||
|
||||
sub nagdie()
|
||||
{
|
||||
print $_[0] . "\n";
|
||||
exit(3);
|
||||
}
|
||||
|
||||
sub listAllComponentsOf()
|
||||
{
|
||||
my $bp = shift;
|
||||
my $bps_hashref = shift;
|
||||
|
||||
my (@act_bp_components, $k, %result_list, $act_component, $bps_left_in_result);
|
||||
|
||||
#print "DEBUG starting func listAllComponentsOf for $bp\n";
|
||||
$result_list{$bp} = 1;
|
||||
$bps_left_in_result = 1;
|
||||
#print "DEBUG func bp: resultlist now contains $bps_left_in_result bps:\n";
|
||||
#printHash(\%result_list);
|
||||
|
||||
while ($bps_left_in_result > 0)
|
||||
{
|
||||
$bps_left_in_result = 0;
|
||||
foreach $act_component (keys %result_list)
|
||||
{
|
||||
if ($act_component !~ m/;/)
|
||||
{
|
||||
$bps_left_in_result++;
|
||||
#print "DEBUG func bp: is bp $act_component\n";
|
||||
$bps_hashref->{$act_component} =~ s/\s*\d+\s+of:\s*//;
|
||||
@act_bp_components = split(/\s*&|\||\+\s*/, $bps_hashref->{$act_component});
|
||||
#print "DEBUG func bp: $act_component contains " . join(/,/, @act_bp_components) . "\n";
|
||||
#print "DEBUG func bp: deleting \"$act_component\"\n";
|
||||
delete($result_list{$act_component});
|
||||
|
||||
for ($k=0; $k<@act_bp_components; $k++)
|
||||
{
|
||||
$act_bp_components[$k] = cutOffSpaces($act_bp_components[$k]);
|
||||
#print "DEBUG func bp: adding \"$act_bp_components[$k]\"\n";
|
||||
$result_list{$act_bp_components[$k]} = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
#print "DEBUG func bp: there were $bps_left_in_result bps\n";
|
||||
#print "DEBUG func bp: resultlist now contains:\n";
|
||||
#printHash(\%result_list);
|
||||
}
|
||||
#foreach $act_bp (keys %$bps_hashref)
|
||||
#{
|
||||
# print "DEBUG func act_bp: $act_bp\n";
|
||||
# #printArray(\@act_bp_components);
|
||||
# for ($k=0; $k<@act_bp_components; $k++)
|
||||
# {
|
||||
# $act_bp_components[$k] = &cutOffSpaces($act_bp_components[$k]);
|
||||
# #print "DEBUG func: act_bp_components \"$act_bp_components[$k]\"\n";
|
||||
# }
|
||||
# #@match = grep(/^$search$/, @component_list);
|
||||
# #printArray(\@match);
|
||||
#}
|
||||
|
||||
return(keys %result_list);
|
||||
}
|
||||
|
||||
return(1);
|
||||
687
lib/ndodb.pm.in
Normal file
687
lib/ndodb.pm.in
Normal file
@@ -0,0 +1,687 @@
|
||||
|
||||
|
||||
# Nagios Business Process View and Nagios Business Process Analysis
|
||||
# Copyright (C) 2003-2010 Sparda-Datenverarbeitung eG, Nuernberg, Germany
|
||||
# Bernd Stroessreuther <berny1@users.sourceforge.net>
|
||||
#
|
||||
# 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; version 2 of the License.
|
||||
#
|
||||
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
|
||||
package ndodb;
|
||||
|
||||
use Exporter;
|
||||
use strict;
|
||||
use DBI;
|
||||
use IO::Socket;
|
||||
use LWP::UserAgent;
|
||||
use JSON::XS;
|
||||
#use Data::Dumper;
|
||||
use Fcntl qw(:DEFAULT :flock);
|
||||
use lib ('@libdir@');
|
||||
use settings;
|
||||
#use bsutils;
|
||||
our $settings = getSettings();
|
||||
our @ISA = qw(Exporter);
|
||||
our @EXPORT = qw(getStates getLastUpdateServiceStatus getDbParam);
|
||||
|
||||
my ($dbh, %dbparam, $sql, $sth, @fields, %hardstates, %statusinfos, $hostdirname, $servicedirname, $parentdirname, $servicelist, $in, $line, $servicename, $hostname, $lasthardstate, $currentstate, $output, $lastservicecheck, @lastservicecheck_local, $rc, $jsonref, $subhash);
|
||||
my $dbConfigFile = $settings->{'NAGIOSBP_ETC'} . "/ndo.cfg";
|
||||
my @services_state=("OK", "WARNING", "CRITICAL", "UNKNOWN");
|
||||
my @host_state=("OK", "CRITICAL", "UNKNOWN");
|
||||
|
||||
|
||||
sub getStates()
|
||||
{
|
||||
my %dbparam = &getDbParam();
|
||||
my $socket;
|
||||
#print STDERR "DEBUG: num of hardstates " . scalar %hardstates . "\n";
|
||||
#print STDERR "DEBUG: num of statusinfos " . scalar %statusinfos . "\n";
|
||||
if ($dbparam{'cache_time'} > 0)
|
||||
{
|
||||
checkCache();
|
||||
#print STDERR "DEBUG: num of hardstates " . scalar %hardstates . "\n";
|
||||
#print STDERR "DEBUG: num of statusinfos " . scalar %statusinfos . "\n";
|
||||
if (scalar %hardstates ne "0")
|
||||
{
|
||||
#print STDERR "DEBUG: using from cache\n";
|
||||
return(\%hardstates, \%statusinfos);
|
||||
}
|
||||
}
|
||||
#print STDERR "DEBUG: fetching info from storage backend\n";
|
||||
#print "DEBUG1: ndo=\"$dbparam{'ndo'}\"\n";
|
||||
if ($dbparam{'ndo'} eq "db")
|
||||
{
|
||||
#print "DEBUG2: ndo=db\n";
|
||||
my $db_prefix = $dbparam{'ndodb_prefix'};
|
||||
|
||||
$dbh = DBI->connect("DBI:mysql:$dbparam{'ndodb_database'}:$dbparam{'ndodb_host'}:$dbparam{'ndodb_port'}", $dbparam{'ndodb_username'}, $dbparam{'ndodb_password'});
|
||||
die "Error: $DBI::errstr\n" unless $dbh;
|
||||
|
||||
#$sql = "select host_name,service_description,last_hard_state,plugin_output from servicestatus";
|
||||
#$sql = "select ${db_prefix}objects.name1,${db_prefix}objects.name2,${db_prefix}servicestatus.current_state,${db_prefix}servicestatus.output from ${db_prefix}objects,${db_prefix}servicestatus where ${db_prefix}objects.objecttype_id=2 and ${db_prefix}objects.is_active=1 and ${db_prefix}objects.object_id=${db_prefix}servicestatus.service_object_id";
|
||||
$sql = "select ${db_prefix}objects.name1,${db_prefix}objects.name2,${db_prefix}servicestatus.last_hard_state,${db_prefix}servicestatus.output from ${db_prefix}objects,${db_prefix}servicestatus where ${db_prefix}objects.objecttype_id=2 and ${db_prefix}objects.is_active=1 and ${db_prefix}objects.object_id=${db_prefix}servicestatus.service_object_id";
|
||||
|
||||
#print STDERR "$sql\n";
|
||||
$sth = $dbh->prepare($sql);
|
||||
die "Error: $DBI::errstr\n" if $DBI::err;
|
||||
|
||||
$sth->execute();
|
||||
die "Error: $DBI::errstr\n" if $DBI::err;
|
||||
|
||||
while (@fields = $sth->fetchrow_array())
|
||||
{
|
||||
#print join("\t", @fields), "\n";
|
||||
$hardstates{"$fields[0];$fields[1]"} = $services_state[$fields[2]] || "UNKNOWN";
|
||||
$statusinfos{"$fields[0];$fields[1]"} = $fields[3];
|
||||
}
|
||||
|
||||
#$sql = "select host_name,host_status,plugin_output from hoststatus";
|
||||
$sql = "select ${db_prefix}objects.name1,${db_prefix}hoststatus.current_state,${db_prefix}hoststatus.output from ${db_prefix}objects,${db_prefix}hoststatus where ${db_prefix}objects.objecttype_id=1 and ${db_prefix}objects.is_active=1 and ${db_prefix}objects.object_id=${db_prefix}hoststatus.host_object_id";
|
||||
#$sql = "select ${db_prefix}objects.name1,${db_prefix}hoststatus.last_hard_state,${db_prefix}hoststatus.output from ${db_prefix}objects,${db_prefix}hoststatus where ${db_prefix}objects.objecttype_id=1 and ${db_prefix}objects.is_active=1 and ${db_prefix}objects.object_id=${db_prefix}hoststatus.host_object_id";
|
||||
|
||||
$sth = $dbh->prepare($sql);
|
||||
die "Error: $DBI::errstr\n" if $DBI::err;
|
||||
|
||||
$sth->execute();
|
||||
die "Error: $DBI::errstr\n" if $DBI::err;
|
||||
|
||||
while (@fields = $sth->fetchrow_array())
|
||||
{
|
||||
#print join("\t", @fields), "\n";
|
||||
$hardstates{"$fields[0];Hoststatus"} = $host_state[$fields[1]] || "UNKNOWN";
|
||||
$statusinfos{"$fields[0];Hoststatus"} = $fields[2];
|
||||
}
|
||||
|
||||
$sth->finish();
|
||||
$dbh->disconnect();
|
||||
}
|
||||
elsif ($dbparam{'ndo'} eq "fs")
|
||||
{
|
||||
#print "DEBUG2: ndo=fs\n";
|
||||
#print "DEBUG: basedir: $dbparam{'ndofs_basedir'}\n";
|
||||
#print "DEBUG: instance: $dbparam{'ndofs_instance_name'}\n\n";
|
||||
|
||||
$servicelist = $dbparam{'ndofs_basedir'} . "/VOLATILE/" . $dbparam{'ndofs_instance_name'} . "/VIEWS/SERVICELIST";
|
||||
$parentdirname="$dbparam{'ndofs_basedir'}/VOLATILE/$dbparam{'ndofs_instance_name'}/HOSTS";
|
||||
|
||||
open (LIST, "<$servicelist") or die "unable to read from file $servicelist\n";
|
||||
flock(LIST, LOCK_SH);
|
||||
|
||||
while ($line = <LIST>)
|
||||
{
|
||||
chomp($line);
|
||||
# print "DEBUG: servicelist: $in\n";
|
||||
# DEBUG: servicelist: "internetconnection":[
|
||||
# DEBUG: servicelist: "Provider 1",
|
||||
# DEBUG: servicelist: "Provider 2"
|
||||
# DEBUG: servicelist: ],
|
||||
if ($line =~ m/"(.+)":\[/)
|
||||
{
|
||||
$hostname = cleanup_for_ndo2fs($1);
|
||||
#print "DEBUG: hostname: $hostname\n";
|
||||
getStatusFromFS($hostname);
|
||||
}
|
||||
if ($line =~ m/"(.+)",?\s*$/)
|
||||
{
|
||||
$servicename = cleanup_for_ndo2fs($1);
|
||||
#print "DEBUG: servicename: $hostname:$servicename\n";
|
||||
getStatusFromFS($hostname, $servicename);
|
||||
}
|
||||
}
|
||||
close(LIST);
|
||||
}
|
||||
elsif ($dbparam{'ndo'} eq "merlin")
|
||||
{
|
||||
#print "DEBUG2: ndo=db\n";
|
||||
my $db_prefix = $dbparam{'ndodb_prefix'};
|
||||
|
||||
$dbh = DBI->connect("DBI:mysql:$dbparam{'ndodb_database'}:$dbparam{'ndodb_host'}:$dbparam{'ndodb_port'}", $dbparam{'ndodb_username'}, $dbparam{'ndodb_password'});
|
||||
die "Error: $DBI::errstr\n" unless $dbh;
|
||||
|
||||
#$sql = "select host_name,service_description,last_hard_state,plugin_output from servicestatus";
|
||||
#$sql = "select ${db_prefix}objects.name1,${db_prefix}objects.name2,${db_prefix}servicestatus.last_hard_state,${db_prefix}servicestatus.output from ${db_prefix}objects,${db_prefix}servicestatus where ${db_prefix}objects.objecttype_id=2 and ${db_prefix}objects.is_active=1 and ${db_prefix}objects.object_id=${db_prefix}servicestatus.service_object_id";
|
||||
$sql = "select host_name,service_description,last_hard_state,output from service";
|
||||
|
||||
#print STDERR "$sql\n";
|
||||
$sth = $dbh->prepare($sql);
|
||||
die "Error: $DBI::errstr\n" if $DBI::err;
|
||||
|
||||
$sth->execute();
|
||||
die "Error: $DBI::errstr\n" if $DBI::err;
|
||||
|
||||
while (@fields = $sth->fetchrow_array())
|
||||
{
|
||||
#print join("\t", @fields), "\n";
|
||||
$hardstates{"$fields[0];$fields[1]"} = $services_state[$fields[2]] || "UNKNOWN";
|
||||
$statusinfos{"$fields[0];$fields[1]"} = $fields[3];
|
||||
}
|
||||
|
||||
#$sql = "select host_name,host_status,plugin_output from hoststatus";
|
||||
#$sql = "select ${db_prefix}objects.name1,${db_prefix}hoststatus.current_state,${db_prefix}hoststatus.output from ${db_prefix}objects,${db_prefix}hoststatus where ${db_prefix}objects.objecttype_id=1 and ${db_prefix}objects.is_active=1 and ${db_prefix}objects.object_id=${db_prefix}hoststatus.host_object_id";
|
||||
#$sql = "select host_name,last_hard_state,output from host";
|
||||
$sql = "select host_name,current_state,output from host";
|
||||
|
||||
$sth = $dbh->prepare($sql);
|
||||
die "Error: $DBI::errstr\n" if $DBI::err;
|
||||
|
||||
$sth->execute();
|
||||
die "Error: $DBI::errstr\n" if $DBI::err;
|
||||
|
||||
while (@fields = $sth->fetchrow_array())
|
||||
{
|
||||
#print join("\t", @fields), "\n";
|
||||
$hardstates{"$fields[0];Hoststatus"} = $host_state[$fields[1]] || "UNKNOWN";
|
||||
$statusinfos{"$fields[0];Hoststatus"} = $fields[2];
|
||||
}
|
||||
|
||||
$sth->finish();
|
||||
$dbh->disconnect();
|
||||
}
|
||||
elsif ($dbparam{'ndo'} eq "mk_livestatus")
|
||||
{
|
||||
$socket = IO::Socket::UNIX->new ("Peer" => $dbparam{'ndo_livestatus_socket'}, "Type" => SOCK_STREAM, "Timeout" => 15) or die "unable to connect to unix socket \"" . $dbparam{'ndo_livestatus_socket'} . "\": $!\n";
|
||||
|
||||
print $socket "GET services\n";
|
||||
print $socket "Columns: host_name description last_hard_state plugin_output\n\n";
|
||||
|
||||
while ($in = <$socket>)
|
||||
{
|
||||
chomp($in);
|
||||
#print STDERR "DEBUG: $in\n";
|
||||
|
||||
@fields = split(/;/, $in);
|
||||
$hardstates{"$fields[0];$fields[1]"} = $services_state[$fields[2]] || "UNKNOWN";
|
||||
$statusinfos{"$fields[0];$fields[1]"} = $fields[3];
|
||||
}
|
||||
|
||||
$socket = IO::Socket::UNIX->new ("Peer" => $dbparam{'ndo_livestatus_socket'}, "Type" => SOCK_STREAM, "Timeout" => 15) or die "unable to connect to unix socket \"" . $dbparam{'ndo_livestatus_socket'} . "\": $!\n";
|
||||
|
||||
print $socket "GET hosts\n";
|
||||
print $socket "Columns: name state plugin_output\n\n";
|
||||
|
||||
while ($in = <$socket>)
|
||||
{
|
||||
chomp($in);
|
||||
#print STDERR "DEBUG: $in\n";
|
||||
|
||||
@fields = split(/;/, $in);
|
||||
$hardstates{"$fields[0];Hoststatus"} = $host_state[$fields[1]] || "UNKNOWN";
|
||||
$statusinfos{"$fields[0];Hoststatus"} = $fields[2];
|
||||
}
|
||||
}
|
||||
elsif ($dbparam{'ndo'} eq "icinga-web")
|
||||
{
|
||||
#print "DEBUG2: ndo=icinga-web\n";
|
||||
my $maxConnectionTime = 10;
|
||||
#print STDERR "URL prefix: " . $dbparam{'ndo_icinga_web_url_prefix'} . "\n";
|
||||
if(substr($dbparam{'ndo_icinga_web_url_prefix'}, -1) eq "/")
|
||||
{
|
||||
$dbparam{'ndo_icinga_web_url_prefix'} = substr($dbparam{'ndo_icinga_web_url_prefix'}, 0, length($dbparam{'ndo_icinga_web_url_prefix'}) -1);
|
||||
}
|
||||
#print STDERR "URL prefix: " . $dbparam{'ndo_icinga_web_url_prefix'} . "\n";
|
||||
my $services_url = $dbparam{'ndo_icinga_web_url_prefix'} . "/web/api/service/columns%5BSERVICE_NAME%7CHOST_NAME%7CSERVICE_LAST_HARD_STATE%7CSERVICE_OUTPUT%5D/authkey=" . $dbparam{'ndo_icinga_web_auth_key'} . "/json";
|
||||
my $hosts_url = $dbparam{'ndo_icinga_web_url_prefix'} . "/web/api/host/columns%5BHOST_NAME%7CHOST_CURRENT_STATE%7CHOST_OUTPUT%5D/authkey=" . $dbparam{'ndo_icinga_web_auth_key'} . "/json";
|
||||
my ($ua, $request, $result, $content);
|
||||
|
||||
#print STDERR "URL: $services_url\n";
|
||||
$ua = new LWP::UserAgent ( 'timeout' => $maxConnectionTime );
|
||||
$request = new HTTP::Request ('GET' => "$services_url");
|
||||
$result = $ua->request($request);
|
||||
#print STDERR "Response Services: " . $result->code() . " " . $result->message() . "\n";
|
||||
|
||||
if ($result->code() >= 400)
|
||||
{
|
||||
die "Error when requesting service information from icinga API, response code: " . $result->code() . ", message: " . $result->message() . "\n";
|
||||
}
|
||||
|
||||
$content = $result->decoded_content();
|
||||
$content =~ s/\r\n/\n/g;
|
||||
#print STDERR "Content: $content\n";
|
||||
|
||||
$jsonref = decode_json($content);
|
||||
|
||||
#print "DEBUG: $jsonref\n";
|
||||
#print "DEBUG: " . Dumper($jsonref) . "\n";
|
||||
#print "DEBUG ref: " . ref($jsonref) . "\n";
|
||||
if (ref($jsonref) eq "HASH" && defined $jsonref->{'error'})
|
||||
{
|
||||
die "Error when requesting service information from icinga API, message: $jsonref->{'error'}->[0]->{'message'}\nerrors: $jsonref->{'error'}->[0]->{'errors'}->[0]\n";
|
||||
}
|
||||
|
||||
foreach $subhash (@$jsonref)
|
||||
{
|
||||
#print "\nDEBUG subhash: $subhash\n";
|
||||
#print "DEBUG service: $subhash->{'HOST_NAME'};$subhash->{'SERVICE_NAME'} = $services_state[$subhash->{'SERVICE_LAST_HARD_STATE'}]\n";
|
||||
#SERVICE_LAST_HARD_STATE
|
||||
#SERVICE_NAME
|
||||
#HOST_NAME
|
||||
$hardstates{"$subhash->{'HOST_NAME'};$subhash->{'SERVICE_NAME'}"} = $services_state[$subhash->{'SERVICE_LAST_HARD_STATE'}] || "UNKNOWN";
|
||||
$statusinfos{"$subhash->{'HOST_NAME'};$subhash->{'SERVICE_NAME'}"} = $subhash->{'SERVICE_OUTPUT'}
|
||||
}
|
||||
|
||||
|
||||
#print STDERR "URL: $hosts_url\n";
|
||||
$request = new HTTP::Request ('GET' => "$hosts_url");
|
||||
$result = $ua->request($request);
|
||||
#print STDERR "Response Hosts: " . $result->code() . " " . $result->message() . "\n";
|
||||
|
||||
if ($result->code() >= 400)
|
||||
{
|
||||
die "Error when requesting host information from icinga API, response code: " . $result->code() . ", message: " . $result->message() . "\n";
|
||||
}
|
||||
|
||||
$content = $result->decoded_content();
|
||||
$content =~ s/\r\n/\n/g;
|
||||
#print STDERR "Content: $content\n";
|
||||
|
||||
$jsonref = decode_json($content);
|
||||
|
||||
#print "DEBUG: $jsonref\n";
|
||||
#print "DEBUG: " . Dumper($jsonref) . "\n";
|
||||
if (ref($jsonref) eq "HASH" && defined $jsonref->{'error'})
|
||||
{
|
||||
die "Error when requesting host information from icinga API, message: $jsonref->{'error'}->[0]->{'message'}\nerrors: $jsonref->{'error'}->[0]->{'errors'}->[0]\n";
|
||||
}
|
||||
|
||||
foreach $subhash (@$jsonref)
|
||||
{
|
||||
#print "DEBUG subhash: $subhash\n";
|
||||
#print "DEBUG host: $subhash->{'HOST_NAME'};Hoststatus = $services_state[$subhash->{'HOST_CURRENT_STATE'}]\n";
|
||||
#HOST_NAME
|
||||
#HOST_CURRENT_STATE
|
||||
#HOST_OUTPUT
|
||||
$hardstates{"$subhash->{'HOST_NAME'};Hoststatus"} = $host_state[$subhash->{'HOST_CURRENT_STATE'}] || "UNKNOWN";
|
||||
$statusinfos{"$subhash->{'HOST_NAME'};Hoststatus"} = $subhash->{'HOST_OUTPUT'}
|
||||
}
|
||||
}
|
||||
|
||||
if ($dbparam{'cache_time'} > 0)
|
||||
{
|
||||
updateCache(\%hardstates, \%statusinfos);
|
||||
}
|
||||
return(\%hardstates, \%statusinfos);
|
||||
}
|
||||
|
||||
sub getLastUpdateServiceStatus()
|
||||
{
|
||||
my %dbparam = &getDbParam();
|
||||
my ($db_prefix, $return, $socket);
|
||||
|
||||
if ($dbparam{'ndo'} eq "db")
|
||||
{
|
||||
$db_prefix = $dbparam{'ndodb_prefix'};
|
||||
$dbh = DBI->connect("DBI:mysql:$dbparam{'ndodb_database'}:$dbparam{'ndodb_host'}:$dbparam{'ndodb_port'}", $dbparam{'ndodb_username'}, $dbparam{'ndodb_password'});
|
||||
die "Error: $DBI::errstr\n" unless $dbh;
|
||||
|
||||
$sql = "select max(last_check) from ${db_prefix}servicestatus";
|
||||
|
||||
$sth = $dbh->prepare($sql);
|
||||
die "Error: $DBI::errstr\n" if $DBI::err;
|
||||
|
||||
$sth->execute();
|
||||
die "Error: $DBI::errstr\n" if $DBI::err;
|
||||
|
||||
while (@fields = $sth->fetchrow_array())
|
||||
{
|
||||
#print join("\t", @fields), "\n";
|
||||
$return = $fields[0];
|
||||
}
|
||||
|
||||
$sth->finish();
|
||||
$dbh->disconnect();
|
||||
}
|
||||
elsif ($dbparam{'ndo'} eq "fs")
|
||||
{
|
||||
$servicelist = $dbparam{'ndofs_basedir'} . "/VOLATILE/" . $dbparam{'ndofs_instance_name'} . "/VIEWS/SERVICELIST";
|
||||
$parentdirname="$dbparam{'ndofs_basedir'}/VOLATILE/$dbparam{'ndofs_instance_name'}/HOSTS";
|
||||
|
||||
open (LIST, "<$servicelist") or die "unable to read from file $servicelist\n";
|
||||
flock(LIST, LOCK_SH);
|
||||
|
||||
while ($line = <LIST>)
|
||||
{
|
||||
chomp($line);
|
||||
# print "DEBUG: servicelist: $in\n";
|
||||
# DEBUG: servicelist: "internetconnection":[
|
||||
# DEBUG: servicelist: "Provider 1",
|
||||
# DEBUG: servicelist: "Provider 2"
|
||||
# DEBUG: servicelist: ],
|
||||
if ($line =~ m/"(.+)":\[/)
|
||||
{
|
||||
$hostname = cleanup_for_ndo2fs($1);
|
||||
#print "DEBUG: hostname: $hostname\n";
|
||||
}
|
||||
if ($line =~ m/"(.+)",?\s*$/)
|
||||
{
|
||||
$servicename = cleanup_for_ndo2fs($1);
|
||||
#print "DEBUG: servicename: $hostname:$servicename\n";
|
||||
|
||||
if (-e "$parentdirname/$hostname/$servicename/STATUS")
|
||||
{
|
||||
open (IN, "<$parentdirname/$hostname/$servicename/STATUS") or die "unable to read file $parentdirname/$hostname/$servicename/STATUS: $!\n";
|
||||
flock(IN, LOCK_SH);
|
||||
while ($in = <IN>)
|
||||
{
|
||||
if ($in =~ m/"LASTSERVICECHECK":\s*"(.*)"/)
|
||||
{
|
||||
#print "$1\n";
|
||||
if ($1 > $lastservicecheck)
|
||||
{
|
||||
$lastservicecheck = $1;
|
||||
#print "$lastservicecheck\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
close(IN);
|
||||
}
|
||||
}
|
||||
}
|
||||
close(LIST);
|
||||
|
||||
@lastservicecheck_local = localtime($lastservicecheck);
|
||||
$lastservicecheck_local[5]+=1900;
|
||||
$lastservicecheck_local[4] = sprintf("%02d", ++$lastservicecheck_local[4]);
|
||||
$lastservicecheck_local[3] = sprintf("%02d", $lastservicecheck_local[3]);
|
||||
$lastservicecheck_local[2] = sprintf("%02d", $lastservicecheck_local[2]);
|
||||
$lastservicecheck_local[1] = sprintf("%02d", $lastservicecheck_local[1]);
|
||||
$lastservicecheck_local[0] = sprintf("%02d", $lastservicecheck_local[0]);
|
||||
|
||||
$return = "$lastservicecheck_local[5]-$lastservicecheck_local[4]-$lastservicecheck_local[3] $lastservicecheck_local[2]:$lastservicecheck_local[1]:$lastservicecheck_local[0]";
|
||||
}
|
||||
elsif ($dbparam{'ndo'} eq "merlin")
|
||||
{
|
||||
$db_prefix = $dbparam{'ndodb_prefix'};
|
||||
$dbh = DBI->connect("DBI:mysql:$dbparam{'ndodb_database'}:$dbparam{'ndodb_host'}:$dbparam{'ndodb_port'}", $dbparam{'ndodb_username'}, $dbparam{'ndodb_password'});
|
||||
die "Error: $DBI::errstr\n" unless $dbh;
|
||||
|
||||
#$sql = "select max(last_check) from ${db_prefix}service";
|
||||
$sql = "select max(last_check) from service";
|
||||
|
||||
$sth = $dbh->prepare($sql);
|
||||
die "Error: $DBI::errstr\n" if $DBI::err;
|
||||
|
||||
$sth->execute();
|
||||
die "Error: $DBI::errstr\n" if $DBI::err;
|
||||
|
||||
while (@fields = $sth->fetchrow_array())
|
||||
{
|
||||
#print join("\t", @fields), "\n";
|
||||
$return = $fields[0];
|
||||
}
|
||||
|
||||
$sth->finish();
|
||||
$dbh->disconnect();
|
||||
}
|
||||
elsif ($dbparam{'ndo'} eq "mk_livestatus")
|
||||
{
|
||||
$socket = IO::Socket::UNIX->new ("Peer" => $dbparam{'ndo_livestatus_socket'}, "Type" => SOCK_STREAM, "Timeout" => 15) or die "unable to connect to unix socket \"" . $dbparam{'ndo_livestatus_socket'} . "\": $!\n";
|
||||
|
||||
print $socket "GET services\n";
|
||||
print $socket "Stats: max last_check\n\n";
|
||||
|
||||
$return = <$socket> || 0;
|
||||
chomp($return);
|
||||
#print STDERR "DEBUG: $return\n";
|
||||
}
|
||||
elsif ($dbparam{'ndo'} eq "icinga-web")
|
||||
{
|
||||
$return = 0;
|
||||
my $maxConnectionTime = 10;
|
||||
my ($ua, $request, $result, $content);
|
||||
if(substr($dbparam{'ndo_icinga_web_url_prefix'}, -1) eq "/")
|
||||
{
|
||||
$dbparam{'ndo_icinga_web_url_prefix'} = substr($dbparam{'ndo_icinga_web_url_prefix'}, 0, length($dbparam{'ndo_icinga_web_url_prefix'}) -1);
|
||||
}
|
||||
my $services_url = $dbparam{'ndo_icinga_web_url_prefix'} . "/web/api/service/columns%5BSERVICE_LAST_CHECK%5D/authkey=" . $dbparam{'ndo_icinga_web_auth_key'} . "/json";
|
||||
|
||||
#print STDERR "URL: $services_url\n";
|
||||
$ua = new LWP::UserAgent ( 'timeout' => $maxConnectionTime );
|
||||
$request = new HTTP::Request ('GET' => "$services_url");
|
||||
$result = $ua->request($request);
|
||||
|
||||
if ($result->code() >= 400)
|
||||
{
|
||||
die "Error when requesting service information from icinga API, response code: " . $result->code() . ", message: " . $result->message() . "\n";
|
||||
}
|
||||
|
||||
#print STDERR "Response Services: " . $result->code() . " " . $result->message() . "\n";
|
||||
$content = $result->decoded_content();
|
||||
$content =~ s/\r\n/\n/g;
|
||||
#print STDERR "Content: $content\n";
|
||||
|
||||
$jsonref = decode_json($content);
|
||||
|
||||
#print "DEBUG: $jsonref\n";
|
||||
#print "DEBUG: " . Dumper($jsonref) . "\n";
|
||||
foreach $subhash (@$jsonref)
|
||||
{
|
||||
#print "DEBUG update: $subhash->{'SERVICE_LAST_CHECK'}\n";
|
||||
if ($subhash->{'SERVICE_LAST_CHECK'} gt $return)
|
||||
{
|
||||
$return = $subhash->{'SERVICE_LAST_CHECK'};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return($return);
|
||||
}
|
||||
|
||||
sub getDbParam()
|
||||
{
|
||||
my (%dbparam, $in, $param, $value);
|
||||
|
||||
open(IN, "<$dbConfigFile") or die "unable to read $dbConfigFile\n";
|
||||
while ($in = <IN>)
|
||||
{
|
||||
if ($in =~ m/^\s*(ndodb_\w+|ndofs_\w+|ndo_livestatus_\w+|ndo_icinga_web_\w+|ndo|cache_\w+)\s*=/)
|
||||
{
|
||||
($param, $value) = split(/=/, $in);
|
||||
chomp($value);
|
||||
$value =~ s/^\s+//;
|
||||
$value =~ s/\s+$//;
|
||||
$param =~ s/^\s+//;
|
||||
$param =~ s/\s+$//;
|
||||
$dbparam{$param} = $value;
|
||||
}
|
||||
}
|
||||
close(IN);
|
||||
|
||||
# set defaults, if we did not get values form config
|
||||
$dbparam{'ndo'}="db" if ($dbparam{'ndo'} eq "");
|
||||
$dbparam{'ndofs_basedir'}="/tmp/ndo2fs" if ($dbparam{'ndofs_basedir'} eq "");
|
||||
$dbparam{'ndofs_instance_name'}="default" if ($dbparam{'ndofs_instance_name'} eq "");
|
||||
$dbparam{'ndodb_host'}="localhost" if ($dbparam{'ndodb_host'} eq "");
|
||||
$dbparam{'ndodb_port'}="3306" if ($dbparam{'ndodb_port'} eq "");
|
||||
$dbparam{'ndodb_database'}="nagios" if ($dbparam{'ndodb_database'} eq "");
|
||||
$dbparam{'cache_time'}=0 if ($dbparam{'cache_time'} !~ m/^\d+$/);
|
||||
$dbparam{'cache_file'}="/tmp/ndo_backend_cache" if ($dbparam{'cache_file'} eq "");
|
||||
|
||||
return (%dbparam);
|
||||
}
|
||||
|
||||
sub getStatusFromFS()
|
||||
{
|
||||
# gets one parameter (hostname) to determine the host status
|
||||
# gets two parameters (hostname and servicename) to determine the service status
|
||||
my $host = shift;
|
||||
my $service = shift || "Hoststatus";
|
||||
|
||||
if ($service eq "Hoststatus")
|
||||
{
|
||||
if (-e "$parentdirname/$host/STATUS")
|
||||
{
|
||||
open (IN, "<$parentdirname/$host/STATUS") or die "unable to read file $parentdirname/$host/STATUS: $!\n";
|
||||
flock(IN, LOCK_SH);
|
||||
|
||||
while($in = <IN>)
|
||||
{
|
||||
#print "DEBUG: $in";
|
||||
if ($in =~ m/"CURRENTSTATE":\s*"(.*)"/)
|
||||
{
|
||||
#print "DEBUG: LASTHARDSTATE: $1\n";
|
||||
$currentstate = $1;
|
||||
}
|
||||
if ($in =~ m/"OUTPUT":\s*"(.*)"/)
|
||||
{
|
||||
#print "DEBUG: OUTPUT: $1\n";
|
||||
$output = $1;
|
||||
}
|
||||
if ($in =~ m/"HOST":\s*"(.*)"/)
|
||||
{
|
||||
#print "DEBUG: HOST: $1\n";
|
||||
$hostname = $1;
|
||||
}
|
||||
}
|
||||
close(IN);
|
||||
$hardstates{"$hostname;Hoststatus"} = $host_state[$currentstate] || "UNKNOWN";
|
||||
$statusinfos{"$hostname;Hoststatus"} = $output;
|
||||
}
|
||||
else
|
||||
{
|
||||
$hardstates{"$hostname;Hoststatus"} = "PENDING";
|
||||
$statusinfos{"$hostname;Hoststatus"} = "";
|
||||
}
|
||||
#print "DEBUG: $host;Hoststatus: $host_state[$currentstate]\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (-e "$parentdirname/$host/$service/STATUS")
|
||||
{
|
||||
open (IN, "<$parentdirname/$host/$service/STATUS") or die "unable to read file $parentdirname/$host/$service/STATUS: $!\n";
|
||||
flock(IN, LOCK_SH);
|
||||
|
||||
while($in = <IN>)
|
||||
{
|
||||
#print "DEBUG: $in";
|
||||
# "OUTPUT": "OK: DNS",
|
||||
# "LASTHARDSTATE": "0",
|
||||
# "SERVICE": "System Check",
|
||||
if ($in =~ m/"LASTHARDSTATE":\s*"(.*)"/)
|
||||
{
|
||||
#print "DEBUG: LASTHARDSTATE: $1\n";
|
||||
$lasthardstate = $1;
|
||||
}
|
||||
if ($in =~ m/"OUTPUT":\s*"(.*)"/)
|
||||
{
|
||||
#print "DEBUG: OUTPUT: $1\n";
|
||||
$output = $1;
|
||||
}
|
||||
if ($in =~ m/"SERVICE":\s*"(.*)"/)
|
||||
{
|
||||
#print "DEBUG: SERVICE: $1\n";
|
||||
$servicename = $1;
|
||||
}
|
||||
}
|
||||
close(IN);
|
||||
$hardstates{"$host;$servicename"} = $services_state[$lasthardstate] || "UNKNOWN";
|
||||
$statusinfos{"$host;$servicename"} = $output;
|
||||
}
|
||||
else
|
||||
{
|
||||
$hardstates{"$host;$servicename"} = "PENDING";
|
||||
$statusinfos{"$host;$servicename"} = "";
|
||||
}
|
||||
#print "DEBUG: $host;$servicename: $host_state[$currentstate]\n";
|
||||
}
|
||||
return(0);
|
||||
}
|
||||
|
||||
sub cleanup_for_ndo2fs {
|
||||
my $host_or_service_name = shift;
|
||||
$host_or_service_name =~ s/[ :\/\\]/_/g;
|
||||
return($host_or_service_name);
|
||||
}
|
||||
|
||||
sub checkCache()
|
||||
{
|
||||
my %dbparam = &getDbParam();
|
||||
my $actDate=time();
|
||||
my ($fileAge, @stat, $in, @tokens, $host, $service, $hardstate);
|
||||
|
||||
if ( -f $dbparam{'cache_file'} )
|
||||
{
|
||||
# ok, we have a cache file already
|
||||
# print STDERR "cache_file exists\n";
|
||||
@stat=stat($dbparam{'cache_file'});
|
||||
$fileAge=$actDate-$stat[9];
|
||||
# print STDERR "actDate: $actDate\n";
|
||||
# print STDERR "FileModificationDate: $stat[9]\n";
|
||||
# print STDERR "FileAge: $fileAge\n";
|
||||
|
||||
if ($fileAge <= $dbparam{'cache_time'})
|
||||
{
|
||||
# print STDERR "cache_file new enough, delivering from cache\n";
|
||||
open(IN, "<$dbparam{'cache_file'}") or die "unable to read cachefile $dbparam{'cache_file'}\n";
|
||||
flock(IN, LOCK_SH);
|
||||
while ($in = <IN>)
|
||||
{
|
||||
@tokens=split(/;/, $in);
|
||||
$host=shift(@tokens);
|
||||
$service=shift(@tokens);
|
||||
$hardstate=shift(@tokens);
|
||||
$hardstates{"$host;$service"} = $hardstate;
|
||||
$statusinfos{"$host;$service"} = join(/;/, @tokens);
|
||||
chomp($statusinfos{"$host;$service"});
|
||||
}
|
||||
close(IN);
|
||||
return(\%hardstates, \%statusinfos);
|
||||
}
|
||||
else
|
||||
{
|
||||
# print STDERR "cache_file too old\n";
|
||||
return('');
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
# print STDERR "cache_file does not exist\n";
|
||||
return('');
|
||||
}
|
||||
|
||||
return('');
|
||||
}
|
||||
|
||||
# call with parameters \%hardstates, \%statusinfos
|
||||
sub updateCache()
|
||||
{
|
||||
my %dbparam = &getDbParam();
|
||||
my $key;
|
||||
my $actDate=time();
|
||||
my @stat=stat($dbparam{'cache_file'});
|
||||
my $fileAge=$actDate-$stat[9];
|
||||
# print STDERR "actDate: $actDate\n";
|
||||
# print STDERR "FileModificationDate: $stat[9]\n";
|
||||
# print STDERR "FileAge: $fileAge\n";
|
||||
|
||||
if ($fileAge > $dbparam{'cache_time'})
|
||||
{
|
||||
# print STDERR "DEBUG: writing cache_file\n";
|
||||
#print "DEBUG: hardstates\n";
|
||||
#printHash(\%hardstates);
|
||||
#print "DEBUG: statusinfos\n";
|
||||
#printHash(\%statusinfos);
|
||||
umask ("0000");
|
||||
open (OUT, ">$dbparam{'cache_file'}") or die "unable to write to $dbparam{'cache_file'}\n";
|
||||
flock(OUT, LOCK_EX);
|
||||
foreach $key (keys %hardstates)
|
||||
{
|
||||
print OUT "$key;$hardstates{$key};$statusinfos{$key}\n";
|
||||
}
|
||||
close (OUT);
|
||||
}
|
||||
#else
|
||||
#{
|
||||
# print STDERR "DEBUG: someone else did write meanwile\n";
|
||||
#}
|
||||
}
|
||||
58
lib/settings.pm.in
Normal file
58
lib/settings.pm.in
Normal file
@@ -0,0 +1,58 @@
|
||||
|
||||
|
||||
# Nagios Business Process View and Nagios Business Process Analysis
|
||||
# Copyright (C) 2003-2010 Sparda-Datenverarbeitung eG, Nuernberg, Germany
|
||||
# Bernd Stroessreuther <berny1@users.sourceforge.net>
|
||||
#
|
||||
# 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; version 2 of the License.
|
||||
#
|
||||
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
|
||||
package settings;
|
||||
|
||||
use Exporter;
|
||||
use strict;
|
||||
use lib ('@libdir@');
|
||||
use bsutils;
|
||||
our @ISA = qw(Exporter);
|
||||
our @EXPORT = qw(getSettings getVersion);
|
||||
|
||||
my $settingsConf = "@sysconfdir@/settings.cfg";
|
||||
my ($in, %settings, $var, $value);
|
||||
|
||||
sub getSettings()
|
||||
{
|
||||
#print "DEBUG: Start of config\n";
|
||||
open(IN, "<$settingsConf") or die "unable to read file $settingsConf\n";
|
||||
while ($in = <IN>)
|
||||
{
|
||||
if ($in !~ m/(^\s*$|^\s*#)/)
|
||||
{
|
||||
#print "DEBUG: $in";
|
||||
chomp($in);
|
||||
($var, $value) = split(/=/, $in);
|
||||
$var = cutOffSpaces($var);
|
||||
$value = cutOffSpaces($value);
|
||||
#print "DEBUG: var \"$var\"\n";
|
||||
#print "DEBUG: value \"$value\"\n";
|
||||
$settings{$var}=$value;
|
||||
}
|
||||
}
|
||||
close(IN);
|
||||
#print "DEBUG: End of config\n";
|
||||
return(\%settings);
|
||||
}
|
||||
|
||||
sub getVersion() { return("0.9.6"); }
|
||||
|
||||
1;
|
||||
Reference in New Issue
Block a user