-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalarm-on-fail
executable file
·106 lines (86 loc) · 2.25 KB
/
alarm-on-fail
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
#!/usr/bin/perl -w
# Die Jun 19 15:19:52 CEST 2007
(my $email='pflanze%gmx,ch')=~ tr/%,/@./;
use strict;
our @alarm= ("alarm");
our $interval= 60*60*24; #seconds
$0=~ /(.*?)([^\/]+)\z/s or die "?";
my ($mydir, $myname)=($1,$2);
sub usage {
print STDERR map{"$_\n"} @_ if @_;
print "$myname cmd args
Runs cmd with args, runs @alarm program with 'cmd args' as arguments
if cmd returns with non-zero exit code.
But only sends an alarm of the same cmd+args once ~a day~.
Does not trap the output of cmd currently (let that go through as cron mails).
(Christian Jaeger <$email>)
";
exit (@_ ? 1 : 0);
}
our @args;
our $DEBUG=0;
our $verbose;
for (my $i=0; $i<=$#ARGV; $i++) {
local $_=$ARGV[$i];
if (/^--?h(elp)?$/) {
usage
} elsif ($_ eq '--') {
push @args, @ARGV[$i+1..$#ARGV];
last;
} elsif (/^--?d(ebug)?$/) {
$DEBUG=1;
} elsif (/^--?v(erbose)?$/) {
$verbose=1;
# } elsif (/^--?X(?:XXX(?:=(.*))?)?$/) {
# if (defined $1) {
# $XXX=$1
# } else {
# $XXX=$ARGV[++$i] or usage "missing argument for '$_' option";
# }
} elsif (/^-./) {
usage("Unknown option '$_'\n");
} else {
#push @args, $_
push @args, @ARGV[$i..$#ARGV];
last;
}
}
usage unless @args>=1;
#use Chj::repl;repl;
sub homedir {
## is this complication needed or not?
if (my $h= $ENV{HOME}) {
$h
} else {
if (my @r= getpwuid($>)) {
$r[7] or die "can't get home dir for user $>";
} else {
die "can't get user entry $>";
}
}
}
our $basedir= homedir."/.$myname";
mkdir $basedir,0700;
use Digest::MD5 'md5_hex';
use Chj::xperlfunc;
use Chj::FileStore::Stamp;
use Chj::singlequote 'singlequote_many';
sub conditional_alarm {
my ($rv)=@_;
local our $md= md5_hex(join "\0",@args);
local our $stamp= Chj::FileStore::Stamp->new("$basedir/$md");
local our $maybe_mtime= $stamp->mtime;
local our $time= time;
if ($maybe_mtime and ($time - $maybe_mtime) < $interval) {
print "$myname: already sent alarm for $md (".singlequote_many(@args).") at ".localtime($maybe_mtime)."\n" if $verbose;
} else {
xxsystem @alarm,$rv,@args;#rvheh.
$stamp->xtouch;
}
}
our $rv= system @args;
if ($rv==0) {
#all well
} else {
conditional_alarm ($rv);
}