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