| 1 | package Collectd::Plugins::RsyslogStats; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Collectd qw( :all ); | ||
| 6 | |||
| 7 | use File::ReadBackwards; | ||
| 8 | |||
| 9 | # ABSTRACT: collectd plugin for reading queue metrics from rsyslog/imstats logfile | ||
| 10 | our $VERSION = '1.001'; # VERSION | ||
| 11 | |||
| 12 | |||
| 13 | my $types = { | ||
| 14 | 'enqueued' => 'counter', | ||
| 15 | 'size' => 'gauge', | ||
| 16 | 'full' => 'counter', | ||
| 17 | 'discarded_full' => 'counter', | ||
| 18 | 'discarded_nf' => 'counter', | ||
| 19 | 'maxqsize' => 'gauge', | ||
| 20 | }; | ||
| 21 | |||
| 22 | our $metrics = [ 'enqueued', 'size' ]; | ||
| 23 | our $prefix = 'rsyslog'; | ||
| 24 | our $path = '/var/log/rsyslog-stats.log'; | ||
| 25 | |||
| 26 | sub rsyslog_stats_config { | ||
| 27 | my ($ci) = @_; | ||
| 28 | foreach my $item (@{$ci->{'children'}}) { | ||
| 29 | my $key = lc($item->{'key'}); | ||
| 30 | my $val = $item->{'values'}->[0]; | ||
| 31 | |||
| 32 | if ($key eq 'path' ) { | ||
| 33 | $path = $val; | ||
| 34 | } elsif ($key eq 'prefix' ) { | ||
| 35 | $prefix = $val; | ||
| 36 | } elsif ($key eq 'metrics') { | ||
| 37 | $metrics = [ split(/\s*,\s*/, $val) ]; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | return 1; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub read_stats_from_log { | ||
| 44 | my $file = File::ReadBackwards->new( $path ) | ||
| 45 | or die("cant open $path: $!"); | ||
| 46 | |||
| 47 | my $stats = {}; | ||
| 48 | my $max_lines = 200; | ||
| 49 | my $count = 0; | ||
| 50 | |||
| 51 | while ( my $line = $file->readline ) { | ||
| 52 | chomp($line); | ||
| 53 | if( $count >= $max_lines ) { | ||
| 54 | return; | ||
| 55 | } | ||
| 56 | $count++; | ||
| 57 | |||
| 58 | my ($timestamp_str) = $line =~ s/^\S+\s+(\S+\s+\d+\s+\d+:\d+:\d+\s+\d+): //; | ||
| 59 | if( ! defined $timestamp_str ) { | ||
| 60 | next; | ||
| 61 | } | ||
| 62 | if( $line !~ s/^(main Q|action \d+ queue[^:]*): // ) { | ||
| 63 | next; | ||
| 64 | } | ||
| 65 | my $queue = $1; | ||
| 66 | $queue =~ s/[\s\.]+/-/g; | ||
| 67 | if( defined $stats->{$queue} ) { | ||
| 68 | last; | ||
| 69 | } | ||
| 70 | $stats->{$queue} = { map { my $v = $_ ; $v =~ s/[\s\.]+/_/g ; $v } split(/[ =]/, $line) }; | ||
| 71 | } | ||
| 72 | return( $stats ); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub rsyslog_stats_read { | ||
| 76 | my $stats = read_stats_from_log(); | ||
| 77 | |||
| 78 | foreach my $queue_name (keys %$stats) { | ||
| 79 | my $queue = $stats->{$queue_name}; | ||
| 80 | foreach my $metric_name ( keys %$queue ) { | ||
| 81 | if( ! grep { $_ eq $metric_name } @$metrics ) { | ||
| 82 | next; | ||
| 83 | } | ||
| 84 | my $vl = { | ||
| 85 | plugin => 'rsyslog', | ||
| 86 | plugin_instance => $queue_name, | ||
| 87 | type => 'counter', | ||
| 88 | type_instance => $metric_name, | ||
| 89 | }; | ||
| 90 | if( defined $types->{$metric_name} ) { | ||
| 91 | $vl->{'type'} = $types->{$metric_name}; | ||
| 92 | } | ||
| 93 | $vl->{'values'} = [ $queue->{$metric_name} ]; | ||
| 94 | plugin_dispatch_values($vl); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | return 1; | ||
| 98 | } | ||
| 99 | |||
| 100 | plugin_register(TYPE_CONFIG, "RsyslogStats", "rsyslog_stats_config"); | ||
| 101 | plugin_register(TYPE_READ, "RsyslogStats", "rsyslog_stats_read"); | ||
| 102 | |||
| 103 | 1; | ||
| 104 | |||
| 105 | |||
| 106 | __END__ |