~ubuntu-branches/ubuntu/hardy/mon/hardy-security

« back to all changes in this revision

Viewing changes to mon.d/reboot.monitor

  • Committer: Bazaar Package Importer
  • Author(s): Roderick Schertler
  • Date: 2001-09-15 08:46:38 UTC
  • Revision ID: james.westby@ubuntu.com-20010915084638-cm3jl93lnhp6cdwg
Tags: upstream-0.99.2
ImportĀ upstreamĀ versionĀ 0.99.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl -w
 
2
#
 
3
# monitor host reboots via SNMP
 
4
#
 
5
# for use with "mon"
 
6
#
 
7
# options:
 
8
#
 
9
#   reboot.monitor --statefile=filename --dir=dir host1 host2...
 
10
#
 
11
#   Since this is scheduled from mon, it must maintain state between
 
12
#   runs. It uses the "statefile" for this, in which it stores a
 
13
#   sysUpTime sample for each host specified on the command line.
 
14
#
 
15
#   THIS STATE FILE MUST NOT BE SHARED BETWEEN MULTIPLE INSTANCES OF THIS
 
16
#   MONITOR, SINCE IT DOES NOT HANDLE LOCKING OF THE FILE DURING UPDATES!!
 
17
#
 
18
# Jim Trocki
 
19
#
 
20
# $Id: reboot.monitor 1.3 Mon, 25 Jun 2001 12:05:08 -0400 trockij $
 
21
#
 
22
#    Copyright (C) 1998, Jim Trocki
 
23
#
 
24
#    This program is free software; you can redistribute it and/or modify
 
25
#    it under the terms of the GNU General Public License as published by
 
26
#    the Free Software Foundation; either version 2 of the License, or
 
27
#    (at your option) any later version.
 
28
#
 
29
#    This program is distributed in the hope that it will be useful,
 
30
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
31
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
32
#    GNU General Public License for more details.
 
33
#
 
34
#    You should have received a copy of the GNU General Public License
 
35
#    along with this program; if not, write to the Free Software
 
36
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
37
#
 
38
 
 
39
# modified June 2000 by Ed Ravin <eravin@panix.com>
 
40
# changed output to conform with other monitors (just hostname on summary)
 
41
# minor cosmetic changes (usage, use default Mon state dir)
 
42
# the old behavior still available with the "--verbose" option
 
43
 
 
44
use SNMP;
 
45
use Getopt::Long;
 
46
 
 
47
($ME = $0) =~ s-.*/--;
 
48
 
 
49
GetOptions (\%opt, "statefile=s", "dir=s", "verbose");
 
50
 
 
51
$STATEDIR = $opt{"dir"} ? $opt{"dir"}
 
52
           : $ENV{"MON_STATEDIR"} ? $ENV{"MON_STATEDIR"}
 
53
           : die "$ME: --dir not specified and \$MON_STATEDIR not set\n";
 
54
 
 
55
$STATEFILE = $opt{"statefile"} || $ME;
 
56
 
 
57
$STATE = "$STATEDIR/$STATEFILE";
 
58
 
 
59
die "$ME: reboot state dir $STATEDIR does not exist\n"
 
60
    if (! -w $STATEDIR);
 
61
 
 
62
$VERBOSE= $opt{"verbose"} || 0;
 
63
 
 
64
die "$ME: no host arguments\n" if (@ARGV == 0);
 
65
 
 
66
if (! -f $STATE) {
 
67
    open (O, ">$STATE");
 
68
    close (O);
 
69
}
 
70
 
 
71
#
 
72
# read in state file
 
73
#
 
74
if (!open (IN, "$STATE")) {
 
75
    die "$ME: could not open state file $STATE\n";
 
76
}
 
77
 
 
78
while (defined ($host = <IN>)) {
 
79
    if ($host =~ /^(\S+) (\d+) (\d+)/) {
 
80
        $last_sample{$1}{"uptime"} = $2;
 
81
        $last_sample{$1}{"lastcheck"} = $3;
 
82
    }
 
83
}
 
84
close (IN);
 
85
 
 
86
#
 
87
# get uptime for each host via SNMP
 
88
#
 
89
@failures = ();
 
