Projet

Général

Profil

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

root / plugins / apache / apache_vhosts / apache_pipelogger @ 942bda31

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

1
#!/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
	next unless m/^([\d\w.-_]+\s){5}([\d\w.-_]+$)/;
43
	$time=sprintf("%d",$time/1000); # microsec to millisec	
44
	
45
	# sitename to munin fieldname
46
	my $vpm=clean_fieldname($vhost);
47
	$temp{$vpm}{'label'}=$vhost;
48
	$temp{$vpm}{'label'}=~s/www\.//;
49
	
50
	# count all requests
51
	$temp{$vpm}{'requests'}++;
52

    
53
	if ($bytes) {
54
	 $bytes=~s/-/0/;
55
 	 # bytes transmitted
56
	 $temp{$vpm}{'bytes'}+=$bytes;
57

    
58
	 # max bytes
59
	 $temp{$vpm}{'max_bytes'}=max($temp{$vpm}{'max_bytes'},$bytes);
60

    
61
         # average bytes
62
         $temp{$vpm}{'avg_bytes'}=$temp{$vpm}{'bytes'}/$temp{$vpm}{'requests'} || 0 if ($bytes);
63
	}
64
	
65
	# count by status / error code
66
	$temp{$vpm}{"status"}{$status}++ if $status;
67

    
68
	if ($time) {
69
	  # microsec to millisec
70
  	  $time=sprintf("%d",$time/1000); 
71

    
72
  	  # min/max execution time
73
  	  $temp{$vpm}{'max_time'}=max($temp{$vpm}{'max_time'},$time);
74

    
75
  	  # cumulative and average execution time
76
  	  $temp{$vpm}{'cml_time'}+=$time;
77

    
78
          # average time
79
          $temp{$vpm}{'avg_time'}=$temp{$vpm}{'cml_time'}/$temp{$vpm}{'requests'} || 0 if ($time);
80
        }
81
};
82

    
83
sub periodic_write {
84
        # begin transaction                
85
        $share->lock(LOCK_EX);
86
        
87
        # get data (may be updated by other loggers too)
88
        my %old=%{thaw $share->fetch};
89

    
90
        foreach my $vpm (keys %temp){
91
                # merge values
92
                $old{$vpm}{'bytes'}+=$temp{$vpm}{'bytes'} if $temp{$vpm}{'bytes'};
93
                $old{$vpm}{'requests'}+=$temp{$vpm}{'requests'} if $temp{$vpm}{'requests'};
94
                $old{$vpm}{'time'}+=$temp{$vpm}{'time'} if $temp{$vpm}{'time'};
95
                $old{$vpm}{'label'}=$temp{$vpm}{'label'};
96
                $old{$vpm}{'avg_time'}=sprintf("%d",($old{$vpm}{'avg_time'}+$temp{$vpm}{'avg_time'})/2);
97
                $old{$vpm}{'max_time'}=max($old{$vpm}{'max_time'},$temp{$vpm}{'max_time'});
98
                $old{$vpm}{'max_bytes'}=max($temp{$vpm}{'max_bytes'},$temp{$vpm}{'max_bytes'});
99
                $old{$vpm}{'avg_bytes'}=sprintf("%d",($old{$vpm}{'avg_bytes'}+$temp{$vpm}{'avg_bytes'})/2);
100

    
101
                # reset local counters
102
                foreach my $check qw(requests bytes time cml_time max_bytes avg_bytes max_time avg_time) {
103
                        $temp{$vpm}{$check}=0;
104
                }
105

    
106
                # reset status counts
107
                foreach my $val (keys %{$temp{$vpm}{'status'}}) {
108
                        $old{$vpm}{'status'}{$val}+=$temp{$vpm}{'status'}{$val};
109
                        $temp{$vpm}{'status'}{$val}=0;
110
                }
111

    
112
        };
113

    
114
        # save to shm
115
#        print Data::Dumper::Dumper(%old) if $debug;
116
        $share->store( freeze \%old );
117

    
118
        # end transaction
119
        $share->unlock;
120

    
121
        # parse/write every n seconds
122
        alarm $nsec;
123
}