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