来源URL: http://liqingbiao.blog.51cto.com/3044896/1894902?b4
1
2
3
4
5
6
7
|
[root@iZ237lzm354Z scripts]
# /usr/local/nginx/sbin/nginx -V
nginx version: nginx
/1
.4.7
built by gcc 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC)
TLS SNI support enabled
configure arguments: --with-http_stub_status_module --with-http_ssl_module --with-pcre
--with-http_realip_module --with-http_image_filter_module
[root@iZ237lzm354Z scripts]
#
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@iZ237lzm354Z scripts]
# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80 ;
server_name www.guojinbao.com;
rewrite ^
/invitejoin/
(.*)\.htm[l]?$
/register
.shtml?$1 last;
index index.jsp index.html;
root
/opt/home
;
location =
/nginx-status
{
stub_status on;
access_log off;
allow 127.0.0.1;
allow 10.253.12.34;
####zabbix服务器端的IP地址一般为内网IP
}
|
1
2
3
4
5
|
[root@iZ237lzm354Z scripts]
# curl www.guojinbao.com/nginx-status
Active connections: 979
server accepts handled requests
756072922 756072922 1136799890
Reading: 0 Writing: 4 Waiting: 975
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
[root@ittestserver1 opt]
# vim /usr/local/zabbix/scripts/nginx-check_performance.sh
#!/bin/bash
##################################
# Zabbix monitoring script
#
# nginx:
# - anything available via nginx stub-status module
#
##################################
# Contact:
# vincent.viallet@gmail.com
# Zabbix requested parameter
ZBX_REQ_DATA=
"$1"
ZBX_REQ_DATA_URL=
"$2"
# Nginx defaults
NGINX_STATUS_DEFAULT_URL=
"www.guojinbao.com/nginx-status"
#(这里写网站的域名)
WGET_BIN=
"/usr/bin/wget"
#
# Error handling:
# - need to be displayable in Zabbix (avoid NOT_SUPPORTED)
# - items need to be of type "float" (allow negative + float)
#
ERROR_NO_ACCESS_FILE=
"-0.9900"
ERROR_NO_ACCESS=
"-0.9901"
ERROR_WRONG_PARAM=
"-0.9902"
ERROR_DATA=
"-0.9903"
# either can not connect / bad host / bad port
# Handle host and port if non-default
if
[ ! -z
"$ZBX_REQ_DATA_URL"
];
then
URL=
"$ZBX_REQ_DATA_URL"
else
URL=
"$NGINX_STATUS_DEFAULT_URL"
fi
# save the nginx stats in a variable for future parsing
NGINX_STATS=$($WGET_BIN -q $URL -O - 2>
/dev/null
)
# error during retrieve
if
[ $? -
ne
0 -o -z
"$NGINX_STATS"
];
then
echo
$ERROR_DATA
exit
1
fi
#
# Extract data from nginx stats
#
case
$ZBX_REQ_DATA
in
active_connections)
echo
"$NGINX_STATS"
|
head
-1 |
cut
-f3 -d
' '
;;
accepted_connections)
echo
"$NGINX_STATS"
|
grep
-Ev
'[a-zA-Z]'
|
cut
-f2 -d
' '
;;
handled_connections)
echo
"$NGINX_STATS"
|
grep
-Ev
'[a-zA-Z]'
|
cut
-f3 -d
' '
;;
handled_requests)
echo
"$NGINX_STATS"
|
grep
-Ev
'[a-zA-Z]'
|
cut
-f4 -d
' '
;;
reading)
echo
"$NGINX_STATS"
|
tail
-1 |
cut
-f2 -d
' '
;;
writing)
echo
"$NGINX_STATS"
|
tail
-1 |
cut
-f4 -d
' '
;;
waiting)
echo
"$NGINX_STATS"
|
tail
-1 |
cut
-f6 -d
' '
;;
*)
echo
$ERROR_WRONG_PARAM;
exit
1;;
esac
exit
0
[root@ittestserver1 opt]
# chmod +x /usr/local/zabbix/scripts/nginx-check_performance.sh
-rw-r--r-x1 root root 1645 2月 4 14:26
/usr/local/zabbix/scripts/nginx-check_performance
.sh
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
[root@ittestserver1 opt]
# vim /usr/local/zabbix/etc/zabbix_agentd.conf
####### USER-DEFINED MONITORED PARAMETERS #######
### Option: UnsafeUserParameters
# Allow all characters to be passed in arguments to user-defined parameters.
# The following characters are not allowed:
# \ ' " ` * ? [ ] { } ~ $ ! & ; ( ) < > | # @
# Additionally, newline characters are not allowed.
# 0 - do not allow
# 1 - allow
#
# Mandatory: no
# Range: 0-1
# Default:
# UnsafeUserParameters=0
UnsafeUserParameters=1
### Option: UserParameter
# User-defined parameter to monitor. There can be several user-defined parameters.
# Format: UserParameter=<key>,<shell command>
# See 'zabbix_agentd' directory for examples.
#
# Mandatory: no
# Default:
# UserParameter=
UserParameter=nginx[*],
/usr/local/zabbix/scripts/nginx-check_performance
.sh
"$1"
|
1
2
3
4
|
[root@zabbix ~]
# /etc/init.d/zabbix_agentd restart
Shutting down zabbix_agentd: [ OK ]
Starting zabbix_agentd: [ OK ]
[root@zabbix ~]
#
|
1
2
3
|
[root@zabbix ~]
# zabbix_get -s 10.253.17.20 -p 10050 -k "nginx[reading]"
0
[root@zabbix ~]
#
|
附件内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
|
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<zabbix_export>
<version>2.0</version>
<date>2013-03-26T04:17:58Z</date>
<groups>
<group>
<name>Templates</name>
</group>
</groups>
<templates>
<template>
<template>Template_Nginx</template>
<name>Template_Nginx</name>
<groups>
<group>
<name>Templates</name>
</group>
</groups>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<items>
<item>
<name>Nginx $1</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>nginx[waiting,{$NGINX_STATUS_URL}]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>0</value_type>
<allowed_hosts/>
<units/>
<delta>0</delta>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authpassphrase/>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<valuemap/>
</item>
<item>
<name>Nginx $1</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>nginx[writing,{$NGINX_STATUS_URL}]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>0</value_type>
<allowed_hosts/>
<units/>
<delta>0</delta>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authpassphrase/>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<valuemap/>
</item>
<item>
<name>Nginx $1</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>nginx[active_connections,{$NGINX_STATUS_URL}]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>0</value_type>
<allowed_hosts/>
<units/>
<delta>0</delta>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authpassphrase/>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<valuemap/>
</item>
<item>
<name>Nginx $1</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>nginx[reading,{$NGINX_STATUS_URL}]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>0</value_type>
<allowed_hosts/>
<units/>
<delta>0</delta>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authpassphrase/>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<valuemap/>
</item>
<item>
<name>Nginx $1/sec</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>nginx[handled_requests,{$NGINX_STATUS_URL}]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>0</value_type>
<allowed_hosts/>
<units/>
<delta>1</delta>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authpassphrase/>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<valuemap/>
</item>
<item>
<name>Nginx $1/sec</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>nginx[accepted_connections,{$NGINX_STATUS_URL}]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>0</value_type>
<allowed_hosts/>
<units/>
<delta>1</delta>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authpassphrase/>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<valuemap/>
</item>
<item>
<name>Nginx $1/sec</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>nginx[handled_connections,{$NGINX_STATUS_URL}]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>0</value_type>
<allowed_hosts/>
<units/>
<delta>1</delta>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authpassphrase/>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<valuemap/>
</item>
<item>
<name>Number of $1 process</name>
<type>0</type>
<snmp_community>public</snmp_community>
<multiplier>0</multiplier>
<snmp_oid>interfaces.ifTable.ifEntry.ifInOctets.1</snmp_oid>
<key>proc.num[nginx]</key>
<delay>300</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units/>
<delta>0</delta>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authpassphrase/>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications/>
<valuemap/>
</item>
</items>
<discovery_rules/>
<macros>
<macro>
<macro>{$NGINX_STATUS_URL}</macro>
<value>http:
//127.0.0.1:10061/nginx_status</value>
</macro>
</macros>
<templates/>
<screens/>
</template>
</templates>
<triggers>
<trigger>
<expression>{Template_Nginx:proc.num[nginx].last(0)}=0</expression>
<name>Nginx is not running on {HOSTNAME}</name>
<url/>
<status>0</status>
<priority>4</priority>
<description>Nginx is not running.&
#13;
&
#13;
It has been stopped / shutdown or has crashed. &
#13;
Check on the server
for
more details:&
#13;
- w / last&
#13;
- dmesg logs&
#13;
- /
var
/log/messages&
#13;
- nginx error logs</description>
<type>0</type>
<dependencies/>
</trigger>
</triggers>
<graphs>
<graph>
<name>Nginx - Connections and Requests status</name>
<width>900</width>
<height>200</height>
<yaxismin>0.0000</yaxismin>
<yaxismax>100.0000</yaxismax>
<show_work_period>0</show_work_period>
<show_triggers>0</show_triggers>
<type>0</type>
<show_legend>1</show_legend>
<show_3d>0</show_3d>
<percent_left>0.0000</percent_left>
<percent_right>0.0000</percent_right>
<ymin_type_1>1</ymin_type_1>
<ymax_type_1>0</ymax_type_1>
<ymin_item_1>0</ymin_item_1>
<ymax_item_1>0</ymax_item_1>
<graph_items>
<graph_item>
<sortorder>0</sortorder>
<drawtype>1</drawtype>
<color>FF9999</color>
<yaxisside>0</yaxisside>
<calc_fnc>4</calc_fnc>
<type>0</type>
<item>
<host>Template_Nginx</host>
<key>nginx[accepted_connections,{$NGINX_STATUS_URL}]</key>
</item>
</graph_item>
<graph_item>
<sortorder>1</sortorder>
<drawtype>2</drawtype>
<color>990000</color>
<yaxisside>0</yaxisside>
<calc_fnc>4</calc_fnc>
<type>0</type>
<item>
<host>Template_Nginx</host>
<key>nginx[handled_connections,{$NGINX_STATUS_URL}]</key>
</item>
</graph_item>
<graph_item>
<sortorder>2</sortorder>
<drawtype>0</drawtype>
<color>009900</color>
<yaxisside>0</yaxisside>
<calc_fnc>4</calc_fnc>
<type>0</type>
<item>
<host>Template_Nginx</host>
<key>nginx[handled_requests,{$NGINX_STATUS_URL}]</key>
</item>
</graph_item>
</graph_items>
</graph>
<graph>
<name>Nginx - Threads status</name>
<width>900</width>
<height>200</height>
<yaxismin>0.0000</yaxismin>
<yaxismax>100.0000</yaxismax>
<show_work_period>0</show_work_period>
<show_triggers>0</show_triggers>
<type>1</type>
<show_legend>1</show_legend>
<show_3d>0</show_3d>
<percent_left>0.0000</percent_left>
<percent_right>0.0000</percent_right>
<ymin_type_1>1</ymin_type_1>
<ymax_type_1>0</ymax_type_1>
<ymin_item_1>0</ymin_item_1>
<ymax_item_1>0</ymax_item_1>
<graph_items>
<graph_item>
<sortorder>0</sortorder>
<drawtype>1</drawtype>
<color>990000</color>
<yaxisside>0</yaxisside>
<calc_fnc>4</calc_fnc>
<type>0</type>
<item>
<host>Template_Nginx</host>
<key>nginx[writing,{$NGINX_STATUS_URL}]</key>
</item>
</graph_item>
<graph_item>
<sortorder>1</sortorder>
<drawtype>1</drawtype>
<color>999900</color>
<yaxisside>0</yaxisside>
<calc_fnc>4</calc_fnc>
<type>0</type>
<item>
<host>Template_Nginx</host>
<key>nginx[reading,{$NGINX_STATUS_URL}]</key>
</item>
</graph_item>
<graph_item>
<sortorder>2</sortorder>
<drawtype>1</drawtype>
<color>009900</color>
<yaxisside>0</yaxisside>
<calc_fnc>4</calc_fnc>
<type>0</type>
<item>
<host>Template_Nginx</host>
<key>nginx[waiting,{$NGINX_STATUS_URL}]</key>
</item>
</graph_item>
</graph_items>
</graph>
</graphs>
</zabbix_export>
|