root / plugins / apache / apache_vhosts / apache_pipelogger @ 8589c6df
Historique | Voir | Annoter | Télécharger (3,44 ko)
| 1 | 942bda31 | Kenyon Ralph | #!/usr/bin/perl |
|---|---|---|---|
| 2 | |||
| 3 | =head1 |
||
| 4 | |||
| 5 | # Log vhost port method response_bytes response_time status |
||
| 6 | <IfModule mod_log_config.c> |
||
| 7 | CustomLog "|/usr/share/munin/apache_pipelogger" "%v %p %m %B %D %s" |
||
| 8 | </IfModule> |
||
| 9 | |||
| 10 | =cut |
||
| 11 | # write every n seconds to shared memory |
||
| 12 | local $nsec=7; |
||
| 13 | local $debug=undef; |
||
| 14 | |||
| 15 | use Storable qw(freeze thaw); |
||
| 16 | use List::Util qw(min max); |
||
| 17 | use IPC::ShareLite ':lock'; |
||
| 18 | require Data::Dumper if $debug; |
||
| 19 | use Munin::Plugin; |
||
| 20 | |||
| 21 | |||
| 22 | local $share = IPC::ShareLite->new( |
||
| 23 | -key => 'mapl', |
||
| 24 | -create => 1, |
||
| 25 | -destroy => 1, |
||
| 26 | -exclusive => 0, |
||
| 27 | -mode => '0666' |
||
| 28 | ) or die $!; |
||
| 29 | |||
| 30 | |||
| 31 | local $SIG{'ALRM'}=\&periodic_write;
|
||
| 32 | alarm $nsec; |
||
| 33 | |||
| 34 | |||
| 35 | # drop stored data on reload |
||
| 36 | local %temp=(); |
||
| 37 | |||
| 38 | while (<STDIN>) {
|
||
| 39 | my ($vhost,$port,$method,$bytes,$time,$status)=split(/\s/,$_); |
||
| 40 | |||
| 41 | # sanity check |
||
| 42 | 672fd1c3 | stimpy23 | next unless m/^([\d\w\.\-_]+\s){5}([\d\w\.\-_]+$)/; # escaped "." and "-"
|
| 43 | 942bda31 | Kenyon Ralph | |
| 44 | # sitename to munin fieldname |
||
| 45 | my $vpm=clean_fieldname($vhost); |
||
| 46 | $temp{$vpm}{'label'}=$vhost;
|
||
| 47 | $temp{$vpm}{'label'}=~s/www\.//;
|
||
| 48 | |||
| 49 | # count all requests |
||
| 50 | $temp{$vpm}{'requests'}++;
|
||
| 51 | |||
| 52 | if ($bytes) {
|
||
| 53 | $bytes=~s/-/0/; |
||
| 54 | # bytes transmitted |
||
| 55 | $temp{$vpm}{'bytes'}+=$bytes;
|
||
| 56 | |||
| 57 | # max bytes |
||
| 58 | $temp{$vpm}{'max_bytes'}=max($temp{$vpm}{'max_bytes'},$bytes);
|
||
| 59 | |||
| 60 | # average bytes |
||
| 61 | $temp{$vpm}{'avg_bytes'}=$temp{$vpm}{'bytes'}/$temp{$vpm}{'requests'} || 0 if ($bytes);
|
||
| 62 | } |
||
| 63 | |||
| 64 | # count by status / error code |
||
| 65 | $temp{$vpm}{"status"}{$status}++ if $status;
|
||
| 66 | |||
| 67 | if ($time) {
|
||
| 68 | # min/max execution time |
||
| 69 | $temp{$vpm}{'max_time'}=max($temp{$vpm}{'max_time'},$time);
|
||
| 70 | |||
| 71 | # cumulative and average execution time |
||
| 72 | $temp{$vpm}{'cml_time'}+=$time;
|
||
| 73 | |||
| 74 | # average time |
||
| 75 | $temp{$vpm}{'avg_time'}=$temp{$vpm}{'cml_time'}/$temp{$vpm}{'requests'} || 0 if ($time);
|
||
| 76 | } |
||
| 77 | }; |
||
| 78 | |||
| 79 | sub periodic_write {
|
||
| 80 | # begin transaction |
||
| 81 | $share->lock(LOCK_EX); |
||
| 82 | |||
| 83 | # get data (may be updated by other loggers too) |
||
| 84 | 672fd1c3 | stimpy23 | my %old=eval{%{thaw($share->fetch)}}; # using eval to suppress thaw error on empty string at the first run
|
| 85 | 942bda31 | Kenyon Ralph | |
| 86 | foreach my $vpm (keys %temp){
|
||
| 87 | # merge values |
||
| 88 | $old{$vpm}{'bytes'}+=$temp{$vpm}{'bytes'} if $temp{$vpm}{'bytes'};
|
||
| 89 | $old{$vpm}{'requests'}+=$temp{$vpm}{'requests'} if $temp{$vpm}{'requests'};
|
||
| 90 | $old{$vpm}{'time'}+=$temp{$vpm}{'time'} if $temp{$vpm}{'time'};
|
||
| 91 | $old{$vpm}{'label'}=$temp{$vpm}{'label'};
|
||
| 92 | 1b60ea87 | Brian Hourigan | $old{$vpm}{'avg_time'}=sprintf("%d",(($old{$vpm}{'avg_time'}+$temp{$vpm}{'avg_time'})/2)/1000);
|
| 93 | $old{$vpm}{'max_time'}=max($old{$vpm}{'max_time'},$temp{$vpm}{'max_time'})/1000;
|
||
| 94 | 942bda31 | Kenyon Ralph | $old{$vpm}{'max_bytes'}=max($temp{$vpm}{'max_bytes'},$temp{$vpm}{'max_bytes'});
|
| 95 | $old{$vpm}{'avg_bytes'}=sprintf("%d",($old{$vpm}{'avg_bytes'}+$temp{$vpm}{'avg_bytes'})/2);
|
||
| 96 | |||
| 97 | # reset local counters |
||
| 98 | foreach my $check qw(requests bytes time cml_time max_bytes avg_bytes max_time avg_time) {
|
||
| 99 | $temp{$vpm}{$check}=0;
|
||
| 100 | } |
||
| 101 | |||
| 102 | # reset status counts |
||
| 103 | foreach my $val (keys %{$temp{$vpm}{'status'}}) {
|
||
| 104 | $old{$vpm}{'status'}{$val}+=$temp{$vpm}{'status'}{$val};
|
||
| 105 | $temp{$vpm}{'status'}{$val}=0;
|
||
| 106 | } |
||
| 107 | |||
| 108 | }; |
||
| 109 | |||
| 110 | # save to shm |
||
| 111 | # print Data::Dumper::Dumper(%old) if $debug; |
||
| 112 | $share->store( freeze \%old ); |
||
| 113 | |||
| 114 | # end transaction |
||
| 115 | $share->unlock; |
||
| 116 | |||
| 117 | # parse/write every n seconds |
||
| 118 | alarm $nsec; |
||
| 119 | 672fd1c3 | stimpy23 | } |
