~ubuntu-branches/ubuntu/gutsy/munin/gutsy

« back to all changes in this revision

Viewing changes to server/munin-nagios.in

  • Committer: Bazaar Package Importer
  • Author(s): Tore Anderson
  • Date: 2004-05-21 20:51:19 UTC
  • Revision ID: james.westby@ubuntu.com-20040521205119-oz8bllbjp9hs80ig
Tags: upstream-0+1.0.0pre5
Import upstream version 0+1.0.0pre5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
# Nagios
 
3
#
 
4
# $Id: munin-nagios.in,v 1.4 2004/01/29 17:40:10 jimmyo Exp $
 
5
#
 
6
# $Log: munin-nagios.in,v $
 
7
# Revision 1.4  2004/01/29 17:40:10  jimmyo
 
8
# Fixed pod typos patched by Lupe Christoph (SF#884092)
 
9
#
 
10
# Revision 1.3  2004/01/29 17:34:06  jimmyo
 
11
# Updated copyright information
 
12
#
 
13
# Revision 1.2  2004/01/15 15:20:01  jimmyo
 
14
# Making things workable after name change. Upping for test verwion.
 
15
#
 
16
# Revision 1.1  2004/01/02 18:50:01  jimmyo
 
17
# Renamed occurrances of lrrd -> munin
 
18
#
 
19
# Revision 1.1.1.1  2004/01/02 15:18:08  jimmyo
 
20
# Import of LRRD CVS tree after renaming to Munin
 
21
#
 
22
# Revision 1.14  2003/12/10 11:59:39  jimmyo
 
23
# Enable/disable notifications at any level
 
24
#
 
25
# Revision 1.13  2003/11/12 12:28:37  jimmyo
 
26
# Only send ok-messages if lockfile was created.
 
27
#
 
28
# Revision 1.12  2003/11/12 12:19:29  jimmyo
 
29
# Fix typo
 
30
#
 
31
# Revision 1.11  2003/11/12 12:17:16  jimmyo
 
32
# Fix typo
 
33
#
 
34
# Revision 1.10  2003/11/12 12:07:46  jimmyo
 
35
# Off by one error. Cosmetic only.
 
36
#
 
37
# Revision 1.9  2003/11/12 12:04:45  jimmyo
 
38
# Make sure extinfo comes accross
 
39
#
 
40
# Revision 1.8  2003/11/10 16:09:00  jimmyo
 
41
# Be nice to Nagios - don't DOS it.
 
42
#
 
43
# Revision 1.7  2003/11/07 20:46:12  jimmyo
 
44
# Only require Config::General if using old config format.
 
45
#
 
46
# Revision 1.6  2003/11/07 19:00:17  jimmyo
 
47
# Put lockfiles in the right place
 
48
#
 
49
# Revision 1.5  2003/11/07 17:43:16  jimmyo
 
50
# Cleanups and log entries
 
51
#
 
52
#
 
53
 
 
54
use strict;
 
55
use Munin;
 
56
 
 
57
use POSIX qw(strftime);
 
58
use Getopt::Long;
 
59
my $VERSION="@@VERSION@@";
 
60
my $DEBUG=0;
 
61
my $conffile = "@@CONFDIR@@/munin.conf";
 
62
my $do_usage = 0;
 
63
my @limit_hosts = ();
 
64
my @limit_services = ();
 
65
my $force_sendmsg = 0;
 
66
my $force_root = 0;
 
67
my $removeok = 0;
 
68
my $do_version = 0;
 
69
 
 
70
# Get options
 
71
$do_usage=1  unless 
 
72
GetOptions ( "force!"       => \$force_sendmsg,
 
73
             "force-root!"  => \$force_root,
 
74
             "host=s"       => \@limit_hosts,
 
75
             "service=s"    => \@limit_services,
 
76
             "config=s"     => \$conffile,
 
77
             "debug!"       => \$DEBUG,
 
78
             "version!"     => \$do_version,
 
79
             "removeok"     => \$removeok,
 
80
             "help"         => \$do_usage );
 
