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

[经验分享] 按照zabbix客户端及自定义监控nginx连接状态

[复制链接]

尚未签到

发表于 2019-1-24 09:00:13 | 显示全部楼层 |阅读模式
一、下载客户端按照包,这里用rpm包安装

[root@localhost ~]# rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-agent-3.0.4-1.el7.x86_64.rpm
修改客户端配置文件

Server=127.0.0.1
ServerActive=127.0.0.1


改为zabbix server实际的IP地址(比如zabbix server的IP地址为10.0.0.100)


Server=10.0.0.100
ServerActive=10.0.0.100


重启zabbix 客户端

[root@zabbix]# systemctl restart zabbix-agent


二、自定义脚本监控nginx连接状态:

#!/bin/bash
HOST=localhost
PORT="9999"
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}'
       }
$1


将脚本存放与脚本目录,注意该目录根据自己情况定义,我这里用rpm 按照zabbix-agent客户端,所以直接放在/etc/zabbix/script目录下

[root@localhost script]# ll nginx-status.sh
-rwx------ 1 root root 947 Mar 22 10:49 nginx-status.sh
[root@localhost script]#
不要忘了给脚本执行权限

[root@localhost script]# chmod +x nginx-status.sh
[root@localhost script]#
[root@localhost script]# ll nginx-status.sh
-rwxr-xr-x 1 root root 947 Mar 22 10:49 nginx-status.sh
[root@localhost script]#
修改配置文件,在末尾添加如下内容:

