Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / relayd @ a3f989a9

Historique | Voir | Annoter | Télécharger (5,09 ko)

1 97fd8968 Antoine Beaupré
#! /usr/bin/perl -w
2
3
use strict;
4
use Munin::Plugin;
5
6
=head1 NAME
7
8
relayd - Plugin to show statistics about relayd load balancer.
9
10
=head1 CONFIGURATION
11
12
The following environment variables are used by this plugin:
13
14
=over 4
15
16
=item logfile
17
18
The file where MailScanner logs its action (Default:
19
/var/log/relayd.log)
20
21
=item logtail
22
23
The location of the logtail command (Default: /usr/sbin/logtail)
24
25
=item offsetfile
26
27
The location of the offset file (Default:
28
/var/log/munin/plugin-state/munin-relayd.offset)
29
30
=back
31
32
=head1 USAGE
33
34
Requires the logtail command somewhere in path
35
36
=head1 TODO
37
38
Parsing relayd.conf would allow us to do the following:
39
40
 * determine the hosts in config (may be necessary!)
41
 * determine if the table is completely down (may be *impossible* if a partial
42
   downtime becomes complete between two runs)
43
44
=head1 MAGIC MARKERS
45
46
 #%# family=contrib
47
 #%# capabilities=
48
49
We should have "suggest" once we implement more than availability. We should
50
also autoconf (check if logtail and the logfile exist, basically).
51
52
See http://munin-monitoring.org/wiki/ConcisePlugins
53
54
=cut
55
56
my $logfile = '/var/log/relayd.log';
57
my $logtail = '/usr/sbin/logtail';
58
my $offsetfile = "/var/munin/plugin-state/munin-relayd.offset";
59
my $configfile = "/usr/local/etc/relayd.conf";
60
61
(defined($ENV{'logfile'})) and $logfile = $ENV{'logfile'};
62
(defined($ENV{'logtail'})) and $logtail = $ENV{'logtail'};
63
(defined($ENV{'offsetfile'})) and $offsetfile = $ENV{'offsetfile'};
64
(defined($ENV{'configfile'})) and $configfile = $ENV{'offsetfile'};
65
66
my $cmd = (defined($ARGV[0])) ? $ARGV[0] : '';
67
68 99df8a0e Antoine Beaupré
my @hosts = ();
69
open(my $conf, "<", $configfile) or die "can't open $configfile: $!";
70
my $content = join("", <$conf>);
71
while ( $content =~ /table\s*<([^>]*)>\s*{([^}]*)}/g) {
72
	my $hosts = $2;
73
	$hosts =~ s/#.*$//mg; # comments
74
	$hosts =~ s/^\s+//mg; # trim spaces before lines
75
	print "table $1: $hosts\n" if defined $ENV{MUNIN_DEBUG};
76
	push @hosts , split /\s+/, $hosts;
77
}
78
79 97fd8968 Antoine Beaupré
if ($cmd eq 'config') {
80
	print("multigraph relayd_avail\n\n");
81
	print("graph_title Relayd host availability\n");
82
	print("graph_args --lower-limit 0\n");
83
	print("graph_vlabel % availability\n");
84
	print("graph_category Load balancer\n");
85
	print("graph_info Ratio of time when this host was up. This is provided by relayd itself (not averaged by this plugin)\n");
86
	for my $host (@hosts) {
87
		my $clean = clean_fieldname($host);
88 cd70960d Antoine Beaupré
		$clean  = clean_fieldname('host'.$host) unless ($clean ne '_');
89 97fd8968 Antoine Beaupré
		print("$clean.label $host\n");
90
	}
91
	print("\nmultigraph relayd_incidents\n\n");
92
	print("graph_title Relayd host incidents\n");
93
	print("graph_args --lower-limit 0\n");
94
	print("graph_vlabel down incidents\n");
95
	print("graph_category Load balancer\n");
96
	print("graph_info Number of times this host went down during \${graph_period}\n");
97
	for my $host (@hosts) {
98
		my $clean = clean_fieldname($host);
99 cd70960d Antoine Beaupré
		$clean  = clean_fieldname('host'.$host) unless ($clean ne '_');
100 97fd8968 Antoine Beaupré
		print("$clean.type ABSOLUTE\n");
101
		print("$clean.label $host\n");
102
	}
103
	exit(0);
104
}
105
106
# sample lines:
107
# Mar  8 23:05:28 rtr0 relayd[81814]: host 209.44.112.101, check http code (2000ms), state up -> down, availability 97.83%
108
# Mar  8 23:05:28 rtr0 relayd[81814]: host 209.44.112.96, check http code (2001ms), state up -> down, availability 98.12%
109
# Mar  8 23:05:31 rtr0 relayd[81813]: table hag: 1 added, 2 deleted, 0 changed, 0 killed
110
# Mar  8 23:05:31 rtr0 relayd[81814]: host 209.44.112.101, check http code (3ms), state down -> up, availability 97.83%
111
# Mar  8 23:05:31 rtr0 relayd[81814]: host 209.44.112.96, check http code (3ms), state down -> up, availability 98.12%
112
# Mar  8 23:05:36 rtr0 relayd[81813]: table hag: 2 added, 1 deleted, 0 changed, 0 killed
113
# Mar  8 23:21:58 rtr0 relayd[81814]: host 209.44.112.96, check http code (2000ms), state up -> down, availability 98.12%
114
# Mar  8 23:22:01 rtr0 relayd[81813]: table hag: 0 added, 1 deleted, 0 changed, 0 killed
115
116
my (%avail, %down);
117
118
open(my $log, "$logtail -f $logfile -o $offsetfile |") or die("cannot open $logfile: $!");
119
#open(my $log, "tail -100 $logfile |") or die("cannot open $logfile: $!");
120
while (<$log>) {
121
	if (/host ([^,]*), check[^,]*, state [^>]* -> ([^,]*), availability ([0-9]+.[0-9]+)%/) {
122
		my $host = clean_fieldname($1);
123
		$host = clean_fieldname('host'.$1) unless ($host ne '_');
124
125
		$down{$host} = 0 unless defined $down{$host};
126
		$down{$host}++ if $2 eq 'down';
127
		# yes, we overwrite previous value and take only the recent one. be sad.
128
		$avail{$host} = $3;
129
	}
130
}
131
close($log) or warn "failed to close pipe: $!";
132
133 a3f989a9 Antoine Beaupré
# get missing availability values from relayctl, if necessary
134
for my $host (@hosts) {
135
	my $ran = 0;
136
 	if (!defined $avail{$host} && !$ran) {
137
		open(my $status, "relayctl show summary|") or die "can't open relayctl: $!";
138
		while (<$status>) {
139
			if (/([\w\.]+)\s+(\d+\.\d+)%/) {
140
				print "found spare value: $2 for $1\n" if defined $ENV{MUNIN_DEBUG};
141
				$avail{$1} = $2 unless defined($avail{$1});
142
			}
143
		}
144
		close $status or die "can't close pipe: $!";
145
		$ran = 1;
146
	}
147
}
148
149 97fd8968 Antoine Beaupré
print "multigraph relayd_avail\n\n";
150 99df8a0e Antoine Beaupré
for my $host (@hosts) {
151
  	print "$host.value " . ($avail{$host} || 'NaN'). "\n";
152 97fd8968 Antoine Beaupré
}
153
154
print "\nmultigraph relayd_incidents\n\n";
155 99df8a0e Antoine Beaupré
for my $host (@hosts) {
156
	print "$host.value " . ($down{$host} || 0). "\n";
157 97fd8968 Antoine Beaupré
}