Zabbix监控基础
1. 安装前准备
setenforce 0 sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 yum install net-tools openssl-devel openssl epel-release wget vim gcc gcc-c++ lsof chrony tree unzip rsync -y
2. LAMP环境搭建
安装:
yum install -y httpd mariadb mariadb-server mariadb-client php php-mysql
启动mysql,设置开机启动,修改mysql密码,注意下面的yourpassword要替换为你自己的密码
systemctl start mariadb.service systemctl enable mariadb.service #设置开机启动 mysqladmin -u root -p password yourpassword
3. 安装Zabbix服务
rpm -ivh http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX yum install -y zabbix-server-mysql zabbix-web-mysql zabbix-get zabbix-agent
4. 创建Zabbix数据库
mysql -uroot -p MariaDB [(none)]> CREATE DATABASE zabbix DEFAULT CHARACTER SET utf8 COLLATE utf8_bin; MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix'; MariaDB [(none)]> flush privileges; MariaDB [(none)]> quit
导入数据:
cd /usr/share/doc/zabbix-server-mysql-3.2.10/ zcat create.sql.gz | mysql -uzabbix -pzabbix
5. 修改zabbix配置
注意:下面的zabbix请替换为你自己的数据库密码
grep -Ev "^$|#" /etc/zabbix/zabbix_server.conf LogFile=/var/log/zabbix/zabbix_server.log LogFileSize=0 PidFile=/var/run/zabbix/zabbix_server.pid DBHost=localhost DBName=zabbix DBName=zabbix DBUser=zabbix DBUser=zabbix DBPassword=zabbix SNMPTrapperFile=/var/log/snmptrap/snmptrap.log Timeout=4 AlertScriptsPath=/usr/lib/zabbix/alertscripts ExternalScripts=/usr/lib/zabbix/externalscripts LogSlowQueries=3000
修改时区:
vim /etc/httpd/conf.d/zabbix.conf php_value date.timezone Asia/Shanghai
6. 修改zabbix登录密码
# mysql -uroot -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 326 Server version: 5.5.56-MariaDB MariaDB Server Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> use zabbix Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MariaDB [zabbix]> select userid, alias, name, passwd from users; +--------+-------+--------+----------------------------------+ | userid | alias | name | passwd | +--------+-------+--------+----------------------------------+ | 1 | Admin | Zabbix | 5fce1b3e34b520afeffb37ce08c7cd66 | | 2 | guest | | d41d8cd98f00b204e9800998ecf8427e | +--------+-------+--------+----------------------------------+ 2 rows in set (0.00 sec) MariaDB [zabbix]> update users set passwd=md5('newpassword') where userid='1'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
7. 启动并设置开机启动
systemctl start httpd systemctl start zabbix-server systemctl start zabbix-agent systemctl enable httpd systemctl enable zabbix-server systemctl enable zabbix-agent
8. 访问zabbix页面
通过http://hostname/zabbix访问zabbix的安装页面,填写安装信息,完成安装。完成安装后,初始登录账户密码为Admin/zabbix。
9. 安装zabbix-agent
zabbix-server安装好了,下面需要“接入”其他机器,将它们纳入到zabbix-server的管理
rpm -ivh http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm yum install -y zabbix-agent
配置zabbix-agent:
# grep -Ev "^$|#" /etc/zabbix/zabbix_agentd.conf PidFile=/var/run/zabbix/zabbix_agentd.pid LogFile=/var/log/zabbix/zabbix_agentd.log LogFileSize=0 Server=192.168.1.1 ServerActive=192.168.1.1 Hostname=192.168.1.2 Include=/etc/zabbix/zabbix_agentd.d/*.conf
启动zabbix-agent:
systemctl start zabbix-agent.service systemctl enable zabbix-agent.service
操作命令:
rpm -ivh http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm yum install -y zabbix-agent cp /etc/zabbix/zabbix_agentd.conf /etc/zabbix/zabbix_agentd.conf.bak sed -i s'$ServerActive=127.0.0.1$ServerActive=192.168.1.1$'g /etc/zabbix/zabbix_agentd.conf sed -i s'$Server=127.0.0.1$Server=192.168.1.1$'g /etc/zabbix/zabbix_agentd.conf sed -i s'$# UnsafeUserParameters=0$UnsafeUserParameters=1$'g /etc/zabbix/zabbix_agentd.conf sed -i s'$# EnableRemoteCommands=0$EnableRemoteCommands=1$'g /etc/zabbix/zabbix_agentd.conf HOSTIP=`ifconfig | grep "inet" | egrep -v '(192.168.50|127.0.0.1)'|awk -F ' ' '{print $2}'` sed -i s"/Hostname=Zabbix server/Hostname=$HOSTIP/"g /etc/zabbix/zabbix_agentd.conf 检查: grep -Ev "^$|#" /etc/zabbix/zabbix_agentd.conf 启动zabbix-agent服务: systemctl start zabbix-agent.service systemctl enable zabbix-agent.service
10. 添加服务器
注意:中间的Agent interfaces是提供给Zabbix Server调用的接口,需确保该ip和port可以访问
绑定模板:
注意:如果zabbix-agent需要被其他机器调用,例如通过zabbix_get获得监控信息,需要在防火墙设置“放开10050端口”
11. Zabbix批量添加web监控
本文参考自 清风拂面 《python实现zabbix批量监控WEB网站和批量监控主机端口》 修改,使得以后添加web时更加方便,zabbix agent端完成1-3步,在zabbix web端直接导入模板即可使用。
web监控模板(zbx_web_monitor_templates.xml)
(1) agent端批量添加脚本/data/zabbix/scripts/web_site_moniter.py
#!/usr/bin/env python # coding:utf-8 import os, sys, json # 将要监控的web站点url添加到urllist列表 # urllist = ["http://baidu.com", # "http://www.qq.com", # "http://www.sina.com.cn/"] files='/etc/zabbix/test/WEB.txt' fd = open(files,'r') urllist=[] for i in fd.readlines(): if i[0] != '#': urllist.append(i.strip('\n')) fd.close() # 这个函数主要是构造出一个特定格式的字典,用于zabbix def web_site_discovery(): web_list = [] web_dict = {"data": None} for url in urllist: url_dict = {} url_dict["{#SITENAME}"] = url web_list.append(url_dict) web_dict["data"] = web_list jsonStr = json.dumps(web_dict, sort_keys=True, indent=4) return jsonStr # 这个函数用于测试站点返回的状态码,注意在cmd命令中如果有%{}这种字符要使用占位符代替,否则 # 会报错 def web_site_code(): cmd = 'curl -o /dev/null -s -w %s %s' % ("%{http_code}", sys.argv[2]) reply_code = os.popen(cmd).readlines()[0] return reply_code if __name__ == "__main__": try: if sys.argv[1] == "web_site_discovery": print web_site_discovery() elif sys.argv[1] == "web_site_code": print web_site_code() else: print "Pls sys.argv[0] web_site_discovery | web_site_code[URL]" except Exception as msg: print msg # 这里对脚本传递进来的第一个参数做判断去执行不同的函数,为什么要这样,因为通过一个脚本写了两个功能
(2) zabbix agent配置文件添加如下内容,内容含义请参考《python实现zabbix批量监控WEB网站和批量监控主机端口》已经详细解释说明
UnsafeUserParameters=1 UserParameter=web_site_discovery,/data/zabbix/scripts/web_site_moniter.py web_site_discovery UserParameter=web_site_code[*],/data/zabbix/scripts/web_site_moniter.py web_site_code $1
(3) zabbix agent添加需要监控web /data/zabbix/test/WEB.txt
http://www.ttlsa.com/zabbix/zabbix-zabbix_get-get-items/ #www.qq.com www.baidu.com www.sina.com
#号将表示取消web监控
修改zabbix agent配置后需要重启agent生效
(4)导入web监控模板zbx_web_monitor_templates.xml
Configuration ---> Templates ---> Import --->选择模板zbx_web_monitor_templates.xml
(5)模板导入之后Configuration ---> Templates可以找到Templates Web Monitor 模板
(6)选择Templates Web Monitor模板进行管理主机组或者主机
(7)Configuration ---> Hosts --->某主机Applications
可以发现Templates Web Monitor: Web Monitor Service监控项目
(8)查看监控项目
11. 设置邮件报警
(1)安装邮件发送工具mailx
yum install -y mailx ln -s /bin/mailx /bin/mail
(2)配置zabbix服务端外部邮箱
打开文件vi /etc/mail.rc 如果mail.rc文件没有,就手动创建 内容如下:
set from=luchuangao@126.com set smtp=smtp.126.com set smtp-auth-user=luchuangao #认证用户 set smtp-auth-password=123456 #认证密码 set smtp-auth=login
(3)测试邮件是否可以发出
echo "zabbix test mail" |mail -s "zabbix" luchuangao@126.com
(4)编写发送邮件脚本
进入下面路径:/usr/lib/zabbix/alertscripts 创建sendmail.sh文件,内容如下
echo "$3" | mail -s "$2" "$1"
上面的这个三个参数是接收从web页面中传递过来的,$1:收件人邮箱地址;$2:邮件标题;$3:邮件内容
在zabbix_serber.conf文件中配置的AlertScriptsPath=/usr/lib/zabbix/alertscripts,如果想修改服务端的配置文件中的AlertScriptsPath的值。
(5)配置web页面
a. 创建媒体类型
参考:http://www.linuxidc.com/Linux/2017-05/143776p5.htm
12.zabbix报错处理
zabbix监控搭建完后打开web界面http://xxx/zabbix报错:
zabbix server is not running: the information displayed may not be current!
修改zabbix.conf.php :
vim /etc/zabbix/web/zabbix.conf.php $DB['SERVER'] = '192.168.1.1'; # 改成本机的ip地址 $ZBX_SERVER = '192.168.1.1'; # 改成本机的ip地址 $ZBX_SERVER_PORT = '30051'; # 端口
重启HTTPD:
systemctl restart httpd
13.Zabbix监控内存百分比
配置zabbix当内存剩余不足20%的时候触发报警
zabbix默认的剩余内存报警:
Average Lack of available memory on server {HOST.NAME}{Template OS Linux:vm.memory.size[available].last(0)}<20M
这个值配置的过小,当实际内存不足10%的时候就需要配置报警了,20M才出发有点晚
1.创建item
Configuration-->Templates-->Template OS Linux-->items-->create item
name:
Ava memory percent
type:
Calculated #计算类型
key:
vm.memory.free[percent]
Formula:
100*last("vm.memory.size[available]")/last("vm.memory.size[total]")
Applications:
Memory
2.创建trigger
Configuration-->Templates-->Template OS Linux-->Triggers-->create trigger
Name:
free mem less 10%
Expression:
{Template OS Linux:vm.memory.free[percent].last()}<10
注意:
如果items或者trigger的Status是not suppoted说明表达式可能有问题,需要检查测试
扩展:
内存不足10% 这个时候服务器其实内存资源比较紧张,可以利用这个触发器触发一个脚本来重启占用内存比较多的服务,一般服务器里面的服务都是比较固定的,吃内存比较多的就是那几个,可以挑几个重启(单实例慎用),而且最好对该服务做好监控,避免自动重启服务脚本没有正常启动服务
14.Zabbix监控数据类型单位
数据类型单位是由前端php来控制得:
vim /usr/share/zabbix/include/func.inc.php
15.Grafana 4及结合Zabbix3.2实现可视化监控图形
https://www.cnblogs.com/zengqinglei/p/6503548.html
16.Zabbix监控磁盘IO
http://www.coooz.com/archives/222
17.zabbix中文乱码问题
https://www.cnblogs.com/kingwwz/p/5919030.html
18.微信报警
https://blog.csdn.net/abcdocker/article/details/73295133
19.推荐博客
https://blog.cactifans.com/
参考:http://www.jianshu.com/p/4d3af373e682