81
 
 
82
if ($do_usage)
 
83
{
 
84
    print "Usage: $0 [options]
 
85
 
 
86
Options:
 
87
    --[no]force         Force sending of messages even if you normally
 
88
                        wouldn't. [--noforce]
 
89
    --[no]force-root    Force running, even as root. [--noforce-root]
 
90
    --help              View this message.
 
91
    --version           View version information.
 
92
    --debug             View debug messages.
 
93
    --service <service> Limit notified services to <service>. Multiple 
 
94
                        --service options may be supplied.
 
95
    --host <host>       Limit notified hosts to <host>. Multiple --host 
 
96
                        options may be supplied.
 
97
    --config <file>     Use <file> as configuration file. 
 
98
                        [/etc/munin/munin.conf]
 
99
    --removeok          Reset warning status (remove .ok-files).
 
100
 
 
101
";
 
102
    exit 0;
 
103
}
 
104
 
 
105
if ($do_version)
 
106
{
 
107
    print "munin-nagios version $VERSION.\n";
 
108
    print "Written by Audun Ytterdal, Jimmy Olsen, Tore Anderson / Linpro AS\n";
 
109
    print "\n";
 
110
    print "Copyright (C) 2002-2004\n";
 
111
    print "This is free software released under the GNU Public License. There is NO\n";
 
112
    print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
 
113
    exit 0;
 
114
}
 
115
 
 
116
if ($> == 0 and !$force_root)
 
117
{
 
118
    print "You are running this program as root, which is neither smart nor necessary.
 
119
If you really want to run it as root, use the --force-root option. Else, run
 
120
it as the user \"munin\". Aborting.\n\n";
 
121
    exit (1);
 
122
}
 
123
 
 
124
my $config = &munin_config ($conffile);
 
125
my $modified=0;
 
126
 
 
127
open LOG,">>$config->{logdir}/munin-nagios.log" or die "Unable to create/access $config->{logdir}/munin-nagios.log\n";
 
128
logger("Starting munin-nagios, checking lock");
 
129
munin_runlock("$config->{rundir}/munin-nagios.lock");
 
130
logger("Created lock: $config->{rundir}/munin-nagios.lock");
 
131
unless ($config->{nsca}) {
 
132
  logger ("No nsca-configuration found, exiting");
 
133
  exit;
 
134
}
 
