minor fix to pathname
[torrus-plus.git] / src / bin / monitor.in
blob0332c59f5e9598c2d9a2a4e65a0cd0007c7b33c9
1 #!@PERL@
2 #  Copyright (C) 2002  Stanislav Sinyagin
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.
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.
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.
18 # $Id$
19 # Stanislav Sinyagin <ssinyagin@yahoo.com>
21 BEGIN { require '@torrus_config_pl@'; }
23 use strict;
24 use Proc::Daemon;
25 use Getopt::Long;
27 use Torrus::Log;
28 use Torrus::Monitor;
29 use Torrus::SiteConfig;
31 exit(1) if not Torrus::SiteConfig::verify();
33 our $tree;
34 our $nodaemon;
35 our $runonce;
36 our $delay = 0;
37 our $debug;
38 our $verbose;
39 our $help_needed;
41 # Derive the process name from the command line
42 our $process_name = $0;
43 $process_name =~ s/^.*\/([^\/]+)$/$1/;
44 $process_name .= ' ' . join(' ', @ARGV);
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);
55 if( not $ok or not $tree or $help_needed or scalar(@ARGV) > 0 )
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;
69 if( not Torrus::SiteConfig::mayRunMonitor( $tree ) )
71     Error('Tree ' . $tree . ' is not configured to run monitor');
72     exit 1;
76 if( $debug )
78     Torrus::Log::setLevel('debug');
80 elsif( $verbose )
82     Torrus::Log::setLevel('verbose');
85 my $logfile = $Torrus::Global::logDir . '/monitor.' . $tree . '.log';
86 my $pidfile;
88 my $rotateLogs = sub
90     Info('Caught SIGHUP. Reopening log file');
91     close( STDERR );
92     open( STDERR, '>>', $logfile );
95 if( not $nodaemon and not $runonce )
97     my $pidfilename =
98         $Torrus::Global::pidDir . '/monitor.' . $tree . '.pid';
100     if( -r $pidfilename )
101     {
102         my $pid;
103         if ( open ( my $PID, '<', $pidfilename ) ) {
104             $pid = <$PID>;
105             close $PID;
106         }
107         $pid ||= 'unsure?';
108         Error("Another collector daemon is running, pid=$pid");
110         exit 1;
111     }
113     &Proc::Daemon::Init();
114     umask 0017; # Proc::Daemon::Init sets the mask to all-writable
116     $SIG{'HUP'} = $rotateLogs;
118     # At this point, we cannot tell anyone if "open" fails
119     open( STDERR, '>>', $logfile );
121     $pidfile = $pidfilename;
123     if( open( my $PID, '>', $pidfile ) )
124     {
125         printf $PID ( '%d', $$ );
126         close $PID;
127     }
128     else
129     {
130         Error("Cannot open $pidfile for writing: $!");
131     }
134 Info(sprintf('Torrus version %s', '@VERSION@'));
135 Info(sprintf('%s started for tree %s', $0, $tree));
136 Debug(sprintf('Process ID %d', $$));
138 if( $delay > 0 )
140     Info(sprintf('Delaying for %d minutes', $delay));
141     sleep($delay * 60);
144 &Torrus::DB::setSafeSignalHandlers();
146 my %options =
147     (
148      '-ProcessName' => $process_name,
149      '-Tree'      => $tree,
150      '-Delay'     => $delay
151      );
152 if( $runonce )
154     $options{'-RunOnce'} = 1;
157 my $scheduler = Torrus::MonitorScheduler->new( %options );
158 $scheduler->run();
160 if( not $options{'-RunOnce'} )
162     Error('Monitor process exited: nothing to collect');
163     unlink $pidfile;
166 exit;
171     if( defined($pidfile) and -r $pidfile )
172     {
173         unlink $pidfile;
174     }
178 # Local Variables:
179 # mode: perl
180 # indent-tabs-mode: nil
181 # perl-indent-level: 4
182 # End: