]> arthur.barton.de Git - netdata.git/blob - charts.d/phpfpm.chart.sh
rename chart fields to avoid conflicts with backends; fixes #1962
[netdata.git] / charts.d / phpfpm.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 # Contributed by @safeie with PR #276
9
10 # first, you need open php-fpm status in php-fpm.conf
11 # second, you need add status location in nginx.conf
12 # you can see, https://easyengine.io/tutorials/php/fpm-status-page/
13
14 declare -A phpfpm_urls=()
15 declare -A phpfpm_curl_opts=()
16
17 # _update_every is a special variable - it holds the number of seconds
18 # between the calls of the _update() function
19 phpfpm_update_every=
20 phpfpm_priority=60000
21
22 declare -a phpfpm_response=()
23 phpfpm_pool=""
24 phpfpm_start_time=""
25 phpfpm_start_since=0
26 phpfpm_accepted_conn=0
27 phpfpm_listen_queue=0
28 phpfpm_max_listen_queue=0
29 phpfpm_listen_queue_len=0
30 phpfpm_idle_processes=0
31 phpfpm_active_processes=0
32 phpfpm_total_processes=0
33 phpfpm_max_active_processes=0
34 phpfpm_max_children_reached=0
35 phpfpm_slow_requests=0
36 phpfpm_get() {
37         local opts="${1}" url="${2}"
38
39         phpfpm_response=($(run curl -Ss ${opts} "${url}"))
40         [ $? -ne 0 -o "${#phpfpm_response[@]}" -eq 0 ] && return 1
41
42         if [[ "${phpfpm_response[0]}" != "pool:" \
43                 || "${phpfpm_response[2]}" != "process" \
44                 || "${phpfpm_response[5]}" != "start" \
45                 || "${phpfpm_response[12]}" != "accepted" \
46                 || "${phpfpm_response[15]}" != "listen" \
47                 || "${phpfpm_response[16]}" != "queue:" \
48                 || "${phpfpm_response[26]}" != "idle" \
49                 || "${phpfpm_response[29]}" != "active" \
50                 || "${phpfpm_response[32]}" != "total" \
51         ]]
52                 then
53                 error "invalid response from phpfpm status server: ${phpfpm_response[*]}"
54                 return 1
55         fi
56
57         phpfpm_pool="${phpfpm_response[1]}"
58         phpfpm_start_time="${phpfpm_response[7]} ${phpfpm_response[8]}"
59         phpfpm_start_since="${phpfpm_response[11]}"
60         phpfpm_accepted_conn="${phpfpm_response[14]}"
61         phpfpm_listen_queue="${phpfpm_response[17]}"
62         phpfpm_max_listen_queue="${phpfpm_response[21]}"
63         phpfpm_listen_queue_len="${phpfpm_response[25]}"
64         phpfpm_idle_processes="${phpfpm_response[28]}"
65         phpfpm_active_processes="${phpfpm_response[31]}"
66         phpfpm_total_processes="${phpfpm_response[34]}"
67         phpfpm_max_active_processes="${phpfpm_response[38]}"
68         phpfpm_max_children_reached="${phpfpm_response[42]}"
69         if [ "${phpfpm_response[43]}" == "slow" ]
70                 then
71                 phpfpm_slow_requests="${phpfpm_response[45]}"
72         else
73                 phpfpm_slow_requests="-1"
74         fi
75         
76         if [[ -z "${phpfpm_pool}" \
77                 || -z "${phpfpm_start_time}" \
78                 || -z "${phpfpm_start_since}" \
79                 || -z "${phpfpm_accepted_conn}" \
80                 || -z "${phpfpm_listen_queue}" \
81                 || -z "${phpfpm_max_listen_queue}" \
82                 || -z "${phpfpm_listen_queue_len}" \
83                 || -z "${phpfpm_idle_processes}" \
84                 || -z "${phpfpm_active_processes}" \
85                 || -z "${phpfpm_total_processes}" \
86                 || -z "${phpfpm_max_active_processes}" \
87                 || -z "${phpfpm_max_children_reached}" \
88         ]]
89                 then
90                 error "empty values got from phpfpm status server: ${phpfpm_response[*]}"
91                 return 1
92         fi
93
94         return 0
95 }
96
97 # _check is called once, to find out if this chart should be enabled or not
98 phpfpm_check() {
99         if [ ${#phpfpm_urls[@]} -eq 0 ]; then
100                 phpfpm_urls[local]="http://localhost/status"
101         fi
102         
103         local m
104         for m in "${!phpfpm_urls[@]}"
105         do
106                 phpfpm_get "${phpfpm_curl_opts[$m]}" "${phpfpm_urls[$m]}"
107                 if [ $? -ne 0 ]; then
108                         error "cannot find status on URL '${phpfpm_url[$m]}'. Please set phpfpm_urls[$m]='http://localhost/status' in $confd/phpfpm.conf"
109                         unset phpfpm_urls[$m]
110                         continue
111                 fi
112         done
113         
114         if [ ${#phpfpm_urls[@]} -eq 0 ]; then
115                 error "no phpfpm servers found. Please set phpfpm_urls[name]='url' to whatever needed to get status to the phpfpm server, in $confd/phpfpm.conf"
116                 return 1
117         fi
118         
119         # this should return:
120         #  - 0 to enable the chart
121         #  - 1 to disable the chart
122
123         return 0
124 }
125
126 # _create is called once, to create the charts
127 phpfpm_create() {
128         local m
129         for m in "${!phpfpm_urls[@]}"
130         do
131                 cat <<EOF
132 CHART phpfpm_$m.connections '' "PHP-FPM Active Connections" "connections" phpfpm phpfpm.connections line $((phpfpm_priority + 1)) $phpfpm_update_every
133 DIMENSION active '' absolute 1 1
134 DIMENSION maxActive 'max active' absolute 1 1
135 DIMENSION idle '' absolute 1 1
136
137 CHART phpfpm_$m.requests '' "PHP-FPM Requests" "requests/s" phpfpm phpfpm.requests line $((phpfpm_priority + 2)) $phpfpm_update_every
138 DIMENSION requests '' incremental 1 1
139
140 CHART phpfpm_$m.performance '' "PHP-FPM Performance" "status" phpfpm phpfpm.performance line $((phpfpm_priority + 3)) $phpfpm_update_every
141 DIMENSION reached 'max children reached' absolute 1 1
142 EOF
143                 if [ $((phpfpm_slow_requests)) -ne -1 ]
144                         then
145                         echo "DIMENSION slow 'slow requests' absolute 1 1"
146                 fi
147         done
148         
149         return 0
150 }
151
152 # _update is called continiously, to collect the values
153 phpfpm_update() {
154         # the first argument to this function is the microseconds since last update
155         # pass this parameter to the BEGIN statement (see bellow).
156
157         # do all the work to collect / calculate the values
158         # for each dimension
159         # remember: KEEP IT SIMPLE AND SHORT
160
161         local m
162         for m in "${!phpfpm_urls[@]}"
163         do
164                 phpfpm_get "${phpfpm_curl_opts[$m]}" "${phpfpm_urls[$m]}"
165                 if [ $? -ne 0 ]; then
166                         continue
167                 fi
168         
169                 # write the result of the work.
170                 cat <<EOF
171 BEGIN phpfpm_$m.connections $1
172 SET active = $((phpfpm_active_processes))
173 SET maxActive = $((phpfpm_max_active_processes))
174 SET idle = $((phpfpm_idle_processes))
175 END
176 BEGIN phpfpm_$m.requests $1
177 SET requests = $((phpfpm_accepted_conn))
178 END
179 BEGIN phpfpm_$m.performance $1
180 SET reached = $((phpfpm_max_children_reached))
181 EOF
182                 if [ $((phpfpm_slow_requests)) -ne -1 ]
183                         then
184                         echo "SET slow = $((phpfpm_slow_requests))"
185                 fi
186                 echo "END"
187         done
188         
189         return 0
190 }
191
192 phpfpm_check
193 phpfpm_create
194 phpfpm_update