servderive commit fix
[freeside.git] / torrus / bin / action_notify.in
1 #!@PERL@
2 #  Copyright (C) 2006  Stanislav Sinyagin
3 #
4 #  This program is free software; you can redistribute it and/or modify
5 #  it under the terms of the GNU General Public License as published by
6 #  the Free Software Foundation; either version 2 of the License, or
7 #  (at your option) any later version.
8 #
9 #  This program is distributed in the hope that it will be useful,
10 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #  GNU General Public License for more details.
13 #
14 #  You should have received a copy of the GNU General Public License
15 #  along with this program; if not, write to the Free Software
16 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
17
18 # $Id: action_notify.in,v 1.1 2010-12-27 00:04:01 ivan Exp $
19 # Stanislav Sinyagin <ssinyagin@yahoo.com>
20
21 # We need this for $Torrus::Global::templateDirs
22 BEGIN { require '@torrus_config_pl@'; }
23
24 use strict;
25 use Date::Format;
26
27 use Torrus::Log;
28
29 require '@notify_siteconfig_pl@';
30
31 if( not $ENV{'TORRUS_TREE'} )
32 {
33     print STDERR ("Torrus environment variables missing. This program ",
34                   "must be run from Torrus Monitor\n");
35     exit 1;
36 }
37
38 our $now = time();
39 our $nowHour = time2str('%H', $now);
40 our $nowWeekday = time2str('%w', $now);
41
42 my $severity = $ENV{'TORRUS_SEVERITY'};
43 my $ok = 1;
44
45 foreach my $policy ( keys %Torrus::Notify::policies )
46 {
47     if( &{$Torrus::Notify::policies{$policy}{'match'}} )
48     {
49         Debug('Notification policy matched: ' . $policy);
50
51         my @targets = ();
52         my $levels = $Torrus::Notify::policies{$policy}{'severity'};
53         
54         foreach my $level ( sort {$a <=>$b} keys %{$levels} )
55         {
56             if( $severity >= $level )
57             {
58                 push( @targets, @{$levels->{$level}} );
59             }
60         }
61
62         if( isDebug() )
63         {
64             Debug('Selected notification targets: ' . join(' ', @targets));
65         }
66
67         foreach my $target ( @targets )
68         {
69             my($protocol, $arg) = split(':', $target);
70             if( defined( $Torrus::Notify::programs{$protocol} ) )
71             {
72                 $ENV{'ARG1'} = $arg;
73                 my $status = system( $Torrus::Notify::programs{$protocol} );
74                 delete $ENV{'ARG1'};
75
76                 if( $status != 0 )
77                 {
78                     Error('The command "' .
79                           $Torrus::Notify::programs{$protocol} .
80                           '" returned error code ' . $status);
81                     $ok = 0;
82                 }
83             }
84         }
85     }
86 }
87    
88 exit( $ok ? 0:1 );                          
89         
90
91
92 # Local Variables:
93 # mode: perl
94 # indent-tabs-mode: nil
95 # perl-indent-level: 4
96 # End: