root/resmon_code.pl

Revision 37cbe43bf668696d2f26324ca62752e777b57d2b, 3.5 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 13 years ago)

Initial revision

git-svn-id: https://labs.omniti.com/resmon/trunk@2 8c0face9-b7db-6ec6-c4b3-d5f7145c7d55

  • Property mode set to 100755
Line 
1 #!/usr/bin/perl
2
3 require 'ext_comm.pl';
4
5 my %coderefs;
6
7 my $rmloading = "Registering";
8
9 sub fetch_monitor {
10   my $type = shift;
11   my $coderef = $coderefs{$type};
12   return $coderef if ($coderef);
13   if ( -r "$type.pl" ) {
14     require "$type.pl";
15   }
16   return $coderef = $coderefs{$type};
17 }
18
19 sub register_monitor {
20   my ($type, $coderef) = @_;
21   $coderefs{$type} = $coderef;
22   print STDERR "$rmloading $type monitor\n";
23 }
24 sub fresh_status {
25   my $arg = shift;
26   return undef unless $arg->{interval};
27   my $now = time;
28   if(($arg->{lastupdate} + $arg->{interval}) >= $now) {
29     return $arg->{laststatus};
30   }
31   return undef;
32 }
33 sub set_status {
34   my $arg = shift;
35   $arg->{laststatus} = shift;
36   $arg->{lastupdate} = time;
37   return $arg->{laststatus};
38 }
39 #### Begin actual monitor functions ####
40
41 register_monitor('DATE', sub {
42   my $arg = shift;
43   my $os = fresh_status($arg);
44   return set_status($arg, "OK(".time().")");
45 });
46
47 register_monitor('DISK', sub {
48   my $arg = shift;
49   my $os = fresh_status($arg);
50   return $os if $os;
51   my $devorpart = $arg->{'object'};
52   my $output = cache_command("df -k", 120);
53   my ($line) = grep(/$devorpart\s*/, split(/\n/, $output));
54   if($line =~ /(\d+)%/) {
55     if($1 <= $arg->{'limit'}) {
56       return set_status($arg, "OK($1% full)");
57     }
58     return set_status($arg, "BAD($1% full)");
59   }
60   return set_status($arg, "BAD(no data)");
61 });
62
63 register_monitor('A1000', sub {
64   my $arg = shift;
65   my $os = fresh_status($arg);
66   return $os if $os;
67   my $unit = $arg->{'object'};
68   my $output = cache_command("/usr/lib/osa/bin/healthck -a", 500);
69   my ($line) = grep(/^$unit:/, split(/\n/, $output));
70   if ($line =~ /:\s+(.+)/) {
71     return set_status($arg, "OK($1)") if($1 eq $arg->{'status'});
72     return set_status($arg, "BAD($1)");
73   }
74   return set_status($arg, "BAD(no data)");
75 });
76
77 my %logfile_stats;
78 register_monitor('LOGFILE', sub {
79   my $arg = shift;
80   my $os = fresh_status($arg);
81   return $os if $os;
82   my $file = $arg->{'object'};
83   my $match = $arg->{'match'};
84   my $errors;
85   my $errorcount = 0;
86   my $start = 0;
87   my @statinfo = stat($filename);
88   if($logfile_stats{$file}) {
89     my($dev, $ino, $size, $errs) = split(/-/, $logfile_stats{$file});
90     if(($dev == $statinfo[0]) && ($ino == $statinfo[1])) {
91       if($size == $statinfo[7]) {
92         return set_status($arg, "OK($errs)");
93       }
94       $start = $size;
95       $errorcount = $errs;
96     }
97   }
98   open(LOG, "<$file");
99   seek(LOG, $size, 0);
100   while(<LOG>) {
101     chomp;
102     if(/$match/) {
103       $errors .= $_;
104       $errorcount++;
105     }
106   }
107   $logfile_stats{$file} = "$statinfo[0]-$statinfo[1]-$statinfo[7]-$errorcount";
108   if($errors) {
109     return set_status($arg, "BAD($errors)");
110   }
111   return set_status($arg, "OK($errorcount)");
112 });
113
114 register_monitor('NETSTAT', sub {
115   my $arg = shift;
116   my $os = fresh_status($arg);
117   return $os if $os;
118   my $output = cache_command("netstat -an", 30);
119   my @lines = split(/\n/, $output);
120   @lines = grep(/\s$arg->{state}$/, @lines) if($arg->{state});
121   @lines = grep(/^$arg->{localip}/, @lines) if($arg->{localip});
122   @lines = grep(/^[\d\*\.]+\.$arg->{localport}/, @lines) if($arg->{localport});
123   @lines = grep(/^[\d\*\.]+\d+\s+$arg->{remoteip}/, @lines)
124         if($arg->{remoteip});
125   @lines = grep(/^[\d\*\.]+\s+[\d\*\.+]\.$arg->{remoteport}/, @lines)
126         if($arg->{remoteport});
127   my $count = scalar(@lines);
128   return set_status($arg, "BAD($count)")
129         if($arg->{limit} && ($count > $arg->{limit}));
130   return set_status($arg, "BAD($count)")
131         if($arg->{atleast} && ($count < $arg->{atleast}));
132   return set_status($arg, "OK($count)");
133 });
134
135 $rmloading = "Demand loading";
136 1;
Note: See TracBrowser for help on using the browser.