#!/opt/perl_64/bin/perl -w #################################################### # check_heartbeat_link hp-ux # # adaptado por claudio elauterio / SEFAZ-MT # #################################################### use strict; use File::Basename; use Getopt::Long; use Sys::Hostname; my $prog_name=basename($0); my $prog_revision='0.1'; my ($debug_mode); my $hb_node = hostname; my $cl_status = "/usr/sbin/cmviewcl"; my @nodes; my (%node, %if, %exclude); my $OK = 0; my $WARNING = 1; my $CRITICAL = 2; my $UNKNOWN = 3; &parse_options; &check_cl_status; &check_heartbeat_status; &find_nodes; &find_links; &check_links; &myexit($UNKNOWN,"$prog_name should never reach here"); sub print_usage { print < \$help, "V|version" => \$version, "n|node=s" => \$hb_node, "p|path=s" => \$cl_status, "x|exclude=s" => \$exclude_node, "debug" => \$debug); if (defined($help) && ($help ne "")) { &print_help; exit $UNKNOWN; } if (defined($version) && ($version ne "")) { &print_revision; exit $UNKNOWN; } if (defined($exclude_node) && ($exclude_node ne "")) { @exclude=split(/,/,$exclude_node); for my $i ( @exclude ) { $exclude{$i} = 1; } } if (defined($debug) && ($debug ne "")) { # # Debugging information # $debug_mode=1; print STDERR "<$prog_name settings>\n"; printf STDERR "Heartbeat Node: %s\n", defined($hb_node)?$hb_node:""; printf STDERR "Heartbeat Exclude Node: %s\n", @exclude?join(" ",@exclude):""; printf STDERR "Path to cl_status: %s\n", defined($cl_status)?$cl_status:""; print STDERR "\n"; } } sub check_cl_status { # # Bail out if cl_status is not executable # if ( ! -x "$cl_status" ) { &myexit($CRITICAL,"Heartbeat UNKNOWN: $prog_name could not execute $cl_status"); } } sub check_heartbeat_status { # # Check to see if heartbeat is running # my ($result); open(CL,"$cl_status |") || &myexit($CRITICAL,"Heartbeat CRITICAL: Could not open $cl_status"); while() { chop($_); $result .= $_; } close(CL); if ($? > 0) { &myexit($CRITICAL,sprintf("Heartbeat CRITICAL: %s",defined($result)?$result:"Unknown error")); } } sub find_nodes { # # Find all nodes that are not ourself and not on the exclude list # my $self; my @exclude; open(CL,"$cl_status |") || &myexit($CRITICAL,"Heartbeat CRITICAL: Could not open $cl_status"); while() { chop($_); if (defined($exclude{$_})) { push(@exclude,$_); } elsif ($_ ne $hb_node) { push(@nodes,$_); } else { $self = $_; } } close(CL); if ((defined($debug_mode)) && ($debug_mode == 1)) { # # Debugging information # print STDERR "<$prog_name nodes>\n"; printf STDERR "Heartbeat Exclude Nodes: %s\n", @exclude?join(" ",sort(@exclude)):""; printf STDERR "Heartbeat External Nodes: %s\n", @nodes?join(" ",sort(@nodes)):""; printf STDERR "Heartbeat Internal Nodes: %s\n", defined($self)?$self:""; print STDERR "\n"; } if (!(@nodes)) { &myexit($CRITICAL,"Heartbeat CRITICAL: No other nodes found"); } } sub find_links { # # For nodes we wish to check, find all available links # for my $node (@nodes) { my $count = 0; open(CL,"$cl_status -n $node|") || &myexit($CRITICAL,"Heartbeat CRITICAL: Could not open $cl_status"); while() { if (/^\s*(.*)/) { $if{$node}{$1} = $1; $node{$node} = $node; $count++; } } close(CL); } if ((defined($debug_mode)) && ($debug_mode == 1)) { # # Debugging information # print STDERR "<$prog_name interfaces>\n"; for my $key ( values %node ) { for my $key2 ( keys %{$if{$key}} ) { print STDERR "$key $key2\n"; } } print STDERR "\n"; } } sub check_links { # # Now that we have nodes and link types, check them. # my ($result); my ($critical)=0; for my $key ( values %node ) { for my $key2 ( keys %{$if{$key}} ) { open(CL,"$cl_status |") || &myexit($CRITICAL,"Heartbeat CRITICAL: Could not open $cl_status"); while() { chop($_); $result .= "$key:$key2:$_ "; } close(CL); if ($? > 0) { $critical = 1; } } } if ($critical == 1) { &myexit($CRITICAL,"Heartbeat CRITICAL: $result"); } else { &myexit($OK,"Heartbeat OK: $result"); } } sub myexit { # # Print status message and exit # my ($error, $message) = @_; print "$message\n"; exit $error; }