1.在要需要监控nginx的节点上安装nginx
nginx安装 看这里 传送门
唯一不同的是,此次安装,需要多安装一个模块
1 2 |
./configure --help | grep status --with-http_stub_status_module enable ngx_http_stub_status_module |
1 2 |
执行如下编译安装 ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module |
安装完成后
接下来就是开启stub_status_module模块,启动监测功能
2.开启 stub_status_module模块
1 |
vim /usr/local/nginx/conf/nginx.conf |
1 2 3 4 5 6 7 8 9 |
在server中添加 server { location /nginx-status{ stub_status on; } } |

然后保存退出,然后重启nginx
1 |
nginx -s reload |
然后重载一下nginx下的pid
1 |
kill -HUP $(cat /usr/local/nginx/logs/nginx.pid) |
然后我们访问我们刚指定的监控页面,nginx-status(我配置了2个节点的nginx)
1 2 |
192.168.66.21/nginx-status 192.168.66.22/nginx-status |

然后我们通过脚本抓取,这个页面的nginx的连接数
脚本内容如下:
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 |
#!/bin/bash HOST="127.0.0.1" PORT="80" function ping { /sbin/pidof nginx | wc -l } # 检测 nginx 性能 function active { /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| grep 'Active' | awk '{print $NF}' } function reading { /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| grep 'Reading' | awk '{print $2}' } function writing { /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| grep 'Writing' | awk '{print $4}' } function waiting { /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}' } function accepts { /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| awk NR==3 | awk '{print $1}' } function handled { /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| awk NR==3 | awk '{print $2}' } function requests { /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| awk NR==3 | awk '{print $3}' } #执行 function $1 |
保存后执行
1 2 3 4 |
./getx.sh active #active为声明的函数 [root@k8s-node1 .local]# ./status-nginx.sh active 1 #说明只有一个进程 |
然后我们去server(20)机器 写一个循环访问
1 2 3 4 |
while 2>1 do curl 192.168.66.21 done |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[root@k8s-node1 .local]# bash status-nginx.sh requests 1547 [root@k8s-node1 .local]# bash status-nginx.sh requests 1692 [root@k8s-node1 .local]# bash status-nginx.sh requests 1986 [root@k8s-node1 .local]# bash status-nginx.sh requests 2030 [root@k8s-node1 .local]# bash status-nginx.sh requests 2084 [root@k8s-node1 .local]# bash status-nginx.sh requests 2118 [root@k8s-node1 .local]# #可以看到请求数量越来越多 |
接下来我们去配置agent端能监测到我们这个脚本
1 2 |
我们将我们的脚本文件放到/etc/zabbix/zabbix_agentd.d/ 下 mv status-nginx.sh /etc/zabbix/zabbix_agentd.d/ |
然后打开vim /etc/zabbix/zabbix_agentd.conf 配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#是否开启自定义脚本? 修改UnsafeUserParameters=1 #下面是我们的格式 #Format: UserParameter=<key>,<shell command> #key:代表调取的函数名称 #shell command:代表脚本 修改 UserParameter=nginx.status[*],/etc/zabbix/zabbix_agentd.d/status-nginx.sh.sh $1 $1:是位参的意思 保存退出 然后重启下 agent systemctl restart zabbix-agent |

