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: collector.in,v 1.1 2010-12-27 00:04:03 ivan Exp $
19 # Stanislav Sinyagin <ssinyagin@yahoo.com>
21 BEGIN { require '@torrus_config_pl@'; }
28 use Torrus::ConfigTree;
29 use Torrus::Collector;
30 use Torrus::SiteConfig;
34 exit(1) if not Torrus::SiteConfig::verify();
45 # Derive the process name from the command line
46 my $process_name = $0;
47 $process_name =~ s/^.*\/([^\/]+)$/$1/;
48 $process_name .= ' ' . join(' ', @ARGV);
50 my $ok = GetOptions ('tree=s' => \$tree,
51 'instance=i' => \$instance,
52 'nodaemon' => \$nodaemon,
53 'runonce' => \$runonce,
54 'runalways' => \$runalways,
56 'verbose' => \$verbose,
57 'help' => \$help_needed);
59 if( not $ok or not $tree or $help_needed or scalar(@ARGV) > 0 )
61 print STDERR "Usage: $0 --tree=NAME [options...]\n",
63 " --tree=NAME tree name\n",
64 " --instance=N instance number for multiple collectors per tree\n",
65 " --nodaemon do not fork daemon and log to STDERR\n",
66 " --runonce run one time and exit. Implies --nodaemon\n",
67 " --runalways continue running if no collectors defined\n",
68 " --debug set the log level to debug\n",
69 " --verbose set the log level to info\n",
70 " --help this help message\n";
74 if( not Torrus::SiteConfig::mayRunCollector( $tree ) )
76 Error('Tree ' . $tree . ' is not configured to run collector');
80 my $nInstances = Torrus::SiteConfig::collectorInstances( $tree );
82 if( $nInstances > 1 and not defined( $instance ) )
84 Error('--instance option is missing');
88 if( not defined( $instance ) )
93 if( $instance >= $nInstances )
95 Error('Invalid instance number. Allowed from 0 to ' . ($nInstances-1));
101 Torrus::Log::setLevel('debug');
105 Torrus::Log::setLevel('verbose');
109 $Torrus::Global::logDir . '/collector.' . $tree . '_' . $instance . '.log';
114 Info('Caught SIGHUP. Reopening log file');
116 open( STDERR, ">>$logfile" );
120 if( not $nodaemon and not $runonce )
123 $Torrus::Global::pidDir . '/collector.' .
124 $tree . '_' . $instance . '.pid';
126 if( -r $pidfilename )
128 Error("Another collector daemon is running, pid=",
133 &Proc::Daemon::Init();
134 umask 0017; # Proc::Daemon::Init sets the mask to all-writable
136 $SIG{'HUP'} = $rotateLogs;
138 # At this point, we cannot tell anyone if "open" fails
139 open(STDERR, ">>$logfile");
141 $pidfile = $pidfilename;
143 if( open( PID, ">$pidfile" ) )
145 printf PID ( "%d", $$ );
150 Error("Cannot open $pidfile for writing: $!");
155 Torrus::Collector::initThreads();
157 &Torrus::DB::setSafeSignalHandlers();
160 Info(sprintf("Torrus version %s", '@VERSION@'));
161 Info(sprintf("%s started for tree %s, instance #%d", $0, $tree, $instance));
162 Debug(sprintf("Process ID %d", $$));
166 '-ProcessName' => $process_name,
168 '-Instance' => $instance
173 $options{'-RunOnce'} = 1;
177 $options{'-RunAlways'} = 1;
181 my $scheduler = new Torrus::CollectorScheduler( %options );
184 if( not $options{'-RunOnce'} )
186 Error("Collector process exited: nothing to collect");
195 if( defined($pidfile) and -r $pidfile )
203 # indent-tabs-mode: nil
204 # perl-indent-level: 4