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: monitor.in,v 1.1 2010-12-27 00:04:03 ivan Exp $
19 # Stanislav Sinyagin <ssinyagin@yahoo.com>
21 BEGIN { require '@torrus_config_pl@'; }
29 use Torrus::SiteConfig;
31 exit(1) if not Torrus::SiteConfig::verify();
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,
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",
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";
69 if( not Torrus::SiteConfig::mayRunMonitor( $tree ) )
71 Error('Tree ' . $tree . ' is not configured to run monitor');
78 Torrus::Log::setLevel('debug');
82 Torrus::Log::setLevel('verbose');
85 my $logfile = $Torrus::Global::logDir . '/monitor.' . $tree . '.log';
90 Info('Caught SIGHUP. Reopening log file');
92 open( STDERR, ">>$logfile" );
95 if( not $nodaemon and not $runonce )
98 $Torrus::Global::pidDir . '/monitor.' . $tree . '.pid';
100 if( -r $pidfilename )
102 Error("Another monitor daemon is running, pid=",
107 &Proc::Daemon::Init();
108 umask 0017; # Proc::Daemon::Init sets the mask to all-writable
110 $SIG{'HUP'} = $rotateLogs;
112 # At this point, we cannot tell anyone if "open" fails
113 open(STDERR, ">>$logfile");
115 $pidfile = $pidfilename;
117 if( open( PID, ">$pidfile" ) )
119 printf PID ( "%d", $$ );
124 Error("Cannot open $pidfile for writing: $!");
128 Info(sprintf("Torrus version %s", '@VERSION@'));
129 Info(sprintf("%s started for tree %s", $0, $tree));
130 Debug(sprintf("Process ID %d", $$));
134 Info(sprintf('Delaying for %d minutes', $delay));
138 &Torrus::DB::setSafeSignalHandlers();
142 '-ProcessName' => $process_name,
148 $options{'-RunOnce'} = 1;
151 my $scheduler = new Torrus::MonitorScheduler( %options );
154 if( not $options{'-RunOnce'} )
156 Error("Monitor process exited: nothing to collect");
165 if( defined($pidfile) and -r $pidfile )
174 # indent-tabs-mode: nil
175 # perl-indent-level: 4