zabbix的安装部署
一、zabbix 环境部署:
1、配置IP和主机名、关闭防火墙
[root@localhost ~]# hostnamectl set-hostname zabbix-server
[root@zabbix-server ~]# systemctl stop firewalld
[root@zabbix-server ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@zabbix-server ~]# sed -i 's/=enforcing/=disabled/' /etc/sysconfig/selinux
[root@zabbix-server ~]# sed -i 's/=enforcing/=disabled/' /etc/selinux/config
[root@zabbix-server ~]# setenforce 0
安装常用软件:
[root@zabbix-server ~]# yum install net-tools bash-completion wget vim ntpdate -y
时间同步:
[root@zabbix-server ~]# ntpdate cn.pool.ntp.org
18 Feb 11:15:44 ntpdate[2701]: step time server 185.209.85.222 offset -278.244560 sec
[root@zabbix-server ~]# hwclock -w
准备LAMP环境:
[root@zabbix-server ~]# yum install httpd mariadb mariadb-server php php-mysql php-gd -y
国内 zabbix 源总结 目前发现的有以下几个站点: 1.阿里巴巴开源镜像站(推荐使用) 地址:https://mirrors.aliyun.com/zabbix/ 2.华为开源镜像站(推荐使用) 地址:https://mirrors.huaweicloud.com/zabbix/ 3.清华大学开源软件镜像站 地址:https://mirror.tuna.tsinghua.edu.cn/zabbix/ 4.上海大学开源镜像站 地址:https://mirrors.shu.edu.cn/zabbix/ 以上几个可根据自身网络情况选择使用
配置 zabbix yum 源
以使用阿里巴巴开源镜像站为例子,介绍如何使用 zabbix 4.0 源
添加源:
[root@zabbix-server yum.repos.d]# cat zabbix.repo [zabbix] name=Zabbix Official Repository - \$basearch baseurl=https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/\$basearch/ enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591 [zabbix-non-supported] name=Zabbix Official Repository non-supported - \$basearch baseurl=https://mirrors.aliyun.com/zabbix/non-supported/rhel/7/\$basearch/ enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX gpgcheck=1
添加gpgkey:
[root@zabbix-server yum.repos.d]# curl https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX-A14FE591 -o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
[root@zabbix-server yum.repos.d]# curl https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX -o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
添加后执行 yum makecache -y
2、安装zabbix server
安装相关软件包:
[root@zabbix-server ~]# yum install -y zabbix-server-mysql zabbix-web-mysql zabbix-agent
[root@zabbix-server ~]# rpm -aq | grep zabbix
修改php时区:
[root@zabbix-server ~]# sed -i 's@# php_value date.timezone Europe/Riga@php_value date.timezone Asia/Shanghai@g' /etc/httpd/conf.d/zabbix.conf
3、数据库设置
启动数据库:
[root@zabbix-server ~]# systemctl start mariadb
[root@zabbix-server ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
创建zabbix数据库及用户:
[root@zabbix-server ~]# mysql -e "create database zabbix character set utf8;"
[root@zabbix-server ~]# mysql -e "grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix';"
[root@zabbix-server ~]# mysqladmin flush-privileges
导入数据库:
[root@zabbix-server ~]# cd /usr/share/doc/zabbix-server-mysql-4.0.17/
[root@zabbix-server zabbix-server-mysql-4.0.17]# zcat create.sql.gz | mysql -uzabbix -p zabbix
Enter password:
[root@zabbix-server zabbix-server-mysql-4.0.17]# mysql -uzabbix -p -e 'use zabbix;show tables;'
4、修改zabbix配置文件,修改主要参数即可正常工作:
vim /etc/zabbix/zabbix_server.conf
[root@zabbix-server zabbix-server-mysql-4.0.17]# grep ^DB /etc/zabbix/zabbix_server.conf
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix
5、启动zabbix及apache
[root@zabbix-server ~]# systemctl start zabbix-server.service httpd.service
[root@zabbix-server ~]# systemctl enable zabbix-server.service httpd.service
[root@zabbix-server ~]# netstat -nltup|egrep '80|10051'
tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 3320/zabbix_server
tcp6 0 0 :::80 :::* LISTEN 3318/httpd
tcp6 0 0 :::10051 :::* LISTEN 3320/zabbix_server
6、web 界面安装master
http://192.168.131.139/zabbix/setup.php
前面没有错误的话,nextstep,直到以下页面;
默认登录用户admin;
密码是/etc/zabbix/zabbix_server.conf 中DBPassword=zabbix
成功进入页面:
修改密码及语言:
现在用户名是:admin
密码是:123456
注意:登录上去立即修改密码
7、菜单说明
二、监控 Linux 服务器 192.168.131.137
1、Linux 服务器安装agent 代理程序
监控方式很多,比如 snmp、ssh、telent 脚本、agent 等,本例使用 agent 实现监控;
安装常用软件:
[root@localhost ~]# yum install net-tools bash-completion wget vim ntpdate -y
时间同步、配置主机名、关闭防火墙
[root@localhost ~]# ntpdate cn.pool.ntp.org [root@localhost ~]# hwclock -w [root@localhost ~]# hostnamectl set-hostname zabbix-agent [root@localhost ~]# su -l Last login: Tue Dec 24 05:28:05 CST 2019 from 192.168.131.1 on pts/1 [root@zabbix-agent ~]# systemctl stop firewalld [root@zabbix-agent ~]# systemctl disable firewalld Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. [root@zabbix-agent ~]# sed -i 's/=enforcing/=disabled/' /etc/sysconfig/selinux [root@zabbix-agent ~]# sed -i 's/=enforcing/=disabled/' /etc/selinux/config [root@zabbix-agent ~]# setenforce 0
2、安装zabbix yum 源
[zabbix] name=Zabbix Official Repository - \$basearch baseurl=https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/\$basearch/ enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591 [zabbix-non-supported] name=Zabbix Official Repository non-supported - \$basearch baseurl=https://mirrors.aliyun.com/zabbix/non-supported/rhel/7/\$basearch/ enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX gpgcheck=1
添加gpgkey
[root@zabbix-agent ~]# curl https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX-A14FE591 -o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
[root@zabbix-agent ~]# curl https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX -o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
添加后 yum makecache -y
3、安装zabbix-agent 并修改配置文件:
[root@zabbix-agent ~]# yum install zabbix-agent.x86_64 -y
Server=192.168.131.139 被动 (zabbix-server 地址)
ServerActive=192.168.131.137 主动
Hostname=zabbix-agnet
4、开启zabbix_agent
[root@zabbix-agent ~]# systemctl start zabbix-agent.service [root@zabbix-agent ~]# systemctl enable zabbix-agent.service Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.
5、查看端口
[root@zabbix-agent ~]# netstat -nltup|grep 10050 tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 7715/zabbix_agentd tcp6 0 0 :::10050 :::* LISTEN 7715/zabbix_agentd
6、测试zabbix 客户端与zabbix 服务的通信是否正常
注意:以下命令在zabbix-server 端执行:
[root@zabbix-server ~]# yum install zabbix-get.x86_64 -y [root@zabbix-server ~]# zabbix_get -s 192.168.131.137 -k system.uptime 4421
三、在 web 界面添加监控主机及设置
1、添加监控主机:
2、创建自定义模板:
3、用户自定义监控:
在客户端配置文件 /etc/zabbix/zabbix_agentd.conf, 里面配置 UserParameter;
语法: UserParameter=key,command 用户自定义参数包含一个 key 和一个命令,key 必须整个系统唯一,配置好之后,重启客户端。
然后配置 item,在 key 的位置填上我们自定义的 key 即可。
例如:监控 Linux 服务器根分区磁盘使用百分比
分析:先在客户端测试命令 [root@agent1 ~]# df -hP |awk '/\/$/ {print +$(NF-1)}' 40 在客户端配置文件添加以下内容,并重启 agent [root@zabbix-agent zabbix_agentd.d]#/etc/zabbix/zabbix_agentd.d/disk_root.conf UserParameter=Per_of_root,/bin/df -hP | awk '/\/$/ {print +$(NF-1)}' [root@zabbix-agent zabbix_agentd.d]# systemctl restart zabbix-agent Zabbix 服务器端测试: [root@Zabbix-server ~]# zabbix_get -s 192.168.131.137 -k Per_of_root 40
注意:和 shell 脚本不同的是,名称执行成功返回 1,失败返回 0
如下为灵活的用户自定义参数
UserParameter=key[*],command
参数 描述 Key 唯一. [*]表示里面可以传递多个参数 Command 需要执行的脚本, key 的[]里面的参数一一对应$1 到$9,一共 9 个参数。 $0 表 示脚本命令. 注意事项: 1. 如果需要使用命令行里面出现$2 这种变量,那么你要使用两个$$2,例如 awk ’{ print $$2 }’,之前 就遇到过这个问题,不停的测试自己脚本输出正常,但是 zabbix 却拿不到数据,原来是出在这里。 为了防止和参数冲突,所以 zabbix 做了这个规定。 2. zabbix 禁止使用一些不安全的参数,如下: \ ' ” ` * ? [ ] { } ~ $ ! & ; ( ) < > | # @ 3. 从 zabbix 2.0 开始,zabbix 返回文本数据可以是空格。
4、配置自定义监控项:
cpuUsed=top -n 1|awk -F'[, %]+' ' NR==3 {print 100-$11}'
100
top -n 1|awk -F '[, %]+' 'NR==3 {print100-$11}
mail.example.com", SMTP HELO: "example.com", SMTP电邮: "zabbix@example.com
df -h|awk '/^/dev/sda1/{print 1}'