Projet

Général

Profil

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

root / plugins / nginx / nginx_vhost_traffic @ 17f78427

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

1 8a3697d7 Joan Perez i Cauhe
#!/bin/sh
2
#
3
# Script for monitoring nginx Virtual host output traffic
4 17f78427 Lars Kruse
#
5 8a3697d7 Joan Perez i Cauhe
# 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 fba800ae Veres Lajos
# Configuration Options (all options have defaults)
10 8a3697d7 Joan Perez i Cauhe
# [nginx_vhost_traffic]
11
#
12
# Virtual host list
13
# env.vhosts "example.com example.net example.org"
14 17f78427 Lars Kruse
#
15 8a3697d7 Joan Perez i Cauhe
# Log path
16
# env.logdir = /var/log/nginx
17 17f78427 Lars Kruse
# env.flogfile = access.log
18 8a3697d7 Joan Perez i Cauhe
#
19
# Position of the $bytes_sent in the access.log file
20
# env.bparam 11
21
#
22
# Aggregate subdomains
23 17f78427 Lars Kruse
# ex: example.com will match www.example.com, webmail.example.com and *example.com
24 8a3697d7 Joan Perez i Cauhe
# BUG: will also match also www.bad-example.com
25 17f78427 Lars Kruse
# env.aggregate true #change to false to disable aggregation
26 8a3697d7 Joan Perez i Cauhe
#
27
# To report bugs, improvements or get updates
28
# see http://github.com/joanpc/nginix_vhost_traffic
29
#
30 17f78427 Lars Kruse
# inspired in postfix_filtered_awk
31 8a3697d7 Joan Perez i Cauhe
# 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 4b2fcbf8 Lars Kruse
STATEFILE=$MUNIN_PLUGSTATE/nginx_vhost_traffic.state
37 8a3697d7 Joan Perez i Cauhe
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 3c98d069 dipohl
	echo 'graph_category webserver'
49 8a3697d7 Joan Perez i Cauhe
50
	i=0
51 17f78427 Lars Kruse
	for vhost in $VHOSTS
52
	do
53 8a3697d7 Joan Perez i Cauhe
		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 17f78427 Lars Kruse
61 8a3697d7 Joan Perez i Cauhe
	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 17f78427 Lars Kruse
BEGIN {
76 8a3697d7 Joan Perez i Cauhe
split(ENVIRON["VHOSTS"], hosts)
77
for (host in hosts) { track[hosts[host]] = host}
78 17f78427 Lars Kruse
}
79 8a3697d7 Joan Perez i Cauhe
{
80 ea1e17cc Joan Perez i Cauhe
		cn[$2]+=$ENVIRON["BPARAM"]
81 8a3697d7 Joan Perez i Cauhe
}
82 17f78427 Lars Kruse
END {
83
		for (host in cn) {
84
		if (match(ENVIRON["AGGREGATE"], "true")) {
85 8a3697d7 Joan Perez i Cauhe
			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 17f78427 Lars Kruse
				res[host] += cn[host]
97 8a3697d7 Joan Perez i Cauhe
			} else rest+=cn[host]
98
		}
99
		}
100 fba800ae Veres Lajos
		print "aggregate: " ENVIRON["AGGREGATE"]
101 8a3697d7 Joan Perez i Cauhe
		for (vhost in track) print "vhost" track[vhost] ".value " res[vhost]+0
102
		print "rest.value " rest + 0
103 ea1e17cc Joan Perez i Cauhe
}'