root / plugins / syslog / syslog_ng_stats @ eb100e33
Historique | Voir | Annoter | Télécharger (3,47 ko)
| 1 |
#!/usr/bin/perl |
|---|---|
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
syslog_ng_stats - Plugin for syslog-ng. |
| 6 |
It uses the C<syslog-ng-ctl> utility to grab values. |
| 7 |
|
| 8 |
=head1 DESCRIPTION |
| 9 |
|
| 10 |
See C<man syslog-ng-ctl> for the C<stats> option. |
| 11 |
All options used are regexp. |
| 12 |
|
| 13 |
=over |
| 14 |
|
| 15 |
=item source_name |
| 16 |
|
| 17 |
=item source_id |
| 18 |
|
| 19 |
=item source_instance |
| 20 |
|
| 21 |
=item state |
| 22 |
|
| 23 |
=item type |
| 24 |
|
| 25 |
=back |
| 26 |
|
| 27 |
Example of input and destination via tcp for my application: |
| 28 |
|
| 29 |
[syslog_ng_stats] |
| 30 |
user root |
| 31 |
env.source_name = source dst\.tcp |
| 32 |
env.source_id = myname |
| 33 |
|
| 34 |
=head1 AUTHORS |
| 35 |
|
| 36 |
Dmitry E. Oboukhov <unera@debian.org>, |
| 37 |
Roman V. Nikolaev <rshadow@rambler.ru> |
| 38 |
|
| 39 |
=head1 COPYRIGHT |
| 40 |
|
| 41 |
Copyright (C) 2013 Dmitry E. Oboukhov <unera@debian.org> |
| 42 |
Copyright (C) 2013 Roman V. Nikolaev <rshadow@rambler.ru> |
| 43 |
|
| 44 |
This library is free software; you can redistribute it and/or modify |
| 45 |
it under the same terms as Perl itself, either Perl version 5.8.8 or, |
| 46 |
at your option, any later version of Perl 5 you may have available. |
| 47 |
|
| 48 |
=cut |
| 49 |
|
| 50 |
use strict; |
| 51 |
use warnings; |
| 52 |
use utf8; |
| 53 |
use open qw(:utf8 :std); |
| 54 |
use List::MoreUtils qw(any); |
| 55 |
|
| 56 |
# Filters |
| 57 |
my (@source_name, @source_id, @source_instance, @state, @type); |
| 58 |
@source_name = split m{\s+}, $ENV{source_name} if $ENV{source_name};
|
| 59 |
@source_id = split m{\s+}, $ENV{source_id} if $ENV{source_id};
|
| 60 |
@source_instance = split m{\s+}, $ENV{source_instance}
|
| 61 |
if $ENV{source_instance};
|
| 62 |
@state = split m{\s+}, $ENV{state} if $ENV{state};
|
| 63 |
@type = split m{\s+}, $ENV{type} if $ENV{type};
|
| 64 |
|
| 65 |
# Get stats |
| 66 |
my $data = `syslog-ng-ctl stats`; |
| 67 |
die 'Can`t get stats from syslog-ng-ctl' unless $data; |
| 68 |
|
| 69 |
# Split to graphs |
| 70 |
my @str = split m{\n+}, $data;
|
| 71 |
# Remove title |
| 72 |
shift @str; |
| 73 |
|
| 74 |
my @gpaths; |
| 75 |
for my $graph (@str) {
|
| 76 |
# Split to data |
| 77 |
$graph = [ split m{;}, $graph ];
|
| 78 |
$graph = {
|
| 79 |
source_name => $graph->[0], |
| 80 |
source_id => $graph->[1], |
| 81 |
source_instance => $graph->[2], |
| 82 |
state => $graph->[3], |
| 83 |
type => $graph->[4], |
| 84 |
number => $graph->[5], |
| 85 |
}; |
| 86 |
|
| 87 |
# Apply filters |
| 88 |
next if @source_name and |
| 89 |
! any {$graph->{source_name} =~ m{$_}i } @source_name;
|
| 90 |
next if @source_id and |
| 91 |
! any {$graph->{source_id} =~ m{$_}i } @source_id;
|
| 92 |
next if @source_instance and |
| 93 |
! any {$graph->{source_instance} =~ m{$_}i } @source_instance;
|
| 94 |
next if @state and |
| 95 |
! any {$graph->{state} =~ m{$_}i } @state;
|
| 96 |
next if @type and |
| 97 |
! any {$graph->{type} =~ m{$_}i } @type;
|
| 98 |
|
| 99 |
# Save graph |
| 100 |
push @gpaths, $graph; |
| 101 |
} |
| 102 |
|
| 103 |
# Show config |
| 104 |
if( exists $ARGV[0] and $ARGV[0] eq "config" ) {
|
| 105 |
print "graph_title Syslog-ng statistics\n"; |
| 106 |
print "graph_vlabel count\n"; |
| 107 |
print "graph_args --base 1000 --lower-limit 0 --rigid\n"; |
| 108 |
print "graph_info This graph show syslog-ng-ctl stats\n"; |
| 109 |
print "graph_category system\n"; |
| 110 |
for my $graph (@gpaths) {
|
| 111 |
# ID |
| 112 |
my $id = sprintf '%s_%s', |
| 113 |
$graph->{source_id} || $graph->{source_instance},
|
| 114 |
$graph->{type};
|
| 115 |
s{#(\d+)}{[$1]}, s{[^\w\]\[]+}{_}g for $id;
|
| 116 |
|
| 117 |
printf "%s.label %s: %s, %s\n", $id, |
| 118 |
$graph->{source_name},$graph->{source_id}, $graph->{type};
|
| 119 |
printf "%s.min 0\n", $id; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
# Print values |
| 124 |
for my $graph (@gpaths) {
|
| 125 |
# ID |
| 126 |
my $id = sprintf '%s_%s', |
| 127 |
$graph->{source_id} || $graph->{source_instance},
|
| 128 |
$graph->{type};
|
| 129 |
s{#(\d+)}{[$1]}, s{[^\w\]\[]+}{_}g for $id;
|
| 130 |
|
| 131 |
printf "%s.value %s\n", $id, $graph->{number};
|
| 132 |
} |
| 133 |
|
| 134 |
exit; |
