Projet

Général

Profil

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

root / plugins / syslog / syslog_ng_stats @ 928f9d3d

Historique | Voir | Annoter | Télécharger (3,44 ko)

1
#!/usr/bin/perl
2

    
3
=head1 NAME
4

    
5
syslog_ng_stats - Plugin for syslog-ng use C<syslog-ng-ctl> utility to
6
make grapths.
7

    
8
=head1 DESCRIPTION
9

    
10
See C<man syslog-ng-ctl> for C<stats> option. All options used as regexp.
11

    
12
=over
13

    
14
=item source_name
15

    
16
=item source_id
17

    
18
=item source_instance
19

    
20
=item state
21

    
22
=item type
23

    
24
=back
25

    
26
Example of input and destination via tcp for my application:
27

    
28
    [syslog_ng_stats]
29
    user root
30
    env.source_name = source dst\.tcp
31
    env.source_id = myname
32

    
33
=head1 AUTHORS
34

    
35
Dmitry E. Oboukhov <unera@debian.org>,
36
Roman V. Nikolaev <rshadow@rambler.ru>
37

    
38
=head1 COPYRIGHT
39

    
40
Copyright (C) 2013 Dmitry E. Oboukhov <unera@debian.org>
41
Copyright (C) 2013 Roman V. Nikolaev <rshadow@rambler.ru>
42

    
43
This library is free software; you can redistribute it and/or modify
44
it under the same terms as Perl itself, either Perl version 5.8.8 or,
45
at your option, any later version of Perl 5 you may have available.
46

    
47
=cut
48

    
49
use strict;
50
use warnings;
51
use utf8;
52
use open qw(:utf8 :std);
53
use List::MoreUtils qw(any);
54

    
55
# Filters
56
my (@source_name, @source_id, @source_instance, @state, @type);
57
@source_name     = split m{\s+}, $ENV{source_name} if $ENV{source_name};
58
@source_id       = split m{\s+}, $ENV{source_id}   if $ENV{source_id};
59
@source_instance = split m{\s+}, $ENV{source_instance}
60
                                                    if $ENV{source_instance};
61
@state           = split m{\s+}, $ENV{state}       if $ENV{state};
62
@type            = split m{\s+}, $ENV{type}        if $ENV{type};
63

    
64
# Get stats
65
my $data = `syslog-ng-ctl stats`;
66
die 'Can`t get stats from syslog-ng-ctl' unless $data;
67

    
68
# Split to graphs
69
my @str = split m{\n+}, $data;
70
# Remove title
71
shift @str;
72

    
73
my @gpaths;
74
for my $graph (@str) {
75
    # Split to data
76
    $graph = [ split m{;}, $graph ];
77
    $graph = {
78
        source_name     => $graph->[0],
79
        source_id       => $graph->[1],
80
        source_instance => $graph->[2],
81
        state           => $graph->[3],
82
        type            => $graph->[4],
83
        number          => $graph->[5],
84
    };
85

    
86
    # Apply filters
87
    next if @source_name        and
88
            ! any {$graph->{source_name} =~ m{$_}i } @source_name;
89
    next if @source_id          and
90
            ! any {$graph->{source_id} =~ m{$_}i } @source_id;
91
    next if @source_instance    and
92
            ! any {$graph->{source_instance} =~ m{$_}i } @source_instance;
93
    next if @state              and
94
            ! any {$graph->{state} =~ m{$_}i } @state;
95
    next if @type               and
96
            ! any {$graph->{type} =~ m{$_}i } @type;
97

    
98
    # Save graph
99
    push @gpaths, $graph;
100
}
101

    
102
# Show config
103
if( exists $ARGV[0] and $ARGV[0] eq "config" ) {
104
    print "graph_title Syslog-ng statistics\n";
105
    print "graph_vlabel count\n";
106
    print "graph_args --base 1000 --lower-limit 0 --rigid\n";
107
    print "graph_info This graph show syslog-ng-ctl stats\n";
108
    print "graph_category syslog\n";
109
    for my $graph (@gpaths) {
110
        # ID
111
        my $id = sprintf '%s_%s',
112
            $graph->{source_id} || $graph->{source_instance},
113
            $graph->{type};
114
        s{#(\d+)}{[$1]}, s{[^\w\]\[]+}{_}g for $id;
115

    
116
        printf "%s.label %s: %s, %s\n", $id,
117
            $graph->{source_name},$graph->{source_id}, $graph->{type};
118
        printf "%s.min 0\n", $id;
119
    }
120
}
121

    
122
for my $graph (@gpaths) {
123
    # ID
124
    my $id = sprintf '%s_%s',
125
        $graph->{source_id} || $graph->{source_instance},
126
        $graph->{type};
127
    s{#(\d+)}{[$1]}, s{[^\w\]\[]+}{_}g for $id;
128

    
129
    printf "%s.value %s\n", $id, $graph->{number};
130
}
131

    
132
exit;