summaryrefslogtreecommitdiff
path: root/bin/icli
blob: c124fd8ff019d2efa59e2e6cb5521db20e365d99 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env perl
use autodie;
use strict;
use warnings;
use 5.010;

use Term::ANSIColor;

my ($data, $cache);
my $status_file = '/var/lib/icinga/status.dat';
my $context;

sub read_status_line {
	my ($line) = @_;

	if ($line =~ / ^ (?<context> \w+) \s+ { /x) {
		$context = $+{context};
	}
	elsif ($line =~ / ^ \t* (?<key> [^=]+ ) = (?<value> .*) $ /x) {
		$cache->{$+{key}} = $+{value};
	}
	elsif ($line =~ / ^ \t* } \s* $ /x) {
		given($context) {
			when(['info', 'programstatus']) {
				$data->{$context} = $cache;
			}
			when('hoststatus') {
				push(@{$data->{hosts}}, $cache);
			}
			when('servicestatus') {
				push(@{$data->{services}->{$cache->{host_name}}}, $cache);
			}
			when('contactstatus') {
				push(@{$data->{contacts}}, $cache);
			}
			default {
				warn("Unknown field in $status_file: $context\n");
			}
		}
		$cache = undef;
	}
}

sub read_status {
	open(my $fh, '<', $status_file);
	while (my $line = <$fh>) {
		chomp($line);
		read_status_line($line);
	}
	close($fh);
}

sub state_to_string {
	my ($digit) = @_;
	given ($digit) {
		when(0) { return colored('   OK   ', 'black on_green' ) }
		when(1) { return colored(' WARNING', 'black on_yellow') }
		when(2) { return colored('CRITICAL', 'white on_red'   ) }
		when(3) { return colored(' UNKNOWN', 'white on_blue'  ) }
		default { return colored('  ???   ', 'white'          ) }
	}
}

read_status();

foreach my $host (sort keys %{$data->{services}}) {
	say "\n$host";
	foreach my $s (@{$data->{services}->{$host}}) {
		printf(
			"\t%-20.20s %s  %s\n",
			$s->{service_description},
			state_to_string($s->{current_state}),
			$s->{plugin_output},
		);
	}
}

__END__

=head1 NAME

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 OPTIONS

=head1 EXIT STATUS

=head1 CONFIGURATION

=head1 DEPENDENCIES

=head1 BUGS AND LIMITATIONS

=head1 AUTHOR

Copyright (C) 2010 by Daniel Friesel E<lt>derf@chaosdorf.deE<gt>

=head1 LICENSE

  0. You just DO WHAT THE FUCK YOU WANT TO.