设为首页 收藏本站
查看: 948|回复: 0

[经验分享] zabbix-----2-----监控nginx的状态

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2017-3-22 09:24:33 | 显示全部楼层 |阅读模式
第一步:
在监控的电脑上安装nginx及状态模块:
yum install nginx.x86_64 nginx-all-modules.noarch
编辑nginx的配置文件:

vim /etc/nginx/nginx.conf
1
2
3
4
#关闭了ipv6
    server {
        listen       80 default_server;
#        listen       [::]:80 default_server;



#插入状态吗的location配置

1
2
3
4
5
6
7
        location /nginx_status {
                stub_status on;
                access_log off;
                allow 192.168.56.0/24;
                allow 127.0.0.1;
                deny all;
        }



启动nginx服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[iyunv@zabbix-node2 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[iyunv@zabbix-node2 ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1544/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2142/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1544/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2142/master         
[iyunv@zabbix-node2 ~]# nginx
[iyunv@zabbix-node2 ~]# !net
netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2850/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1544/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2142/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1544/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2142/master



下面要自定义一个conf文件,然后定义一个模板,然后倒入
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
[iyunv@zabbix-node2 zabbix_agentd.d]# vim nginx_status.conf
UserParameter=nginx_status
  • ,/etc/zabbix/zabbix_agentd.d/nginx_monitor.sh $1
    #上传脚本到指定的目录下
    如果默认放在这个配置文件下面会当成配置文件,所以这里会修改agent的配置文件:
    [iyunv@zabbix-node2 zabbix_agentd.d]# vim /etc/zabbix/zabbix_agentd.conf
    Include=/etc/zabbix/zabbix_agentd.d/*.conf #这里给配置文件添加*.conf 来指定以*.conf为配置文件
    [iyunv@zabbix-node2 zabbix_agentd.d]# cat nginx_monitor.sh
    #!/bin/bash
    NGINX_COMMAND=$1
    NGINX_PORT=80
    CACHEFILE="/tmp/nginx_status.txt"
    CMD="/usr/bin/curl http://127.0.0.1:"$NGINX_PORT"/nginx_status/"
    if [ ! -f $CACHEFILE  ];then
       $CMD >$CACHEFILE 2>/dev/null
    fi
    # Check and run the script
    TIMEFLM=`stat -c %Y $CACHEFILE`
    TIMENOW=`date +%s`
    if [ `expr $TIMENOW - $TIMEFLM` -gt 60 ]; then
        rm -f $CACHEFILE
    fi
    if [ ! -f $CACHEFILE  ];then
       $CMD >$CACHEFILE 2>/dev/null
    fi
    nginx_active(){
         grep 'Active' $CACHEFILE| awk '{print $NF}'
             exit 0;
    }
    nginx_reading(){
         grep 'Reading' $CACHEFILE| awk '{print $2}'
             exit 0;
    }
    nginx_writing(){
         grep 'Writing' $CACHEFILE | awk '{print $4}'
             exit 0;
    }
    nginx_waiting(){
         grep 'Waiting' $CACHEFILE| awk '{print $6}'
             exit 0;
    }
    nginx_accepts(){
         awk NR==3 $CACHEFILE| awk '{print $1}'
             exit 0;
    }
    nginx_handled(){
         awk NR==3 $CACHEFILE| awk '{print $2}'
             exit 0;
    }
    nginx_requests(){
         awk NR==3 $CACHEFILE| awk '{print $3}'
             exit 0;
    }
    case $NGINX_COMMAND in
    active)
    nginx_active;
    ;;
    reading)
    nginx_reading;
    ;;
    writing)
    nginx_writing;
    ;;
    waiting)
    nginx_waiting;
    ;;
    accepts)
    nginx_accepts;
    ;;
    handled)
    nginx_handled;
    ;;
    requests)
    nginx_requests;
    ;;
    *)
    echo 'Invalid credentials';
    exit 2;
    esac
    #给脚本授权
    [iyunv@zabbix-node2 zabbix_agentd.d]# chmod +x nginx_monitor.sh
    #上传xml模板文件文件
    root@zabbix-node2 zabbix_agentd.d]# cat zbx_nginx_templates.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <zabbix_export>
        <version>3.0</version>
        <date>2016-11-09T14:50:50Z</date>
        <groups>
            <group>
                <name>Templates</name>
            </group>
        </groups>
        <templates>
            <template>
                <template>Template Nginx Status</template>
                <name>Template Nginx Status</name>
                <description>Nginx监控模板</description>
                <groups>
                    <group>
                        <name>Templates</name>
                    </group>
                </groups>
                <applications>
                    <application>
                        <name>Nginx Status</name>
                    </application>
                </applications>
                <items>
                    <item>
                        <name>Nginx Status Accepts</name>
                        <type>0</type>
                        <snmp_community/>
                        <multiplier>0</multiplier>
                        <snmp_oid/>
                        <key>nginx_status[accepts]</key>
                        <delay>60</delay>
                        <history>90</history>
                        <trends>365</trends>
                        <status>0</status>
                        <value_type>3</value_type>
                        <allowed_hosts/>
                        <units/>
                        <delta>0</delta>
                        <snmpv3_contextname/>
                        <snmpv3_securityname/>
                        <snmpv3_securitylevel>0</snmpv3_securitylevel>
                        <snmpv3_authprotocol>0</snmpv3_authprotocol>
                        <snmpv3_authpassphrase/>
                        <snmpv3_privprotocol>0</snmpv3_privprotocol>
                        <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 Status</name>
                            </application>
                        </applications>
                        <valuemap/>
                        <logtimefmt/>
                    </item>
                    <item>
                        <name>Nginx Status Active</name>
                        <type>0</type>
                        <snmp_community/>
                        <multiplier>0</multiplier>
                        <snmp_oid/>
                        <key>nginx_status[active]</key>
                        <delay>60</delay>
                        <history>90</history>
                        <trends>365</trends>
                        <status>0</status>
                        <value_type>3</value_type>
                        <allowed_hosts/>
                        <units/>
                        <delta>0</delta>
                        <snmpv3_contextname/>
                        <snmpv3_securityname/>
                        <snmpv3_securitylevel>0</snmpv3_securitylevel>
                        <snmpv3_authprotocol>0</snmpv3_authprotocol>
                        <snmpv3_authpassphrase/>
                        <snmpv3_privprotocol>0</snmpv3_privprotocol>
                        <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 Status</name>
                            </application>
                        </applications>
                        <valuemap/>
                        <logtimefmt/>
                    </item>
                    <item>
                        <name>Nginx Status Handled</name>
                        <type>0</type>
                        <snmp_community/>
                        <multiplier>0</multiplier>
                        <snmp_oid/>
                        <key>nginx_status[handled]</key>
                        <delay>60</delay>
                        <history>90</history>
                        <trends>365</trends>
                        <status>0</status>
                        <value_type>3</value_type>
                        <allowed_hosts/>
                        <units/>
                        <delta>0</delta>
                        <snmpv3_contextname/>
                        <snmpv3_securityname/>
                        <snmpv3_securitylevel>0</snmpv3_securitylevel>
                        <snmpv3_authprotocol>0</snmpv3_authprotocol>
                        <snmpv3_authpassphrase/>
                        <snmpv3_privprotocol>0</snmpv3_privprotocol>
                        <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 Status</name>
                            </application>
                        </applications>
                        <valuemap/>
                        <logtimefmt/>
                    </item>
                    <item>
                        <name>Nginx Status Reading</name>
                        <type>0</type>
                        <snmp_community/>
                        <multiplier>0</multiplier>
                        <snmp_oid/>
                        <key>nginx_status[reading]</key>
                        <delay>60</delay>
                        <history>90</history>
                        <trends>365</trends>
                        <status>0</status>
                        <value_type>3</value_type>
                        <allowed_hosts/>
                        <units/>
                        <delta>0</delta>
                        <snmpv3_contextname/>
                        <snmpv3_securityname/>
                        <snmpv3_securitylevel>0</snmpv3_securitylevel>
                        <snmpv3_authprotocol>0</snmpv3_authprotocol>
                        <snmpv3_authpassphrase/>
                        <snmpv3_privprotocol>0</snmpv3_privprotocol>
                        <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 Status</name>
                            </application>
                        </applications>
                        <valuemap/>
                        <logtimefmt/>
                    </item>
                    <item>
                        <name>Nginx Status Requests</name>
                        <type>0</type>
                        <snmp_community/>
                        <multiplier>0</multiplier>
                        <snmp_oid/>
                        <key>nginx_status[requests]</key>
                        <delay>60</delay>
                        <history>90</history>
                        <trends>365</trends>
                        <status>0</status>
                        <value_type>3</value_type>
                        <allowed_hosts/>
                        <units/>
                        <delta>0</delta>
                        <snmpv3_contextname/>
                        <snmpv3_securityname/>
                        <snmpv3_securitylevel>0</snmpv3_securitylevel>
                        <snmpv3_authprotocol>0</snmpv3_authprotocol>
                        <snmpv3_authpassphrase/>
                        <snmpv3_privprotocol>0</snmpv3_privprotocol>
                        <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 Status</name>
                            </application>
                        </applications>
                        <valuemap/>
                        <logtimefmt/>
                    </item>
                    <item>
                        <name>Nginx Status Waiting</name>
                        <type>0</type>
                        <snmp_community/>
                        <multiplier>0</multiplier>
                        <snmp_oid/>
                        <key>nginx_status[waiting]</key>
                        <delay>60</delay>
                        <history>90</history>
                        <trends>365</trends>
                        <status>0</status>
                        <value_type>3</value_type>
                        <allowed_hosts/>
                        <units/>
                        <delta>0</delta>
                        <snmpv3_contextname/>
                        <snmpv3_securityname/>
                        <snmpv3_securitylevel>0</snmpv3_securitylevel>
                        <snmpv3_authprotocol>0</snmpv3_authprotocol>
                        <snmpv3_authpassphrase/>
                        <snmpv3_privprotocol>0</snmpv3_privprotocol>
                        <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 Status</name>
                            </application>
                        </applications>
                        <valuemap/>
                        <logtimefmt/>
                    </item>
                    <item>
                        <name>Nginx Status Writing</name>
                        <type>0</type>
                        <snmp_community/>
                        <multiplier>0</multiplier>
                        <snmp_oid/>
                        <key>nginx_status[writing]</key>
                        <delay>60</delay>
                        <history>90</history>
                        <trends>365</trends>
                        <status>0</status>
                        <value_type>3</value_type>
                        <allowed_hosts/>
                        <units/>
                        <delta>0</delta>
                        <snmpv3_contextname/>
                        <snmpv3_securityname/>
                        <snmpv3_securitylevel>0</snmpv3_securitylevel>
                        <snmpv3_authprotocol>0</snmpv3_authprotocol>
                        <snmpv3_authpassphrase/>
                        <snmpv3_privprotocol>0</snmpv3_privprotocol>
                        <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 Status</name>
                            </application>
                        </applications>
                        <valuemap/>
                        <logtimefmt/>
                    </item>
                </items>
                <discovery_rules/>
                <macros/>
                <templates/>
                <screens/>
            </template>
        </templates>
        <graphs>
            <graph>
                <name>Nginx Clients Status</name>
                <width>900</width>
                <height>200</height>
                <yaxismin>0.0000</yaxismin>
                <yaxismax>100.0000</yaxismax>
                <show_work_period>1</show_work_period>
                <show_triggers>1</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>0</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>0</drawtype>
                        <color>1A7C11</color>
                        <yaxisside>0</yaxisside>
                        <calc_fnc>2</calc_fnc>
                        <type>0</type>
                        <item>
                            <host>Template Nginx Status</host>
                            <key>nginx_status[active]</key>
                        </item>
                    </graph_item>
                    <graph_item>
                        <sortorder>1</sortorder>
                        <drawtype>0</drawtype>
                        <color>F63100</color>
                        <yaxisside>0</yaxisside>
                        <calc_fnc>2</calc_fnc>
                        <type>0</type>
                        <item>
                            <host>Template Nginx Status</host>
                            <key>nginx_status[reading]</key>
                        </item>
                    </graph_item>
                    <graph_item>
                        <sortorder>2</sortorder>
                        <drawtype>0</drawtype>
                        <color>2774A4</color>
                        <yaxisside>0</yaxisside>
                        <calc_fnc>2</calc_fnc>
                        <type>0</type>
                        <item>
                            <host>Template Nginx Status</host>
                            <key>nginx_status[waiting]</key>
                        </item>
                    </graph_item>
                    <graph_item>
                        <sortorder>3</sortorder>
                        <drawtype>0</drawtype>
                        <color>A54F10</color>
                        <yaxisside>0</yaxisside>
                        <calc_fnc>2</calc_fnc>
                        <type>0</type>
                        <item>
                            <host>Template Nginx Status</host>
                            <key>nginx_status[writing]</key>
                        </item>
                    </graph_item>
                </graph_items>
            </graph>
        </graphs>
    </zabbix_export>
    [iyunv@zabbix-node2 zabbix_agentd.d]#



  • 导入模板

    QQ截图20170322092320.png
    QQ截图20170322092327.png
    QQ截图20170322092334.png

    下面我们新创建一个主机
    QQ截图20170322092338.png wKiom1jQzbnitJz6AABE8UdHcvI315.png
    验证

    QQ截图20170322092353.png


    运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
    2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
    3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
    4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
    5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
    6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
    7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
    8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

    所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-353351-1-1.html 上篇帖子: zabbix snmp 常见OID 下篇帖子: zabbix监控hadoop进程 监控
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    扫码加入运维网微信交流群X

    扫码加入运维网微信交流群

    扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

    扫描微信二维码查看详情

    客服E-mail:kefu@iyunv.com 客服QQ:1061981298


    QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


    提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


    本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



    合作伙伴: 青云cloud

    快速回复 返回顶部 返回列表