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.
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 )
103 if ( open ( my $PID, '<', $pidfilename ) ) {
108 Error("Another collector daemon is running, pid=$pid");
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 ) )
125 printf $PID ( '%d', $$ );
130 Error("Cannot open $pidfile for writing: $!");
134 Info(sprintf('Torrus version %s', '@VERSION@'));
135 Info(sprintf('%s started for tree %s', $0, $tree));
136 Debug(sprintf('Process ID %d', $$));
140 Info(sprintf('Delaying for %d minutes', $delay));
144 &Torrus::DB::setSafeSignalHandlers();
148 '-ProcessName' => $process_name,
154 $options{'-RunOnce'} = 1;
157 my $scheduler = Torrus::MonitorScheduler->new( %options );
160 if( not $options{'-RunOnce'} )
162 Error('Monitor process exited: nothing to collect');
171 if( defined($pidfile) and -r $pidfile )
180 # indent-tabs-mode: nil
181 # perl-indent-level: 4