summaryrefslogtreecommitdiff
path: root/bin/icli
blob: 427d12e9aba0d552d047280840a6b19d09e04dee (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
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
#!/usr/bin/env perl
use autodie;
use strict;
use warnings;
use 5.010;

use Getopt::Long;
use Term::ANSIColor;

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


sub with_colour {
	my ($text, $colour) = @_;
	if ($colours) {
		return colored($text, $colour);
	}
	else {
		return $text;
	}
}

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 with_colour('   OK   ', 'black on_green' ) }
		when(1) { return with_colour(' WARNING', 'black on_yellow') }
		when(2) { return with_colour('CRITICAL', 'white on_red'   ) }
		when(3) { return with_colour(' UNKNOWN', 'white on_blue'  ) }
		default { return with_colour('  ???   ', 'white'          ) }
	}
}

sub display_service {
	my ($s) = @_;
	printf(
		"%-20.20s %s %s\n",
		$s->{service_description},
		state_to_string($s->{current_state}),
		$s->{plugin_output},
	);
}

sub display_host {
	my ($host, $all) = @_;

	if ($all) {
		say "\n$host";
	}

	foreach my $service (@{$data->{services}->{$host}}) {

		if ($all) {
			print "\t";
		}

		display_service($service);
	}
}

GetOptions(
	'C|no-colours'    => sub { $colours = 0 },
	'f|status-file=s' => \$status_file,
	'h|host=s'        => \$for_host,
);

read_status();

if ($for_host) {
	if (exists $data->{services}->{$for_host}) {
		display_host($for_host, 0);
	}
	else {
		die("Unknown host: $for_host (You need to use the Alias name)\n");
	}
}
else {
	foreach my $host (sort keys %{$data->{services}}) {
		display_host($host, 1);
	}
}

__END__

=head1 NAME

B<icli> - Icinga Command Line Interface

=head1 SYNOPSIS

B<icli> [B<-h> I<host>]

=head1 DESCRIPTION

B<icli> is a command line interface to Icinga. By default it lists all
services and their states.

=head1 OPTIONS

=over

=item B<-C>/B<--no-colours>

Disable colours in output

=item B<-f>/B<--status-file> I<file>

Read the status from I<file> instead of the default
F</var/lib/icinga/status.dat>

=item B<-h>/B<--host> I<host>

Only show I<host>'s services

=back

=head1 EXIT STATUS

Zero, unless errors occured.

=head1 CONFIGURATION

None.

=head1 DEPENDENCIES

None, so far.

=head1 BUGS AND LIMITATIONS

This software is in early development stages. So there will probably be quite
a lot.

=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.