保存退出 然后重启下 agent
1 |
systemctl restart zabbix-agent |
这边脚本配置完了,但是我还不知道,agent与脚本进行联系,所以要安装一个软件叫,zabbix-get 我们可以通过他去测试
1 2 3 4 |
#这个只能装在服务端,客户端无法使用 注意,切换到server端 yum install -y zabbix-get 这里可能下载不了,直接从外部官网下载好rpm包,进行安装 yum install -y zabbix-get-3.2.1-1.el7.x86_64.rpm |
1 2 3 4 5 6 7 |
#安装完成后,进行测试 -s 指定客户端地址 -k 指定函数名 zabbix_get -s 192.168.66.21 -k 'nginx.status[accepts]' zabbix_get -s 192.168.66.21 -k 'nginx.status[ping]' 5 zabbix_get -s 192.168.66.21 -k 'nginx.status[requests]' |
测试无问题后,然后我们去web界面制作监控
1 2 3 |
注:点击主机列表,监控那台机器的,监控项,你会看到很多监控项(这里都是系统定义的),但是这里没有nginx的监控项。我们需要导入nginx的监控项 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 518 |
<?xml version="1.0" encoding="UTF-8"?> <zabbix_export> <version>3.0</version> <date>2015-10-25T01:29:50Z</date> <groups> <group> <name>Templates</name> </group> </groups> <templates> <template> <template>Template App NGINX</template> <name>Template App NGINX</name> <description>nginx监控模板 - create by 凉白开(www.ttlsa.com)</description> <groups> <group> <name>Templates</name> </group> </groups> <applications> <application> <name>nginx</name> </application> </applications> <items> <item> <name>nginx status connections 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>active</description> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap/> <logtimefmt/> </item> <item> <name>nginx status connections 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>reading</description> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap/> <logtimefmt/> </item> <item> <name>nginx status connections 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>waiting</description> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap/> <logtimefmt/> </item> <item> <name>nginx status connections 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>writing</description> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap/> <logtimefmt/> </item> <item> <name>nginx status PING</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>nginx.status[ping]</key> <delay>60</delay> <history>30</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>is live</description> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap> <name>Service state</name> </valuemap> <logtimefmt/> </item> <item> <name>nginx status server 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>1</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>accepts</description> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap/> <logtimefmt/> </item> <item> <name>nginx status server 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>1</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>handled</description> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap/> <logtimefmt/> </item> <item> <name>nginx status server 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>1</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>requests</description> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap/> <logtimefmt/> </item> </items> <discovery_rules/> <macros/> <templates/> <screens/> </template> </templates> <triggers> <trigger> <expression>{Template App NGINX:nginx.status[ping].last()}=0</expression> <name>nginx was down!</name> <url/> <status>0</status> <priority>4</priority> <description>NGINX进程数:0,请注意</description> <type>0</type> <dependencies/> </trigger> </triggers> <graphs> <graph> <name>nginx status connections</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>00C800</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template App NGINX</host> <key>nginx.status[active]</key> </item> </graph_item> <graph_item> <sortorder>1</sortorder> <drawtype>0</drawtype> <color>C80000</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template App NGINX</host> <key>nginx.status[reading]</key> </item> </graph_item> <graph_item> <sortorder>2</sortorder> <drawtype>0</drawtype> <color>0000C8</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template App NGINX</host> <key>nginx.status[waiting]</key> </item> </graph_item> <graph_item> <sortorder>3</sortorder> <drawtype>0</drawtype> <color>C800C8</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template App NGINX</host> <key>nginx.status[writing]</key> </item> </graph_item> </graph_items> </graph> <graph> <name>nginx status server</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>00C800</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template App NGINX</host> <key>nginx.status[accepts]</key> </item> </graph_item> <graph_item> <sortorder>1</sortorder> <drawtype>0</drawtype> <color>C80000</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template App NGINX</host> <key>nginx.status[handled]</key> </item> </graph_item> <graph_item> <sortorder>2</sortorder> <drawtype>0</drawtype> <color>0000C8</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template App NGINX</host> <key>nginx.status[requests]</key> </item> </graph_item> </graph_items> </graph> </graphs> </zabbix_export> |
1 |
导入完成后,我们再点击模板,可以看到了nginx模板,点击进入我们可以看到,我们脚本中定义的函数 |


然后你可以新添加这台机器,也可以修改原来机器,在监控模板里勾上 Template App NGINX

然后再次执行 循环访问的脚本
1 2 3 4 5 |
while 2>1 do curl 192.168.66.21 sleep 1 done |
然后查看监控图表

- 本文固定链接: https://www.yoyoask.com/?p=2647
- 转载请注明: shooter 于 SHOOTER 发表