root / plugins / apt / acng @ 08479bda
Historique | Voir | Annoter | Télécharger (4,14 ko)
| 1 |
#!/usr/bin/perl |
|---|---|
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
acng - Graph activity for Apt-Cacher NG, request count and bytes |
| 6 |
|
| 7 |
=head1 APPLICABLE SYSTEMS |
| 8 |
|
| 9 |
Systems with "Apt-Cacher NG" installed and running. |
| 10 |
|
| 11 |
=head1 DESCRIPTION |
| 12 |
|
| 13 |
This plugin will add graphs for "bytes in and out" and "requests in |
| 14 |
and out" for systems with "Apt-Cacher NG" installed. |
| 15 |
|
| 16 |
=head1 CONFIGURATION |
| 17 |
|
| 18 |
The plugin must have permission to read the log of Apt-Cacher NG. (On |
| 19 |
Debian 8, this file is world readable by default). |
| 20 |
|
| 21 |
The path to the logfile can be set with the "logfile" environment |
| 22 |
variable. |
| 23 |
|
| 24 |
=head2 DEFAULT CONFIGURATION |
| 25 |
|
| 26 |
[acng] |
| 27 |
env.logfile /var/log/apt-cacher-ng/apt-cacher.log |
| 28 |
|
| 29 |
=head1 USAGE |
| 30 |
|
| 31 |
Link this plugin to /etc/munin/plugins/ and restart the munin-node. |
| 32 |
|
| 33 |
=head1 MAGIC MARKERS |
| 34 |
|
| 35 |
#%# family=contrib |
| 36 |
#%# capabilities=autoconf |
| 37 |
|
| 38 |
=head1 AUTHOR |
| 39 |
|
| 40 |
Stig Sandbeck Mathisen |
| 41 |
|
| 42 |
=head1 LICENSE |
| 43 |
|
| 44 |
GPLv3 |
| 45 |
|
| 46 |
=cut |
| 47 |
|
| 48 |
use strict; |
| 49 |
use warnings; |
| 50 |
use Munin::Plugin; |
| 51 |
|
| 52 |
use Storable qw(nfreeze thaw); |
| 53 |
use MIME::Base64; |
| 54 |
|
| 55 |
my $logfile = $ENV{'logfile'} ||= '/var/log/apt-cacher-ng/apt-cacher.log';
|
| 56 |
|
| 57 |
need_multigraph; |
| 58 |
|
| 59 |
# Read or initialize state used by the log tailer, and the plugin. |
| 60 |
sub read_state {
|
| 61 |
|
| 62 |
my ($pos, $statsin) = restore_state; |
| 63 |
my $stats = thaw(decode_base64 $statsin) if $statsin; |
| 64 |
|
| 65 |
$pos = 0 unless defined $pos; |
| 66 |
$stats = {} unless defined $stats;
|
| 67 |
|
| 68 |
return ($pos, $stats); |
| 69 |
} |
| 70 |
|
| 71 |
# Write state. |
| 72 |
# |
| 73 |
# "pos" is logfile position, and "stats" is a data structure with |
| 74 |
# counters used by the plugin. |
| 75 |
# |
| 76 |
# Note: Munin::Plugin::save_state has limited functionality, so the |
| 77 |
# data structure is serialized and converted to plain text. |
| 78 |
sub write_state {
|
| 79 |
my ($pos, $stats) = @_; |
| 80 |
|
| 81 |
my $statsout = encode_base64 nfreeze($stats); |
| 82 |
save_state($pos, $statsout); |
| 83 |
} |
| 84 |
|
| 85 |
sub parse_logfile {
|
| 86 |
my $logfile = shift; |
| 87 |
my ($pos, $stats) = read_state; |
| 88 |
|
| 89 |
my @keys = ( 'time', 'direction', 'size', 'client', 'file' ); |
| 90 |
|
| 91 |
# Open log |
| 92 |
my ( $fh, $reset ) = tail_open( $logfile, $pos ); |
| 93 |
|
| 94 |
die "Unable to open logfile\n" unless ($fh); |
| 95 |
|
| 96 |
while (<$fh>) {
|
| 97 |
chomp; |
| 98 |
my @values = split( /\|/, $_ ); |
| 99 |
|
| 100 |
my %logentry; |
| 101 |
@logentry{@keys} = @values;
|
| 102 |
|
| 103 |
$stats->{'bytes'}{ $logentry{'direction'} } += $logentry{'size'};
|
| 104 |
$stats->{'requests'}{ $logentry{'direction'} }++;
|
| 105 |
} |
| 106 |
|
| 107 |
# Close log |
| 108 |
$pos = tail_close($fh); |
| 109 |
|
| 110 |
write_state($pos, $stats); |
| 111 |
|
| 112 |
return $stats; |
| 113 |
} |
| 114 |
|
| 115 |
sub print_autoconf{
|
| 116 |
my $logfile = shift; |
| 117 |
if ( open(my $fh, '<', $logfile) ) {
|
| 118 |
print "yes\n"; |
| 119 |
} |
| 120 |
else {
|
| 121 |
printf "no (could not open %s)\n", $logfile; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
sub print_config{
|
| 126 |
my $stats = shift; |
| 127 |
|
| 128 |
print << 'EOC'; |
| 129 |
multigraph acng_bytes |
| 130 |
graph_title Apt-Cacher NG bytes |
| 131 |
graph_order origin client |
| 132 |
graph_vlabel bytes per ${graph_period}
|
| 133 |
graph_info Bytes transferred between origin, apt-cacher-ng and clients |
| 134 |
origin.info bytes transferred between origin and apt-cacher-ng |
| 135 |
origin.label origin |
| 136 |
origin.type DERIVE |
| 137 |
origin.min 0 |
| 138 |
client.info bytes transferred between apt-cacher-ng and clients |
| 139 |
client.label client |
| 140 |
client.type DERIVE |
| 141 |
client.min 0 |
| 142 |
EOC |
| 143 |
print << "EOV" if $ENV{'MUNIN_CAP_DIRTYCONFIG'};
|
| 144 |
origin.value $stats->{bytes}{I}
|
| 145 |
client.value $stats->{bytes}{O}
|
| 146 |
EOV |
| 147 |
|
| 148 |
print << 'EOC'; |
| 149 |
|
| 150 |
multigraph acng_requests |
| 151 |
graph_title Apt-Cacher NG requests |
| 152 |
graph_order origin client |
| 153 |
graph_vlabel requests per ${graph_period}
|
| 154 |
graph_info Requests from clients to apt-cacher-ng, and from apt-cacher-ng to origin |
| 155 |
origin.info requests from apt-cacher-ng to origin |
| 156 |
origin.label origin |
| 157 |
origin.type DERIVE |
| 158 |
origin.min 0 |
| 159 |
client.info requests from clients to apt-cacher-ng |
| 160 |
client.label client |
| 161 |
client.type DERIVE |
| 162 |
client.min 0 |
| 163 |
EOC |
| 164 |
|
| 165 |
print << "EOV" if $ENV{'MUNIN_CAP_DIRTYCONFIG'};
|
| 166 |
origin.value $stats->{requests}{I}
|
| 167 |
client.value $stats->{requests}{O}
|
| 168 |
EOV |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
sub print_values{
|
| 173 |
my $stats = shift; |
| 174 |
|
| 175 |
print << "EOV"; |
| 176 |
multigraph acng_bytes |
| 177 |
origin.value $stats->{bytes}{I}
|
| 178 |
client.value $stats->{bytes}{O}
|
| 179 |
|
| 180 |
multigraph acng_requests |
| 181 |
origin.value $stats->{requests}{I}
|
| 182 |
client.value $stats->{requests}{O}
|
| 183 |
EOV |
| 184 |
} |
| 185 |
|
| 186 |
if ($ARGV[0] and $ARGV[0] eq 'autoconf') {
|
| 187 |
print_autoconf($logfile); |
| 188 |
} |
| 189 |
elsif ($ARGV[0] and $ARGV[0] eq 'config') {
|
| 190 |
my $stats = parse_logfile($logfile); |
| 191 |
print_config($stats); |
| 192 |
} |
| 193 |
else {
|
| 194 |
my $stats = parse_logfile($logfile); |
| 195 |
print_values($stats); |
| 196 |
} |
