aboutsummaryrefslogtreecommitdiffstats
path: root/vhost_bandwith
blob: 420ee0f2b57a9c22c00996dfbabf736ab8086f03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/perl -w
use strict;

# copied and modified from: https://github.com/munin-monitoring/contrib/tree/master/plugins/apache/apache_byprojects

my $server = 'Apache';

my $statepath = '/var/lib/munin/plugin-state';
my $logtail = '/usr/sbin/logtail';

my @logfiles = `ls /var/log/ispconfig/httpd/*/access.log`;

my $logs = {};

foreach (@logfiles) {
  my $actFile = $_;
  chomp($actFile);
  (my $actLabel = $actFile) =~ s:.*/([^/]*)/access\.log:$1:;
  (my $actId = $actLabel) =~ s/[^a-zA-Z0-9]/_/g;
  $logs->{$actId}{'label'} = $actLabel;
  $logs->{$actId}{'file'} = $actFile;
}

if(defined($ARGV[0])) {
  if ($ARGV[0] eq 'autoconf') {
    print "yes\n";
    exit(0);
  } elsif ($ARGV[0] eq 'config') {
    print "graph_title $server total bandwidth vhost\n";
    print "graph_total Total\n";
    print "graph_vlabel Bits\n";
    print "graph_category $server\n";
    print "graph_info This graph show $server total bandwidth used by vhost.\n";
    foreach my $vhostid (keys $logs) {
      print $vhostid.".label $logs->{$vhostid}{'label'}\n";
      print $vhostid.".type GAUGE\n";
    }
    exit(0);
  }
}

foreach my $vhostid ( keys $logs )  {
  my $i = 0;
  my $state = $statepath.'/'.$vhostid.'_totalbandwidth.state';
  open(LT, "$logtail -f ".$logs->{$vhostid}{'file'}." -o $state |") or die "Can't open $logtail : $!";
  while (<LT>) {
    my $buf = $_;
    if($buf eq '') { next }
    if($buf =~ m/" \d+ (\d+) "/) {
      $i += $1;
    }
  }
  close(LT);
  print $vhostid.".value $i\n";
}