90
 
 
91
foreach $host (@ARGV) {
 
92
    if (!defined($s = new SNMP::Session (DestHost => $host))) {
 
93
        print "reboot.monitor: cannot create SNMP session to $host\n";
 
94
        next;
 
95
    }
 
96
 
 
97
    if (!defined($u = $s->get("sysUpTime.0"))) {
 
98
        next;
 
99
    }
 
100
 
 
101
    #
 
102
    # If the uptime is lower than the last sample,
 
103
    # assume this is a reboot. Note that this cannot
 
104
    # account for counter rollover!
 
105
    #
 
106
 
 
107
    #
 
108
    # no history for this
 
109
    #
 
110
    if (!defined $last_sample{$host}{"uptime"}) {
 
111
 
 
112
    } elsif ($u < $last_sample{$host}{"uptime"}) {
 
113
        push (@failures, $host);
 
114
        $last_sample{$host}{"olduptime"} = $last_sample{$host}{"uptime"};
 
115
        $last_sample{$host}{"oldcheck"} = $last_sample{$host}{"lastcheck"};
 
116
    }
 
117
 
 
118
    $last_sample{$host}{"uptime"} = $u;
 
119
    $last_sample{$host}{"lastcheck"} = time;
 
120
}
 
121
 
 
122
#
 
123
# update state file
 
124
#
 
125
if (!open (OUT, ">$STATE")) {
 
126
    die "$ME: could not open $STATEFILE for writing: $!\n";
 
127
}
 
128
 
 
129
foreach $k (sort keys %last_sample) {
 
130
    print OUT "$k $last_sample{$k}{uptime} $last_sample{$k}{lastcheck}\n";
 
131
}
 
132
 
 
133
close (OUT);
 
134
 
 
135
#
 
136
# all is OK, nobody has rebooted
 
137
#
 
138
if (@failures == 0) {
 
139
    exit;
 
140
}
 
141
 
 
142
#
 
143
# we have reboots, so calculate uptime, downtime,
 
144
# and report it
 
145
#
 
146
$t = time;
 
147
foreach $host (@failures) {
 
148
    $downtime = &secs_to_hms (
 
149
        $t - $last_sample{$host}{"oldcheck"} -
 
150
        int ($last_sample{$host}{"uptime"} / 100));
 
151
    $uptime = &secs_to_hms (
 
152
        int ($last_sample{$host}{"olduptime"} / 100));
 
153
    if ($VERBOSE)
 
154
    {
 
155
        push (@f, "$host / down $downtime / up $uptime");
 
156
    }
 
157
 
 
158
    else
 
159
    {
 
160
        push (@f, "$host");
 
161
    }
 
162
}
 
163
@f = sort @f;
 
164
print "@f\n";
 
165
 
 
166
printf ("%-20s  %-25s  %-25s\n", "host", "rebooted on", "last seen up on");
 
167
printf ("%-20s  %-25s  %-25s\n", "-" x 20, "-" x 25, "-" x 25);
 
168
 
 
169
$timen = time;
 
170
 
 
171
foreach $host (@failures) {
 
172
    $secs = int($last_sample{$host}{"uptime"} / 100);
 
173
    $t = localtime ($timen - $secs);
 
174
    $downtime= localtime($last_sample{$host}{"oldcheck"} - ($last_sample{$host}{"uptime"} / 100) );
 
175
    printf ("%-20s  %-25s  %-25s\n", $host, $t, $downtime);
 
176
}
 
177
 
 
178
exit 1;
 
179
 
 
180
 
 
181
sub secs_to_hms {
 
182
    my ($s) = @_;
 
183
    my ($dd, $hh, $mm, $ss);
 
184
 
 
185
    $dd = int ($s / 86400);
 
186
    $s -= $dd * 86400;
 
187
 
 
188
    $hh = int ($s / 3600);
 
189
    $s -= $hh * 3600;
 
190
 
 
191
    $mm = int ($s / 60);
 
192
    $s -= $mm * 60;
 
193
 
 
194
    $ss = $s;
 
195
 
 
196
    if ($dd == 0) {
 
197
        sprintf("%02d:%02d", $hh, $mm);
 
198
    } else {
 
199
        sprintf("%d days, %02d:%02d", $dd, $hh, $mm);
 
200
    }
 
201
}
 
202