OpsDev挨踢(IT)

二、zabbix之监控nginx并实现自动报警,故障自治愈

2019-07-21  本文已影响121人  M36_tongwei

监控nginx(proxy主动模式)

root@node1:~# apt -y install nginx
root@node1:~# cd /etc/nginx/sites-enabled/
root@node1:/etc/nginx/sites-enabled# ls
default
root@node1:/etc/nginx/sites-enabled# vim default 

        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }
        location /nginx_status {   注:加上状态页配置
                stub_status;
        }


oot@node1:/etc/nginx/sites-enabled# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@node1:/etc/nginx/sites-enabled# systemctl restart nginx

image.png
image.png

写脚本

root@node1:~# cd /etc/zabbix/zabbix_agentd.d/

root@node1:/etc/zabbix/zabbix_agentd.d# vim nginx_status.sh 

#!/bin/bash 
  
nginx_status_fun(){ #函数内容
        NGINX_PORT=$1 #端口,函数的第一个参数是脚本的第二个参数,即脚本的第二个参数是段端口号
        NGINX_COMMAND=$2 #命令,函数的第二个参数是脚本的第三个参数,即脚本的第三个参数是命令
        nginx_active(){ #获取nginx_active数量,以下相同,这是开启了nginx状态但是只能从本机看到
        /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
        }
        nginx_reading(){ #获取nginx_reading状态的数量
        /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
       }
        nginx_writing(){
        /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
       }
        nginx_waiting(){
        /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
       }
        nginx_accepts(){
        /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $1}'
       }
        nginx_handled(){
        /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $2}'
       }
        nginx_requests(){
        /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $3}'
       }
        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;
                esac
}

main(){ #主函数内容
        case $1 in #分支结构,用于判断用户的输入而进行响应的操作
                nginx_status) #当输入nginx_status就调用nginx_status_fun,并传递第二和第三个参数
                        nginx_status_fun $2 $3;
                        ;;
                *) #其他的输入打印帮助信息
                        echo $"Usage: $0 {nginx_status key}"
        esac #分支结束符
}

main $1 $2 $3

root@node1:/etc/zabbix/zabbix_agentd.d# /usr/bin/curl "http://127.0.0.1:80/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $1}'
7

root@node1:/etc/zabbix/zabbix_agentd.d# chmod +x nginx_status.sh      

测试
root@node1:/etc/zabbix/zabbix_agentd.d# ./nginx_status.sh nginx_status 80 active
1
root@node1:/etc/zabbix/zabbix_agentd.d# ./nginx_status.sh nginx_status 80 reading
0                                                                                  

在/etc/zabbix/zabbix_agentd.d/目录下写一个zabbix的配置文件

nginx_status.sh  tcp_conn.sh  zabbix_agent_linux36.conf
root@node1:/etc/zabbix/zabbix_agentd.d# vim zabbix_agent_linux36.conf 

UserParameter=memcached_status[*],/etc/zabbix/zabbix_agentd.d/memcached.sh $1 $2 $3
UserParameter=redis_status[*],/etc/zabbix/zabbix_agentd.d/redis.sh $1 $2 $3
UserParameter=nginx.status[*],/etc/zabbix/zabbix_agentd.d/nginx_status.sh $1 $2 $3  

root@node1:/etc/zabbix/zabbix_agentd.d# systemctl restart zabbix-agent
root@Zabbix:~# zabbix_get -s 192.168.18.112 -p 10050 -k "nginx.status[nginx_status,80,active]"
1
root@proxy:~# zabbix_get -s 192.168.18.112 -p 10050 -k "nginx.status[nginx_status,80,active]"
1

image.png

将获取的授权码就是下图中的报警媒介类型中的用户密码


image.png image.png

给用户添加报警媒介


image.png image.png
image.png
image.png

创建报警动作


image.png
image.png image.png image.png image.png image.png

刚才添加的nginx模板,有一个监控项是80端口,如果nginx服务运行状态,则有80端口,如果服务停止了,就没有80监听端口,就触发报警动作,接着就一定时间内发送邮件(上面定义的报警动作邮件)



停止nginx服务,看是否能导致触发器触发,接着发邮件通知到指定用户组里的人

root@node1:/etc/zabbix/zabbix_agentd.d# systemctl stop nginx
停止
image.png

也可以自定义邮件报警信息

root@node1:/etc/zabbix/zabbix_agentd.d# systemctl stop nginx
image.png
image.png image.png
image.png

重启服务

root@node1:/etc/zabbix/zabbix_agentd.d# systemctl start nginx
image.png

注:还可以实现二级报警,邮件发送给更高级别的管理人员,如果1-3分钟还没有解决问题,就发送给更高级别人物,发送给新的用户


image.png

修改配置文件,允许所在主机zabbix agent自行命令,将2个选项打开

vim /etc/zabbix/zabbix_agentd.conf

root@node1:~# grep ^[a-Z] /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
EnableRemoteCommands=1  注选项一:允许执行远程服务器zabbix server的远程命令
Server=192.168.18.111,192.168.18.115
ServerActive=192.168.18.115
Hostname=192.168.18.112
Include=/etc/zabbix/zabbix_agentd.d/*.conf
UnsafeUserParameters=1   注选项二:允许将所有字符(包括特殊字符)作为参数传递给用户定义的参数

重启服务
root@node1:~# systemctl restart zabbix-agent

设置zabbix-agent拥有root的所有权限

root@node1:~# vim /etc/sudoers
...
root    ALL=(ALL:ALL) ALL

zabbix  ALL=(ALL) NOPASSWD: ALL
...

测试

root@node1:~# systemctl stop nginx
root@node1:~# ss -tnl
State      Recv-Q      Send-Q            Local Address:Port            Peer Address:Port      
LISTEN     0           128                     0.0.0.0:10050                0.0.0.0:*         
LISTEN     0           128                     0.0.0.0:11211                0.0.0.0:*         
LISTEN     0           128                     0.0.0.0:6379                 0.0.0.0:*         
LISTEN     0           128               127.0.0.53%lo:53                   0.0.0.0:*         
LISTEN     0           128                     0.0.0.0:22                   0.0.0.0:*         
LISTEN     0           128                   127.0.0.1:6010                 0.0.0.0:*         
LISTEN     0           128                        [::]:10050                   [::]:*         
LISTEN     0           128                        [::]:22                      [::]:*         
LISTEN     0           128                       [::1]:6010                    [::]:*
image.png
image.png

表明nginx自恢复了,80端口重新启动了

root@node1:~# ss -tnl
State      Recv-Q      Send-Q            Local Address:Port            Peer Address:Port      
LISTEN     0           128                     0.0.0.0:10050                0.0.0.0:*         
LISTEN     0           128                     0.0.0.0:11211                0.0.0.0:*         
LISTEN     0           128                     0.0.0.0:6379                 0.0.0.0:*         
LISTEN     0           128                     0.0.0.0:80                   0.0.0.0:*         
LISTEN     0           128               127.0.0.53%lo:53                   0.0.0.0:*         
LISTEN     0           128                     0.0.0.0:22                   0.0.0.0:*         
LISTEN     0           128                   127.0.0.1:6010                 0.0.0.0:*         
LISTEN     0           128                        [::]:10050                   [::]:*         
LISTEN     0           128                        [::]:80                      [::]:*         
LISTEN     0           128                        [::]:22                      [::]:*         
LISTEN     0           128                       [::1]:6010                    [::]:* 
上一篇 下一篇

猜你喜欢

热点阅读