File: reboot.monitor

package info (click to toggle)
mon 0.99.2-9%2Betch2
  • links: PTS
  • area: main
  • in suites: etch
  • size: 908 kB
  • ctags: 299
  • sloc: perl: 9,801; ansic: 778; sh: 372; makefile: 122
file content (202 lines) | stat: -rwxr-xr-x 4,914 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/perl -w
#
# monitor host reboots via SNMP
#
# for use with "mon"
#
# options:
#
#   reboot.monitor --statefile=filename --dir=dir host1 host2...
#
#   Since this is scheduled from mon, it must maintain state between
#   runs. It uses the "statefile" for this, in which it stores a
#   sysUpTime sample for each host specified on the command line.
#
#   THIS STATE FILE MUST NOT BE SHARED BETWEEN MULTIPLE INSTANCES OF THIS
#   MONITOR, SINCE IT DOES NOT HANDLE LOCKING OF THE FILE DURING UPDATES!!
#
# Jim Trocki
#
# $Id: reboot.monitor 1.3 Mon, 25 Jun 2001 12:05:08 -0400 trockij $
#
#    Copyright (C) 1998, Jim Trocki
#
#    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 2 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, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

# modified June 2000 by Ed Ravin <eravin@panix.com>
# changed output to conform with other monitors (just hostname on summary)
# minor cosmetic changes (usage, use default Mon state dir)
# the old behavior still available with the "--verbose" option

use SNMP;
use Getopt::Long;

($ME = $0) =~ s-.*/--;

GetOptions (\%opt, "statefile=s", "dir=s", "verbose");

$STATEDIR = $opt{"dir"} ? $opt{"dir"}
           : $ENV{"MON_STATEDIR"} ? $ENV{"MON_STATEDIR"}
           : die "$ME: --dir not specified and \$MON_STATEDIR not set\n";

$STATEFILE = $opt{"statefile"} || $ME;

$STATE = "$STATEDIR/$STATEFILE";

die "$ME: reboot state dir $STATEDIR does not exist\n"
    if (! -w $STATEDIR);

$VERBOSE= $opt{"verbose"} || 0;

die "$ME: no host arguments\n" if (@ARGV == 0);

if (! -f $STATE) {
    open (O, ">$STATE");
    close (O);
}

#
# read in state file
#
if (!open (IN, "$STATE")) {
    die "$ME: could not open state file $STATE\n";
}

while (defined ($host = <IN>)) {
    if ($host =~ /^(\S+) (\d+) (\d+)/) {
	$last_sample{$1}{"uptime"} = $2;
	$last_sample{$1}{"lastcheck"} = $3;
    }
}
close (IN);

#
# get uptime for each host via SNMP
#
@failures = ();

foreach $host (@ARGV) {
    if (!defined($s = new SNMP::Session (DestHost => $host))) {
	print "reboot.monitor: cannot create SNMP session to $host\n";
	next;
    }

    if (!defined($u = $s->get("sysUpTime.0"))) {
	next;
    }

    #
    # If the uptime is lower than the last sample,
    # assume this is a reboot. Note that this cannot
    # account for counter rollover!
    #

    #
    # no history for this
    #
    if (!defined $last_sample{$host}{"uptime"}) {

    } elsif ($u < $last_sample{$host}{"uptime"}) {
    	push (@failures, $host);
	$last_sample{$host}{"olduptime"} = $last_sample{$host}{"uptime"};
	$last_sample{$host}{"oldcheck"} = $last_sample{$host}{"lastcheck"};
    }

    $last_sample{$host}{"uptime"} = $u;
    $last_sample{$host}{"lastcheck"} = time;
}

#
# update state file
#
if (!open (OUT, ">$STATE")) {
    die "$ME: could not open $STATEFILE for writing: $!\n";
}

foreach $k (sort keys %last_sample) {
    print OUT "$k $last_sample{$k}{uptime} $last_sample{$k}{lastcheck}\n";
}

close (OUT);

#
# all is OK, nobody has rebooted
#
if (@failures == 0) {
    exit;
}

#
# we have reboots, so calculate uptime, downtime,
# and report it
#
$t = time;
foreach $host (@failures) {
    $downtime = &secs_to_hms (
	$t - $last_sample{$host}{"oldcheck"} -
    	int ($last_sample{$host}{"uptime"} / 100));
    $uptime = &secs_to_hms (
	int ($last_sample{$host}{"olduptime"} / 100));
    if ($VERBOSE)
    {
    	push (@f, "$host / down $downtime / up $uptime");
    }

    else
    {
    	push (@f, "$host");
    }
}
@f = sort @f;
print "@f\n";

printf ("%-20s  %-25s  %-25s\n", "host", "rebooted on", "last seen up on");
printf ("%-20s  %-25s  %-25s\n", "-" x 20, "-" x 25, "-" x 25);

$timen = time;

foreach $host (@failures) {
    $secs = int($last_sample{$host}{"uptime"} / 100);
    $t = localtime ($timen - $secs);
    $downtime= localtime($last_sample{$host}{"oldcheck"} - ($last_sample{$host}{"uptime"} / 100) );
    printf ("%-20s  %-25s  %-25s\n", $host, $t, $downtime);
}

exit 1;


sub secs_to_hms {
    my ($s) = @_;
    my ($dd, $hh, $mm, $ss);

    $dd = int ($s / 86400);
    $s -= $dd * 86400;

    $hh = int ($s / 3600);
    $s -= $hh * 3600;

    $mm = int ($s / 60);
    $s -= $mm * 60;

    $ss = $s;

    if ($dd == 0) {
        sprintf("%02d:%02d", $hh, $mm);
    } else {
        sprintf("%d days, %02d:%02d", $dd, $hh, $mm);
    }
}