1 |
dpavlin |
1.1 |
#!/usr/bin/perl -w |
2 |
|
|
# |
3 |
|
|
# sms.alert - send an alert via smstools |
4 |
|
|
# |
5 |
|
|
# based on qpage.alert by Jim Trocki, trockij@transmeta.com |
6 |
|
|
# |
7 |
|
|
# usage: |
8 |
|
|
# sms.alter [-f][-l][-u] 385abxxxxxx |
9 |
|
|
# |
10 |
|
|
# flags: |
11 |
|
|
# -u this is upalert |
12 |
|
|
# -f send flash sms (directly to screen) |
13 |
|
|
# -l send long message (all output, smstools with split it in |
14 |
|
|
# multiple smses if needed) |
15 |
|
|
# |
16 |
|
|
|
17 |
|
|
my $sms_outgoing = "/var/spool/sms/outgoing"; |
18 |
|
|
my $sms_addressbook = "/usr/local/etc/sms.addressbook"; |
19 |
|
|
|
20 |
|
|
use strict; |
21 |
|
|
my %opt; |
22 |
|
|
use Getopt::Std; |
23 |
|
|
getopts ("s:g:fu", \%opt); |
24 |
|
|
|
25 |
|
|
# read addressbook |
26 |
|
|
my %name2phone; |
27 |
|
|
if (open(A, $sms_addressbook)) { |
28 |
|
|
while(<A>) { |
29 |
|
|
chomp; |
30 |
|
|
next if (/^[#;]/); |
31 |
|
|
my ($phone,$name) = split(/\s+/,$_,2); |
32 |
|
|
$name2phone{lc($name)} = $phone; |
33 |
|
|
} |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
# |
37 |
|
|
# the first line is summary information, adequate to send to a pager |
38 |
|
|
# or email subject line |
39 |
|
|
# |
40 |
|
|
# |
41 |
|
|
# the following lines normally contain more detailed information, |
42 |
|
|
# but this is monitor-dependent |
43 |
|
|
# |
44 |
|
|
my @MSG=<STDIN>; |
45 |
|
|
my $summary = shift @MSG; |
46 |
|
|
chomp $summary; |
47 |
|
|
|
48 |
|
|
my ($wday,$mon,$day,$tm) = split (/\s+/, localtime); |
49 |
|
|
|
50 |
|
|
my $ALERT = $opt{u} ? "UPALERT" : "ALERT"; |
51 |
|
|
|
52 |
|
|
my $sms = "From: mon\n"; |
53 |
|
|
$sms .= "Flash: yes\n" if $opt{'f'}; |
54 |
|
|
$sms .= "\n$ALERT $opt{g}/$opt{s} $summary ($wday $mon $day $tm)\n"; |
55 |
|
|
$sms .= join("\n",@MSG) if $opt{'l'}; |
56 |
|
|
|
57 |
|
|
foreach my $to (@ARGV) { |
58 |
|
|
if (open(SMS, "> $sms_outgoing/mon$$")) { |
59 |
|
|
my $phone = $to; |
60 |
|
|
$phone = $name2phone{lc($to)} if ($name2phone{lc($to)}); |
61 |
|
|
print SMS "To: $phone\n$sms"; |
62 |
|
|
close SMS; |
63 |
|
|
} else { |
64 |
|
|
die "could not open sms file in '$sms_outgoing': $!"; |
65 |
|
|
} |
66 |
|
|
} |