Projet

Général

Profil

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

root / plugins / nginx / nginx_error @ 24ea5d29

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

1 90f35c58 v
#!/bin/bash
2
# -*- bash -*-
3
4 9598bc7c Kenyon Ralph
: <<=cut
5 90f35c58 v
6
=head1 NAME
7
8
nginx error - Munin plugin to monitor nginx error rates (http status codes per minute).
9
10 24ea5d29 Lars Kruse
11 90f35c58 v
=head1 APPLICABLE SYSTEMS
12
13 24ea5d29 Lars Kruse
Any host running nginx, with bash version > 4.0
14
15 90f35c58 v
16
=head1 CONFIGURATION
17
18
This shows the default configuration of this plugin.  You can override
19
the log file path and the logpattern.
20 24ea5d29 Lars Kruse
Additionally you may want to adjust 'group' (or 'user') based on the
21
permissions required for reading the log file.
22 90f35c58 v
23
  [nginx_error]
24 24ea5d29 Lars Kruse
        group          adm
25 90f35c58 v
	env.logpath    /var/log/nginx
26
	env.logpattern a.*.log
27
28
	Nginx must also be configured to log accesses in "combined" log format (default)
29
30 24ea5d29 Lars Kruse
31 90f35c58 v
=head1 USAGE
32
33
Link this plugin to /etc/munin/plugins/ and restart the munin-node.
34
35
This plugin also can be used like wildcard-plugin for monitoring particular virtual host log.
36
E.g.
37
    ln -s /usr/share/munin/plugins/nginx_error /etc/munin/plugins/nginx_error_mydomaincom
38
will parse the log file /var/log/nginx/a.mydomaincom.log
39
40
You can change 'env.logpattern' using asterisk ('*') to match your logs filenames.
41
42 24ea5d29 Lars Kruse
'env.logpattern' is ignored for a non-symlink configuration.
43
44
45 90f35c58 v
=head1 INTERPRETATION
46
47
The plugin shows nginx http "error" status rates by parsing access log.
48
49 24ea5d29 Lars Kruse
50 90f35c58 v
=head1 MAGIC MARKERS
51
52
 #%# family=auto
53
 #%# capabilities=autoconf
54
55 24ea5d29 Lars Kruse
56 90f35c58 v
=head1 BUGS
57
58
None known.
59
60 24ea5d29 Lars Kruse
61 90f35c58 v
=head1 VERSION
62
63 808a21d4 Lars Kruse
1.1 - 2018/01/20
64 7ed71b44 Lars Kruse
      * add 'dirty config' capability support
65 8a68200a Lars Kruse
      * fix shell style issues reported by shellcheck
66 808a21d4 Lars Kruse
      * improve readability of symlink configuration code
67
68 f4c19246 Lars Kruse
1.0 - 2017/02/21
69 90f35c58 v
70 808a21d4 Lars Kruse
71 90f35c58 v
=head1 AUTHOR
72
73
vovansystems@gmail.com, 2013
74
75 24ea5d29 Lars Kruse
76 90f35c58 v
=head1 LICENSE
77
78
GPLv3
79
80
=cut
81
82
83 808a21d4 Lars Kruse
# default environment variable values
84
logpath=${logpath:-/var/log/nginx}
85 90f35c58 v
86 808a21d4 Lars Kruse
87
# derive the name of the log file from a potential symlink-configured virtual host
88
script_name=$(basename "$0")
89
plugin_suffix=${script_name#nginx_error}
90
if [ -n "${plugin_suffix#_}" ]; then
91
  # a domain was given via symlink configuration: adjust the logpattern
92
  domain=${plugin_suffix#_}
93
  # default logpattern for symlink configuration mode
94
  logpattern=${logpattern:-a.*.log}
95
  log_filename=${logpattern/\*/$domain}
96 90f35c58 v
else
97 808a21d4 Lars Kruse
  log_filename='access.log'
98 90f35c58 v
fi
99
100 808a21d4 Lars Kruse
log="$logpath/$log_filename"
101 90f35c58 v
102
# declaring an array with http status codes, we are interested in
103
declare -A http_codes
104
http_codes[400]='Bad Request'
105
http_codes[401]='Unauthorized'
106
http_codes[403]='Forbidden'
107
http_codes[404]='Not Found'
108
http_codes[405]='Method Not Allowed'
109
http_codes[406]='Not Acceptable'
110
http_codes[408]='Request Timeout'
111
http_codes[429]='Too Many Requests'
112
http_codes[499]='Client Connection Terminated'
113
http_codes[500]='Internal Server Error'
114
http_codes[502]='Bad Gateway'
115
http_codes[503]='Service Unavailable'
116
117 8a68200a Lars Kruse
118
# parse error counts from log file
119 7ed71b44 Lars Kruse
do_fetch () {
120 0e864944 Lars Kruse
  local count status_code
121 37a7317c Daniel Schmitz
  declare -A line_counts
122 0e864944 Lars Kruse
  while read -r count status_code; do
123
      line_counts[$status_code]=$count
124
  done <<< "$(awk '{print $9}' "$log" | sort | uniq -c)"
125
126
  for status_code in "${!http_codes[@]}"; do
127
    echo "error${status_code}.value ${line_counts[$status_code]:-0}"
128 90f35c58 v
  done
129
}
130
131 8a68200a Lars Kruse
132 90f35c58 v
do_config () {
133 0e864944 Lars Kruse
  local status_code
134 808a21d4 Lars Kruse
  echo "graph_title $(basename "$log") - Nginx errors per minute"
135 8a68200a Lars Kruse
  echo "graph_vlabel pages with http error codes / \${graph_period}"
136 3c98d069 dipohl
  echo "graph_category webserver"
137 90f35c58 v
  echo "graph_period minute"
138 24ea5d29 Lars Kruse
  echo "graph_info This graph shows nginx error rate per minute"
139 0e864944 Lars Kruse
  for status_code in "${!http_codes[@]}"; do
140
    echo "error${status_code}.type DERIVE"
141
    echo "error${status_code}.min 0"
142
    echo "error${status_code}.label $status_code ${http_codes[$status_code]}"
143 90f35c58 v
  done
144
}
145
146 8a68200a Lars Kruse
147 90f35c58 v
do_autoconf () {
148
  echo yes
149
}
150
151 8a68200a Lars Kruse
152 90f35c58 v
case $1 in
153 7ed71b44 Lars Kruse
  config)
154
    do_config
155
    # support "dirty config" capability
156
    if [ "${MUNIN_CAP_DIRTYCONFIG:-}" = "1" ]; then do_fetch; fi
157
    ;;
158
  autoconf)
159
    do_autoconf
160
    ;;
161
  '')
162
    do_fetch
163
    ;;
164 90f35c58 v
esac
165
166 9598bc7c Kenyon Ralph
exit $?