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 |
|
|
# |
24 |
dpavlin |
1.4 |
# it users file /usr/local/etc/sms.addressbook as addressbook in format |
25 |
|
|
# |
26 |
|
|
# 123456 name |
27 |
|
|
# |
28 |
|
|
# where 123456 is phone number (with country prefix, but without +) and |
29 |
|
|
# name is alias used like: |
30 |
|
|
# |
31 |
|
|
# sms.alter -f dobrica |
32 |
|
|
# |
33 |
|
|
# in mon.cf |
34 |
dpavlin |
1.1 |
|
35 |
|
|
my $sms_outgoing = "/var/spool/sms/outgoing"; |
36 |
|
|
my $sms_addressbook = "/usr/local/etc/sms.addressbook"; |
37 |
|
|
|
38 |
|
|
use strict; |
39 |
|
|
my %opt; |
40 |
|
|
use Getopt::Std; |
41 |
dpavlin |
1.4 |
getopts ("S:s:g:h:t:l:uf"); |
42 |
dpavlin |
1.1 |
|
43 |
|
|
# read addressbook |
44 |
|
|
my %name2phone; |
45 |
|
|
if (open(A, $sms_addressbook)) { |
46 |
|
|
while(<A>) { |
47 |
|
|
chomp; |
48 |
|
|
next if (/^[#;]/); |
49 |
|
|
my ($phone,$name) = split(/\s+/,$_,2); |
50 |
|
|
$name2phone{lc($name)} = $phone; |
51 |
|
|
} |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
# |
55 |
|
|
# the first line is summary information, adequate to send to a pager |
56 |
|
|
# or email subject line |
57 |
|
|
# |
58 |
|
|
# |
59 |
|
|
# the following lines normally contain more detailed information, |
60 |
|
|
# but this is monitor-dependent |
61 |
|
|
# |
62 |
|
|
my @MSG=<STDIN>; |
63 |
|
|
my $summary = shift @MSG; |
64 |
|
|
chomp $summary; |
65 |
|
|
|
66 |
dpavlin |
1.4 |
$summary = $opt{S} if $opt{S}; |
67 |
|
|
|
68 |
dpavlin |
1.1 |
my ($wday,$mon,$day,$tm) = split (/\s+/, localtime); |
69 |
|
|
|
70 |
|
|
my $ALERT = $opt{u} ? "UPALERT" : "ALERT"; |
71 |
|
|
|
72 |
|
|
my $sms = "From: mon\n"; |
73 |
dpavlin |
1.4 |
$sms .= "Flash: yes\n" if $opt{f}; |
74 |
dpavlin |
1.1 |
$sms .= "\n$ALERT $opt{g}/$opt{s} $summary ($wday $mon $day $tm)\n"; |
75 |
dpavlin |
1.4 |
# add next alter time |
76 |
|
|
$sms .= "Secs until next alert: $opt{l}\n" if $opt{l}; |
77 |
|
|
# add rest of text |
78 |
|
|
$sms .= join("\n",@MSG) if $opt{x}; |
79 |
dpavlin |
1.1 |
|
80 |
dpavlin |
1.4 |
foreach my $to (@ARGV) { |
81 |
dpavlin |
1.1 |
if (open(SMS, "> $sms_outgoing/mon$$")) { |
82 |
|
|
my $phone = $to; |
83 |
|
|
$phone = $name2phone{lc($to)} if ($name2phone{lc($to)}); |
84 |
dpavlin |
1.4 |
die "$phone is not phone number! " if ($phone !~ m/^\d+$/); |
85 |
dpavlin |
1.1 |
print SMS "To: $phone\n$sms"; |
86 |
|
|
close SMS; |
87 |
|
|
} else { |
88 |
|
|
die "could not open sms file in '$sms_outgoing': $!"; |
89 |
|
|
} |
90 |
|
|
} |