135
 
 
136
        
 
137
 
 
138
 
 
139
for my $domain ( keys %{$config->{domain}}) {
 
140
  if ($removeok) {
 
141
     my $deletepath = "$config->{dbdir}/$domain";
 
142
     if (@limit_hosts)
 
143
     {
 
144
         foreach my $host (@limit_hosts)
 
145
         {
 
146
             my $deleted = unlink <$deletepath/$host-*.ok>;
 
147
             logger("$deleted .ok-files successfully deleted for \"$host\"");
 
148
         }
 
149
     }
 
150
     else
 
151
     {
 
152
         logger("Removing nagios .ok-files for $domain");
 
153
         my $deleted = unlink <$deletepath/*.ok>;
 
154
         logger("$deleted .ok-files successfully deleted");
 
155
     }
 
156
  } else {
 
157
     logger ("processing domain: $domain");
 
158
     process_domain($domain);
 
159
  }
 
160
  logger("munin-nagios finished.");
 
161
}
 
162
 
 
163
 
 
164
sub process_domain {
 
165
    my ($domain) = @_;
 
166
    for my $node ( keys %{$config->{domain}->{$domain}->{node}}) {
 
167
        if (@limit_hosts and !grep (/^$node$/, @limit_hosts))
 
168
        {
 
169
                logger ("skipping node: $node");
 
170
                next;
 
171
        }
 
172
        logger ("processing node: $node");
 
173
        process_node($domain,$node ,$config->{domain}->{$domain}->{node}->{$node} );
 
174
    }
 
175
}
 
176
 
 
177
sub process_node {
 
178
  my ($domain,$name,$node) = @_;
 
179
  for my $client (keys %{$node->{client}}) {
 
180
      logger ("processing node $client") if $DEBUG;
 
181
      process_service($domain,$name,$client,$node->{client}->{$client});
 
182
  }
 
183
}
 
184
 
 
185
sub process_service {
 
186
  my $critical= undef;
 
187
  my ($domain, $name,$clientname,$client) = @_;
 
188
  return unless $client;
 
189
 
 
190
  my @crits    = ();
 
191
  my @warns    = ();
 
192
  my @unknowns = ();
 
193
  my @oks      = ();
 
194
  my $update   = 0;
 
195
 
 
196
  for my $service (keys %$client) {
 
197
    logger ("processing service: $service") if $DEBUG;
 
198
    if ($service =~ /(^.*)\.label/) {
 
199
        my $key = $1;
 
200
        next unless ((exists $client->{"$key.warning"}) || ($client->{"$key.critical"}));
 
201
        next unless (munin_get_bool ($config, "nagios", 1, $domain, $name, $clientname, $key));
 
202
        if (@limit_services and !grep (/^$service$/, @limit_services))
 
203
        {
 
204
                next;
 
205
        }
 
206
 
 
207
        my @critical = (undef, undef);
 
208
        my @warning  = (undef, undef);
 
209
 
 
210
        if (defined $client->{"$key.critical"} and 
 
211
            $client->{"$key.critical"} =~ /^\s*([-\d]*):([-\d]*)\s*$/)
 
212
        {
 
213
                $critical[0] = $1 if defined $1;
 
214
                $critical[1] = $2 if defined $2;
 
215
        }
 
216
        elsif (defined $client->{"$key.critical"} and
 
217
            $client->{"$key.critical"} =~ /^\s*([-\d]+)\s*$/)
 
218
        {
 
219
                $critical[1] = $1 if defined $1;
 
220
        }
 
221
        elsif (defined $client->{"$key.critical"})
 
222
        {
 
223
            @critical = (0, 0);
 
224
        }
 
225
        if (defined $client->{"$key.warning"} and 
 
226
            $client->{"$key.warning"} =~ /^\s*([-\d]*):([-\d]*)\s*$/)
 
227
        {
 
228
                $warning[0] = $1 if defined $1;
 
229
                $warning[1] = $2 if defined $2;
 
230
        }
 
231
        elsif (defined $client->{"$key.warning"} and
 
232
            $client->{"$key.warning"} =~ /^\s*([-\d]+)\s*$/)
 
233
        {
 
234
                $warning[1] = $1 if defined $1;
 
235
        }
 
236
        elsif (defined $client->{"$key.warning"})
 
237
        {
 
238
            @warning = (0, 0);
 
239
        }
 
240
        my $filename = "$config->{dbdir}/$domain/$name-$clientname-$key-".
 
241
            lc substr (($client->{"$key.type"}||"GAUGE"),0,1) . ".rrd";
 
242
        my $value = sprintf "%.2f",munin_fetch("$filename");
 
243
        
 
244
        if ((defined ($critical[0]) and $value < $critical[0]) or
 
245
            (defined ($critical[1]) and $value > $critical[1])) {
 
246
#           print "Sending; $name, $clientname ", $client->{"$key.label"}, ",2,Value is $value. Critical range ($critical[0]:$critical[1]) exceeded\n";
 
247
          # munin_nscasend($name,$clientname,$client->{"$key.label"},2,
 
248
          push (@crits, $client->{"$key.label"} . 
 
249
                  (defined $client->{"$key.extinfo"}?
 
250
                        " ($value ($critical[0]:$critical[1]): ".
 
251
                        $client->{"$key.extinfo"} . ")":
 
252
                        " $value ($critical[0]:$critical[1])"));
 
253
          munin_removelock("$config->{dbdir}/$domain/$name-$clientname-$key.ok");
 
254
        }
 
255
        elsif ((defined ($warning[0]) and $value < $warning[0]) or
 
256
               (defined ($warning[1]) and $value > $warning[1])) {
 
257
#           print "Sending; $name, $clientname ", $client->{"$key.label"}, ",2,Value is $value. Warning range ($warning[0]:$warning[1]) exceeded\n";
 
258
#           print "Extinfo; $name, $clientname ", $client->{"$key.extinfo"}, "\n";
 
259
          #munin_nscasend($name,$clientname,$client->{"$key.label"},1,
 
260
          push (@warns, $client->{"$key.label"}.
 
261
                  (defined $client->{"$key.extinfo"}?
 
262
                        " ($value ($warning[0]:$warning[1]): ".
 
263
                        $client->{"$key.extinfo"} . ")":
 
264
                        " $value ($warning[0]:$warning[1])"));
 
265
          munin_removelock("$config->{dbdir}/$domain/$name-$clientname-$key.ok");
 
266
        }
 
267
        elsif ($value eq "U") {
 
268
          #munin_nscasend($name,$clientname,$client->{"$key.label"},1,
 
269
          push (@unknowns, $client->{"$key.label"});
 
270
          munin_removelock("$config->{dbdir}/$domain/$name-$clientname-$key.ok");
 
271
        }
 
272
        else {
 
273
          push (@oks, $client->{"$key.label"}.
 
274
                  " ($value)");
 
275
          if (munin_createlock("$config->{dbdir}/$domain/$name-$clientname-$key.ok"))
 
276
          {
 
277
            $update ++;
 
278
          }
 
279
        }
 
280
      }
 
281
  }
 
282
  if (@crits)
 
283
  {
 
284
    munin_nscasend ($name, $clientname, "", 2, 
 
285
        scalar (@crits) . " CRITICALs: ". join (',', @crits[0 .. (@crits>20?20:@crits)]) .
 
286
        (@warns    ? "; " . scalar (@warns)    . " WARNINGs: " . join (', ', @warns[0 .. (@warns>20?20:@warns-1)]) : "") .
 
287
        (@unknowns ? "; " . scalar (@unknowns) . " UNKNOWNs: " . join (', ', @unknowns[0 .. (@unknowns>20?20:@unknowns-1)]) : "") .
 
288
        (@oks      ? "; " . scalar (@oks)      . " OKs: "      . join (', ', @oks[0 .. (@oks>20?20:@oks-1)]) : "") .
 
289
        ".");
 
290
  }
 
291
  elsif (@warns)
 
292
  {
 
293
    munin_nscasend ($name, $clientname, "", 1, 
 
294
        scalar (@warns) . " WARNINGs: " . join (',', @warns[0 .. (@warns>20?20:@warns)]) .
 
295
        (@unknowns ? "; " . scalar (@unknowns) . " UNKNOWNs: "  . join (', ', @unknowns[0 .. (@unknowns>20?20:@unknowns-1)]) : "") .
 
296
        (@oks      ? "; " . scalar (@oks)      . " OKs: "       . join (', ', @oks[0 .. (@oks>20?20:@oks-1)]) : "") .
 
297
        ".");
 
298
  }
 
299
  elsif (@unknowns)
 
300
  {
 
301
    munin_nscasend ($name, $clientname, "", 3, 
 
302
        scalar (@unknowns) . " UNKNOWNs: " . join (',', @unknowns[0 .. (@unknowns>20?20:@unknowns-1)]) .
 
303
        (@oks      ? "; " . scalar (@oks)      . " OKs: "       . join (', ', @oks[0 .. (@oks>20?20:@oks-1)]) : "") .
 
304
        ".");
 
305
  }
 
306
  elsif (@oks and $update)
 
307
  {
 
308
    munin_nscasend ($name, $clientname, "", 0, 
 
309
        scalar (@oks) . " OKs: " . join (', ', @oks[0 .. (@oks>20?20:@oks-1)]) .
 
310
        ".");
 
311
  }
 
312
}
 
313
 
 
314
sub logger {
 
315
  my ($comment) = @_;
 
316
  my $now = strftime "%b %d %H:%M:%S", localtime;
 
317
  print LOG "$now - $comment\n";
 
318
}
 
319
 
 
320
close LOG;
 
321
 
 
322
=head1 NAME
 
323
 
 
324
munin-nagios - A program to warn nagios of any off-limit values
 
325
 
 
326
=head1 SYNOPSIS
 
327
 
 
328
munin-nagios [options]
 
329
 
 
330
=head1 OPTIONS
 
331
 
 
332
=over 5
 
333
 
 
334
=item B<< --service <service> >>
 
335
 
 
336
Limit services to those of E<lt>serviceE<gt>. Multiple --service options may be supplied. [unset]
 
337
 
 
338
=item B<< --host <host> >>
 
339
 
 
340
Limit hosts to those of E<lt>host<gt>. Multiple --host options may be supplied. [unset]
 
341
 
 
342
=item B<< --config <file> >>
 
343
 
 
344
Use E<lt>fileE<gt> as configuration file. [/etc/munin/munin.conf]
 
345
 
 
346
=item B<< --[no]force >>
 
347
 
 
348
Force sending of messages ieven if you normally wouldn't. [--noforce]
 
349
 
 
350
=item B<< --[no]force-root >>
 
351
 
 
352
Force running as root (stupid and unnecessary). [--noforce-root]
 
353
 
 
354
=item B<< --removeok >>
 
355
 
 
356
Reset warning status (remove .ok-files).
 
357
 
 
358
=item B<< --help >>
 
359
 
 
360
View help message.
 
361
 
 
362
=item B<< --[no]debug >>
 
363
 
 
364
If set, view debug messages. [--nodebug]
 
365
 
 
366
=back
 
367
 
 
368
=head1 DESCRIPTION
 
369
 
 
370
Munin-nagios is a part of the package Munin, which is used in
 
371
combination with Munin's node.  Munin is a group of programs to gather
 
372
data from Munin's nodes, graph them, create html-pages, and optionally
 
373
warn Nagios about any off-limit values.
 
374
 
 
375
Munin-nagios can warn any nagios-servers about off-limit values.
 
376
 
 
377
If a service has fields with "warning" or "critical"-options (e.g.
 
378
"load.warning 10"), and the munin configuration file contains the
 
379
necessary configuration options, munin-nagios will warn the
 
380
nagios-server.
 
381
 
 
382
=head1 CONFIGURATION
 
383
 
 
384
The configuration file "munin.conf" must have the following optinons set correctly for munin-nagios to work:
 
385
 
 
386
        nsca         /usr/bin/send_nsca
 
387
        nsca_server  nagios-server.your.dom
 
388
        nsca_config  /etc/nagios/send_nsca.cfg
 
389
        
 
390
In addition NSCA must be installed and configured correctly.
 
391
 
 
392
=head1 FILES
 
393
 
 
394
        @@CONFDIR@@/munin.conf
 
395
        @@DBDIR@@/*
 
396
        @@LOGDIR@@/munin-nagios
 
397
        @@STATEDIR@@/*
 
398
 
 
399
=head1 VERSION
 
400
 
 
401
This is munin-nagios version @@VERSION@@
 
402
 
 
403
=head1 AUTHORS
 
404
 
 
405
Audun Ytterdal and Jimmy Olsen.
 
406
 
 
407
=head1 BUGS
 
408
 
 
409
munin-nagios does, as of now, not check the syntax of the configuration file.
 
410
 
 
411
Please report other bugs in the bug tracker at L<http://munin.sf.net/>.
 
412
 
 
413
=head1 COPYRIGHT
 
414
 
 
415
Copyright � 2002-2004 Knut Haugen, Audun Ytterdal, and Jimmy Olsen / Linpro AS.
 
416
 
 
417
This is free software; see the source for copying conditions. There is
 
418
NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
 
419
PURPOSE.
 
420
 
 
421
This program is released under the GNU General Public License
 
422
 
 
423
=cut
 
424