This commit was generated by cvs2svn to compensate for changes in r10640,
[freeside.git] / torrus / bin / monitor.in
1 #!@PERL@
2 #  Copyright (C) 2002  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: monitor.in,v 1.1 2010-12-27 00:04:03 ivan Exp $
19 # Stanislav Sinyagin <ssinyagin@yahoo.com>
20
21 BEGIN { require '@torrus_config_pl@'; }
22
23 use strict;
24 use Proc::Daemon;
25 use Getopt::Long;
26
27 use Torrus::Log;
28 use Torrus::Monitor;
29 use Torrus::SiteConfig;
30
31 exit(1) if not Torrus::SiteConfig::verify();
32
33 our $tree;
34 our $nodaemon;
35 our $runonce;
36 our $delay = 0;
37 our $debug;
38 our $verbose;
39 our $help_needed;
40
41 # Derive the process name from the command line
42 our $process_name = $0;
43 $process_name =~ s/^.*\/([^\/]+)$/$1/;
44 $process_name .= ' ' . join(' ', @ARGV);
45
46
47 my $ok = GetOptions ('tree=s'   => \$tree,
48                      'nodaemon' => \$nodaemon,
49                      'runonce'  => \$runonce,
50                      'delay=i'  => \$delay,
51                      'debug'    => \$debug,
52                      'verbose'  => \$verbose,
53                      'help'     => \$help_needed);
54
55 if( not $ok or not $tree or $help_needed or scalar(@ARGV) > 0 )
56 {
57     print STDERR "Usage: $0 --tree=NAME [options...]\n",
58     "Options:\n",
59     "  --tree=NAME     tree name\n",
60     "  --nodaemon      do not fork daemon and log to STDERR\n",
61     "  --runonce       run one time and exit. Implies --nodaemon\n",
62     "  --delay         delay the start of the first cycle, minutes\n",
63     "  --debug         set the log level to debug\n",
64     "  --verbose       set the log level to info\n",
65     "  --help          this help message\n";
66     exit 1;
67 }
68
69 if( not Torrus::SiteConfig::mayRunMonitor( $tree ) )
70 {
71     Error('Tree ' . $tree . ' is not configured to run monitor');
72     exit 1;
73 }
74
75
76 if( $debug )
77 {
78     Torrus::Log::setLevel('debug');
79 }
80 elsif( $verbose )
81 {
82     Torrus::Log::setLevel('verbose');
83 }
84
85 my $logfile = $Torrus::Global::logDir . '/monitor.' . $tree . '.log';
86 my $pidfile;
87
88 my $rotateLogs = sub
89 {
90     Info('Caught SIGHUP. Reopening log file');
91     close( STDERR );
92     open( STDERR, ">>$logfile" );
93 };
94
95 if( not $nodaemon and not $runonce )
96 {
97     my $pidfilename =
98         $Torrus::Global::pidDir . '/monitor.' . $tree . '.pid';
99     
100     if( -r $pidfilename )
101     {
102         Error("Another monitor daemon is running, pid=",
103               `cat $pidfilename`);
104         exit 1;
105     }
106
107     &Proc::Daemon::Init();
108     umask 0017; # Proc::Daemon::Init sets the mask to all-writable
109
110     $SIG{'HUP'} = $rotateLogs;
111
112     # At this point, we cannot tell anyone if "open" fails
113     open(STDERR, ">>$logfile");
114
115     $pidfile = $pidfilename;
116     
117     if( open( PID, ">$pidfile" ) )
118     {
119         printf PID ( "%d", $$ );
120         close PID;
121     }
122     else
123     {
124         Error("Cannot open $pidfile for writing: $!");
125     }
126 }
127
128 Info(sprintf("Torrus version %s", '@VERSION@'));
129 Info(sprintf("%s started for tree %s", $0, $tree));
130 Debug(sprintf("Process ID %d", $$));
131
132 if( $delay > 0 )
133 {
134     Info(sprintf('Delaying for %d minutes', $delay));
135     sleep($delay * 60);
136 }
137
138 &Torrus::DB::setSafeSignalHandlers();
139
140 my %options =
141     (
142      '-ProcessName' => $process_name,
143      '-Tree'      => $tree,
144      '-Delay'     => $delay
145      );
146 if( $runonce )
147 {
148     $options{'-RunOnce'} = 1;
149 }
150
151 my $scheduler = new Torrus::MonitorScheduler( %options );
152 $scheduler->run();
153
154 if( not $options{'-RunOnce'} )
155 {
156     Error("Monitor process exited: nothing to collect");
157     unlink $pidfile;
158 }
159
160 exit;
161
162
163 END
164 {
165     if( defined($pidfile) and -r $pidfile )
166     {
167         unlink $pidfile;
168     }
169 }
170
171
172 # Local Variables:
173 # mode: perl
174 # indent-tabs-mode: nil
175 # perl-indent-level: 4
176 # End: