]> arthur.barton.de Git - netdata.git/blob - charts.d/nginx.chart.sh
better log management for charts.d.plugin - fixes #1144
[netdata.git] / charts.d / nginx.chart.sh
1 # no need for shebang - this file is loaded from charts.d.plugin
2
3 # if this chart is called X.chart.sh, then all functions and global variables
4 # must start with X_
5
6 nginx_url="http://127.0.0.1:80/stub_status"
7 nginx_curl_opts=""
8
9 # _update_every is a special variable - it holds the number of seconds
10 # between the calls of the _update() function
11 nginx_update_every=
12 nginx_priority=60000
13
14 declare -a nginx_response=()
15 nginx_active_connections=0
16 nginx_accepts=0
17 nginx_handled=0
18 nginx_requests=0
19 nginx_reading=0
20 nginx_writing=0
21 nginx_waiting=0
22 nginx_get() {
23         nginx_response=($(run curl -Ss ${nginx_curl_opts} "${nginx_url}"))
24         [ $? -ne 0 -o "${#nginx_response[@]}" -eq 0 ] && return 1
25
26         if [ "${nginx_response[0]}" != "Active" \
27                  -o "${nginx_response[1]}" != "connections:" \
28                  -o "${nginx_response[3]}" != "server" \
29                  -o "${nginx_response[4]}" != "accepts" \
30                  -o "${nginx_response[5]}" != "handled" \
31                  -o "${nginx_response[6]}" != "requests" \
32                  -o "${nginx_response[10]}" != "Reading:" \
33                  -o "${nginx_response[12]}" != "Writing:" \
34                  -o "${nginx_response[14]}" != "Waiting:" \
35            ]
36                 then
37                 error "Invalid response from nginx server: ${nginx_response[*]}"
38                 return 1
39         fi
40
41         nginx_active_connections="${nginx_response[2]}"
42         nginx_accepts="${nginx_response[7]}"
43         nginx_handled="${nginx_response[8]}"
44         nginx_requests="${nginx_response[9]}"
45         nginx_reading="${nginx_response[11]}"
46         nginx_writing="${nginx_response[13]}"
47         nginx_waiting="${nginx_response[15]}"
48
49         if [ -z "${nginx_active_connections}" \
50                 -o -z "${nginx_accepts}" \
51                 -o -z "${nginx_handled}" \
52                 -o -z "${nginx_requests}" \
53                 -o -z "${nginx_reading}" \
54                 -o -z "${nginx_writing}" \
55                 -o -z "${nginx_waiting}" \
56                 ]
57                 then
58                 error "empty values got from nginx server: ${nginx_response[*]}"
59                 return 1
60         fi
61
62         return 0
63 }
64
65 # _check is called once, to find out if this chart should be enabled or not
66 nginx_check() {
67
68         nginx_get
69         if [ $? -ne 0 ]
70                 then
71                 error "cannot find stub_status on URL '${nginx_url}'. Please set nginx_url='http://nginx.server/stub_status' in $confd/nginx.conf"
72                 return 1
73         fi
74
75         # this should return:
76         #  - 0 to enable the chart
77         #  - 1 to disable the chart
78
79         return 0
80 }
81
82 # _create is called once, to create the charts
83 nginx_create() {
84         cat <<EOF
85 CHART nginx_local.connections '' "nginx Active Connections" "connections" nginx nginx.connections line $((nginx_priority + 1)) $nginx_update_every
86 DIMENSION active '' absolute 1 1
87
88 CHART nginx_local.requests '' "nginx Requests" "requests/s" nginx nginx.requests line $((nginx_priority + 2)) $nginx_update_every
89 DIMENSION requests '' incremental 1 1
90
91 CHART nginx_local.connections_status '' "nginx Active Connections by Status" "connections" nginx nginx.connections.status line $((nginx_priority + 3)) $nginx_update_every
92 DIMENSION reading '' absolute 1 1
93 DIMENSION writing '' absolute 1 1
94 DIMENSION waiting idle absolute 1 1
95
96 CHART nginx_local.connect_rate '' "nginx Connections Rate" "connections/s" nginx nginx.connections.rate line $((nginx_priority + 4)) $nginx_update_every
97 DIMENSION accepts accepted incremental 1 1
98 DIMENSION handled '' incremental 1 1
99 EOF
100
101         return 0
102 }
103
104 # _update is called continiously, to collect the values
105 nginx_update() {
106         # the first argument to this function is the microseconds since last update
107         # pass this parameter to the BEGIN statement (see bellow).
108
109         # do all the work to collect / calculate the values
110         # for each dimension
111         # remember: KEEP IT SIMPLE AND SHORT
112
113         nginx_get || return 1
114
115         # write the result of the work.
116         cat <<VALUESEOF
117 BEGIN nginx_local.connections $1
118 SET active = $((nginx_active_connections))
119 END
120 BEGIN nginx_local.requests $1
121 SET requests = $((nginx_requests))
122 END
123 BEGIN nginx_local.connections_status $1
124 SET reading = $((nginx_reading))
125 SET writing = $((nginx_writing))
126 SET waiting = $((nginx_waiting))
127 END
128 BEGIN nginx_local.connect_rate $1
129 SET accepts = $((nginx_accepts))
130 SET handled = $((nginx_handled))
131 END
132 VALUESEOF
133
134         return 0
135 }