1#!/usr/bin/perl -wT
2# -*- cperl -*-
3#
4# Copyright (C) 2002-2009 Audun Ytterdal, Jimmy Olsen, Tore Anderson,
5#    Nicolai Langfeldt
6#
7# This program is free software; you can redistribute it and/or
8# modify it under the terms of the GNU General Public License
9# as published by the Free Software Foundation; version 2 dated June,
10# 1991.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20#
21
22use strict;
23use warnings;
24
25# Trust PERL5LIB from environment
26use lib map { /(.*)/ } split(/:/, ($ENV{PERL5LIB} || ''));
27
28use Getopt::Long;
29
30use Munin::Common::Defaults;
31use Munin::Node::Config;
32use Munin::Node::OS;
33use Munin::Node::Service;
34use Munin::Node::Server;
35
36my $servicedir;
37my $sconfdir = "$Munin::Common::Defaults::MUNIN_CONFDIR/plugin-conf.d";
38my $conffile = "$Munin::Common::Defaults::MUNIN_CONFDIR/munin-node.conf";
39my $DEBUG    = 0;
40my $PIDEBUG  = 0;
41my $paranoia = 0;
42my $foreground = 0;
43
44sub main
45{
46    chdir ("/");
47
48    # "Clean" environment to disable taint-checking on the environment. We _know_
49    # that the environment is insecure, but we want to let admins shoot themselves
50    # in the foot with it, if they want to.
51    foreach my $key (keys %ENV) {
52        $ENV{$key} =~ /^(.*)$/;
53        $ENV{$key} = $1;
54    }
55
56    # plugins run in taint mode because the uid is changed, so the path
57    # must not contain writable directories.
58    $ENV{PATH}='/bin:/sbin:/usr/bin:/usr/sbin:%%PREFIX%%/bin:%%PREFIX%%/sbin';
59
60    parse_args();
61
62    my $config = Munin::Node::Config->instance();
63    $config->parse_config_from_file($conffile);
64
65    $paranoia = $config->{paranoia} if defined $config->{paranoia};
66
67    my $services = Munin::Node::Service->new(
68        servicedir => $servicedir,
69        defuser    => $config->{defuser},
70        defgroup   => $config->{defgroup},
71        pidebug    => $PIDEBUG,
72        timeout    => $config->{timeout},
73    );
74
75    $config->reinitialize({
76        %$config,
77
78        services => $services,
79        sconfdir => $sconfdir,
80        conffile => $conffile,
81
82        DEBUG    => $DEBUG,
83
84        paranoia => $paranoia,
85    });
86
87    my %server_params = (
88        syslog_ident => 'munin-node',
89        conf_file    => $conffile,
90    );
91    # Optionally force foreground mode (overriding settings in the configuration file).
92    # This is necessary for using the optional "sd_notify" feature:
93    #     * Net::Server::Fork goes into background before the socket is ready.  Thus we need to
94    #       prevent it from daemonizing.
95    #     * start-stop-daemon supports '--notify-await' only combined with '--background'. Thus
96    #       munin-node may not daemonize on its own.
97    if ($foreground) {
98        $server_params{background} = 0;
99        $server_params{setsid} = 0;
100    }
101    Munin::Node::Server->run(%server_params);
102
103    # Untaint $0 after Munin::Node::Server has had a chance of getting
104    # the original value
105    $0 =~ /([^\/]*)$/;
106    $0 = $1;
107
108    return 0;
109}
110
111
112sub parse_args
113{
114    my @ORIG_ARGV  = @ARGV;
115
116    print_usage_and_exit() unless GetOptions(
117        "config=s"     => \$conffile,
118        "debug!"       => \$DEBUG,
119        "pidebug!"     => \$PIDEBUG,
120        "foreground!"  => \$foreground,
121        "paranoia!"    => \$paranoia,
122        "version"      => \&print_version_and_exit,
123        "help"         => \&print_usage_and_exit,
124    );
125
126    # Reset ARGV (for HUPing)
127    @ARGV = @ORIG_ARGV;
128
129    return;
130}
131
132
133sub print_usage_and_exit
134{
135    require Pod::Usage;
136    Pod::Usage::pod2usage(-verbose => 1);
137}
138
139
140sub print_version_and_exit
141{
142    require Pod::Usage;
143    Pod::Usage::pod2usage(
144        -verbose => 99,
145        -sections => 'VERSION|COPYRIGHT',
146    );
147}
148
149
150exit main() unless caller;
151
152
1531;
154
155__END__
156
157=head1 NAME
158
159munin-node - A daemon to gather information in cooperation with the main
160Munin program
161
162=head1 SYNOPSIS
163
164munin-node [--options]
165
166=head1 DESCRIPTION
167
168munin-node is a daemon for reporting statistics on system performance.
169
170It doesn't produce these itself, but instead relies on a number of plugins
171which are responsible for gathering the data they require, and
172describing how this should be graphed.  In fact, it does little more than
173fielding requests from the Munin master, running the appropriate plugins,
174and returning the output they produce.
175
176=head1 OPTIONS
177
178=over 5
179
180=item B<< --config <configfile> >>
181
182Use E<lt>fileE<gt> as configuration file. [@@CONFDIR@@/munin-node.conf]
183
184=item B< --[no]paranoia >
185
186Only run plugins owned by root. Check permissions as well. [--noparanoia]
187
188=item B< --help >
189
190View this help message.
191
192=item B< --[no]debug >
193
194View debug messages.  This can be very verbose.
195
196=item B< --[no]pidebug >
197
198Plugin debug.  Sets the environment variable MUNIN_DEBUG to 1 so that plugins
199may enable debugging. [--nopidebug]
200
201=back
202
203=head1 FILES
204
205    @@CONFDIR@@/munin-node.conf
206    @@CONFDIR@@/plugins/*
207    @@CONFDIR@@/plugin-conf.d/*
208    @@STATEDIR@@/munin-node.pid
209    @@LOGDIR@@/munin-node.log
210
211=head1 VERSION
212
213This is munin-node v@@VERSION@@
214
215=head1 AUTHORS
216
217Audun Ytterdal, Jimmy Olsen, and Tore Anderson.
218
219=head1 BUGS
220
221Please see L<http://munin-monitoring.org/report/1>.
222
223=head1 COPYRIGHT
224
225Copyright (C) 2002-2006 Audun Ytterdal, Jimmy Olsen, and Tore Anderson / Linpro AS.
226
227This is free software; see the source for copying conditions. There is
228NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
229PURPOSE.
230
231This program is released under the GNU General Public License
232
233=head1 SEE ALSO
234
235For information on configuration options, please refer to the man page for
236F<munin-node.conf>.
237
238Many plugins can report whether or not they can reasonably be used on the
239node.  C<munin-node-configure> can use this information to help manage
240installed plugins.
241
242The network protocol is documented at
243L<http://munin-monitoring.org/wiki/network-protocol>
244
245
246=cut
247
248# vim: sw=4 : ts=4 : expandtab
249