[root@localhost zabbix]# vim zabbix_agentd.conf
...................................省略部分......................................................
### DISK I/O
UserParameter=custom.vfs.dev.read.ops
  • ,cat /proc/diskstats | grep $1 | head -1 | awk '{print $$4}'  
    UserParameter=custom.vfs.dev.read.ms
  • ,cat /proc/diskstats | grep $1 | head -1 | awk '{print $$7}'  
    UserParameter=custom.vfs.dev.write.ops
  • ,cat /proc/diskstats | grep $1 | head -1 | awk '{print $$8}'  
    UserParameter=custom.vfs.dev.write.ms
  • ,cat /proc/diskstats | grep $1 | head -1 | awk '{print $$11}'  
    UserParameter=custom.vfs.dev.io.active
  • ,cat /proc/diskstats | grep $1 | head -1 | awk '{print $$12}'  
    UserParameter=custom.vfs.dev.io.ms
  • ,cat /proc/diskstats | grep $1 | head -1 | awk '{print $$13}'  
    UserParameter=custom.vfs.dev.read.sectors
  • ,cat /proc/diskstats | grep $1 | head -1 | awk '{print $$6}'  
    UserParameter=custom.vfs.dev.write.sectors
  • ,cat /proc/diskstats | grep $1 | head -1 | awk '{print $$10}'
    ######## TCP connections total
    UnsafeUserParameters=1
    UserParameter=synrecv,/etc/zabbix/script/tcp_connection_status.sh SYNRECV
    UserParameter=estab,/etc/zabbix/script/tcp_connection_status.sh ESTAB
    UserParameter=timewait,/etc/zabbix/script/tcp_connection_status.sh TIMEWAIT
    UserParameter=last_ack,/etc/zabbix/script/tcp_connection_status.sh LAST_ACK
    UserParameter=total,/etc/zabbix/script/tcp_connection_status.sh TOTAL
    #####Nginx Status
    UserParameter=nginx.accepts,/etc/zabbix/script/nginx-status.sh accepts
    UserParameter=nginx.handled,/etc/zabbix/script/nginx-status.sh handled
    UserParameter=nginx.requests,/etc/zabbix/script/nginx-status.sh requests
    UserParameter=nginx.connections.active,/etc/zabbix/script/nginx-status.sh active
    UserParameter=nginx.connections.reading,/etc/zabbix/script/nginx-status.sh reading
    UserParameter=nginx.connections.writing,/etc/zabbix/script/nginx-status.sh writing
    UserParameter=nginx.connections.waiting,/etc/zabbix/script/nginx-status.sh waiting
    EnableRemoteCommands=1


    然后重启zabbix客户端

    [root@zabbix]# systemctl restart zabbix-agent
    [root@localhost zabbix]# systemctl status zabbix-agent
    ● zabbix-agent.service - Zabbix Agent
       Loaded: loaded (/usr/lib/systemd/system/zabbix-agent.service; disabled; vendor preset: disabled)
       Active: active (running) since Wed 2017-03-22 11:03:05 CST; 6min ago
      Process: 13613 ExecStop=/bin/kill -SIGTERM $MAINPID (code=exited, status=0/SUCCESS)
      Process: 13615 ExecStart=/usr/sbin/zabbix_agentd -c $CONFFILE (code=exited, status=0/SUCCESS)
    Main PID: 13617 (zabbix_agentd)
       CGroup: /system.slice/zabbix-agent.service
               ├─13617 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
               ├─13618 /usr/sbin/zabbix_agentd: collector [idle 1 sec]
               ├─13619 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
               ├─13620 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
               └─13621 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]
    Mar 22 11:03:05 localhost systemd[1]: Starting Zabbix Agent...
    Mar 22 11:03:05 localhost systemd[1]: PID file /run/zabbix/zabbix_agentd.pid not readable (yet?) after start.
    Mar 22 11:03:05 localhost systemd[1]: Started Zabbix Agent.
    [root@localhost zabbix]#


      

    最后在服务端添加主机对应的监控项,键值为如下字段:

    nginx.accepts
    nginx.handled
    nginx.requests
    nginx.connections.active,
    nginx.connections.reading
    nginx.connections.writing
    nginx.connections.waiting  

      

      网上看到另外一篇关于tcp连接数的监控转载下来:
      http://www.codes51.com/article/detail_341554.html
      Tcp的连接状态对于我们web服务器来说是至关重要的,尤其是并发量ESTAB;或者是syn_recv值,假如这个值比较大的话我们可以认为是不是受到了***,或是是time_wait值比较高的话,我们要考虑看我们内核是否需要调优,太高的time_wait值的话会占用太多端口,要是端口少的话后果不堪设想:
      监控脚本如下:
      #!/bin/bash
      #Linux sir
      #scripts for tcp status
      function SYNRECV {
      /usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'SYN-RECV' | awk '{print $2}'
      }
      function ESTAB {
      /usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'ESTAB' | awk '{print $2}'
      }
      function FINWAIT1 {
      /usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'FIN-WAIT-1' | awk '{print $2}'
      }
      function FINWAIT2 {
      /usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'FIN-WAIT-2' | awk '{print $2}'
      }
      function TIMEWAIT {
      /usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'TIME-WAIT' | awk '{print $2}'
      }
      function LASTACK {
      /usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'LAST-ACK' | awk '{print $2}'
      }
      function LISTEN {
      /usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'LISTEN' | awk '{print $2}'
      }
      $1
      

      二、填写key值:当然大家在加入key值之后最好再服务器上面去执行看有没有返回值:
      [root@WEB_007 scripts]# cat /usr/local/zabbix/etc/zabbix_agentd.conf.d/tcp
      #monitor tcp
      UserParameter=tcp
  • ,/usr/local/zabbix/scripts/tcp_status $1
      三、导入模板(在我的附件上,然后触发器根据自己的情况调整:)
      四、查看效果:
      


    到此结束:
    在写这篇的时候刚好也写完了监控lvs的活动连接。下面也给出模板、大家自己下载,我这里给出脚本和key:
    一、脚本:
      #!/bin/bash
      #xiaoluo
      #status about lvs forward
      function first {
      /sbin/ipvsadm -L 2>/dev/null | grep '192.168.10.234:http' | sed '2d' | awk '{print $5}'
      }
      function second {
      /sbin/ipvsadm -L 2>/dev/null | grep '192.168.10.235:http' | sed '2d' | awk '{print $5}'
      }
      function third {
      /sbin/ipvsadm -L 2>/dev/null | grep '192.168.10.236:http' | sed '2d' | awk '{print $5}'
      }
      $1
      
    二、key:
    #monitor lvs
    UserParameter=lvs
  • ,/usr/local/zabbix/scripts/ipvsadm_status $1
    效果展示:

      

      
      模板在下面自己下载:
      ------------------------------------------分割线------------------------------------------
      免费下载地址在 http://linux.linuxidc.com/
      用户名与密码都是www.linuxidc.com
      具体下载目录在 /2015年资料/5月/14日/Zabbix 监控tcp连接的状态/
      下载方法见 http://www.linuxidc.com/Linux/2013-07/87684.htm
      以上就介绍了Zabbix监控TCP连接数,包括了方面的内容,希望对系统运维有兴趣的朋友有所帮助。
      






  • 运维网声明 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-666866-1-1.html 上篇帖子: Zabbix集成iyunv实现短信、邮件、微信、电话、App告警 下篇帖子: 利用Zabbix监控Nginx
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

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

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

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

    扫描微信二维码查看详情

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


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


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


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



    合作伙伴: 青云cloud

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