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
#!/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

    
11
=head1 APPLICABLE SYSTEMS
12

    
13
Any host running nginx, with bash version > 4.0
14

    
15

    
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
Additionally you may want to adjust 'group' (or 'user') based on the
21
permissions required for reading the log file.
22

    
23
  [nginx_error]
24
        group          adm
25
	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

    
31
=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
'env.logpattern' is ignored for a non-symlink configuration.
43

    
44

    
45
=head1 INTERPRETATION
46

    
47
The plugin shows nginx http "error" status rates by parsing access log.
48

    
49

    
50
=head1 MAGIC MARKERS
51

    
52
 #%# family=auto
53
 #%# capabilities=autoconf
54

    
55

    
56
=head1 BUGS
57

    
58
None known.
59

    
60

    
61
=head1 VERSION
62

    
63
1.1 - 2018/01/20
64
      * add 'dirty config' capability support
65
      * fix shell style issues reported by shellcheck
66
      * improve readability of symlink configuration code
67

    
68
1.0 - 2017/02/21
69

    
70

    
71
=head1 AUTHOR
72

    
73
vovansystems@gmail.com, 2013
74

    
75

    
76
=head1 LICENSE
77

    
78
GPLv3
79

    
80
=cut
81

    
82

    
83
# default environment variable values
84
logpath=${logpath:-/var/log/nginx}
85

    
86

    
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
else
97
  log_filename='access.log'
98
fi
99

    
100
log="$logpath/$log_filename"
101

    
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

    
118
# parse error counts from log file
119
do_fetch () {
120
  local count status_code
121
  declare -A line_counts
122
  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
  done
129
}
130

    
131

    
132
do_config () {
133
  local status_code
134
  echo "graph_title $(basename "$log") - Nginx errors per minute"
135
  echo "graph_vlabel pages with http error codes / \${graph_period}"
136
  echo "graph_category webserver"
137
  echo "graph_period minute"
138
  echo "graph_info This graph shows nginx error rate per minute"
139
  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
  done
144
}
145

    
146

    
147
do_autoconf () {
148
  echo yes
149
}
150

    
151

    
152
case $1 in
153
  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
esac
165

    
166
exit $?