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