运维之路-监控篇-Zabbix的安装和使用
安装环境说明:
此次安装环境,系统为centos7,zabbix版本为5.0,centos7最高支持的zabbix为5.0版本,如欲安装更高版本的zabbix,需centos8或者centos9。

一、安装相关的软件包
安装前最好更新一下系统软件
yum -y update
1.1 安装httpd和php
yum install -y httpd php
1.2 安装php扩展
yum install –y php-mysql php-snmp php-xml php-ldap php-gd php-mbstring php-posix
1.3 设置php,据系统实际情况设定时区
[root@localhost ~]# vim /etc/php.ini
[PHP]
……
date.timezone = Asia/Shanghai
1.4 启动httpd并设置开机启动
systemctl start httpd && systemctl enable httpd
1.5 安装MariaDB 10.3数据库
yum -y install mariadb mariadb-server
1.6 启动mysql数据库服务并设为开机启动
systemctl start mariadb && systemctl enable mariadb
1.7 安装net-snmp
yum install –y net-snmp net-snmp-libs net-snmp-utils net-snmp-devel net-snmp-perl
1.8 启动snmp并设置开机启动
systemctl start snmpd.service && systemctl enable snmpd.service
二、安装zabbix
2.1 下载软件包
rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release- 5.0-1.el7.noarch.rpm
yum clean all
2.2 安装zabbix的包
yum install -y zabbix-server-mysql zabbix-agent zabbix-get zabbix-sender zabbix-web centos-release-scl
2.3 编辑配置文件
vim /etc/yum.repos.d/zabbix.repo,将enabled=0改为=1

2.4 安装zabbix前端包
yum install -y zabbix-web-mysql-scl zabbix-apache-conf-scl
2.5 创建一个zabbix库并设置为utf8的字符编码格式
mysql -uroot
create database zabbix character set utf8 collate utf8_bin;
2.6 创建账户并且授权设置密码
create user zabbix@localhost identified by 'zabbix';
grant all privileges on zabbix.* to zabbix@localhost;
flush privileges;
2.7 导入表
导入初始架构和数据,password就是上面步骤中设置的密码
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix
2.8 为Zabbix server配置数据库
编辑配置文件,将DBPassword修改为=zabbix
vim /etc/zabbix/zabbix_server.conf

2.9 为Zabbix前端配置修改时区
编辑配置文件 ,修改匹配自己的城市
vim /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf

2.10 启动Zabbix server和agent进程
启动Zabbix server和agent进程,并为它们设置开机自启:
systemctl restart zabbix-server zabbix-agent httpd rh-php72-php-fpm
systemctl enable zabbix-server zabbix-agent httpd rh-php72-php-fpm
2.11 重启相关服务
systemctl restart httpd.service && systemctl restart mariadb.service && service httpd restart && service mariadb restart && systemctl restart snmpd.service
三、禁用防火墙和selinux
3.1 禁用防火墙 (如果是跑在公网的建议放行相关端口,不禁用防火墙)
systemctl stop firewalld.service
systemctl disable firewalld
3.2 禁用selinux
否则会报下图这个错
Zabbix server is not running:the information displayed may not be current

vim /etc/sysconfig/selinux,将SELINUX修改为=disabled,然后重启,shutdown -r now,该操作重启后才会生效,所以一定要重启系统;
四、登陆zabbix网址设置
http://ip/zabbix
初始登录账号Admin,密码zabbix
五、zabbix添加交换机端口监控
实验环境说明:
zabbix安装在vmware虚拟机中,在华为ensp中使用虚拟交换机通过vmware的虚拟网卡实现网络层zabbix服务器和虚拟交换机的互通
5.1 添加主机
主机名称中不能有中文,可见名称中可以有中文,群组选Templates/network devices,移除客户端接口,添加SNMP接口,输入要监控的设备和snmp团体属性值



5.2 模板中第一次添加设备时需要选择主机群组,选择Templates/network devices,模板选择Template Net Network Generic Device SNMP


5.3 宏里输入{$SNMP_COMMUNITY},值等于public,然后添加,这个public就是交换机上snmp配置的团体属性;

5.4 创建监控图形;
进入主机配置页面,选择图形---创建图形,此处以ping监测可达性,丢包率及响应时间为例;



5.5 被监控设备配置,以华为交换机为例
snmp-agent sys-info version v2c
snmp-agent community write cipher public
snmp-agent target-host inform address udp-domain 192.168.141.141(zabbix服务器的ip地址) params securityname public v2c
5.6 结果验证
图形ping监测是我自己加的,下面的接口流量图是自动生成的(一般主机添加后5到10分钟自动生成,前提是整个安装过程没有出错),也可以使用过滤器查看特定的图形。

六、图形不显示中文的处理方法
6.1 先找到字体所在目录
find / -name graphfont.ttf

6.2 安装中文字体(系统一般都是安装好的,不用再装)
yum install -y wqy-microhei-fonts.noarch
更新字体
update-alternatives --install /usr/share/zabbix/assets/fonts/graphfont.ttf \zabbix-web-font /usr/share/fonts/wqy-microhei/wqy-microhei.ttc 10

6.3 重新选择字体
update-alternatives --config zabbix-web-font,输入2

6.4 重启服务
systemctl restart zabbix-server