#!/usr/bin/perl ################################################################################ ################################################################################ ## ## ## check_mod_jk_status ## ## Copyright © 2014 Christian ter Stein, Hochschule Niederrhein - University ## ## of Applied Sciences ## ## ## ## Check the status of mod_jk load balancers by analysis of jkmanager's ## ## XML page from status ## ## ## ## 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 3 of the License, or ## ## (at your option) 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, see . ## ## ## ## ## ## ****************** ## ## * Requires * ## ## ****************** ## ## * * ## ## * Perl Modules: * ## ## * ------------- * ## ## * XML::Simple * ## ## * LWP::Simple * ## ## * * ## ## ****************** ## ## ## ## ## ## *************************************************************** ## ## * Configuration * ## ## *************************************************************** ## ## * * ## ## * Set Server vars: * ## ## * ---------------- * ## ## * #If jkmanager is protected * ## ## * my $username = 'user'; * ## ## * my $pass = 'pass'; * ## ## * #Location of jkmanager * ## ## * my $domain = 'domain.com/jkmanager'; * ## ## * my $server_ip = $username.":".$pass."@".$domain; * ## ## * my $balancer = 'mybalancer'; * ## ## * * ## ## * # Balancer properties to monitor * ## ## * my @member_data = ("activation", "route","state","host"); * ## ## * #If you change the member_data list, make sure that checks * ## ## * #in sub ParseXML() also changed * ## ## * * ## ## *************************************************************** ## ## ## ## v1 17/09/2014 ## ## ## ################################################################################ ################################################################################ use strict; use warnings; use XML::Simple; use LWP::Simple; #################################################### # Custom vars for monitoring # #################################################### ## Server vars my $username = 'user'; my $pass = 'pass'; my $domain = 'domain.com/jkmanager'; my $server_ip = $username.":".$pass."@".$domain; my $balancer = 'mybalancer'; # Balancer properties to monitor my @member_data = ("activation", "route","state","host"); #Regex to check whether unauthorized person has changed host and route my $route_exp = qr/my_server_name_0[0-9]/; #e.g. qr/myserver[0-9]/ my $host_exp = qr/127.0.0.[0-9]/; #e.g. qr/127.0.0.[0-9]/; #################################################### # Main Application # #################################################### #Global vars my $state_code = 0; my $state_message = ""; my $message = ""; ## Fetch the status my $xml = GetXML($server_ip); ## Parse the XML and generate status message ParseXML($xml); ## Print status message SendStatus(); ################################################### # Subs # ################################################### ## Fetch the XML from management address sub GetXML { ### Get the XML page my $ip = shift; my $url = "https://$ip?mime=xml"; my $page = get $url; die "Couldn't get $url" unless defined $page; ## Return the XML return $page; } ## Parse the XML and return the results sub ParseXML { ### Get XML to parse my $xml = shift; my $status = XMLin($xml); my %members; ## Exit if specified balancer wasn't found PrintException ("Supplied balancer wasn't found!") unless defined ( $status->{'jk:balancers'}->{'jk:balancer'} ); ## Get members count my $member_count = $status->{'jk:balancers'}->{'jk:balancer'}{'member_count'}; ## Get member data foreach my $member ( sort keys %{$status->{'jk:balancers'}->{'jk:balancer'}->{'jk:member'}} ) { foreach (@member_data) { $members{$member}{$_} = $status->{'jk:balancers'}->{'jk:balancer'}->{'jk:member'}->{$member}{$_}; } } ##Check Members foreach my $m ( keys %members ) { $state_message = ""; for my $attribute ( keys %{ $members{$m} } ) { # check attribute activation if($attribute eq "activation") { my $activation = $members{$m}{$attribute}; if($activation eq "DIS") { if($state_code != 2) { $state_code = 1; } $state_message .= "$attribute = $activation, "; } if($activation eq "STP") { $state_code = 2; $state_message .= "$attribute = $activation, "; } } # check attribute state if($attribute eq "state") { my $state = substr($members{$m}{$attribute},0,2); if($state eq "BUSY") { if($state_code != 2) { $state_code = 1; } $state_message .= "$attribute = $state, "; } if($state eq "REC") { if($state_code != 2) { $state_code = 1; } $state_message .= "$attribute = $state, "; } if($state eq "PRB") { if($state_code != 2) { $state_code = 1; } $state_message .= "$attribute = $state, "; } if($state eq "FRC") { if($state_code != 2) { $state_code = 1; } $state_message .= "$attribute = $state, "; } if($state eq "ERR") { $state_code = 2; $state_message .= "$attribute = $state, "; } } # check attribute route if($attribute eq "route") { my $route = $members{$m}{$attribute}; unless($route =~ m/$route_exp/) { $state_code = 2; $state_message .= "$attribute = $route, "; } } # check attribute host if($attribute eq "host") { my $host = $members{$m}{$attribute}; unless($host =~ m/$host_exp/) { $state_code = 2; $state_message .= "$attribute = $host, "; } } } ## generate status message if($state_message ne "") { $message .= " +++ $m: $state_message "; } elsif($message eq "") { $message = "All Balancer Members OK"; } } } sub SendStatus { print $message; print "\n"; exit $state_code; } sub PrintException { my $msg = shift; print "$msg\n"; exit 1; }