fix ticketing system error on bootstrap of new install
[freeside.git] / torrus / bin / collector.in
1 #!@PERL@
2 #  Copyright (C) 2002  Stanislav Sinyagin
3 #
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.
8 #
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.
13 #
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.
17
18 # $Id: collector.in,v 1.1 2010-12-27 00:04:03 ivan Exp $
19 # Stanislav Sinyagin <ssinyagin@yahoo.com>
20
21 BEGIN { require '@torrus_config_pl@'; }
22
23 use strict;
24 use Proc::Daemon;
25 use Getopt::Long;
26
27 use Torrus::Log;
28 use Torrus::ConfigTree;
29 use Torrus::Collector;
30 use Torrus::SiteConfig;
31
32 $| = 1;
33
34 exit(1) if not Torrus::SiteConfig::verify();
35
36 my $tree;
37 my $instance;
38 my $nodaemon;
39 my $runonce;
40 my $runalways;
41 my $debug;
42 my $verbose;
43 my $help_needed;
44
45 # Derive the process name from the command line
46 my $process_name = $0;
47 $process_name =~ s/^.*\/([^\/]+)$/$1/;
48 $process_name .= ' ' . join(' ', @ARGV);
49                             
50 my $ok = GetOptions ('tree=s'     => \$tree,
51                      'instance=i' => \$instance,
52                      'nodaemon'   => \$nodaemon,
53                      'runonce'    => \$runonce,
54                      'runalways'  => \$runalways,
55                      'debug'      => \$debug,
56                      'verbose'    => \$verbose,
57                      'help'       => \$help_needed);
58
59 if( not $ok or not $tree or $help_needed or scalar(@ARGV) > 0 )
60 {
61     print STDERR "Usage: $0 --tree=NAME [options...]\n",
62     "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";
71     exit 1;
72 }
73
74 if( not Torrus::SiteConfig::mayRunCollector( $tree ) )
75 {
76     Error('Tree ' . $tree . ' is not configured to run collector');
77     exit 1;
78 }
79
80 my $nInstances = Torrus::SiteConfig::collectorInstances( $tree );
81
82 if( $nInstances > 1 and not defined( $instance ) )
83 {
84     Error('--instance option is missing');
85     exit 1;
86 }
87
88 if( not defined( $instance ) )
89 {
90     $instance = 0;
91 }
92
93 if( $instance >= $nInstances )
94 {
95     Error('Invalid instance number. Allowed from 0 to ' . ($nInstances-1));
96     exit 1;
97 }
98
99 if( $debug )
100 {
101     Torrus::Log::setLevel('debug');
102 }
103 elsif( $verbose )
104 {
105     Torrus::Log::setLevel('verbose');
106 }
107
108 my $logfile =
109     $Torrus::Global::logDir . '/collector.' . $tree . '_' . $instance . '.log';
110 my $pidfile;
111
112 my $rotateLogs = sub
113 {
114     Info('Caught SIGHUP. Reopening log file');
115     close( STDERR );
116     open( STDERR, ">>$logfile" );
117     $| = 1;
118 };
119
120 if( not $nodaemon and not $runonce )
121 {
122     my $pidfilename = 
123         $Torrus::Global::pidDir . '/collector.' .
124         $tree . '_' . $instance . '.pid';
125     
126     if( -r $pidfilename )
127     {
128         Error("Another collector daemon is running, pid=",
129               `cat $pidfilename`);
130         exit 1;
131     }
132
133     &Proc::Daemon::Init();
134     umask 0017; # Proc::Daemon::Init sets the mask to all-writable
135     
136     $SIG{'HUP'} = $rotateLogs;
137
138     # At this point, we cannot tell anyone if "open" fails
139     open(STDERR, ">>$logfile");
140
141     $pidfile = $pidfilename;
142     
143     if( open( PID, ">$pidfile" ) )
144     {
145         printf PID ( "%d", $$ );
146         close PID;
147     }
148     else
149     {
150         Error("Cannot open $pidfile for writing: $!");
151     }
152 }
153
154
155 Torrus::Collector::initThreads();
156
157 &Torrus::DB::setSafeSignalHandlers();
158
159
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", $$));
163
164 my %options =
165     (
166      '-ProcessName' => $process_name,
167      '-Tree'        => $tree,
168      '-Instance'    => $instance
169      );
170
171 if( $runonce )
172 {
173     $options{'-RunOnce'} = 1;
174 }
175 if( $runalways )
176 {
177     $options{'-RunAlways'} = 1;
178 }
179
180
181 my $scheduler = new Torrus::CollectorScheduler( %options );
182 $scheduler->run();
183
184 if( not $options{'-RunOnce'} )
185 {
186     Error("Collector process exited: nothing to collect");
187     unlink $pidfile;
188 }
189
190 exit;
191
192
193 END
194 {
195     if( defined($pidfile) and -r $pidfile )
196     {
197         unlink $pidfile;
198     }
199 }
200
201 # Local Variables:
202 # mode: perl
203 # indent-tabs-mode: nil
204 # perl-indent-level: 4
205 # End: