#### zabbix5.0安装步骤
安装前准备工作:关闭防火墙与SELinux,配置本地yum源
```shell
#关闭防火墙
[root@zabbix-server ~]# systemctl stop firewalld
[root@zabbix-server ~]# systemctl disable firewalld
#关闭SELinux
[root@zabbix-server ~]# setenforce 0
[root@zabbix-server ~]# vim /etc/selinux/config
...
SELINUX=disabled
#搭建本地yum源
[root@zabbix-server ~]# mkdir /mnt/centos
[root@zabbix-server ~]# mount /dev/cdrom /mnt/centos
[root@zabbix-server ~]# rm -rf /etc/yum.repos.d/*
[root@zabbix-server ~]# vim /etc/yum.repos.d/local.repo
[local]
name=local_centos
baseurl=file:///mnt/centos
enabled=1
gpgcheck=0
```
#### 配置zabbix的存储库
```shell
#下载zabbix5.0存储库
[root@zabbix-server ~]# wget https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
#安装存储库
[root@zabbix-server ~]# rpm -ivh zabbix-release-5.0-1.el7.noarch.rpm
#修改zabbix仓库文件
[root@zabbix-server ~]# vim /etc/yum.repos.d/zabbix.repo
[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
[zabbix-frontend]
name=Zabbix Official Repository frontend - $basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/$basearch/frontend
enabled=0
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
[zabbix-debuginfo]
name=Zabbix Official Repository debuginfo - $basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/$basearch/debuginfo/
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
gpgcheck=0
[zabbix-non-supported]
name=Zabbix Official Repository non-supported - $basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/non-supported/rhel/7/$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck=0
#安装zabbix软件包
[root@zabbix-server ~]# yum install zabbix-server-mysql zabbix-agent
#创建阿里源
[root@zabbix-server ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
#下载CentOS软件包收藏
[root@zabbix-server ~]# yum -y install centos-release-scl
#启用zabbix-frontend存储库
root@zabbix-server ~]# vim /etc/yum.repos.d/zabbix.repo
...
[zabbix-frontend]
name=Zabbix Official Repository frontend - $basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/$basearch/frontend
enabled=1 #启用仓库
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
#安装Zabbix前端软件包
[root@zabbix-server ~]# yum install zabbix-web-mysql-scl zabbix-apache-conf-scl
#安装数据库软件
[root@zabbix-server ~]# yum -y install mariadb-server
#启动数据库服务
[root@zabbix-server ~]# systemctl start mariadb
#设置随机自启
[root@zabbix-server ~]# systemctl enable mariadb
#进入数据库
[root@zabbix-server ~]# mysql
#创建存储数据的库,并支持中文(库名:zabbix)
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)
#授权连接数据库的用户,并设置密码(用户名:zabbix)
MariaDB [(none)]> create user zabbix@localhost identified by '123456';
Query OK, 0 rows affected (0.00 sec)
#数据库授权
MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost;
Query OK, 0 rows affected (0.00 sec)
#查看所有库
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| zabbix |
+--------------------+
5 rows in set (0.01 sec)
#进入zabbix库
MariaDB [(none)]> use zabbix;
Database changed
#查看当前库下表
MariaDB [zabbix]> show tables;
Empty set (0.00 sec) #空
#在Zabbix服务器主机上,导入初始架构和数据。系统将提示您输入新创建的密码
[root@zabbix-server ~]# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix
Enter password: password #输入zabbix用户密码
#为Zabbix服务器配置数据库
[root@zabbix-server ~]# vim /etc/zabbix/zabbix_server.conf
...
100 DBName=zabbix #存储监控数据的库名
116 DBUser=zabbix #连接数据库的用户
124 DBPassword=123456 #设置zabbix用户密码
#为Zabbix配置正确的时区
[root@zabbix-server ~]# vim /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
...
php_value[date.timezone] = Asia/Shanghai #亚洲/上海
#启动所有服务
root@zabbix-server ~]# systemctl restart zabbix-server zabbix-agent httpd rh-php72-php-fpm
#设置服务随机自启
[root@zabbix-server ~]# systemctl enable zabbix-server zabbix-agent httpd rh-php72-php-fpm
#查看zabbix服务占用端口信息
[root@zabbix-server ~]# ss -anptul | grep zabbix
tcp LISTEN 0 128 *:10051 #zabbix-server端口
tcp LISTEN 0 128 *:10050 #zabbix-anent端口
#访问zabbix的web界面:
Http://192.168.0.24/zabbix
```
#### 解决zabbix中文乱码
```shell
#进入zabbix默认存放语言文件路径
[root@zabbix-server]# cd /usr/share/zabbix/assets/fonts/
#安装传输工具
[root@zabbix-server fonts]# yum -y install lrzsz
#将windows语言文件覆盖zabbix默认语言文件
[root@zabbix-server fonts]# cp simhei.ttf graphfont.ttf
cp:是否覆盖"graphfont.ttf"? y
#刷新web页面
```
#### 部署被监控端
```shell
#安装下载工具
[root@web-26 ~]# yum -y install wget
#下载zabbix-agent软件包
[root@web-26 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-agent-5.0.0-1.el7.x86_64.rpm
#查询软件包安装到系统中文件
[root@web-26 ~]# rpm -ql zabbix-agent
...
/etc/zabbix/zabbix_agentd.conf
#修改agent配置文件
[root@web-26 ~]# vim /etc/zabbix/zabbix_agentd.conf
...
119 Server=192.168.0.24 #指定zabbix-server地址
#启动服务并设置服务随机自启
[root@web-26 ~]# systemctl start zabbix-agent
[root@web-26 ~]# systemctl enable zabbix-agent
#下载传输工具
[root@web-26 ~]# yum -y install lrzsz
#安装nginx依赖包
[root@web-26 ~]# yum -y install gcc pcre-devel openssl-devel
#解压源码包并进入源码包路径
[root@web-26 nginx-1.18.0]# tar -xf nginx-1.18.0.tar.gz
[root@web-26 nginx-1.18.0]# cd nginx-1.18.0
#编译源码包&&安装nginx状态模块
[root@web-26 nginx-1.18.0]# ./configure --with-http_stub_status_module
#安装源码包
[root@web-26 nginx-1.18.0]# make && make install
#修改nginx配置文件,开启状态页面功能
[root@web-26 nginx-1.18.0]# vim /usr/local/nginx/conf/nginx.conf
...
location /status {
stub_status on; #开启状态页面功能
}
#启动nginx服务
[root@web-26 nginx-1.18.0]# /usr/local/nginx/sbin/nginx
#访问测试
http://192.168.0.26/
http://192.168.0.26/status #访问状态页面
#命令行查看nginx并发连接数
[root@web-26 nginx-1.18.0]# curl http://192.168.0.26/status
Active connections: 2
server accepts handled requests
11 11 6
Reading: 0 Writing: 1 Waiting: 1
#解释:
active #当前活动用户的连接数量
accepts #已接收客户端连接总数量
handled #已处理的连接总数
reading #正在请求连接的用户数量
writing #正在响应的用户数量
waiting #当前客户端正在等待服务器的响应数量
requests #客户端请求总数
```
#### 自动义监控
```shell
#命令行获取nginx状态值
[root@web26 ~]# curl http://192.168.0.26/status
Active connections: 1
server accepts handled requests
57 57 44
Reading: 0 Writing: 1 Waiting:
#编写脚本,通过脚本将各种状态的值取出来,然后通过zabbix监控
[root@web26 ~]# mkdir /etc/zabbix/script/
[root@web26 ~]# vim /etc/zabbix/script/ngx_status.sh
#定义Nginx status页面
ngx_status="http://127.0.0.1/status"
#判断status页面是否存活
ngx_status_code() {
http_code=`curl -o /dev/null -s -w %{http_code} ${ngx_status}`
if [ ${http_code} == "200" ];then
return 1
else
echo "Nginx status is not running."
fi
}
#获取当前活动的客户端连接数
active() {
ngx_status_code || curl -s ${ngx_status} | grep "Active" | awk '{print $NF}'
}
#获取接收客户端连接的总数量
accepts() {
ngx_status_code || curl -s ${ngx_status} | awk NR==3 | awk '{print $1}'
}
#获取已处理的连接总数量
handled() {
ngx_status_code || curl -s ${ngx_status} | awk NR==3 | awk '{print $2}'
}
#获取客户端请求总数量
requests() {
ngx_status_code || curl -s ${ngx_status} | awk NR==3 | awk '{print $3}'
}
#获取正在读取请求标头的当前连接数量
reading() {
ngx_status_code || curl -s ${ngx_status} | grep "Reading" | awk '{print $2}'
}
#获取正在将响应写回到客户端的当前连接数量
writing() {
ngx_status_code || curl -s ${ngx_status} | grep "Writing" | awk '{print $2}'
}
#获取当前正在等待响应的客户端连接数量
waiting() {
ngx_status_code || curl -s ${ngx_status} | grep "Waiting" | awk '{print $2}'
}
#使用位置变量控制脚本输出
case $1 in
active)
active;;
accepts)
accepts;;
handled)
handled;;
requests)
requests;;
reading)
reading;;
writing)
writing;;
waiting)
waiting;;
*)
echo "Unknown options"
esac
#添加执行权限
[root@web26 script]# chmod +x ngx_status.sh
#修改zabbix-agent配置文件,开启以定义监控功能
[root@web-26 nginx-1.18.0]# vim /etc/zabbix/zabbix_agentd.conf
...
Include=/etc/zabbix/zabbix_agentd.d/*.conf #自定监控路径
UnsafeUserParameters=1 #开启自定义监控
#进入自定义监控路径
[root@web-26 nginx-1.18.0]# cd /etc/zabbix/zabbix_agentd.d/
#自定义监控项
[root@web-26 zabbix_agentd.d]# vim userparameter_nginx.conf
UserParameter=nginx.active,bash /etc/zabbix/script/ngx_status.sh active
UserParameter=nginx.accepts,bash /etc/zabbix/script/ngx_status.sh accepts
UserParameter=nginx.handled,bash /etc/zabbix/script/ngx_status.sh handled
UserParameter=nginx.requests,bash /etc/zabbix/script/ngx_status.sh requests
UserParameter=nginx.reading,bash /etc/zabbix/script/ngx_status.sh reading
UserParameter=nginx.writing,bash /etc/zabbix/script/ngx_status.sh writing
UserParameter=nginx.waiting,bash /etc/zabbix/script/ngx_status.sh waiting
#解释
UserParameter= #自定义监控固定格式
nginx. #监控项名称
#重启zabbix-agent服务
[root@web26 zabbix_agentd.d]# systemctl restart zabbix-agent
```
#### 服务端获取数据
```shell
#下载软件包
[root@zabbix-server ~]# yum -y install zabbix-get
#获取自定义监控项值
[root@zabbix-server ~]# zabbix_get -s 192.168.0.26 -k nginx.active #当前活动用户的连接数量
[root@zabbix-server ~]# zabbix_get -s 192.168.0.26 -k nginx.accepts #已接收客户端连接总数量
[root@zabbix-server ~]# zabbix_get -s 192.168.0.26 -k nginx.handled #已处理的连接总数
[root@zabbix-server ~]# zabbix_get -s 192.168.0.26 -k nginx.reading #正在请求连接的用户数量
[root@zabbix-server ~]# zabbix_get -s 192.168.0.26 -k nginx.writing #正在响应的用户数量
[root@zabbix-server ~]# zabbix_get -s 192.168.0.26 -k nginx.waiting #当前客户端正在等待服务器的响应数量
[root@zabbix-server ~]# zabbix_get -s 192.168.0.26 -k nginx.requests #客户端请求总数
```
#### grafana展示数据
官网:https://grafana.com 开源数据可视化工具
1.从清华源下载grafana软件包
```shell
[root@zabbix-server fonts]# wget https://mirrors.tuna.tsinghua.edu.cn/grafana/yum/rpm/grafana-7.3.6-1.x86_64.rpm
```
2.安装grafana
```shell
[root@zabbix-server ~]# yum -y install grafana-7.3.6-1.x86_64.rpm
```
3.安装ntpdate同步阿里云时间服务器
```shell
[root@zabbix-server ~]# yum -y install ntpdate
[root@zabbix-server ~]# ntpdate ntp.aliyun.com
```
4.启动grafana服务
```shell
[root@zabbix-server ~]# systemctl start grafana-server.service
[root@zabbix-server ~]# systemctl enable grafana-server.service
```
5.查看grafana监听端口
```shell
[root@zabbix-server ~]# ss -ntlp | grep grafana
tcp LISTEN 0 128 :::3000 :::* users:(("grafana-
```
6.安装zabbix插件
```shell
[root@zabbix-server ~]# grafana-cli plugins install alexanderzobnin-zabbix-app
[root@zabbix-server ~]# systemctl restart grafana-server.service #重启服务
```
7.访问grafana的web页面,默认用户名:admin 默认密码:admin




