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