File: monitor.in

package info (click to toggle)
torrus 3.00-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 4,548 kB
  • sloc: perl: 34,614; xml: 20,934; sh: 3,650; makefile: 713
file content (223 lines) | stat: -rw-r--r-- 5,316 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!@PERL@
#  Copyright (C) 2002  Stanislav Sinyagin
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

# Stanislav Sinyagin <ssinyagin@k-open.com>

# we cannot report a failure of STDERR reopening, as we are already a
# forked child, so this Perl::Critic warning is disabled.
## no critic (InputOutput::RequireCheckedOpen)

use strict;
use warnings;
BEGIN { require '@torrus_config_pl@'; }

use IO::File;
use Proc::Daemon;
use Getopt::Long;

use Torrus::Log;
use Torrus::Monitor;
use Torrus::AgentScheduler;
use Torrus::SiteConfig;

exit(1) unless Torrus::SiteConfig::verify();

my $tree;
my $nodaemon;
my $runonce;
my $runalways;
my $delay = 0;
my $debug;
my $verbose;
my $help_needed;

# Derive the process name from the command line
my $process_name = $0;
$process_name =~ s/^.*\/([^\/]+)$/$1/;
$process_name .= ' ' . join(' ', @ARGV);


my $ok = GetOptions ('tree=s'   => \$tree,
                     'nodaemon' => \$nodaemon,
                     'runonce'  => \$runonce,
                     'runalways' => \$runalways,
                     'delay=i'  => \$delay,
                     'debug'    => \$debug,
                     'verbose'  => \$verbose,
                     'help'     => \$help_needed);

if( not $ok or not $tree or $help_needed or scalar(@ARGV) > 0 )
{
    print STDERR "Usage: $0 --tree=NAME [options...]\n",
    "Options:\n",
    "  --tree=NAME     tree name\n",
    "  --nodaemon      do not fork daemon and log to STDERR\n",
    "  --runonce       run one time and exit. Implies --nodaemon\n",
    "  --runalways     continue running if no monitors defined\n",
    "  --delay         delay the start of the first cycle, minutes\n",
    "  --debug         set the log level to debug\n",
    "  --verbose       set the log level to info\n",
    "  --help          this help message\n";
    exit 1;
}

if( not Torrus::SiteConfig::mayRunMonitor( $tree ) )
{
    Error('Tree ' . $tree . ' is not configured to run monitor');
    exit 1;
}


if( $debug )
{
    Torrus::Log::setLevel('debug');
}
elsif( $verbose )
{
    Torrus::Log::setLevel('verbose');
}


my $pidfile;

if( not $nodaemon and not $runonce )
{
    my $pidfilename =
        $Torrus::Global::pidDir . '/monitor.' . $tree . '.pid';
    
    if( -r $pidfilename )
    {
        my $oldpid;
        my $fh = IO::File->new($pidfilename, 'r');
        if( defined($fh) )
        {
            $oldpid = $fh->getline();
        }
        $fh->close();
        
        $oldpid = 'unknown' unless defined($oldpid);
        
        Error('Another monitor daemon is running, pid=', $oldpid);
        exit 1;
    }

    &Proc::Daemon::Init();
    umask 0017; # Proc::Daemon::Init sets the mask to all-writable

    setup_sighandlers();

    # now we're forked, save the PID file for the END block
    $pidfile = $pidfilename;
    
    if( $Torrus::Monitor::useSyslog )
    {
        Torrus::Log::enableSyslog('torrus/monitor_' . $tree);
    }
    else
    {
        my $logfile = $Torrus::Global::logDir . '/monitor.' . $tree . '.log';
        
        # At this point, we cannot tell anyone if "open" fails
        open(STDERR, '>>', $logfile);
        *STDERR->autoflush();
    }

    my $fh = IO::File->new($pidfile, 'w');
    if( defined($fh) )
    {
        $fh->printf('%d', $$);
        $fh->close();
    }
    else
    {
        Error("Cannot open $pidfile for writing: $!");
    }
}

Info(sprintf("Torrus version %s", '@VERSION@'));
Info(sprintf("%s started for tree %s", $0, $tree));
Info(sprintf("Process ID %d", $$));

if( $delay > 0 )
{
    Info(sprintf('Delaying for %d minutes', $delay));
    sleep($delay * 60);
}

my %options =
    (
     '-ProcessName' => $process_name,
     '-AgentName'   => 'monitor',
     '-Tree'      => $tree,
     '-Instance'    => 0
    );
if( $runonce )
{
    $options{'-RunOnce'} = 1;
}
if( $runalways or $Torrus::Monitor::runAlways )
{
    $options{'-RunAlways'} = 1;
}

eval {
    my $scheduler = new Torrus::AgentScheduler( %options );
    $scheduler->run();
};

if( not $options{'-RunOnce'} )
{
    Error('Monitor process exited: ' .
          'there is no datasource where monitoring is enabled');
}

exit;


sub setup_sighandlers
{
    $SIG{'HUP'} = sub {
        Warn('Received SIGHUP. Stopping the process.');
        exit;
    };

    $SIG{'TERM'} = sub {
        Warn('Received SIGTERM. Stopping the process.');
        exit;
    };

    $SIG{'INT'} = sub {
        Warn('Received SIGINT. Stopping the process');
        exit;
    };
};    


END
{
    if( defined($pidfile) and -r $pidfile )
    {
        unlink $pidfile;
    }
}


# Local Variables:
# mode: perl
# indent-tabs-mode: nil
# perl-indent-level: 4
# End: