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
| #!/usr/bin/perl
# Perl script that scans vol$log.err for last mount time, then scans
# abend.log to determine if abends occured after last start.
# Script uses Date::Parse for parsing the time strings from log files
# you could get it at CPAN: http://search.cpan.org/search?dist=TimeDate
# Script uses Mail::Sendmail for sending mail which is part of NetWare Perl #331
# or you could get it at CPAN: http://search.cpan.org/search?dist=Mail-Sendmail
# if you have older Perl, you should update to minimum #331:
# you could get it at CPAN: http://www.cpan.org/ports/index.html#netware
# install the script as cron job with: perl abendchk.pl -c[2,3,4,6,0]
# v0.10 08-Dez-2000 (c) info@gknw.de
use Getopt::Std;
use Date::Parse;
use Mail::Sendmail;
getopts('dhlmf:c:');
inst_cron() if ($opt_c);
$to = 'admin@your.domain';
$smtp = 'mail.your.domain';
#$from = "admin\@$host";
$vfile = '/vol$log.err';
$afile = '/system/abend.log';
$cfile = '/etc/abendchk.log';
$stamp = time();
$time = localtime($stamp);
if (-e $cfile) {
$start = find_lasttime($cfile);
print "Last abendchk: $start ->",scalar localtime($start),"\n\n" if ($opt_d);
} else {
$start = read_vlog($vfile);
$upsec = $stamp - $start;
print "Server uptime: ",&make_upstring(),"\n\n" if ($opt_d);
}
if (-e $afile) {
$astat = (stat($afile))[9];
print "Test FileDate: $astat -> ",scalar localtime($astat),"\n" if ($opt_d);
if ($start < $astat) {
$new = 1;
}
}
$abend = read_alog($afile);
if ($start < $abend) {
print "\rThere were abends after last server start at ",scalar localtime($start),"\n";
$new = 1;
}
print "Server: $host\n" if ($opt_d);
$opt_d=0;
if ($opt_d) {
for (@abends) {print $_;};
}
update_log($cfile) if (!$opt_l);
#---------------------------------------------------
if (!$opt_m && $new) {
$from = "admin\@$host" if (!$from);
$realname = 'Service Auto Mailer';
$from = "\"$realname\" <$from>";
$reply = 'NO_REPLY!@NO_REPLY!';
$subject = "ALERT: Abend report from $host";
$message = "Service Mail:\n$subject at $time\n\n";
for (@abends) {$message .= $_;};
%mail = ( To => $to,
From => $from,
Subject => $subject,
Message => $message,
Smtp => $smtp
);
if (sendmail %mail) {
print "\r$0: Mail sent OK.\n";
} else {
print "\rError sending mail: $Mail::Sendmail::error \n";
}
}
exit;
#---------------------------------------------------
sub read_vlog {
my ($file)=@_;
my $stamp,$temp;
open(LOG,$file) || die "cannot read $file: $!";
while (<LOG>) {
chomp;
next if (!/^Volume SYS mounted on \w+,\s(.+)./);
$temp = $1;
$temp =~ s/\./\:/g; # replace . with : for str2time()
$stamp = Date::Parse::str2time($temp);
# if (/Volume SYS mounted on \w+, (\w+ \d+, \d+\ +\d+:\d+:\d+ \w+)./) {
# if (/Volume SYS mounted on \w+, (.+)./) {
# print "\n$_\n" if ($opt_d);
print "$temp -> $stamp -> ",scalar localtime($stamp),"\n" if ($opt_d);
}
close(LOG);
print "\n" if ($opt_d);
return $stamp;
}
sub read_alog {
my ($file)=@_;
my $stamp,$temp;
open(LOG,$file) || die "cannot read $file: $!";
while (<LOG>) {
chomp;
next if (!/^Server (\w+) halted \w+,\s(.+)/);
# print "$2\n" if ($opt_d);
$host = $1;
$temp = $2;
$temp =~ s/\./\:/g; # replace . with : for str2time()
$stamp = Date::Parse::str2time($temp);
next if ($start > $stamp);
push (@abends,("*" x 60)."\n");
push (@abends,"$_\n");
$line = <LOG>;
push (@abends,$line);
while (<LOG>) {
next if (!/^Running process:/);
push (@abends,$_);
last;
}
print "$temp -> $stamp -> ",scalar localtime($stamp),"\n" if ($opt_d);
}
close(LOG);
print "\n" if ($opt_d);
return $stamp;
}
sub make_upstring {
$updays=int($upsec/86400);
$upsec-=$updays*86400;
$uphours=int($upsec/3600);
$upsec-=$uphours*3600;
$upmin=int($upsec/60);
$upsec-=$upmin*60;
$upstring=sprintf("%5d %02d:%02d:%02d",$updays,$uphours,$upmin,$upsec);
}
sub find_lasttime {
my ($file)=@_;
my $stamp;
open(LOG,$file) || die "cannot open $file: $!";
while (<LOG>) {
if (/^\.+|(\d+)$/) {
$stamp = $1;
}
}
close(LOG);
return $stamp;
}
sub update_log {
my ($file)=@_;
open(LOG,">> $file") || die "cannot append to $file: $!";
print LOG "AbendChk: $time |$stamp\n";
close(LOG);
}
sub inst_cron {
if ($opt_c eq '2') {
# The following command executes the script at 04:00 and 16:00 every day.
$croncmd = "0 4,16 * * * perl $0 --noscreen";
} elsif ($opt_c eq '3') {
# The following command executes the script at 04.00, 12:00, 20:00 every day.
$croncmd = "0 4,12,20 * * * perl $0 --noscreen";
} elsif ($opt_c eq '4') {
# The following command executes the script at 04.00, 10:00, 16:00, 22:00 every day.
$croncmd = "0 4,10,16,22 * * * perl $0 --noscreen";
} elsif ($opt_c eq '6') {
# The following command executes the script at 04:00, 08:00, 12:00, 16:00, 20:00, 00.00 every day.
$croncmd = "0 4,8,12,16,20,0 * * * perl $0 --noscreen";
} elsif ($opt_c eq '0') {
# The following command executes the script every 6 minutes (only for testing!)
$croncmd = '0,6,12,18,24,30,36,42,48,54 * * * * perl $0 --noscreen';
} else {
# The following command executes the script at 04:00 every day.
$croncmd = "0 4 * * * perl $0 --noscreen";
}
$file = '/etc/crontab';
open(OUT, ">> $file") or die "Couldn't open $file: $!";
print OUT "$croncmd\n";
close(OUT) or die "Couldn't close $file: $!";
print "Cron command successfully added to $file.\n";
exit;
} |