1 |
dpavlin |
1.1 |
#!/usr/bin/perl -w |
2 |
|
|
# |
3 |
dpavlin |
1.2 |
# sms.alert - send an alert via smstools available at |
4 |
|
|
# http://www.isis.de/~s.frings/smstools/ |
5 |
dpavlin |
1.1 |
# |
6 |
dpavlin |
1.4 |
# based on qpage.alert and mail.alert by Jim Trocki, trockij@transmeta.com |
7 |
dpavlin |
1.1 |
# |
8 |
|
|
# usage: |
9 |
dpavlin |
1.4 |
# sms.alter [flags...] -g group -s system 385abxxxxxx|name[ 12345|name...] |
10 |
|
|
# |
11 |
|
|
# flags which are standard: |
12 |
dpavlin |
1.1 |
# |
13 |
|
|
# -u this is upalert |
14 |
dpavlin |
1.4 |
# -g grp group |
15 |
|
|
# -s sys system |
16 |
|
|
# -l time time to next alert |
17 |
|
|
# |
18 |
|
|
# flags specific to sms.alert: (all optional) |
19 |
|
|
# |
20 |
dpavlin |
1.1 |
# -f send flash sms (directly to screen) |
21 |
dpavlin |
1.4 |
# -x send extended message (all output, smstools with split it in |
22 |
dpavlin |
1.1 |
# multiple smses if needed) |
23 |
dpavlin |
1.8 |
# -i hh:mm-hh:mm send sms only if it falls in this interval |
24 |
dpavlin |
1.1 |
# |
25 |
dpavlin |
1.4 |
# it users file /usr/local/etc/sms.addressbook as addressbook in format |
26 |
|
|
# |
27 |
|
|
# 123456 name |
28 |
|
|
# |
29 |
|
|
# where 123456 is phone number (with country prefix, but without +) and |
30 |
|
|
# name is alias used like: |
31 |
|
|
# |
32 |
dpavlin |
1.7 |
# sms.alert -f dobrica |
33 |
dpavlin |
1.4 |
# |
34 |
|
|
# in mon.cf |
35 |
dpavlin |
1.1 |
|
36 |
|
|
my $sms_outgoing = "/var/spool/sms/outgoing"; |
37 |
|
|
my $sms_addressbook = "/usr/local/etc/sms.addressbook"; |
38 |
|
|
|
39 |
|
|
use strict; |
40 |
|
|
my %opt; |
41 |
|
|
use Getopt::Std; |
42 |
dpavlin |
1.8 |
getopts ("S:s:g:h:t:l:ufi:", \%opt); |
43 |
dpavlin |
1.1 |
|
44 |
|
|
# read addressbook |
45 |
|
|
my %name2phone; |
46 |
|
|
if (open(A, $sms_addressbook)) { |
47 |
|
|
while(<A>) { |
48 |
|
|
chomp; |
49 |
|
|
next if (/^[#;]/); |
50 |
|
|
my ($phone,$name) = split(/\s+/,$_,2); |
51 |
|
|
$name2phone{lc($name)} = $phone; |
52 |
|
|
} |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
# |
56 |
|
|
# the first line is summary information, adequate to send to a pager |
57 |
|
|
# or email subject line |
58 |
|
|
# |
59 |
|
|
# |
60 |
|
|
# the following lines normally contain more detailed information, |
61 |
|
|
# but this is monitor-dependent |
62 |
|
|
# |
63 |
|
|
my @MSG=<STDIN>; |
64 |
|
|
my $summary = shift @MSG; |
65 |
|
|
chomp $summary; |
66 |
|
|
|
67 |
dpavlin |
1.8 |
if ($opt{i}) { |
68 |
|
|
if ($opt{i} =~ /(\d+):(\d+)-(\d+):(\d+)/) { |
69 |
|
|
my $from = $1 * 60 + $2; |
70 |
|
|
my $to = $3 * 60 + $4; |
71 |
|
|
my ($sec,$min,$hour) = localtime(time); |
72 |
|
|
my $t = $hour * 60 + $min; |
73 |
|
|
exit 0 if ($t < $from || $t > $to); |
74 |
|
|
} else { |
75 |
|
|
die "interval time format: hh:mm-hh:mm" |
76 |
|
|
} |
77 |
|
|
} |
78 |
|
|
|
79 |
dpavlin |
1.4 |
$summary = $opt{S} if $opt{S}; |
80 |
|
|
|
81 |
dpavlin |
1.1 |
my ($wday,$mon,$day,$tm) = split (/\s+/, localtime); |
82 |
|
|
|
83 |
|
|
my $ALERT = $opt{u} ? "UPALERT" : "ALERT"; |
84 |
|
|
|
85 |
|
|
my $sms = "From: mon\n"; |
86 |
dpavlin |
1.4 |
$sms .= "Flash: yes\n" if $opt{f}; |
87 |
dpavlin |
1.5 |
$sms .= "\n$ALERT"; |
88 |
|
|
$sms .= " $opt{g}/$opt{s}" if ($opt{g} && $opt{s}); |
89 |
|
|
$sms .= " $summary ($wday $mon $day $tm)\n"; |
90 |
dpavlin |
1.4 |
# add next alter time |
91 |
|
|
$sms .= "Secs until next alert: $opt{l}\n" if $opt{l}; |
92 |
|
|
# add rest of text |
93 |
|
|
$sms .= join("\n",@MSG) if $opt{x}; |
94 |
dpavlin |
1.1 |
|
95 |
dpavlin |
1.7 |
my $suffix = 0; |
96 |
|
|
|
97 |
dpavlin |
1.4 |
foreach my $to (@ARGV) { |
98 |
dpavlin |
1.7 |
if (open(SMS, "> $sms_outgoing/mon-$$-$suffix")) { |
99 |
dpavlin |
1.1 |
my $phone = $to; |
100 |
|
|
$phone = $name2phone{lc($to)} if ($name2phone{lc($to)}); |
101 |
dpavlin |
1.4 |
die "$phone is not phone number! " if ($phone !~ m/^\d+$/); |
102 |
dpavlin |
1.1 |
print SMS "To: $phone\n$sms"; |
103 |
|
|
close SMS; |
104 |
dpavlin |
1.7 |
$suffix++; |
105 |
dpavlin |
1.1 |
} else { |
106 |
|
|
die "could not open sms file in '$sms_outgoing': $!"; |
107 |
|
|
} |
108 |
|
|
} |