summaryrefslogtreecommitdiffstats
path: root/hosts-state
blob: 2bb4aee93082f0209a8068cc6eebde273880ce14 (plain)
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
#!/usr/bin/perl -w
# The output of this script is formated to be usable as input for
# send_nsca (nsca-ng) and submit status data for all hosts known
# by bcfg2.

use strict;
use DateTime;
use DateTime::Format::Strptime;

# some date initialization
my $parser = DateTime::Format::Strptime->new(pattern => '%Y-%m-%d %H:%M:%S');
my $stale = DateTime->now(time_zone => 'local')->set_time_zone('floating');
$stale->subtract( hours => 24 );

my $summary = {
  stale => [],
  bad => [],
  clean => [],
  msg => 'OK',
  hosts => '',
  status => 0
};

open(my $reports, '-|', '/usr/sbin/bcfg2-reports', 'clients', '--no-dry-run', '--no-only-important', '--fields=state,time,total,good,bad,extra,modified');

my $header = 1;
while(<$reports>) {
    if ($header) {
        $header = 0;
        next;
    }

    my ($host, $state, $date, $time, $total, $good, $bad, $extra, $modified) = split(/\s+/, $_);
    my $short_host = $host;
    $short_host =~ s/\.spline\.inf\.fu-berlin\.de$//;

    my $msg = '';
    my $perf = "total=$total;;;0 good=$good;;;0 bad=$bad;;;0 extra=$extra;;;0 modified=$modified;;;0";
    my $status = 3; # UNKNOWN

    my $dt = $parser->parse_datetime("$date $time");
    if ($dt < $stale) {
        $msg = "CRITICAL $host is stale";
        $status = 2;
        push(@{$summary->{stale}}, $short_host);
    }
    else {
        if ($bad > 0) {
            $msg = "CRITICAL $bad bad entries";
            $status = 2;
            push(@{$summary->{bad}}, $short_host);
        }
        else {
            $msg = "OK Total managed entries: $total (extra: $extra)";
            $status = 0;
            push(@{$summary->{clean}}, $short_host);
        }
    }

    #host[tab]service[tab]status[tab]message[newline]
    print("$host\tBcfg2\t$status\t$msg | $perf\n\x17");
}

my $bad_count = @{ $summary->{bad} };
my $stale_count = @{ $summary->{stale} };
my $clean_count = @{ $summary->{clean} };

if ($stale_count > 0) {
    $summary->{msg} = 'WARNING';
    $summary->{hosts} = ' STALE: ' . join(', ', @{ $summary->{stale} });
    $summary->{status} = 1;
}

if ($bad_count > 0) {
    $summary->{msg} = 'CRITICAL';
    $summary->{hosts} = ' BAD: ' . join(', ', @{ $summary->{bad} }) . $summary->{hosts};
    $summary->{status} = 2;
}

print("vm-bcfg2.spline.inf.fu-berlin.de\tBcfg2 Status\t$summary->{status}\t$summary->{msg}$summary->{hosts} | clean=$clean_count;;;0 bad=$bad_count;;;0 stale=$stale_count;;;0\n\x17");

close($reports);