Projet

Général

Profil

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

root / plugins / nginx / nginx_error @ 9f85e0ae

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

1
#!/bin/bash
2
# -*- bash -*-
3

    
4
: <<=cut
5

    
6
=head1 NAME
7

    
8
nginx error - Munin plugin to monitor nginx error rates (http status codes per minute).
9

    
10
=head1 APPLICABLE SYSTEMS
11

    
12
Any Linux host, running nginx, with bash version > 4.0
13

    
14
=head1 CONFIGURATION
15

    
16
This shows the default configuration of this plugin.  You can override
17
the log file path and the logpattern.
18

    
19
  [nginx_error]
20
	env.logpath    /var/log/nginx
21
	env.logpattern a.*.log
22

    
23
	Nginx must also be configured to log accesses in "combined" log format (default)
24

    
25
=head1 USAGE
26

    
27
Link this plugin to /etc/munin/plugins/ and restart the munin-node.
28

    
29
This plugin also can be used like wildcard-plugin for monitoring particular virtual host log.
30
E.g.
31
    ln -s /usr/share/munin/plugins/nginx_error /etc/munin/plugins/nginx_error_mydomaincom
32
will parse the log file /var/log/nginx/a.mydomaincom.log
33

    
34
You can change 'env.logpattern' using asterisk ('*') to match your logs filenames.
35

    
36
=head1 INTERPRETATION
37

    
38
The plugin shows nginx http "error" status rates by parsing access log.
39

    
40
=head1 MAGIC MARKERS
41

    
42
 #%# family=auto
43
 #%# capabilities=autoconf
44

    
45
=head1 BUGS
46

    
47
None known.
48

    
49
=head1 VERSION
50

    
51
$Id:$
52

    
53
=head1 AUTHOR
54

    
55
vovansystems@gmail.com, 2013
56

    
57
=head1 LICENSE
58

    
59
GPLv3
60

    
61
=cut
62

    
63
if [ -z $logpath ]; then
64
  logpath='/var/log/nginx'
65
fi
66

    
67
name=`basename $0`
68

    
69
domain=${name/nginx_error/}
70
if [[ $domain != '_' && ${#domain} -ne 0 ]]; then
71
  domain=${domain:1}
72
  if [ -z $logpattern ]; then
73
    logpattern='a.*.log'
74
  fi
75
  logpattern=${logpattern/\*/$domain}
76
else
77
  logpattern='access.log'
78
fi
79

    
80
log="$logpath/$logpattern"
81

    
82
# declaring an array with http status codes, we are interested in
83
declare -A http_codes
84
http_codes[400]='Bad Request'
85
http_codes[401]='Unauthorized'
86
http_codes[403]='Forbidden'
87
http_codes[404]='Not Found'
88
http_codes[405]='Method Not Allowed'
89
http_codes[406]='Not Acceptable'
90
http_codes[408]='Request Timeout'
91
http_codes[429]='Too Many Requests'
92
http_codes[499]='Client Connection Terminated'
93
http_codes[500]='Internal Server Error'
94
http_codes[502]='Bad Gateway'
95
http_codes[503]='Service Unavailable'
96

    
97
do_ () { # Fetch
98
  for k in ${!http_codes[@]}; do
99
    #value=`awk -v k=$k '{if ($9 == k) { print $9 }}' $log | wc -l` # this is very slow and ugly
100
    value=`awk -v k=$k '{if ($9 == k) { i += 1 }} END { print i }' $log` # this is much better
101
    echo "error$k.value ${value:-0}"
102
  done
103
  exit 0
104
}
105

    
106
do_config () {
107
  echo "graph_title $logpattern - Nginx errors per minute"
108
  echo 'graph_vlabel pages with http error codes / ${graph_period}'
109
  echo "graph_category nginx"
110
  echo "graph_period minute"
111
  echo "graph_info This graph shows nginx error amount per minute"
112
  for k in ${!http_codes[@]}; do
113
    echo "error$k.type DERIVE"
114
    echo "error$k.min 0"
115
    echo "error$k.label $k ${http_codes[$k]}"
116
  done
117
  exit 0
118
}
119

    
120
do_autoconf () {
121
  echo yes
122
  exit 0
123
}
124

    
125
case $1 in
126
  config|autoconf|'')
127
  eval do_$1
128
esac
129

    
130
exit $?