zabbix进阶
1.在客户机上安装agent
//下载zabbix,解压
[[email protected] ~]# cd /usr/src/
[[email protected] src]# wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/4.0.3/zabbix-4.0.3.tar.gz
[[email protected] src]# tar xf zabbix-3.4.12.tar.gz
//安装依赖包
[[email protected] src]# yum -y install net-snmp-devel libevent-devel
[[email protected] src]# yum -y insta gcc gcc-c++
//创建zabbix用户和组
[[email protected] src]# groupadd -r zabbix
[[email protected] src]# useradd -r -g zabbix -M -s /sbin/nologin zabbix
//编译安装zabbix
[[email protected] mysql]# cd /usr/src/zabbix-3.4.12
[[email protected] zabbix-4.0.3]# ./configure --enable-agent
[[email protected] ~]# make install
//配置zabbix配置文件
[[email protected] ~]# vim /usr/local/etc/zabbix_agentd.conf
//分别配上一下内容
Hostname=zhangzhengguang001
ServerActive=192.168.220.40
Server=192.168.220.40
//启动服务
[email protected] ~]# zabbix_agentd
2.解决中文乱码问题
首先在windos的c:\windows\fonts\fonts简体楷体复制出来
在服务器上:
//把这个字体文件放到/usr/local/apache/htdocs/zabbix/fonts/
[[email protected] fonts]# ls
DejaVuSans.ttf simkai.ttf
//修改文件的权限为644,属主.属组为apache
[[email protected] fonts]# chmod 644 simkai.ttf
[[email protected] fonts]# chown -R apache.apache /usr/local/apache/htdocs/zabbix/fonts/
[[email protected] fonts]# ll
total 12252
-rw-r--r--. 1 apache apache 756072 Feb 4 04:57 DejaVuSans.ttf
-rw-r--r--. 1 apache apache 11787328 Feb 23 02:14 simkai.ttf
//修改字体的配置文件
[[email protected] fonts]# cd ../include/
//以下两步是为了防止出现意外
[[email protected] include]# mkdir /opt/backup
[[email protected] include]# cp defines.inc.php /opt/backup/
[[email protected] include]# vim defines.inc.php
%s/DejaVuSans/simkai/g //替换命题
//重启服务
[[email protected] include]# pkill zabbix
[[email protected] include]# zabbix_server
[[email protected] include]# zabbix_agentd
2.1结果
3.主机与主机组的添加
- 添加主机群组
点击添加 - 添加主机
点击添加,此时就可以到组和主机
点击zzg001,添加模板
点击更新
4.模板使用
添加模板
更改创建用户的更新间隔时间为1min
在客户端创建一个用户,仪表板检查到问题
5.添加监控项
- 1.在客户端创建监控文件/opt/test
[[email protected] ~]# touch /opt/test
[[email protected] ~]# ll /opt/test
-rw-r--r--. 1 root root 0 Feb 25 01:31 /opt/test
- 2.在主机监控项中点击创建监控项
点击添加
添加成功
6.添加触发器
- 1.在主机触发器中添加触发器
点击添加,此时监控项与触发器都已完成 - 检测,在/opt/test插入内容
[[email protected] ~]# echo 'hello world' >> /opt/test
[[email protected] ~]# cat /opt/test
hello world
hello world
7.监控进程
- 1.配置zabbix配置文件,监控apache服务
[[email protected] ~]# sed -i 's/#UnsafeUserParameters=0/UnsafeUserParameters=1/g' /usr/local/etc/zabbix_agentd.conf
[[email protected] ~]# cat >> /usr/local/etc/zabbix_agentd.conf <<EOF
> UserParameter=check_apache,/scripts/check_process.sh httpd
> EOF
- 2.写控制脚本
[[email protected] ~]# mkdir /scripts
[[email protected] ~]# vim /scripts/check_process.sh
[[email protected] ~]# cat /scripts/check_process.sh
#/bin/bash
a=$(ps -ef |grep -vE "grep|$0" |grep $1|wc -l)
if [ $a -eq 0 ];then
echo '1'
else
echo '0'
fi
[[email protected] ~]# chown zabbix.zabbix /scripts/check_process.sh
[[email protected] ~]# chmod +x /scripts/check_process.sh
[[email protected] ~]# ll /scripts/check_process.sh
-rwxr-xr-x. 1 zabbix zabbix 111 Feb 25 02:26 /scripts/check_process.sh
//重新启动服务,检验
客户端
[[email protected] ~]# pkill zabbix
[[email protected] ~]# zabbix_agentd
服务端
[[email protected] ~]# zabbix_get -s 192.168.220.50 -k check_apache
0
- 添加监控项和触发器
点击添加
点击添加
说明添加成功 - 检验,在客户端关闭appche服务
[[email protected] ~]# systemctl stop httpd
8.监控日志
- 下载log.py文件到/scripts 目录下面
[[email protected] ~]# unzip pyscripts-master.zip
[[email protected] ~]# cd pyscripts-master
[[email protected] pyscripts-master]# ls
log.py mail_send.py README.md
[[email protected] pyscripts-master]# mv log.py /scripts/
[[email protected] pyscripts-master]# ll /scripts/
total 8
-rwxr-xr-x. 1 zabbix zabbix 111 Feb 25 02:26 check_process.sh
-rw-r--r--. 1 root root 1854 Feb 26 2018 log.py
[[email protected] pyscripts-master]# cd /scripts/
[[email protected] scripts]# chown zabbix.zabbix log.py
- 配置zabbix配置文件
[[email protected] vsftpd]# vim /usr/local/etc/zabbix_agentd.conf
[[email protected] vsftpd]# tail -1 /usr/local/etc/zabbix_agentd.conf
UserParameter=check_log_ftp,python /scripts/log.py /var/log/vsftpd.log
//赋予权限
[[email protected] vsftpd]# chmod 644 /var/log/vsftpd.log
手动验证
//重启服务
客户端
[[email protected] ~]# pkill zabbix
[[email protected] ~]# zabbix_agentd
服务端
[[email protected] ~]# zabbix_get -s 192.168.220.50 -k check_log_ftp
0
- 添加控制项和触发器
点击添加
点击添加
说明添加成功 - log.py说明
- 验证
//在客户机上
[[email protected] ~]# echo "Errorhelloworld" >> /var/log/vsftpd.log
验证成功
9.邮件警告
服务端
- 1.停止postfix服务设置开启不自启
[[email protected] opt]# systemctl stop postfix
[[email protected] opt]# systemctl disable postfix
- 2.安装mailx
[[email protected] opt]# yum -y install mailx
- 3.配置/etc/mail.rc文件
[[email protected] ~]# vim /etc/mail.rc
[[email protected] ~]# tail -5 /etc/mail.rc
set [email protected] (网易邮箱,发送方)
set smtp=smtp.163.com (邮箱服务器地址)
set [email protected] (用户,发送方)
set smtp-auth-password=guangguang.. (密码)
set smtp-auth=login
- 4.手动测试
[[email protected] ~]# echo "hello world" | mail -s "you are great" [email protected]
- 5.修改配置文件/usr/local/etc/zabbix_server.conf设置警告目录,创建警告目录,在警告目录中写脚本
[[email protected] ~]# vim /usr/local/etc/zabbix_server.conf
# AlertScriptsPath=${datadir}/zabbix/alertscripts
AlertScriptsPath=/scripts/mail.zabbix/ //加上此行
[[email protected] ~]# rm -rf /scripts/
[[email protected] ~]# mkdir -p /scripts/mail.zabbix
[[email protected] ~]# cd /scripts/mail.zabbix/
[[email protected] mail.zabbix]# vim mail.sh
[[email protected] mail.zabbix]# cat mail.sh
#/bin/bash
echo $3 | mail -s $2 $1
//给脚本设置权限
[[email protected] mail.zabbix]# chown zabbix.zabbix mail.sh
[[email protected] mail.zabbix]# chmod +x mail.sh
- 6.重新启动服务
[[email protected] mail.zabbix]# pkill zabbix
[[email protected] mail.zabbix]# zabbix_server
[[email protected] mail.zabbix]# zabbix_agentd
手动验证
[[email protected] ~]# su - zabbix -s /bin/bash
su: warning: cannot change directory to /home/zabbix: No such file or directory
-bash-4.2$ /scripts/mail.zabbix/mail.sh [email protected] "hello" "hello world"
- 7.在web上创建脚本报警媒介
点击更新 - 8.创建用户报警媒介
点击添加,更新
- 9.设置触发器报警动作
事件源为触发器
点击添加
点击添加
此时多了一个mail_scripts动作
- 10.验证
我们对这个监控项进行验证
在客户端
[[email protected] opt]# echo 'hellow world' >>/opt/zzg