zabbix
zabbix部署
1. zabbix介绍
zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案。
zabbix能监视各种网络参数,保证服务器系统的安全运营;并提供灵活的通知机制以让系统管理员快速定位/解决存在的各种问题。
zabbix由2部分构成,zabbix server与可选组件zabbix agent。
zabbix server可以通过SNMP,zabbix agent,ping,端口监视等方法提供对远程服务器/网络状态的监视,数据收集等功能,它可以运行在Linux,Ubuntu,Solaris,HP-UX,AIX,Free BSD,Open BSD,OS X等平台上。
zabbix agent需要安装在被监视的目标服务器上,它主要完成对硬件信息或与操作系统有关的内存,CPU等信息的收集。
zabbix server可以单独监视远程服务器的服务状态;同时也可以与zabbix agent配合,可以轮询zabbix agent主动接收监视数据(agent方式),同时还可被动接收zabbix agent发送的数据(trapping方式)。
另外zabbix server还支持SNMP (v1,v2),可以与SNMP软件(例如:net-snmp)等配合使用。
2. zabbix特点
zabbix的主要特点:
安装与配置简单,学习成本低
支持多语言(包括中文)
免费开源
自动发现服务器与网络设备
分布式监视以及WEB集中管理功能
可以无agent监视
用户安全认证和柔软的授权方式
通过WEB界面设置或查看监视结果
email等通知功能
Zabbix主要功能:
CPU负荷
内存使用
磁盘使用
网络状况
端口监视
日志监视
3. zabbix配置文件
zabbix配置文件有两种:
服务器端配置文件(/usr/local/etc/zabbix_server.conf)
客户端配置文件(/usr/local/etc/zabbix_agentd.conf)
zabbix代理配置文件(/usr/local/etc/zabbix_proxy.conf)
服务器端配置文件zabbix_server.conf常用配置参数:
参数 | 作用 |
---|---|
LogFile | 设置服务端日志文件存放路径 |
ListenIP | 设置服务端监听IP |
ListenPort | 设置服务端监听的端口号 |
PidFile | 设置服务端进程号文件存放路径 |
DBHost | 指定zabbix的数据库服务器IP |
DBName | 指定zabbix使用的数据库库名 |
DBUser | 指定zabbix数据库登录用户 |
DBPassword | 指定zabbix数据库登录密码 |
DBPort | 指定zabbix数据库端口号 |
User | 设置zabbix以什么用户的身份运行 |
AlertScriptsPath | 设置告警脚本存放路径 |
ExternalScripts | 外部脚本存放路径 |
客户端配置文件zabbix_agentd.conf常用配置参数:
参数 | 作用 |
---|---|
Server | 指定zabbix服务器的IP或域名 |
ServerActive | 指定zabbix服务器的IP或域名 |
Hostname | 指定本机的主机名,此项必须与web界面配置项一致 |
UnsafeUserParameters | 是否启用自定义监控项,可选值为{1 |
UserParameter | 指定自定义监控脚本参数 |
LogFile | 设置客户端日志文件存放路径 |
4. 部署zabbix
所需环境 | 需要安装的应用 |
---|---|
服务器 IP:192.168.160.100 | lamp架构 , zabbix server , zabbix agent |
客户端:IP:192.168.160.200 | zabbix agent |
4.1 zabbix服务端安装
//安装依赖包
[[email protected] ~]# yum -y install net-snmp-devel libevent-devel
安装过程省略......
//下载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
//创建zabbix用户和组
[[email protected] ~]# groupadd -r zabbix
[[email protected] ~]# useradd -r -g zabbix -s /sbin/nologin -M zabbix
//配置zabbix数据库
mysql> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)
mysql> grant all privileges on *.* to [email protected] identified by 'zabbix123!';
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
[[email protected] mysql]# mysql -uzabbix -pchenwanli! zabbix < schema.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[[email protected] mysql]# mysql -uzabbix -pchenwanli! zabbix < images.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[[email protected] mysql]# mysql -uzabbix -pchenwanli! zabbix < data.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
//编译安装zabbix
[[email protected] ~]# cd /usr/src/zabbix-3.4.12
[[email protected] zabbix-3.4.12]# ./configure --enable-server \
--enable-agent \
--with-mysql \
--with-net-snmp \
--with-libcurl \
--with-libxml2
[[email protected] zabbix-3.4.12]# make install
安装过程省略......
4.2 zabbix服务端配置
//修改服务端配置文件
//设置数据库信息
[[email protected] ~]# cd /usr/local/etc/
[[email protected] etc]# ls
zabbix_agentd.conf zabbix_agentd.conf.d zabbix_server.conf zabbix_server.conf.d
[[email protected] etc]# cat zabbix_server.conf|grep DBPassword
......
DBPassword=zabbix123!
//启动zabbix_server和zabbix_agentd
[[email protected] ~]# zabbix_server
[[email protected] ~]# zabbix_agentd
[[email protected] ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 *:10051 *:*
LISTEN 0 128 127.0.0.1:9000 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
4.3 zabbix服务端web界面安装与配置
4.3.1 zabbix web界面安装前配置
//修改/etc/php.ini的配置并重启php-fpm
[[email protected] ~]# sed -ri 's/(post_max_size =).*/\1 16M/g' /etc/php.ini
[[email protected] ~]# sed -ri 's/(max_execution_time =).*/\1 300/g' /etc/php.ini
[[email protected] ~]# sed -ri 's/(max_input_time =).*/\1 300/g' /etc/php.ini
[[email protected] ~]# sed -i '/;date.timezone/a date.timezone = Asia/Shanghai' /etc/php.ini
[[email protected] ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[[email protected] zabbix-3.4.12]# mkdir /usr/local/apache/htdocs/zabbix
[[email protected] zabbix-3.4.12]# cp -a frontends/php/* /usr/local/apache/htdocs/zabbix/
[[email protected] zabbix-3.4.12]# chown -R apache.apache /usr/local/apache/htdocs/
[[email protected] zabbix-3.4.12]# ll /usr/local/apache/htdocs/
总用量 8
-rw-r--r--. 1 apache apache 45 6月 12 2007 index.html
drwxr-xr-x. 2 apache apache 23 2月 20 16:38 lizhao.com
drwxr-xr-x 13 apache apache 4096 2月 23 00:21 zabbix
//配置apache虚拟主机
[[email protected] ~]# vim /etc/httpd24/httpd.conf
[[email protected] ~]# tail -11 /etc/httpd24/httpd.conf
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs/zabbix"
ServerName www.zabbix.com
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/zabbix/$1
<Directory "/usr/local/apache/htdocs/zabbix">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
//设置zabbix/conf目录的权限,让zabbix有权限生成配置文件zabbix.conf.php
[[email protected] ~]# chmod 777 /usr/local/apache/htdocs/zabbix/conf
[[email protected] ~]# ll -d /usr/local/apache/htdocs/zabbix/conf
drwxrwxrwx 2 apache apache 81 7月 30 2018 /usr/local/apache/htdocs/zabbix/conf
//重启apache
[[email protected] ~]# apachectl -t(检查服务是否有问题)
Syntax OK
[[email protected] ~]# apachectl stop
[[email protected] ~]# apachectl start
[[email protected] ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 *:10051 *:*
LISTEN 0 128 127.0.0.1:9000 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
验证
- 默认账号Admin
- 默认密码zabbix
zabbix客户端配置
//安装依赖包
[[email protected] ~]# yum -y install net-snmp-devel libevent-devel
//编译安装zabbix
[[email protected] ~]# cd /usr/src/
[[email protected] src]# ls
debug kernels zabbix-4.0.3.tar.gz
[[email protected] src]# tar -xf zabbix-4.0.3.tar.gz
[[email protected] src]# ls
debug kernels zabbix-4.0.3 zabbix-4.0.3.tar.gz
/创建zabbix用户和组
[[email protected] ~]# groupadd -r zabbix
[[email protected] ~]# useradd -M -r -s /sbin/nologin -g zabbix zabbix
//编译安装zabbix
[[email protected] ~]# cd /usr/src/zabbix-4.0.3
[[email protected] zabbix-4.0.3]# ./configure --enable-agent
[[email protected] zabbix-4.0.3]# make install
//修改客户端配置文件
[[email protected] ~]# cd /usr/local/etc/
[[email protected] etc]# ls
zabbix_agentd.conf zabbix_agentd.conf.d
[[email protected] etc]# sed -i '/Server=127.0.0.1/s/127.0.0.1/192.168.19.100/g' zabbix_agentd.conf
[[email protected] etc]# sed -i '/ServerActive=127.0.0.1/s/127.0.0.1/192.168.19.100/g' zabbix_agentd.conf
[[email protected] etc]# sed -i '/Hostname=Zabbix server/s/Zabbix server/wxl/g' zabbix_agentd.conf
//启动服务
[[email protected] ~]# zabbix_agentd
[[email protected] ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100
解决监控中文乱码
[[email protected] ~]# cd /usr/local/apache/htdocs/zabbix/fonts/
[[email protected] fonts]# ls
DejaVuSans.ttf simkai.ttf
//usr/local/apache/htdocs,此处路径为网页存放的路径,并非唯一
//进入/usr/local/apache/htdocs/zabbix/include,修改defines.inc.php
[[email protected] include]# vim defines.inc.php
define('ZBX_GRAPH_FONT_NAME', 'simkai'); // font file name
(//将原先的DejaVuSans,替换为simkai)
//重启zabbix
[[email protected] include]# pkill zabbix
[[email protected] include]# zabbix_server
[[email protected] include]# zabbix_agentd :::*
zabbix主机和主机组的添加
添加主机组
添加主机
zabbix模板
链接指示器选择Tenmplate OS Linux
查看监控项
查看触发器
文件监控
添加监控项
[[email protected] ~]# touch /opt/wxl
[[email protected] ~]# cd /opt/
[[email protected] opt]# chown zabbix.zabbix wxl
[[email protected] opt]# ll
总用量 0
-rw-r--r--. 1 zabbix zabbix 0 2月 28 02:09 wxl
添加触发器并测试
[[email protected] ~]# echo “xxx” > /opt/wxl
服务进程监控
客户端
[[email protected] ~]# mkdir /scripts
[[email protected] ~]# cd /scripts/
[[email protected] scripts]# vim process.sh
[[email protected] scripts]# cat process.sh
#!/bin/bash
a=$(ps -ef|egrep -v "grep|$0"|grep $1|wc -l)
if [ $a -eq 0 ];then
echo 1
else
echo 0
fi
[[email protected] scripts]# chmod 755 process.sh
[[email protected] scripts]# cd ..
[[email protected] /]# chown -R zabbix.zabbix scripts/
[[email protected] ~]# sed -i '/# UnsafeUserParameters=0/s/#//g' /usr/local/etc/zabbix_agentd.conf
[[email protected] ~]# sed -i '/ UnsafeUserParameters=0/s/0/1/g' /usr/local/etc/zabbix_agentd.conf
[[email protected] ~]# echo "UserParameter=check_apache,/scripts/process.sh vsftpd" >> /usr/local/etc/zabbix_agentd.conf
[[email protected] ~]# pkill zabbix
[[email protected] ~]# zabbix_agentd
添加ftp监控项
添加触发器
测试结果
[[email protected] ~]# systemctl stop apache
进程用命令行监控
[[email protected] ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[[email protected] ~]# zabbix_get -s 192.168.204.100 -k check_ftp
1
日志监控
[[email protected] ~]# cd /scripts/
[[email protected] scripts]#wget https://github.com/chendao2015/pyscripts/blob/master/log.py //下载python脚本
[[email protected] scripts]# ls
log.py process.sh
[[email protected] scripts]# chown zabbix.zabbix log.py
[[email protected] scripts]# ll
总用量 8
-rw-r–r--. 1 zabbix zabbix 1854 2月 25 20:45 log.py
-rwxr-xr-x. 1 zabbix zabbix 102 2月 25 20:32 process.sh
[[email protected] ~]# echo “UserParameter=check_log_apache,python /scripts/log.py /usr/local/apache/logs/error_log” >> /usr/local/etc/zabbix_agentd.conf
[[email protected] ~]# pkill zabbix
[[email protected] ~]# zabbix_agentd
[[email protected] ~]# cd /usr/local/apache/logs/
[[email protected] logs]# ll -d
-rw-r–r--. 1 root root 711 2月 27 23:40 .
[[email protected] logs]# cd …
[[email protected] apache]# chmod 755 logs/
[[email protected] apache]# cd logs/
[[email protected] logs]# ll -d
drwxr-xr-x. 2 root root 41 2月 27 23:40 .
添加监控项
添加触发器
测试结果
[[email protected] ~]# echo "Error" >> /usr/local/apache/logs/error_log
命令行监控
服务端
[[email protected] ~]# zabbix_get -s 192.168.19.150 -k check_log_apache
1
邮件告警
服务端
停止postfix和sendmail服务
[[email protected] ~]# systemctl stop postfix
[[email protected] ~]# systemctl disable postfix
Removed symlink /etc/systemd/system/multi-user.target.wants/postfix.service.
安装mailx
[[email protected] ~]# yum -y install mailx
添加配置文件
[[email protected] ~]# vim /etc/mail.rc
在末尾添加:
set from=143423******@qq.com (发件人邮箱地址)
set smtp=smtp.qq.com (邮箱服务器地址)
set smtp-auth-user=143423*****@qq.com (发件人邮箱地址)
set smtp-auth-password=***********(邮箱密码/邮箱授权码)
set smtp-auth=login
手动测试发送邮件
[[email protected] ~]# echo 'W T F' |mail -s 'zabbix' [email protected]
添加告警媒介
添加用户
添加出发动作
创建问题解决成功发送邮件
间隔时间–>标题–>内容
创建告警目录
[[email protected] ~]# mkdir /usr/local/etc/alertscripts
[[email protected] ~]# ll /usr/local/etc/
总用量 28
drwxr-xr-x. 2 root root 6 2月 26 16:34 alertscripts
-rw-r--r--. 1 root root 10630 2月 22 15:28 zabbix_agentd.conf
drwxr-xr-x. 2 root root 6 2月 22 15:28 zabbix_agentd.conf.d
-rw-r--r--. 1 root root 16373 2月 26 16:34 zabbix_server.conf
drwxr-xr-x. 2 root root 6 2月 22 15:28 zabbix_server.conf.d
在告警目录写脚本
[[email protected] ~]# cd /usr/local/etc/alertscripts/
[[email protected] alertscripts]# vim mail.sh
#!/bin/bash
echo $3|mail -s $2 $1
($1是message信息,$2是subuject标题,$1是sendto收件人)
给脚本执行权限和修改属组属主
[[email protected] alertscripts]# chmod +x mail.sh
[[email protected] alertscripts]# ll mail.sh
-rwxr-xr-x. 1 root root 34 2月 26 16:42 mail.sh
[[email protected] alertscripts]# cd ..
[[email protected] etc]# chown -R zabbix.zabbix alertscripts/
[[email protected] etc]# ll -d alertscripts/
drwxr-xr-x. 2 zabbix zabbix 21 2月 26 16:42 alertscripts/
服务端配置文件
[[email protected] ~]# vim /usr/local/etc/zabbix_server.conf
# Mandatory: no
# Default:
# AlertScriptsPath=${datadir}/zabbix/alertscripts
AlertScriptsPath=/usr/local/etc/alertscripts (添加此行)
重启服务
[[email protected] etc]# pkill zabbix
[[email protected] etc]# zabbix_server
[[email protected] etc]# zabbix_agentd
测试
[[email protected] opt]# echo 'hahahahaha' >> /opt/wxl