zabbix监控nginx(python脚本)

释放双眼,带上耳机,听听看~!

1 配置nginx的http_stub_status_module模块

  1. 编译nginx加上参数:–with-http_stub_status_module
  2. 配置nginx.conf:

location /nginx-status {
stub_status on;
access_log off;
allow 127.0.0.1;
allow 10.24.xx.xx;#可以多个
deny all;#可以用alow all; 不限制IP访问
}

  1. 使用 http://xxx.xx.x.x/nginx-status 访问:

Active connections: 1
server accepts handled requests
73 73 172
Reading: 0 Writing: 1 Waiting: 0

  1. 显示如上信息则成功

2 python脚本编写

脚本与nginx同一台服务器
python需安装requests模块

2.1 脚本(放在 /usr/local/zabbix/script)


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
1#!/usr/bin/python
2
3import re
4import requests
5import subprocess
6import sys
7import os
8
9from cStringIO import StringIO
10
11
12accepted_modes = ['all']
13if len(sys.argv) == 2 and sys.argv[1] in accepted_modes:
14    mode = sys.argv[1]
15else:
16    sys.exit(1)
17
18
19zabbix_sender = '/usr/local/zabbix/bin/zabbix_sender'
20zabbix_conf = '/usr/local/zabbix/etc/zabbix_agentd.conf'
21
22#keys
23keys=[
24'all'
25'accepts',
26'handled',
27'requests',
28'connections.active',
29'connections.reading',
30'connections.writing',
31'connections.waiting']
32
33
34def send_to_zabbix(metric, value):
35    key = "nginx.status[" +  metric + "]"
36    try:
37        subprocess.call([zabbix_sender, "-c", zabbix_conf, "-k", key, "-o", str(value) ], stdout=FNULL, stderr=FNULL, shell=False)
38    except OSError, detail:
39        print "Something went wrong while exectuting zabbix_sender : ", detail
40
41
42try:
43    r = requests.get(url='http://localhost:80/nginx-status')
44except Exception:
45    print "0"
46    sys.exit(1)
47
48# print(r.status_code)
49
50# print(r.text)
51
52isAlive = r.status_code
53print isAlive
54
55active = 0
56accepts = 0
57handled = 0
58requests = 0
59reading = 0
60writing = 0
61waiting = 0
62
63
64line = 1
65for s in StringIO(r.text):
66    # print (line, s)
67    if line == 1:
68        g = re.match(".+(\d+).+", s)
69        active = g.group(1)
70    if line == 3:
71        g = re.match(".+(\d+).+(\d+).+(\d+).+", s)
72        accepts = g.group(1)
73        handled = g.group(2)
74        requests = g.group(3)
75    if line == 4:
76        g = re.match(".+(\d+).+(\d+).+(\d+).+", s)
77        reading = g.group(1)
78        writing = g.group(2)
79        waiting = g.group(3)
80
81    line+=1
82
83# print(isAlive, active, accepts, handled, requests, reading, writing, waiting)
84values = [isAlive, active, accepts, handled, requests, reading, writing, waiting]
85
86
87# send to zabbix
88FNULL = open(os.devnull, 'w')
89for key,value in zip(keys,values):
90    # print key,value
91    send_to_zabbix(key, value)
92
93FNULL.close()
94

2.2 配置zabbix代理文件

  1. zabbix_agentd.conf新增key

UserParameter=nginx.status[*],/usr/bin/python /usr/local/zabbix/script/nginx_status.py $1

  1. 重启zabbix代理

killall -9 zabbix_agentd
/usr/local/zabbix/sbin/zabbix_agentd

3 Zabbix 的Template Nginx模版

