no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | monitors:xymonext.pm [2010/07/29 07:00] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== My Monitor (CHANGEME) ====== | ||
+ | |||
+ | ^ Author | [[ dbaldwin@users.sf.net | David Baldwin ]] | | ||
+ | ^ Compatibility | Xymon 4.2 | | ||
+ | ^ Requirements | Perl | | ||
+ | ^ Download | XymonExt.pm | | ||
+ | ^ Last Update | 2010-07-29 | | ||
+ | |||
+ | ===== Description ===== | ||
+ | |||
+ | ===== Installation ===== | ||
+ | === Client side === | ||
+ | |||
+ | === Server side === | ||
+ | |||
+ | ===== Source ===== | ||
+ | ==== XymonExt.pm ==== | ||
+ | |||
+ | <hidden onHidden=" | ||
+ | <code perl XymonExt.pm> | ||
+ | package XymonExt; | ||
+ | |||
+ | use 5.008005; | ||
+ | use strict ' | ||
+ | use strict ' | ||
+ | use warnings; | ||
+ | |||
+ | require Exporter; | ||
+ | #use AutoLoader qw(AUTOLOAD); | ||
+ | |||
+ | our @ISA = qw(Exporter); | ||
+ | |||
+ | # Items to export into callers namespace by default. Note: do not export | ||
+ | # names by default without a very good reason. Use EXPORT_OK instead. | ||
+ | # Do not simply export all your public functions/ | ||
+ | |||
+ | # This allows declaration use XymonExt ': | ||
+ | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||
+ | # will save memory. | ||
+ | our %EXPORT_TAGS = ( ' | ||
+ | |||
+ | ) ] ); | ||
+ | |||
+ | our @EXPORT_OK = ( @{ $EXPORT_TAGS{' | ||
+ | |||
+ | our @EXPORT = qw( | ||
+ | |||
+ | ); | ||
+ | |||
+ | our $VERSION = ' | ||
+ | |||
+ | |||
+ | =pod | ||
+ | =head1 NAME | ||
+ | |||
+ | XymonExt - Perl extension to simplify writing XymonExt external scripts in PERL. | ||
+ | |||
+ | =head1 SYNOPSIS | ||
+ | |||
+ | use XymonExt; | ||
+ | |||
+ | XymonExt-> | ||
+ | |||
+ | test stuff... | ||
+ | |||
+ | XymonExt-> | ||
+ | |||
+ | =head1 DESCRIPTION | ||
+ | |||
+ | Requires: $BBHOME environment variable to be set | ||
+ | |||
+ | =head2 EXPORT | ||
+ | |||
+ | None by default. | ||
+ | |||
+ | |||
+ | |||
+ | =head2 Methods | ||
+ | |||
+ | =over 12 | ||
+ | |||
+ | =item import() | ||
+ | |||
+ | This function imports the Xymon environment referenced using $ENV{BBHOME} | ||
+ | |||
+ | =cut | ||
+ | |||
+ | my(%bbhosts, | ||
+ | my(%fromtime, | ||
+ | |||
+ | sub import { | ||
+ | # reinitialise all variables since sometimes gets called in persistent processes (e.g. client-check.pl) | ||
+ | # | ||
+ | %bbhosts = (); | ||
+ | %bbhost = (); | ||
+ | %bbhostsIP = (); | ||
+ | %positional = (); | ||
+ | %bbitems = (); | ||
+ | %parms = (); | ||
+ | %client = (); | ||
+ | %fromtime = (); | ||
+ | %tilltime = (); | ||
+ | |||
+ | my ($caller_package)=caller; | ||
+ | #print " | ||
+ | if (!exists $ENV{BBHOME}) { | ||
+ | my($work, | ||
+ | $work=" | ||
+ | chomp(my $dir=`pwd`); | ||
+ | chdir($work); | ||
+ | chomp($work=`pwd`); | ||
+ | $ENV{BBHOME}=$work; | ||
+ | chdir($dir); | ||
+ | } | ||
+ | my $BBHOME=$ENV{BBHOME}; | ||
+ | #warn " | ||
+ | #printf " | ||
+ | if (!exists $ENV{BBTMP}) { # only run if not set | ||
+ | foreach (`sh -c 'cd $BBHOME;. / | ||
+ | chomp; | ||
+ | if(my ($var, | ||
+ | $ENV{$var}=$val unless $var eq " | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | foreach my $env_key (keys %ENV) { | ||
+ | next if ($env_key=~/ | ||
+ | *{" | ||
+ | } | ||
+ | return if $ENV{BBHOME} =~ /client/; | ||
+ | if( -x " | ||
+ | open(IN," | ||
+ | } else { | ||
+ | open(IN," | ||
+ | } | ||
+ | foreach (< | ||
+ | chomp; | ||
+ | next if (/ | ||
+ | my($ip, | ||
+ | my $name=lc($host); | ||
+ | if(defined $pound && $pound =~ /^#(.+)/) { # handle missed space after "#" | ||
+ | $pound = "#"; | ||
+ | unshift @parms, | ||
+ | } | ||
+ | next unless (defined $pound && $pound eq '#' | ||
+ | $bbhostsIP{$name}=$ip unless $ip =~ / | ||
+ | foreach my $parm (@parms) { # Process all the parms | ||
+ | $bbhosts{" | ||
+ | $bbhost{" | ||
+ | } | ||
+ | } | ||
+ | close(IN); | ||
+ | foreach my $key (keys %bbhosts) { # We also need to parse parms | ||
+ | my($host, | ||
+ | if (($host, | ||
+ | } elsif (($host, | ||
+ | } else { | ||
+ | ($host, | ||
+ | $parms=$function; | ||
+ | } | ||
+ | my $name=$bbhost{" | ||
+ | my %temp=(); | ||
+ | my @positional=(); | ||
+ | foreach (split(/,/, | ||
+ | if (/=/) { # two choices, positional or keyword | ||
+ | my ($var, | ||
+ | $var=lc($var); | ||
+ | $temp{$var}=$val; | ||
+ | } else { | ||
+ | push @positional, | ||
+ | } | ||
+ | } | ||
+ | if (@positional) { | ||
+ | } else { | ||
+ | $positional[0]=$function; | ||
+ | } | ||
+ | my $positional=join(':', | ||
+ | $bbitems{" | ||
+ | $positional{" | ||
+ | foreach (keys %temp) { | ||
+ | $parms{" | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | =pod | ||
+ | |||
+ | =item InitStatus([$host]) | ||
+ | |||
+ | Resets test status to " | ||
+ | |||
+ | =item UpdateStatus($status[, | ||
+ | |||
+ | Updates test status to $status if more severe than current status (optionally for $host) | ||
+ | order is: green clear yellow red | ||
+ | |||
+ | =item GetStatus([$host]) | ||
+ | |||
+ | Returns current test status (optionally for $host) | ||
+ | |||
+ | =cut | ||
+ | |||
+ | my %bbstatus; | ||
+ | |||
+ | sub InitStatus { | ||
+ | my ($package, | ||
+ | $host ||= ""; | ||
+ | $bbstatus{$host} = " | ||
+ | } | ||
+ | |||
+ | sub UpdateStatus { | ||
+ | my ($package, | ||
+ | $host ||= ""; | ||
+ | if (($sigsts eq " | ||
+ | ($bbstatus{$host} ne " | ||
+ | ($sigsts eq " | ||
+ | $bbstatus{$host} = $sigsts; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | sub GetStatus { | ||
+ | my ($package, | ||
+ | $host ||= ""; | ||
+ | return $bbstatus{$host}; | ||
+ | } | ||
+ | |||
+ | =pod | ||
+ | |||
+ | =item Positional($forkey) | ||
+ | |||
+ | =cut | ||
+ | |||
+ | sub Positional { | ||
+ | my ($package, | ||
+ | my $hosts=''; | ||
+ | foreach (grep(/ | ||
+ | $hosts.=$positional{$_}.' | ||
+ | } | ||
+ | return $hosts; | ||
+ | } | ||
+ | |||
+ | =pod | ||
+ | |||
+ | =item Parms($key, | ||
+ | |||
+ | =cut | ||
+ | |||
+ | sub Parms { | ||
+ | my ($package, | ||
+ | if (exists $parms{$key}) { | ||
+ | return $parms{$key}; | ||
+ | } else { | ||
+ | return $default; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | =pod | ||
+ | |||
+ | =item Items($forkey) | ||
+ | |||
+ | returns space separated list of items that match $forkey | ||
+ | |||
+ | =cut | ||
+ | |||
+ | sub Items { | ||
+ | my ($package, | ||
+ | my $items=''; | ||
+ | my @items = (); | ||
+ | if ($forkey) { | ||
+ | foreach (keys %bbitems) { | ||
+ | next unless $bbitems{$_}=~/ | ||
+ | push @items,$_; | ||
+ | } | ||
+ | $items=join(' | ||
+ | } else { | ||
+ | $items=join(' | ||
+ | } | ||
+ | return $items; | ||
+ | } | ||
+ | |||
+ | =pod | ||
+ | |||
+ | =item HostItems($host, | ||
+ | |||
+ | returns space separated list of items for $host that match $forkey | ||
+ | |||
+ | =cut | ||
+ | |||
+ | sub HostItems { | ||
+ | my ($package, | ||
+ | my $items=''; | ||
+ | my @items = (); | ||
+ | if ($forkey) { | ||
+ | foreach (keys %bbhosts) { | ||
+ | next unless / | ||
+ | push @items, | ||
+ | } | ||
+ | $items=join(' | ||
+ | } else { | ||
+ | $items=join(' | ||
+ | } | ||
+ | return $items; | ||
+ | } | ||
+ | |||
+ | =pod | ||
+ | |||
+ | =item HostsByTest($test) | ||
+ | |||
+ | returns list of hosts for plain $test | ||
+ | |||
+ | =cut | ||
+ | |||
+ | sub HostsByTest { | ||
+ | my ($package, | ||
+ | my $host=''; | ||
+ | my @hosts = (); | ||
+ | if ($test) { | ||
+ | foreach (keys %bbhost) { | ||
+ | next unless / | ||
+ | push @hosts, | ||
+ | } | ||
+ | } | ||
+ | return @hosts; | ||
+ | } | ||
+ | |||
+ | =pod | ||
+ | |||
+ | =item HostIP($host) | ||
+ | |||
+ | returns IP for $host | ||
+ | |||
+ | =cut | ||
+ | |||
+ | sub HostIP { | ||
+ | my ($package, | ||
+ | return $bbhostsIP{$host}; | ||
+ | } | ||
+ | |||
+ | =pod | ||
+ | |||
+ | =item Report($HostName, | ||
+ | |||
+ | Reports to BB server that $Hostname.$test has status $colour and with status message $status | ||
+ | |||
+ | =cut | ||
+ | |||
+ | sub Report { | ||
+ | my($package, | ||
+ | ($inst)=split(/ | ||
+ | # Substitute dots by commas in the host name | ||
+ | $HostName =~ s/\./,/g; | ||
+ | # Build the command to report to Xymon | ||
+ | $color=lc($color); | ||
+ | # delete trailing spaces before line feeds in message | ||
+ | $status =~ s/[ \t]+\n/ | ||
+ | $status =~ s/"/ | ||
+ | $lifetime ||= ""; | ||
+ | my @bbds = ($ENV{BBDISP} eq " | ||
+ | foreach my $dh (@bbds) { | ||
+ | warn " | ||
+ | my $MyCmd= " | ||
+ | # For debugging purposes | ||
+ | # Execute the command. | ||
+ | # print " | ||
+ | `$MyCmd`; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | =pod | ||
+ | |||
+ | =item Client($HostName, | ||
+ | |||
+ | Reports client report for $Hostname with OS type $ostype (linux, | ||
+ | client report details in $rep | ||
+ | |||
+ | =cut | ||
+ | |||
+ | sub Client { | ||
+ | my($package, | ||
+ | # Substitute dots by commas in the host name | ||
+ | $HostName =~ s/\./,/g; | ||
+ | # Build the command to report to Xymon | ||
+ | $ostype=lc($ostype); | ||
+ | $configclass=lc($configclass); | ||
+ | $rep =~ s/ | ||
+ | # redirect STDOUT and STDERR to /dev/null since client report returns local config updates | ||
+ | my $MyCmd= " | ||
+ | # For debugging purposes | ||
+ | # Execute the command. | ||
+ | #print " | ||
+ | open CL," | ||
+ | print CL " | ||
+ | close CL; | ||
+ | } | ||
+ | |||
+ | =pod | ||
+ | |||
+ | =item QueryColor($HostName, | ||
+ | |||
+ | returns current colour of test from hobbit server | ||
+ | |||
+ | =cut | ||
+ | |||
+ | sub QueryColor { | ||
+ | my($package, | ||
+ | #my $MyCmd= " | ||
+ | my $MyCmd= " | ||
+ | # For debugging purposes | ||
+ | # Execute the command. | ||
+ | #print " | ||
+ | my $str = `$MyCmd`; | ||
+ | #chomp $str; | ||
+ | #print "PRE: $str\n"; | ||
+ | $str =~ s/ | ||
+ | chomp $str; | ||
+ | #print "POST: $str\n"; | ||
+ | return $str; | ||
+ | } | ||
+ | |||
+ | sub Dump_Vars { | ||
+ | use Data:: | ||
+ | print " | ||
+ | print Dumper(\%bbhosts); | ||
+ | print " | ||
+ | print Dumper(\%bbhost); | ||
+ | print " | ||
+ | print Dumper(\%bbhostsIP); | ||
+ | print " | ||
+ | print Dumper(\%positional); | ||
+ | print " | ||
+ | print Dumper(\%bbitems); | ||
+ | print " | ||
+ | print Dumper(\%parms); | ||
+ | } | ||
+ | |||
+ | =pod | ||
+ | |||
+ | =back | ||
+ | |||
+ | =head1 SEE ALSO | ||
+ | |||
+ | Xymon website: http:// | ||
+ | |||
+ | =head1 AUTHOR | ||
+ | |||
+ | David Baldwin, E< | ||
+ | |||
+ | =head1 COPYRIGHT AND LICENSE | ||
+ | |||
+ | This library is free software; you can redistribute it and/or modify | ||
+ | it under the same terms as Perl itself, either Perl version 5.8.5 or, | ||
+ | at your option, any later version of Perl 5 you may have available. | ||
+ | |||
+ | =cut | ||
+ | |||
+ | 1; | ||
+ | __END__ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== Known Bugs and Issues ===== | ||
+ | |||
+ | ===== To Do ===== | ||
+ | |||
+ | ===== Credits ===== | ||
+ | |||
+ | ===== Changelog ===== | ||
+ | |||
+ | * **2010-07-29** | ||
+ | * Initial release | ||