| 1 |
package Resmon::Module; |
|---|
| 2 |
|
|---|
| 3 |
use strict; |
|---|
| 4 |
use Data::Dumper; |
|---|
| 5 |
|
|---|
| 6 |
my %coderefs; |
|---|
| 7 |
|
|---|
| 8 |
my $rmloading = "Registering"; |
|---|
| 9 |
|
|---|
| 10 |
sub fetch_monitor { |
|---|
| 11 |
my $type = shift; |
|---|
| 12 |
my $coderef = $coderefs{$type}; |
|---|
| 13 |
return $coderef if ($coderef); |
|---|
| 14 |
eval "use $type;"; |
|---|
| 15 |
eval "use Resmon::Module::$type;"; |
|---|
| 16 |
return undef; |
|---|
| 17 |
} |
|---|
| 18 |
|
|---|
| 19 |
sub register_monitor { |
|---|
| 20 |
my ($type, $ref) = @_; |
|---|
| 21 |
if(ref $ref eq 'CODE') { |
|---|
| 22 |
$coderefs{$type} = $ref; |
|---|
| 23 |
} |
|---|
| 24 |
print STDERR "$rmloading $type monitor\n"; |
|---|
| 25 |
} |
|---|
| 26 |
sub fresh_status { |
|---|
| 27 |
my $arg = shift; |
|---|
| 28 |
return undef unless $arg->{interval}; |
|---|
| 29 |
my $now = time; |
|---|
| 30 |
if(($arg->{lastupdate} + $arg->{interval}) >= $now) { |
|---|
| 31 |
return $arg->{laststatus}; |
|---|
| 32 |
} |
|---|
| 33 |
return undef; |
|---|
| 34 |
} |
|---|
| 35 |
sub set_status { |
|---|
| 36 |
my $arg = shift; |
|---|
| 37 |
$arg->{laststatus} = shift; |
|---|
| 38 |
$arg->{lastmessage} = shift; |
|---|
| 39 |
$arg->{lastupdate} = time; |
|---|
| 40 |
if($arg->{laststatus} =~ /^([A-Z]+)\(([^\)]+)\)$/s) { |
|---|
| 41 |
# This handles old-style modules that return just set status as |
|---|
| 42 |
# STATE(message) |
|---|
| 43 |
$arg->{laststatus} = $1; |
|---|
| 44 |
$arg->{lastmessage} = $2; |
|---|
| 45 |
} |
|---|
| 46 |
return ($arg->{laststatus}, $arg->{lastmessage}); |
|---|
| 47 |
} |
|---|
| 48 |
#### Begin actual monitor functions #### |
|---|
| 49 |
|
|---|
| 50 |
package Resmon::Module::DATE; |
|---|
| 51 |
use vars qw/@ISA/; |
|---|
| 52 |
@ISA = qw/Resmon::Module/; |
|---|
| 53 |
|
|---|
| 54 |
sub handler { |
|---|
| 55 |
my $arg = shift; |
|---|
| 56 |
my $os = $arg->fresh_status(); |
|---|
| 57 |
return $arg->set_status("OK(".time().")"); |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
package Resmon::Module::DISK; |
|---|
| 61 |
use Resmon::ExtComm qw/cache_command/; |
|---|
| 62 |
use vars qw/@ISA/; |
|---|
| 63 |
@ISA = qw/Resmon::Module/; |
|---|
| 64 |
|
|---|
| 65 |
sub handler { |
|---|
| 66 |
my $arg = shift; |
|---|
| 67 |
my $os = $arg->fresh_status(); |
|---|
| 68 |
return $os if $os; |
|---|
| 69 |
my $devorpart = $arg->{'object'}; |
|---|
| 70 |
my $output = cache_command("df -k", 120); |
|---|
| 71 |
my ($line) = grep(/$devorpart\s*/, split(/\n/, $output)); |
|---|
| 72 |
if($line =~ /(\d+)%/) { |
|---|
| 73 |
if($1 <= $arg->{'limit'}) { |
|---|
| 74 |
return $arg->set_status("OK($1% full)"); |
|---|
| 75 |
} |
|---|
| 76 |
return $arg->set_status("BAD($1% full)"); |
|---|
| 77 |
} |
|---|
| 78 |
return $arg->set_status("BAD(no data)"); |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
package Resmon::Module::LOGFILE; |
|---|
| 82 |
use vars qw/@ISA/; |
|---|
| 83 |
@ISA = qw/Resmon::Module/; |
|---|
| 84 |
|
|---|
| 85 |
my %logfile_stats; |
|---|
| 86 |
sub handler { |
|---|
| 87 |
my $arg = shift; |
|---|
| 88 |
my $os = $arg->fresh_status(); |
|---|
| 89 |
return $os if $os; |
|---|
| 90 |
my $file = $arg->{'object'}; |
|---|
| 91 |
my $match = $arg->{'match'}; |
|---|
| 92 |
my $errors; |
|---|
| 93 |
my $errorcount = 0; |
|---|
| 94 |
my $start = 0; |
|---|
| 95 |
my @statinfo = stat($file); |
|---|
| 96 |
if($logfile_stats{$file}) { |
|---|
| 97 |
my($dev, $ino, $size, $errs) = split(/-/, $logfile_stats{$file}); |
|---|
| 98 |
if(($dev == $statinfo[0]) && ($ino == $statinfo[1])) { |
|---|
| 99 |
if($size == $statinfo[7]) { |
|---|
| 100 |
return $arg->set_status("OK($errs)"); |
|---|
| 101 |
} |
|---|
| 102 |
$start = $size; |
|---|
| 103 |
$errorcount = $errs; |
|---|
| 104 |
} |
|---|
| 105 |
} |
|---|
| 106 |
$logfile_stats{$file} = "$statinfo[0]-$statinfo[1]-$statinfo[7]-$errorcount"; |
|---|
| 107 |
if(!open(LOG, "<$file")) { |
|---|
| 108 |
return $arg->set_status("BAD(ENOFILE)"); |
|---|
| 109 |
} |
|---|
| 110 |
seek(LOG, $statinfo[7], 0); |
|---|
| 111 |
while(<LOG>) { |
|---|
| 112 |
chomp; |
|---|
| 113 |
if(/$match/) { |
|---|
| 114 |
$errors .= $_; |
|---|
| 115 |
$errorcount++; |
|---|
| 116 |
} |
|---|
| 117 |
} |
|---|
| 118 |
if($errors) { |
|---|
| 119 |
return $arg->set_status("BAD($errors)"); |
|---|
| 120 |
} |
|---|
| 121 |
return $arg->set_status("OK($errorcount)"); |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|
| 124 |
package Resmon::Module::FILEAGE; |
|---|
| 125 |
use vars qw/@ISA/; |
|---|
| 126 |
@ISA = qw/Resmon::Module/; |
|---|
| 127 |
|
|---|
| 128 |
sub handler { |
|---|
| 129 |
my $arg = shift; |
|---|
| 130 |
my $os = $arg->fresh_status(); |
|---|
| 131 |
return $os if $os; |
|---|
| 132 |
my $file = $arg->{'object'}; |
|---|
| 133 |
my @statinfo = stat($file); |
|---|
| 134 |
my $age = time() - $statinfo[9]; |
|---|
| 135 |
return $arg->set_status("BAD(to old $age seconds)") |
|---|
| 136 |
if($arg->{maximum} && ($age > $arg->{maximum})); |
|---|
| 137 |
return $arg->set_status("BAD(to new $age seconds)") |
|---|
| 138 |
if($arg->{minimum} && ($age > $arg->{minimum})); |
|---|
| 139 |
return $arg->set_status("OK($age)"); |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
package Resmon::Module::NETSTAT; |
|---|
| 143 |
use Resmon::ExtComm qw/cache_command/; |
|---|
| 144 |
use vars qw/@ISA/; |
|---|
| 145 |
@ISA = qw/Resmon::Module/; |
|---|
| 146 |
|
|---|
| 147 |
sub handler { |
|---|
| 148 |
my $arg = shift; |
|---|
| 149 |
my $os = $arg->fresh_status(); |
|---|
| 150 |
return $os if $os; |
|---|
| 151 |
my $output = cache_command("netstat -an", 30); |
|---|
| 152 |
my @lines = split(/\n/, $output); |
|---|
| 153 |
@lines = grep(/\s$arg->{state}$/, @lines) if($arg->{state}); |
|---|
| 154 |
@lines = grep(/^$arg->{localip}/, @lines) if($arg->{localip}); |
|---|
| 155 |
@lines = grep(/^[\d\*\.]+\.$arg->{localport}/, @lines) if($arg->{localport}); |
|---|
| 156 |
@lines = grep(/^[\d\*\.]+\d+\s+$arg->{remoteip}/, @lines) |
|---|
| 157 |
if($arg->{remoteip}); |
|---|
| 158 |
@lines = grep(/^[\d\*\.]+\s+[\d\*\.+]\.$arg->{remoteport}/, @lines) |
|---|
| 159 |
if($arg->{remoteport}); |
|---|
| 160 |
my $count = scalar(@lines); |
|---|
| 161 |
return $arg->set_status("BAD($count)") |
|---|
| 162 |
if($arg->{limit} && ($count > $arg->{limit})); |
|---|
| 163 |
return $arg->set_status("BAD($count)") |
|---|
| 164 |
if($arg->{atleast} && ($count < $arg->{atleast})); |
|---|
| 165 |
return $arg->set_status("OK($count)"); |
|---|
| 166 |
} |
|---|
| 167 |
|
|---|
| 168 |
$rmloading = "Demand loading"; |
|---|
| 169 |
1; |
|---|