Projet

Général

Profil

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

root / plugins / nginx / nginx_vhost_traffic @ 9f85e0ae

Historique | Voir | Annoter | Télécharger (2,33 ko)

1 8a3697d7 Joan Perez i Cauhe
#!/bin/sh
2
#
3
# Script for monitoring nginx Virtual host output traffic
4
# 
5
# Requierements: logtail awk
6
# one unique access log file with $bytes_sent value for more accuracy
7
# check http://wiki.nginx.org/NginxHttpLogModule
8
#
9
# Configuration Options (all options have defauts)
10
# [nginx_vhost_traffic]
11
#
12
# Virtual host list
13
# env.vhosts "example.com example.net example.org"
14
# 
15
# Log path
16
# env.logdir = /var/log/nginx
17
# env.flogfile = access.log 
18
#
19
# Position of the $bytes_sent in the access.log file
20
# env.bparam 11
21
#
22
# Aggregate subdomains
23
# ex: example.com will match www.example.com, webmail.example.com and *example.com 
24
# BUG: will also match also www.bad-example.com
25
# env.aggregate true #change to false to disable aggregation 
26
#
27
# To report bugs, improvements or get updates
28
# see http://github.com/joanpc/nginix_vhost_traffic
29
#
30
# inspired in postfix_filtered_awk 
31
# Copyright (c) 2010, Joan Perez i Cauhe
32
33
LOGDIR=${logdir:-/var/log/nginx}
34
ACCESS_LOG=$LOGDIR/${logfile:-access.log}
35
LOGTAIL=${logtail:-`which logtail`}
36
STATEFILE=/var/lib/munin/plugin-state/nginx_vhost_traffic.state
37
VHOSTS=${vhosts:-`hostname`}
38
AGGREGATE=${aggregate:true}
39
40
BPARAM=${bparam:-11}
41
42
case $1 in
43
   config)
44
   	DRAW=AREA
45
	echo 'graph_title Nginx Virtual host traffic'
46
	echo 'graph_vlabel bits out / ${graph_period}'
47
	echo 'graph_args --base 1000 -l 0'
48
	echo 'graph_category Nginx'
49
50
	i=0
51
	for vhost in $VHOSTS 
52
	do 
53
		i=$(($i + 1))
54
		echo vhost$i.label $vhost
55
		echo vhost$i.type ABSOLUTE
56
		echo vhost$i.cdef vhost$i,8,*
57
		echo vhost$i.draw $DRAW
58
		DRAW=STACK
59
	done
60
	
61
	echo rest.label Rest
62
	echo rest.type ABSOLUTE
63
	echo rest.cdef rest,8,*
64
	echo rest.draw STACK
65
    exit 0;;
66
esac
67
68
export BPARAM
69
export VHOSTS
70
export AGGREGATE
71
72
# Awk Script
73 ea1e17cc Joan Perez i Cauhe
$LOGTAIL ${ACCESS_LOG} -o $STATEFILE | awk '
74 8a3697d7 Joan Perez i Cauhe
75
BEGIN { 
76
split(ENVIRON["VHOSTS"], hosts)
77
for (host in hosts) { track[hosts[host]] = host}
78
}        
79
{
80 ea1e17cc Joan Perez i Cauhe
		cn[$2]+=$ENVIRON["BPARAM"]
81 8a3697d7 Joan Perez i Cauhe
}
82
END { 
83
		for (host in cn) {		
84
		if (match(ENVIRON["AGGREGATE"], "true")) {	
85
			found = 0
86
			for (vhost in track) {
87
				if (index(host, vhost)) {
88
				res[vhost] += cn[host]
89
				found = 1
90
				break
91
				}
92
			}
93
			if (! found) rest+=cn[host]
94
		} else {
95
			if (host in track) {
96
				res[host] += cn[host]	
97
			} else rest+=cn[host]
98
		}
99
		}
100
		print "agregate: " ENVIRON["AGGREGATE"]
101
		for (vhost in track) print "vhost" track[vhost] ".value " res[vhost]+0
102
		print "rest.value " rest + 0
103 ea1e17cc Joan Perez i Cauhe
}'