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