3.1 导入如下模版


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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
1<?xml version="1.0" encoding="UTF-8"?>
2<zabbix_export>
3    <version>3.0</version>
4    <date>2016-11-09T03:56:39Z</date>
5    <groups>
6        <group>
7            <name>Templates</name>
8        </group>
9    </groups>
10    <templates>
11        <template>
12            <template>Template Nginx</template>
13            <name>Template Nginx</name>
14            <description/>
15            <groups>
16                <group>
17                    <name>Templates</name>
18                </group>
19            </groups>
20            <applications>
21                <application>
22                    <name>Nginx Status</name>
23                </application>
24            </applications>
25            <items>
26                <item>
27                    <name>Nginx Accepts</name>
28                    <type>2</type>
29                    <snmp_community/>
30                    <multiplier>0</multiplier>
31                    <snmp_oid/>
32                    <key>nginx.status[accepts]</key>
33                    <delay>0</delay>
34                    <history>90</history>
35                    <trends>365</trends>
36                    <status>0</status>
37                    <value_type>3</value_type>
38                    <allowed_hosts/>
39                    <units/>
40                    <delta>0</delta>
41                    <snmpv3_contextname/>
42                    <snmpv3_securityname/>
43                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
44                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
45                    <snmpv3_authpassphrase/>
46                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
47                    <snmpv3_privpassphrase/>
48                    <formula>1</formula>
49                    <delay_flex/>
50                    <params/>
51                    <ipmi_sensor/>
52                    <data_type>0</data_type>
53                    <authtype>0</authtype>
54                    <username/>
55                    <password/>
56                    <publickey/>
57                    <privatekey/>
58                    <port/>
59                    <description/>
60                    <inventory_link>0</inventory_link>
61                    <applications>
62                        <application>
63                            <name>Nginx Status</name>
64                        </application>
65                    </applications>
66                    <valuemap/>
67                    <logtimefmt/>
68                </item>
69                <item>
70                    <name>Nginx Alive</name>
71                    <type>0</type>
72                    <snmp_community/>
73                    <multiplier>0</multiplier>
74                    <snmp_oid/>
75                    <key>nginx.status[all]</key>
76                    <delay>10</delay>
77                    <history>90</history>
78                    <trends>365</trends>
79                    <status>0</status>
80                    <value_type>3</value_type>
81                    <allowed_hosts/>
82                    <units/>
83                    <delta>0</delta>
84                    <snmpv3_contextname/>
85                    <snmpv3_securityname/>
86                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
87                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
88                    <snmpv3_authpassphrase/>
89                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
90                    <snmpv3_privpassphrase/>
91                    <formula>1</formula>
92                    <delay_flex/>
93                    <params/>
94                    <ipmi_sensor/>
95                    <data_type>0</data_type>
96                    <authtype>0</authtype>
97                    <username/>
98                    <password/>
99                    <publickey/>
100                    <privatekey/>
101                    <port/>
102                    <description/>
103                    <inventory_link>0</inventory_link>
104                    <applications>
105                        <application>
106                            <name>Nginx Status</name>
107                        </application>
108                    </applications>
109                    <valuemap/>
110                    <logtimefmt/>
111                </item>
112                <item>
113                    <name>Nginx Connections Active</name>
114                    <type>2</type>
115                    <snmp_community/>
116                    <multiplier>0</multiplier>
117                    <snmp_oid/>
118                    <key>nginx.status[connections.active]</key>
119                    <delay>0</delay>
120                    <history>90</history>
121                    <trends>365</trends>
122                    <status>0</status>
123                    <value_type>3</value_type>
124                    <allowed_hosts/>
125                    <units/>
126                    <delta>0</delta>
127                    <snmpv3_contextname/>
128                    <snmpv3_securityname/>
129                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
130                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
131                    <snmpv3_authpassphrase/>
132                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
133                    <snmpv3_privpassphrase/>
134                    <formula>1</formula>
135                    <delay_flex/>
136                    <params/>
137                    <ipmi_sensor/>
138                    <data_type>0</data_type>
139                    <authtype>0</authtype>
140                    <username/>
141                    <password/>
142                    <publickey/>
143                    <privatekey/>
144                    <port/>
145                    <description/>
146                    <inventory_link>0</inventory_link>
147                    <applications>
148                        <application>
149                            <name>Nginx Status</name>
150                        </application>
151                    </applications>
152                    <valuemap/>
153                    <logtimefmt/>
154                </item>
155                <item>
156                    <name>Nginx Connections Reading</name>
157                    <type>2</type>
158                    <snmp_community/>
159                    <multiplier>0</multiplier>
160                    <snmp_oid/>
161                    <key>nginx.status[connections.reading]</key>
162                    <delay>0</delay>
163                    <history>90</history>
164                    <trends>365</trends>
165                    <status>0</status>
166                    <value_type>3</value_type>
167                    <allowed_hosts/>
168                    <units/>
169                    <delta>0</delta>
170                    <snmpv3_contextname/>
171                    <snmpv3_securityname/>
172                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
173                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
174                    <snmpv3_authpassphrase/>
175                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
176                    <snmpv3_privpassphrase/>
177                    <formula>1</formula>
178                    <delay_flex/>
179                    <params/>
180                    <ipmi_sensor/>
181                    <data_type>0</data_type>
182                    <authtype>0</authtype>
183                    <username/>
184                    <password/>
185                    <publickey/>
186                    <privatekey/>
187                    <port/>
188                    <description/>
189                    <inventory_link>0</inventory_link>
190                    <applications>
191                        <application>
192                            <name>Nginx Status</name>
193                        </application>
194                    </applications>
195                    <valuemap/>
196                    <logtimefmt/>
197                </item>
198                <item>
199                    <name>Nginx Connections Waiting</name>
200                    <type>2</type>
201                    <snmp_community/>
202                    <multiplier>0</multiplier>
203                    <snmp_oid/>
204                    <key>nginx.status[connections.waiting]</key>
205                    <delay>0</delay>
206                    <history>90</history>
207                    <trends>365</trends>
208                    <status>0</status>
209                    <value_type>3</value_type>
210                    <allowed_hosts/>
211                    <units/>
212                    <delta>0</delta>
213                    <snmpv3_contextname/>
214                    <snmpv3_securityname/>
215                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
216                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
217                    <snmpv3_authpassphrase/>
218                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
219                    <snmpv3_privpassphrase/>
220                    <formula>1</formula>
221                    <delay_flex/>
222                    <params/>
223                    <ipmi_sensor/>
224                    <data_type>0</data_type>
225                    <authtype>0</authtype>
226                    <username/>
227                    <password/>
228                    <publickey/>
229                    <privatekey/>
230                    <port/>
231                    <description/>
232                    <inventory_link>0</inventory_link>
233                    <applications>
234                        <application>
235                            <name>Nginx Status</name>
236                        </application>
237                    </applications>
238                    <valuemap/>
239                    <logtimefmt/>
240                </item>
241                <item>
242                    <name>Nginx Connections Writing</name>
243                    <type>2</type>
244                    <snmp_community/>
245                    <multiplier>0</multiplier>
246                    <snmp_oid/>
247                    <key>nginx.status[connections.writing]</key>
248                    <delay>0</delay>
249                    <history>90</history>
250                    <trends>365</trends>
251                    <status>0</status>
252                    <value_type>3</value_type>
253                    <allowed_hosts/>
254                    <units/>
255                    <delta>0</delta>
256                    <snmpv3_contextname/>
257                    <snmpv3_securityname/>
258                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
259                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
260                    <snmpv3_authpassphrase/>
261                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
262                    <snmpv3_privpassphrase/>
263                    <formula>1</formula>
264                    <delay_flex/>
265                    <params/>
266                    <ipmi_sensor/>
267                    <data_type>0</data_type>
268                    <authtype>0</authtype>
269                    <username/>
270                    <password/>
271                    <publickey/>
272                    <privatekey/>
273                    <port/>
274                    <description/>
275                    <inventory_link>0</inventory_link>
276                    <applications>
277                        <application>
278                            <name>Nginx Status</name>
279                        </application>
280                    </applications>
281                    <valuemap/>
282                    <logtimefmt/>
283                </item>
284                <item>
285                    <name>Nginx Handled</name>
286                    <type>2</type>
287                    <snmp_community/>
288                    <multiplier>0</multiplier>
289                    <snmp_oid/>
290                    <key>nginx.status[handled]</key>
291                    <delay>0</delay>
292                    <history>90</history>
293                    <trends>365</trends>
294                    <status>0</status>
295                    <value_type>3</value_type>
296                    <allowed_hosts/>
297                    <units/>
298                    <delta>0</delta>
299                    <snmpv3_contextname/>
300                    <snmpv3_securityname/>
301                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
302                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
303                    <snmpv3_authpassphrase/>
304                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
305                    <snmpv3_privpassphrase/>
306                    <formula>1</formula>
307                    <delay_flex/>
308                    <params/>
309                    <ipmi_sensor/>
310                    <data_type>0</data_type>
311                    <authtype>0</authtype>
312                    <username/>
313                    <password/>
314                    <publickey/>
315                    <privatekey/>
316                    <port/>
317                    <description/>
318                    <inventory_link>0</inventory_link>
319                    <applications>
320                        <application>
321                            <name>Nginx Status</name>
322                        </application>
323                    </applications>
324                    <valuemap/>
325                    <logtimefmt/>
326                </item>
327                <item>
328                    <name>Nginx Requests</name>
329                    <type>2</type>
330                    <snmp_community/>
331                    <multiplier>0</multiplier>
332                    <snmp_oid/>
333                    <key>nginx.status[requests]</key>
334                    <delay>0</delay>
335                    <history>90</history>
336                    <trends>365</trends>
337                    <status>0</status>
338                    <value_type>3</value_type>
339                    <allowed_hosts/>
340                    <units/>
341                    <delta>0</delta>
342                    <snmpv3_contextname/>
343                    <snmpv3_securityname/>
344                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
345                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
346                    <snmpv3_authpassphrase/>
347                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
348                    <snmpv3_privpassphrase/>
349                    <formula>1</formula>
350                    <delay_flex/>
351                    <params/>
352                    <ipmi_sensor/>
353                    <data_type>0</data_type>
354                    <authtype>0</authtype>
355                    <username/>
356                    <password/>
357                    <publickey/>
358                    <privatekey/>
359                    <port/>
360                    <description/>
361                    <inventory_link>0</inventory_link>
362                    <applications>
363                        <application>
364                            <name>Nginx Status</name>
365                        </application>
366                    </applications>
367                    <valuemap/>
368                    <logtimefmt/>
369                </item>
370            </items>
371            <discovery_rules/>
372            <macros/>
373            <templates/>
374            <screens/>
375        </template>
376    </templates>
377    <triggers>
378        <trigger>
379            <expression>{Template Nginx:nginx.status[all].last()}<>200</expression>
380            <name>Nginx is down on {HOST.NAME}</name>
381            <url/>
382            <status>0</status>
383            <priority>4</priority>
384            <description/>
385            <type>0</type>
386            <dependencies/>
387        </trigger>
388    </triggers>
389    <graphs>
390        <graph>
391            <name>Nginx Clients Status</name>
392            <width>900</width>
393            <height>200</height>
394            <yaxismin>0.0000</yaxismin>
395            <yaxismax>100.0000</yaxismax>
396            <show_work_period>1</show_work_period>
397            <show_triggers>1</show_triggers>
398            <type>0</type>
399            <show_legend>1</show_legend>
400            <show_3d>0</show_3d>
401            <percent_left>0.0000</percent_left>
402            <percent_right>0.0000</percent_right>
403            <ymin_type_1>0</ymin_type_1>
404            <ymax_type_1>0</ymax_type_1>
405            <ymin_item_1>0</ymin_item_1>
406            <ymax_item_1>0</ymax_item_1>
407            <graph_items>
408                <graph_item>
409                    <sortorder>0</sortorder>
410                    <drawtype>0</drawtype>
411                    <color>1A7C11</color>
412                    <yaxisside>0</yaxisside>
413                    <calc_fnc>2</calc_fnc>
414                    <type>0</type>
415                    <item>
416                        <host>Template Nginx</host>
417                        <key>nginx.status[connections.active]</key>
418                    </item>
419                </graph_item>
420                <graph_item>
421                    <sortorder>1</sortorder>
422                    <drawtype>0</drawtype>
423                    <color>F63100</color>
424                    <yaxisside>0</yaxisside>
425                    <calc_fnc>2</calc_fnc>
426                    <type>0</type>
427                    <item>
428                        <host>Template Nginx</host>
429                        <key>nginx.status[connections.reading]</key>
430                    </item>
431                </graph_item>
432                <graph_item>
433                    <sortorder>2</sortorder>
434                    <drawtype>0</drawtype>
435                    <color>2774A4</color>
436                    <yaxisside>0</yaxisside>
437                    <calc_fnc>2</calc_fnc>
438                    <type>0</type>
439                    <item>
440                        <host>Template Nginx</host>
441                        <key>nginx.status[connections.waiting]</key>
442                    </item>
443                </graph_item>
444                <graph_item>
445                    <sortorder>3</sortorder>
446                    <drawtype>0</drawtype>
447                    <color>A54F10</color>
448                    <yaxisside>0</yaxisside>
449                    <calc_fnc>2</calc_fnc>
450                    <type>0</type>
451                    <item>
452                        <host>Template Nginx</host>
453                        <key>nginx.status[connections.writing]</key>
454                    </item>
455                </graph_item>
456            </graph_items>
457        </graph>
458        <graph>
459            <name>Nginx Socket Status</name>
460            <width>900</width>
461            <height>200</height>
462            <yaxismin>0.0000</yaxismin>
463            <yaxismax>100.0000</yaxismax>
464            <show_work_period>1</show_work_period>
465            <show_triggers>1</show_triggers>
466            <type>0</type>
467            <show_legend>1</show_legend>
468            <show_3d>0</show_3d>
469            <percent_left>0.0000</percent_left>
470            <percent_right>0.0000</percent_right>
471            <ymin_type_1>0</ymin_type_1>
472            <ymax_type_1>0</ymax_type_1>
473            <ymin_item_1>0</ymin_item_1>
474            <ymax_item_1>0</ymax_item_1>
475            <graph_items>
476                <graph_item>
477                    <sortorder>0</sortorder>
478                    <drawtype>0</drawtype>
479                    <color>1A7C11</color>
480                    <yaxisside>0</yaxisside>
481                    <calc_fnc>2</calc_fnc>
482                    <type>0</type>
483                    <item>
484                        <host>Template Nginx</host>
485                        <key>nginx.status[accepts]</key>
486                    </item>
487                </graph_item>
488                <graph_item>
489                    <sortorder>1</sortorder>
490                    <drawtype>0</drawtype>
491                    <color>F63100</color>
492                    <yaxisside>0</yaxisside>
493                    <calc_fnc>2</calc_fnc>
494                    <type>0</type>
495                    <item>
496                        <host>Template Nginx</host>
497                        <key>nginx.status[handled]</key>
498                    </item>
499                </graph_item>
500                <graph_item>
501                    <sortorder>2</sortorder>
502                    <drawtype>0</drawtype>
503                    <color>2774A4</color>
504                    <yaxisside>0</yaxisside>
505                    <calc_fnc>2</calc_fnc>
506                    <type>0</type>
507                    <item>
508                        <host>Template Nginx</host>
509                        <key>nginx.status[requests]</key>
510                    </item>
511                </graph_item>
512            </graph_items>
513        </graph>
514    </graphs>
515</zabbix_export>
516
517

3.2 将模版应用到主机

监控nginx配置完毕

给TA打赏
共{{data.count}}人
人已打赏
安全技术

java千万级别数据生成文件思路和优化

2022-1-11 12:36:11

安全经验

Web应用何时成了金融行业的安全噩梦?

2016-12-25 0:41:00

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索