minor fix to pathname
[torrus-plus.git] / src / bin / collector.in
blob7063b13d3bfc8b195fa52c2604edff990d8176a0
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::ConfigTree;
29 use Torrus::Collector;
30 use Torrus::CollectorScheduler;
31 use Torrus::SiteConfig;
33 $| = 1;
35 exit(1) if not Torrus::SiteConfig::verify();
37 my $tree;
38 my $instance;
39 my $nodaemon;
40 my $runonce;
41 my $runalways;
42 my $debug;
43 my $verbose;
44 my $help_needed;
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,
56                      'debug'      => \$debug,
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",
63     "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";
72     exit 1;
75 if( not Torrus::SiteConfig::mayRunCollector( $tree ) )
77     Error('Tree ' . $tree . ' is not configured to run collector');
78     exit 1;
81 my $nInstances = Torrus::SiteConfig::collectorInstances( $tree );
83 if( $nInstances > 1 and not defined( $instance ) )
85     Error('--instance option is missing');
86     exit 1;
89 if( not defined( $instance ) )
91     $instance = 0;
94 if( $instance >= $nInstances )
96     Error('Invalid instance number. Allowed from 0 to ' . ($nInstances-1));
97     exit 1;
100 if( $debug )
102     Torrus::Log::setLevel('debug');
104 elsif( $verbose )
106     Torrus::Log::setLevel('verbose');
109 my $logfile =
110     $Torrus::Global::logDir . '/collector.' . $tree . '_' . $instance . '.log';
111 my $pidfile;
113 my $rotateLogs = sub
115     Info('Caught SIGHUP. Reopening log file');
116     close( STDERR );
117     open( STDERR, '>>', $logfile );
118     $| = 1;
121 if( not $nodaemon and not $runonce )
123     my $pidfilename =
124         $Torrus::Global::pidDir . '/collector.' .
125         $tree . '_' . $instance . '.pid';
127     if( -r $pidfilename )
128     {
129         my $pid;
130         if ( open ( my $PID, '<', $pidfilename ) ) {
131             $pid = <$PID>;
132             close $PID;
133         }
134         $pid ||= 'unsure?';
135         Error("Another collector daemon is running, pid=$pid");
137         exit 1;
138     }
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 ) )
151     {
152         printf $PID ( '%d', $$ );
153         close $PID;
154     }
155     else
156     {
157         Error("Cannot open $pidfile for writing: $!");
158     }
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', $$));
171 my %options =
172     (
173      '-ProcessName' => $process_name,
174      '-Tree'        => $tree,
175      '-Instance'    => $instance
176      );
178 if( $runonce )
180     $options{'-RunOnce'} = 1;
182 if( $runalways )
184     $options{'-RunAlways'} = 1;
188 my $scheduler = Torrus::CollectorScheduler->new( %options );
189 $scheduler->run();
191 if( not $options{'-RunOnce'} )
193     Error('Collector process exited: nothing to collect');
194     unlink $pidfile;
197 exit;
202     if( defined($pidfile) and -r $pidfile )
203     {
204         unlink $pidfile;
205     }
208 # Local Variables:
209 # mode: perl
210 # indent-tabs-mode: nil
211 # perl-indent-level: 4
212 # End: