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@'; }
28 use Torrus::ConfigTree;
29 use Torrus::Collector;
30 use Torrus::CollectorScheduler;
31 use Torrus::SiteConfig;
35 exit(1) if not Torrus::SiteConfig::verify();
46 # Derive the process name from the command line
47 my $process_name = $0;
48 $process_name =~ s/^.*\/([^\/]+)$/$1/;
49 $process_name .= ' ' . join(' ', @ARGV);
51 my $ok = GetOptions ('tree=s' => \$tree,
52 'instance=i' => \$instance,
53 'nodaemon' => \$nodaemon,
54 'runonce' => \$runonce,
55 'runalways' => \$runalways,
57 'verbose' => \$verbose,
58 'help' => \$help_needed);
60 if( not $ok or not $tree or $help_needed or scalar(@ARGV) > 0 )
62 print STDERR "Usage: $0 --tree=NAME [options...]\n",
64 " --tree=NAME tree name\n",
65 " --instance=N instance number for multiple collectors per tree\n",
66 " --nodaemon do not fork daemon and log to STDERR\n",
67 " --runonce run one time and exit. Implies --nodaemon\n",
68 " --runalways continue running if no collectors defined\n",
69 " --debug set the log level to debug\n",
70 " --verbose set the log level to info\n",
71 " --help this help message\n";
75 if( not Torrus::SiteConfig::mayRunCollector( $tree ) )
77 Error('Tree ' . $tree . ' is not configured to run collector');
81 my $nInstances = Torrus::SiteConfig::collectorInstances( $tree );
83 if( $nInstances > 1 and not defined( $instance ) )
85 Error('--instance option is missing');
89 if( not defined( $instance ) )
94 if( $instance >= $nInstances )
96 Error('Invalid instance number. Allowed from 0 to ' . ($nInstances-1));
102 Torrus::Log::setLevel('debug');
106 Torrus::Log::setLevel('verbose');
110 $Torrus::Global::logDir . '/collector.' . $tree . '_' . $instance . '.log';
115 Info('Caught SIGHUP. Reopening log file');
117 open( STDERR, '>>', $logfile );
121 if( not $nodaemon and not $runonce )
124 $Torrus::Global::pidDir . '/collector.' .
125 $tree . '_' . $instance . '.pid';
127 if( -r $pidfilename )
130 if ( open ( my $PID, '<', $pidfilename ) ) {
135 Error("Another collector daemon is running, pid=$pid");
140 &Proc::Daemon::Init();
141 umask 0017; # Proc::Daemon::Init sets the mask to all-writable
143 $SIG{'HUP'} = $rotateLogs;
145 # At this point, we cannot tell anyone if "open" fails
146 open( STDERR, '>>', $logfile );
148 $pidfile = $pidfilename;
150 if( open( my $PID, '>', $pidfile ) )
152 printf $PID ( '%d', $$ );
157 Error("Cannot open $pidfile for writing: $!");
162 Torrus::Collector::initThreads();
164 &Torrus::DB::setSafeSignalHandlers();
167 Info(sprintf('Torrus version %s', '@VERSION@'));
168 Info(sprintf('%s started for tree %s, instance #%d', $0, $tree, $instance));
169 Debug(sprintf('Process ID %d', $$));
173 '-ProcessName' => $process_name,
175 '-Instance' => $instance
180 $options{'-RunOnce'} = 1;
184 $options{'-RunAlways'} = 1;
188 my $scheduler = Torrus::CollectorScheduler->new( %options );
191 if( not $options{'-RunOnce'} )
193 Error('Collector process exited: nothing to collect');
202 if( defined($pidfile) and -r $pidfile )
210 # indent-tabs-mode: nil
211 # perl-indent-level: 4