zabbix agent批量安装并自动监控TCP的11种状态实战案例
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.编写zabbx agent自动安装脚本
1>.TCP状态监控脚本及配置文件
[root@hdp101.yinzhengjie.org.cn /usr/local/src]# cat tcp_status_monitoring.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-23 #FileName: shell/menu.sh #URL: http://www.cnblogs.com/yinzhengjie #Description: The test script #Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated. #******************************************************************** function tcp_conn_status(){ TCP_STAT=$1 ss -ant | awk 'NR>1 {++s[$1]} END {for(k in s) print k,s[k]}' > /tmp/tcp_status_monitoring.txt TCP_NUM=$(grep "$TCP_STAT" /tmp/tcp_status_monitoring.txt | cut -d ' ' -f2) if [ -z $TCP_NUM ];then TCP_NUM=0 fi echo $TCP_NUM } function main(){ case $1 in tcp_status) tcp_conn_status $2; ;; esac } main $1 $2 [root@hdp101.yinzhengjie.org.cn /usr/local/src]# [root@hdp101.yinzhengjie.org.cn /usr/local/src]#
[root@hdp101.yinzhengjie.org.cn /usr/local/src]# ll total 16828 -rw-r--r-- 1 root root 2234 Mar 3 20:43 install-zabbix-agent-4.0.17.sh -rw-r--r-- 1 root root 887 Mar 1 07:35 tcp_status_monitoring.sh -rw-r--r-- 1 root root 811 Mar 1 12:20 uninstall-zabbix-agent-4.0.17.sh -rw-r--r-- 1 root root 125 Mar 1 12:31 yinzhengjie_agent.conf drwxr-xr-x 13 zookeeper hadoop 4096 Mar 3 20:45 zabbix-4.0.17 -rw-r--r-- 1 root root 17191889 Feb 14 09:20 zabbix-4.0.17.tar.gz -rw-r--r-- 1 root root 10953 Mar 1 07:48 zabbix_agentd.conf -rw-r--r-- 1 root root 519 Mar 1 07:06 zabbix-agent.service [root@hdp101.yinzhengjie.org.cn /usr/local/src]# [root@hdp101.yinzhengjie.org.cn /usr/local/src]# [root@hdp101.yinzhengjie.org.cn /usr/local/src]# cat yinzhengjie_agent.conf UserParameter=tcp_status_monitoring[*],/yinzhengjie/softwares/zabbix/etc/zabbix_agentd.conf.d/tcp_status_monitoring.sh $1 $2 [root@hdp101.yinzhengjie.org.cn /usr/local/src]# [root@hdp101.yinzhengjie.org.cn /usr/local/src]#
2>.zabbix agent服务的配置文件及服务管理脚本
[root@hdp101.yinzhengjie.org.cn /usr/local/src]# grep "^[a-Z]" zabbix_agentd.conf PidFile=/yinzhengjie/softwares/zabbix/pid/zabbix_agentd.pid LogType=file LogFile=/yinzhengjie/softwares/zabbix/logs/zabbix_agentd.log Server=172.200.5.203 ServerActive=172.200.5.203 Hostname= Timeout=30 Include=/yinzhengjie/softwares/zabbix/etc/zabbix_agentd.conf.d/*.conf [root@hdp101.yinzhengjie.org.cn /usr/local/src]# [root@hdp101.yinzhengjie.org.cn /usr/local/src]#
[root@hdp101.yinzhengjie.org.cn /usr/local/src]# [root@hdp101.yinzhengjie.org.cn /usr/local/src]# cat zabbix-agent.service [Unit] Description=Yinzhengjie's Zabbix Agent After=syslog.target After=network.target [Service] Environment="CONFFILE=/yinzhengjie/softwares/zabbix/etc/zabbix_agentd.conf" EnvironmentFile=-/etc/default/zabbix-agent Type=forking Restart=on-failure PIDFile=/yinzhengjie/softwares/zabbix/pid/zabbix_agentd.pid KillMode=control-group ExecStart=/yinzhengjie/softwares/zabbix/sbin/zabbix_agentd -c $CONFFILE ExecStop=/bin/kill -SIGTERM $MAINPID RestartSec=10s User=zabbix Group=zabbix [Install] WantedBy=multi-user.target [root@hdp101.yinzhengjie.org.cn /usr/local/src]# [root@hdp101.yinzhengjie.org.cn /usr/local/src]#
3>.编写zabbix agent安装和卸载脚本
[root@hdp101.yinzhengjie.org.cn /usr/local/src]# cat install-zabbix-agent-4.0.17.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-23 #URL: http://www.cnblogs.com/yinzhengjie #Description: The test script #Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated. #******************************************************************** #定义安装文件存放路径 WORKDIR=/usr/local/src ZABBIX_AGENT=/yinzhengjie/softwares/zabbix/etc/zabbix_agentd.conf.d AGENTCONF=/yinzhengjie/softwares/zabbix/etc #取当前zabbix agent的IP地址 IP=`ifconfig bond0 | grep -w inet | awk '{print $2}'` #安装CentOS操作系统的依赖包 grep "Kernel" /etc/issue &> /dev/null if [ $? -eq 0 ];then yum -y install gcc fi #安装Ubuntu操作系统的依赖包 grep "Ubuntu" /etc/issue &> /dev/null if [ $? -eq 0 ];then apt -y install gcc fi #将zabbix的安装包放入到指定位置开始安装 tar xf zabbix-4.0.17.tar.gz && cd zabbix-4.0.17 && ./configure --prefix=/yinzhengjie/softwares/zabbix --enable-agent && make && make install #创建存放pid和log日志的目录 mkdir -v /yinzhengjie/softwares/zabbix/{pid,logs} #强制拷贝zabbix agent的启动脚本 cp ${WORKDIR}/zabbix-agent.service /usr/lib/systemd/system/zabbix-agent.service #强制拷贝zabbix agent的配置文件 cp ${WORKDIR}/zabbix_agentd.conf ${AGENTCONF}/zabbix_agentd.conf #强制拷贝监控脚本 cp ${WORKDIR}/tcp_status_monitoring.sh ${ZABBIX_AGENT}/tcp_status_monitoring.sh #为脚本添加执行权限 chmod +x ${ZABBIX_AGENT}/tcp_status_monitoring.sh #强制拷贝zabbix agent的子配置文件 cp ${WORKDIR}/yinzhengjie_agent.conf ${ZABBIX_AGENT}/yinzhengjie_agent.conf #修改zabbix agent的主机名为上面取到的IP地址 sed -r -i 's#(Hostname=)#1'${IP}'#' ${AGENTCONF}/zabbix_agentd.conf #创建zabbix用户用来管理zabbix服务,因此需要将zabbix的安装目录授权给zabbix用户 useradd zabbix -s /sbin/nologin && chown zabbix:zabbix /yinzhengjie/softwares/zabbix -R #启动zabbix agent并设置为开机启动 systemctl daemon-reload && systemctl start zabbix-agent.service && systemctl enable zabbix-agent.service [root@hdp101.yinzhengjie.org.cn /usr/local/src]# [root@hdp101.yinzhengjie.org.cn /usr/local/src]#
[root@hdp101.yinzhengjie.org.cn /usr/local/src]# cat uninstall-zabbix-agent-4.0.17.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-23 #URL: http://www.cnblogs.com/yinzhengjie #Description: The test script #Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated. #******************************************************************** #停止nginx服务 systemctl stop zabbix-agent.service && systemctl disable zabbix-agent.service #删除zabbix的安装目录 rm -rf /yinzhengjie/softwares/zabbix #删除解压目录 rm -rf /usr/local/src/zabbix-4.0.17 #强制删除zabbix agent的启动脚本 rm -f /usr/lib/systemd/system/zabbix-agent.service systemctl daemon-reload #删除zabbix用户 userdel -r zabbix [root@hdp101.yinzhengjie.org.cn /usr/local/src]# [root@hdp101.yinzhengjie.org.cn /usr/local/src]#
4>.运行自动安装zabbix agent服务脚本进行测试
[root@hdp101.yinzhengjie.org.cn /usr/local/src]# bash install-zabbix-agent-4.0.17.sh
5>.自定义zabbix WebUI的TCP监控模板
博主推荐阅读: https://www.cnblogs.com/yinzhengjie2020/p/12347349.html
6>.zabbix WebUI查看图形数据
7>.归档文件到其它
[root@hdp101.yinzhengjie.org.cn /usr/local/src]# ll total 16824 -rw-r--r-- 1 root root 2052 Mar 1 12:21 install-zabbix-agent-4.0.17.sh -rw-r--r-- 1 root root 887 Mar 1 07:35 tcp_status_monitoring.sh -rw-r--r-- 1 root root 811 Mar 1 12:20 uninstall-zabbix-agent-4.0.17.sh -rw-r--r-- 1 root root 125 Mar 1 12:31 yinzhengjie_agent.conf -rw-r--r-- 1 root root 17191889 Feb 14 09:20 zabbix-4.0.17.tar.gz -rw-r--r-- 1 root root 10953 Mar 1 07:48 zabbix_agentd.conf -rw-r--r-- 1 root root 519 Mar 1 07:06 zabbix-agent.service [root@hdp101.yinzhengjie.org.cn /usr/local/src]# [root@hdp101.yinzhengjie.org.cn /usr/local/src]# [root@hdp101.yinzhengjie.org.cn /usr/local/src]# [root@hdp101.yinzhengjie.org.cn /usr/local/src]# tar czvf yinzhengjie-zabbix-agent-autoInstall.tar.gz ./* ./install-zabbix-agent-4.0.17.sh ./tcp_status_monitoring.sh ./uninstall-zabbix-agent-4.0.17.sh ./yinzhengjie_agent.conf ./zabbix-4.0.17.tar.gz ./zabbix_agentd.conf ./zabbix-agent.service [root@hdp101.yinzhengjie.org.cn /usr/local/src]# [root@hdp101.yinzhengjie.org.cn /usr/local/src]# [root@hdp101.yinzhengjie.org.cn /usr/local/src]# ll total 33544 -rw-r--r-- 1 root root 2052 Mar 1 12:21 install-zabbix-agent-4.0.17.sh -rw-r--r-- 1 root root 887 Mar 1 07:35 tcp_status_monitoring.sh -rw-r--r-- 1 root root 811 Mar 1 12:20 uninstall-zabbix-agent-4.0.17.sh -rw-r--r-- 1 root root 125 Mar 1 12:31 yinzhengjie_agent.conf -rw-r--r-- 1 root root 17117826 Mar 1 12:47 yinzhengjie-zabbix-agent-autoInstall.tar.gz -rw-r--r-- 1 root root 17191889 Feb 14 09:20 zabbix-4.0.17.tar.gz -rw-r--r-- 1 root root 10953 Mar 1 07:48 zabbix_agentd.conf -rw-r--r-- 1 root root 519 Mar 1 07:06 zabbix-agent.service [root@hdp101.yinzhengjie.org.cn /usr/local/src]# [root@hdp101.yinzhengjie.org.cn /usr/local/src]# [root@hdp101.yinzhengjie.org.cn /usr/local/src]#
8>.将归档文件拷贝到其他zabbix agent节点进行自动安装
[root@hdp101.yinzhengjie.org.cn /usr/local/src]# scp yinzhengjie-zabbix-agent-autoInstall.tar.gz hdp102.yinzhengjie.org.cn:/usr/local/src/
二.在hdp102.yinzhengjie.org.cn节点运行自动安装脚本
1>.将拷贝过来的压缩文件解压
[root@hdp102.yinzhengjie.org.cn /usr/local/src]# tar zvxf yinzhengjie-zabbix-agent-autoInstall.tar.gz
2>.运行自动安装脚本
[root@hdp102.yinzhengjie.org.cn /usr/local/src]# bash install-zabbix-agent-4.0.17.sh
3>.检查服务是否安装成功
[root@hdp102.yinzhengjie.org.cn /usr/local/src]# hostname -i 172.200.1.102 [root@hdp102.yinzhengjie.org.cn /usr/local/src]# [root@hdp102.yinzhengjie.org.cn /usr/local/src]# grep ^Hostname /yinzhengjie/softwares/zabbix/etc/zabbix_agentd.conf Hostname=172.200.1.102 [root@hdp102.yinzhengjie.org.cn /usr/local/src]# [root@hdp102.yinzhengjie.org.cn /usr/local/src]# grep '^[a-Z]' /yinzhengjie/softwares/zabbix/etc/zabbix_agentd.conf PidFile=/yinzhengjie/softwares/zabbix/pid/zabbix_agentd.pid LogType=file LogFile=/yinzhengjie/softwares/zabbix/logs/zabbix_agentd.log Server=172.200.5.203 ServerActive=172.200.5.203 Hostname=172.200.1.102 Timeout=30 Include=/yinzhengjie/softwares/zabbix/etc/zabbix_agentd.conf.d/*.conf [root@hdp102.yinzhengjie.org.cn /usr/local/src]# [root@hdp102.yinzhengjie.org.cn /usr/local/src]#
三.在zabbix webUI上克隆配置
1>.查看"hdp101.yinzhengjie.org.cn"主机的配置信息
2>.点击"克隆",会将"hdp101.yinzhengjie.org.cn"这个主机的所有的配置都克隆一份
3>.基于克隆的主机模板修改相应的参数即可(必须修改主机名及zabbix agent端的地址信息)
4>.主机添加成功
5>.刷新主机页面
6>.查看hdp102.yinzhengjie.org.cn主机的监控页面