Create phpfpm.chart.sh
safeie committed
Apr 19, 2016 at 21:46 UTC
5ac17a75844f71c9011c5b75164cacd04c84f2e7
1 file changed
+148
charts.d/phpfpm.chart.sh
new
+148
@@ -0,0 +1,148 @@
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
+phpfpm_url="http://nginx.server/php-status"
11
+
12
+# _update_every is a special variable - it holds the number of seconds
13
+# between the calls of the _update() function
14
+phpfpm_update_every=
15
+phpfpm_priority=60000
16
+
17
+declare -a phpfpm_response=()
18
+phpfpm_pool=""
19
+phpfpm_start_time=""
20
+phpfpm_start_since=0
21
+phpfpm_accepted_conn=0
22
+phpfpm_listen_queue=0
23
+phpfpm_max_listen_queue=0
24
+phpfpm_listen_queue_len=0
25
+phpfpm_idle_processes=0
26
+phpfpm_active_processes=0
27
+phpfpm_total_processes=0
28
+phpfpm_max_active_processes=0
29
+phpfpm_max_children_reached=0
30
+phpfpm_slow_requests=0
31
+phpfpm_get() {
32
+ phpfpm_response=($(curl -s "${phpfpm_url}"))
33
+ [ $? -ne 0 -o "${#phpfpm_response[@]}" -eq 0 ] && return 1
34
+
35
+ if [[ "${phpfpm_response[0]}" != "pool:" \
36
+ || "${phpfpm_response[2]}" != "process" \
37
+ || "${phpfpm_response[5]}" != "start" \
38
+ || "${phpfpm_response[12]}" != "accepted" \
39
+ || "${phpfpm_response[15]}" != "listen" \
40
+ || "${phpfpm_response[16]}" != "queue:" \
41
+ || "${phpfpm_response[26]}" != "idle" \
42
+ || "${phpfpm_response[29]}" != "active" \
43
+ || "${phpfpm_response[32]}" != "total" \
44
+ || "${phpfpm_response[43]}" != "slow" \
45
+ ]]
46
+ then
47
+ echo >&2 "phpfpm: invalid response from phpfpm status server: ${phpfpm_response[*]}"
48
+ return 1
49
+ fi
50
+
51
+ phpfpm_pool="${phpfpm_response[1]}"
52
+ phpfpm_start_time="${phpfpm_response[7]} ${phpfpm_response[8]}"
53
+ phpfpm_start_since="${phpfpm_response[11]}"
54
+ phpfpm_accepted_conn="${phpfpm_response[14]}"
55
+ phpfpm_listen_queue="${phpfpm_response[17]}"
56
+ phpfpm_max_listen_queue="${phpfpm_response[21]}"
57
+ phpfpm_listen_queue_len="${phpfpm_response[25]}"
58
+ phpfpm_idle_processes="${phpfpm_response[28]}"
59
+ phpfpm_active_processes="${phpfpm_response[31]}"
60
+ phpfpm_total_processes="${phpfpm_response[34]}"
61
+ phpfpm_max_active_processes="${phpfpm_response[38]}"
62
+ phpfpm_max_children_reached="${phpfpm_response[42]}"
63
+ phpfpm_slow_requests="${phpfpm_response[45]}"
64
+
65
+ if [[ -z "${phpfpm_pool}" \
66
+ || -z "${phpfpm_start_time}" \
67
+ || -z "${phpfpm_start_since}" \
68
+ || -z "${phpfpm_accepted_conn}" \
69
+ || -z "${phpfpm_listen_queue}" \
70
+ || -z "${phpfpm_max_listen_queue}" \
71
+ || -z "${phpfpm_listen_queue_len}" \
72
+ || -z "${phpfpm_idle_processes}" \
73
+ || -z "${phpfpm_active_processes}" \
74
+ || -z "${phpfpm_total_processes}" \
75
+ || -z "${phpfpm_max_active_processes}" \
76
+ || -z "${phpfpm_max_children_reached}" \
77
+ || -z "${phpfpm_slow_requests}" \
78
+ ]]
79
+ then
80
+ echo >&2 "phpfpm: empty values got from phpfpm status server: ${phpfpm_response[*]}"
81
+ return 1
82
+ fi
83
+
84
+ return 0
85
+}
86
+
87
+# _check is called once, to find out if this chart should be enabled or not
88
+phpfpm_check() {
89
+ phpfpm_get
90
+ if [ $? -ne 0 ]; then
91
+ echo >&2 "phpfpm: cannot find status on URL '${phpfpm_url}'. Please set phpfpm_url='http://nginx.server/php_status' in $confd/phpfpm.conf"
92
+ return 1
93
+ fi
94
+
95
+ # this should return:
96
+ # - 0 to enable the chart
97
+ # - 1 to disable the chart
98
+
99
+ return 0
100
+}
101
+
102
+# _create is called once, to create the charts
103
+phpfpm_create() {
104
+ cat <<EOF
105
+CHART phpfpm.connections '' "PHP-FPM Active Connections" "connections" phpfpm phpfpm.connections line $[phpfpm_priority + 1] $phpfpm_update_every
106
+DIMENSION active '' absolute 1 1
107
+DIMENSION maxActive 'max active' absolute 1 1
108
+DIMENSION idle '' absolute 1 1
109
+
110
+CHART phpfpm.requests '' "PHP-FPM Requests" "requests/s" phpfpm phpfpm.requests line $[phpfpm_priority + 2] $phpfpm_update_every
111
+DIMENSION requests '' incremental 1 1
112
+
113
+CHART phpfpm.performance '' "PHP-FPM Performance" "status" phpfpm phpfpm.performance line $[phpfpm_priority + 3] $phpfpm_update_every
114
+DIMENSION reached 'max children reached' absolute 1 1
115
+DIMENSION slow 'slow requests' absolute 1 1
116
+EOF
117
+
118
+ return 0
119
+}
120
+
121
+# _update is called continiously, to collect the values
122
+phpfpm_update() {
123
+ # the first argument to this function is the microseconds since last update
124
+ # pass this parameter to the BEGIN statement (see bellow).
125
+
126
+ # do all the work to collect / calculate the values
127
+ # for each dimension
128
+ # remember: KEEP IT SIMPLE AND SHORT
129
+
130
+ phpfpm_get || return 1
131
+
132
+ # write the result of the work.
133
+ cat <<EOF
134
+BEGIN phpfpm.connections $1
135
+SET active = $[phpfpm_active_processes]
136
+SET maxActive = $[phpfpm_max_active_processes]
137
+SET idle = $[phpfpm_idle_processes]
138
+END
139
+BEGIN phpfpm.requests $1
140
+SET requests = $[phpfpm_accepted_conn]
141
+END
142
+BEGIN phpfpm.performance $1
143
+SET reached = $[phpfpm_max_children_reached]
144
+SET slow = $[phpfpm_slow_requests]
145
+END
146
+EOF
147
+ return 0
148
+}