zabbix下载地址:下载Zabbix
一、zabbix的安装与配置 1.安装 Zabbix repository[root@server5 ~]# rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm [root@server5 ~]# cd /etc/yum.repos.d/ [root@server5 yum.repos.d]# ls dvd.repo redhat.repo zabbix.repo [root@server5 yum.repos.d]# cat zabbix.repo [zabbix] name=Zabbix Official Repository - $basearch baseurl=http://repo.zabbix.com/zabbix/5.0/rhel/7/$basearch/ enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591 [zabbix-frontend] name=Zabbix Official Repository frontend - $basearch baseurl=http://repo.zabbix.com/zabbix/5.0/rhel/7/$basearch/frontend enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591 [zabbix-debuginfo] name=Zabbix Official Repository debuginfo - $basearch baseurl=http://repo.zabbix.com/zabbix/5.0/rhel/7/$basearch/debuginfo/ enabled=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591 gpgcheck=1 [zabbix-non-supported] name=Zabbix Official Repository non-supported - $basearch baseurl=http://repo.zabbix.com/non-supported/rhel/7/$basearch/ enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX gpgcheck=12.安装Zabbix server 和agent
yum install zabbix-server-mysql zabbix-agent
3.依赖包在阿里云下载:阿里巴巴开源镜像站-阿里云官网开发者社区_云计算社区
编辑配置文件 /etc/yum.repos.d/zabbix.repo
[root@server5 yum.repos.d]# vim zabbix.repo [zabbix-frontend] name=Zabbix Official Repository frontend - $basearch baseurl=http://repo.zabbix.com/zabbix/5.0/rhel/7/$basearch/frontend enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591安装Zabbix frontend packages
[root@server5 yum.repos.d]# yum install zabbix-web-mysql-scl zabbix-apache-conf-scl
4.创建初始数据库 安装数据库
tar xf mysql-5.7.35-1.el7.x86_64.rpm-bundle.tar
[root@server5 ~]# ls anaconda-screenshots mysql-community-embedded-5.7.35-1.el7.x86_64.rpm centos-release-scl-2-3.el7.centos.noarch.rpm mysql-community-embedded-compat-5.7.35-1.el7.x86_64.rpm centos-release-scl-rh-2-3.el7.centos.noarch.rpm mysql-community-embedded-devel-5.7.35-1.el7.x86_64.rpm mysql-5.7.35-1.el7.x86_64.rpm-bundle.tar mysql-community-libs-5.7.35-1.el7.x86_64.rpm mysql-community-client-5.7.35-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.35-1.el7.x86_64.rpm mysql-community-common-5.7.35-1.el7.x86_64.rpm mysql-community-server-5.7.35-1.el7.x86_64.rpm mysql-community-devel-5.7.35-1.el7.x86_64.rpm mysql-community-test-5.7.35-1.el7.x86_64.rpm [root@server5 ~]# yum install mysql-community-client-5.7.35-1.el7.x86_64.rpm mysql-community-common-5.7.35-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.35-1.el7.x86_64.rpm mysql-community-server-5.7.35-1.el7.x86_64.rpm mysql-community-libs-5.7.35-1.el7.x86_64.rpm [root@server5 ~]# systemctl start mysqld
在数据库主机上运行以下代码:初始化数据库并设置密码
[root@server5 ~]# mysql_secure_installation ##修改密码Westos+007
[root@server5 ~]# mysql -p mysql> create database zabbix character set utf8 collate utf8_bin; Query OK, 1 row affected (0.00 sec) mysql> create user zabbix@localhost identified by 'Westos+007'; Query OK, 0 rows affected (0.00 sec) mysql> grant all privileges on zabbix.* to zabbix@localhost; Query OK, 0 rows affected (0.00 sec)导入初始架构和数据:
[root@server5 ~]# cd /usr/share/doc/zabbix-server-mysql-5.0.18/ [root@server5 zabbix-server-mysql-5.0.18]# ls AUTHORS ChangeLog COPYING create.sql.gz double.sql NEWS README [root@server5 zabbix-server-mysql-5.0.18]# zcat create.sql.gz | mysql -uzabbix -p zabbix ##导入初始架构和数据,系统将提示您输入新创建的密码。 [root@server5 zabbix-server-mysql-5.0.18]# mysql -p ##导入完成后查看 mysql> use zabbix mysql> show tables;5. 为Zabbix server配置数据库
编辑配置文件 /etc/zabbix/zabbix_server.conf
[root@server5 zabbix]# vim zabbix_server.conf DBPassword=Westos+007 [root@server5 zabbix]# systemctl enable --now zabbix-server.service zabbix-agent.service [root@server5 zabbix]# ps ax | grep zabbix6. 为Zabbix前端配置PHP
[root@server5 zabbix]# vim /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf ##为Zabbix前端配置PHP php_value[date.timezone] = Asia/Shanghai [root@server5 zabbix]# systemctl enable --now httpd.service rh-php72-php-fpm.service
7.配置Zabbix前端
在浏览器中访问: http://172.25.52.5/zabbix/zabbix.php
根据步骤设置用户和密码
设置完成后用默认用户:Admin 密码:zabbix登陆
选择语言
可以看到选择中文之后还是会有乱码
重新设置语言
[root@server5 fonts]# ls graphfont.ttf [root@server5 fonts]# ll total 0 lrwxrwxrwx 1 root root 33 Dec 21 14:48 graphfont.ttf -> /etc/alternatives/zabbix-web-font ##需改变这里的软链接 [root@server5 fonts]# pwd /usr/share/zabbix/assets/fonts [root@server5 fonts]# cd [root@server5 ~]# mv simkai.ttf /usr/share/zabbix/assets/fonts/ ##从外部下载的楷体,移动到fonts目录下 [root@server5 ~]# cd - /usr/share/zabbix/assets/fonts [root@server5 fonts]# ll total 4040 lrwxrwxrwx 1 root root 33 Dec 21 14:48 graphfont.ttf -> /etc/alternatives/zabbix-web-font -rw------- 1 root root 4135804 Dec 21 15:52 simkai.ttf [root@server5 fonts]# rm -f graphfont.ttf [root@server5 fonts]# ln -s simkai.ttf graphfont.ttf ##重新建立软连接 [root@server5 fonts]# ll total 4040 lrwxrwxrwx 1 root root 10 Dec 21 15:57 graphfont.ttf -> simkai.ttf -rw------- 1 root root 4135804 Dec 21 15:52 simkai.ttf [root@server5 fonts]# chmod 644 simkai.ttf [root@server5 fonts]# ll total 4040 lrwxrwxrwx 1 root root 10 Dec 21 15:57 graphfont.ttf -> simkai.ttf -rw-r--r-- 1 root root 4135804 Dec 21 15:52 simkai.ttf [root@server5 fonts]#
刷新页面可以看到没有乱码了
二、设置被监控端
将镜像传给被监控端server6、server7
[root@server5 fonts]# cd [root@server5 ~]# cd /etc/yum.repos.d/ [root@server5 yum.repos.d]# ls CentOS-SCLo-scl.repo CentOS-SCLo-scl-rh.repo dvd.repo redhat.repo zabbix.repo [root@server5 yum.repos.d]# scp zabbix.repo server6:/etc/yum.repos.d/ [root@server5 yum.repos.d]# scp zabbix.repo server7:/etc/yum.repos.d/
[root@server6 ~]# cd /etc/yum.repos.d/ [root@server6 yum.repos.d]# ls dvd.repo redhat.repo zabbix.repo [root@server6 yum.repos.d]# vim zabbix.repo [zabbix] name=Zabbix Official Repository - $basearch baseurl=http://repo.zabbix.com/zabbix/5.0/rhel/7/$basearch/ enabled=1 gpgcheck=0 ###设置gpgcheck=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591 [zabbix-frontend] name=Zabbix Official Repository frontend - $basearch baseurl=http://repo.zabbix.com/zabbix/5.0/rhel/7/$basearch/frontend enabled=1 gpgcheck=0 ###设置gpgcheck=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591 [root@server6 yum.repos.d]# yum install -y zabbix-agent 安装zabbix-agent
[root@server6 ~]# cd /etc/zabbix/ [root@server6 zabbix]# vim zabbix_agentd.conf ##修改配置文件 Server=172.25.52.5 ServerActive=172.25.52.5 Hostname=server6 [root@server6 zabbix]# systemctl enable --now zabbix-agent.service ##启动服务 [root@server6 zabbix]# cat /var/log/zabbix/zabbix_agentd.log ##查看日志三、设置被监控主机 方法1.手动添加
配置——主机——创建主机
添加模板:
填写被监控主机名称、IP,群组选择linux servers,端口是10050
应用之后可以在看到server6被监控,可用性为绿色表示可用,一开始添加可能为灰色,等待1分钟后刷新,为绿色
查看server6被监控项:
方法 2、自动发现 (适合批量添加)首先在server7中进行设置:
[root@server7 ~]# cd /etc/yum.repos.d/ [root@server7 yum.repos.d]# vim zabbix.repo [root@server7 yum.repos.d]# yum install -y zabbix-agent [root@server7 yum.repos.d]# cd [root@server7 ~]# cd /etc/zabbix/ [root@server7 zabbix]# ls zabbix_agentd.conf zabbix_agentd.d [root@server7 zabbix]# vim zabbix_agentd.conf Server=172.25.52.5 ServerActive=172.25.52.5 Hostname=server7 [root@server7 zabbix]# systemctl enable --now zabbix-agent.service
配置——动作——左上角改为Discovery actions
启用动作
配置——>自动发现
更改ip范围
启用自动发现
在主机中可以看到,搜索到了server7并添加成功
方法3、自动注册 (适合需要定制的被监测端)关闭自动发现和动作
删除server7
配置——动作——左上角改为Autoregistration actions
创建动作
添加条件
设置操作
应用之后我们可以看到被监控主机中server7出现
自动注册不同于手动添加的1分钟刷新和自动发现的1小时刷新,自动注册是实时更新的,所以我们关掉server7
[root@server7 zabbix]# systemctl stop zabbix-agent.service
可以看到server7不可用
开启之后变为可用
[root@server7 zabbix]# systemctl start zabbix-agent.service
方法4.API参考文档:19. API
(1)验证
在访问Zabbix中的任何数据之前,需要登录并获取身份验证令牌,可以使用该 user.login 方法完成。
[root@server5 ~]# vim zabbix-api.sh
curl -s -XPOST http://172.25.52.5/zabbix/api_jsonrpc.php -H 'Content-Type: application/json-rpc' -d '
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "zabbix"
},
"id": 1,
"auth": null
}' | python -m json.tool
[root@server5 ~]# chmod +x zabbix-api.sh
- jsonrpc - API使用的JSON-RPC协议的版本; Zabbix API实现的JSON-RPC版本是2.0;
- method - 被调用的API方法名;
- params - 将被传递给API方法的参数;
- id - 请求的任意标识符;
- auth -用户认证令牌; 如果没有的话可以设置为null。
API返回的响应将包含用户身份验证令牌:
(2)检索主机
现在有一个有效的用户身份验证令牌,可以用来访问Zabbix中的数据。
curl -s -XPOST http://172.25.52.5/zabbix/api_jsonrpc.php -H 'Content-Type: application/json-rpc' -d '
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": [
"hostid",
"host"
],
"selectInterfaces": [
"interfaceid",
"ip"
]
},
"id": 1,
"auth": "7517b4c3f34d37aa2a68f0c7fe1f8839" ##设置为通过调用user.login方法获得的身份验证令牌。
}' | python -m json.tool
响应对象将包含有关主机的请求的数据:
(3) 删除
curl -s -XPOST http://172.25.52.5/zabbix/api_jsonrpc.php -H 'Content-Type: application/json-rpc' -d '
{
"jsonrpc": "2.0",
"method": "host.delete",
"params": [
"10435"
],
"id": 1,
"auth": "7517b4c3f34d37aa2a68f0c7fe1f8839"
}' | python -m json.tool
(4)添加
curl -x -XPOST http://172.25.52.5/zabbix/api_jsonrpc.php -H 'Content-Type: application/json-rpc' -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "server7",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "172.25.52.7",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "2"
}
],
"templates": [
{
"templateid": "10001"
}
]
},
"id": 1,
"auth": "7517b4c3f34d37aa2a68f0c7fe1f8839"
}' | python -m json.tool
可以看到浏览器中添加上了
四.自定义模板使用自定义模板
在server6中配置nginx
[root@server6 ~]# tar zxf nginx-1.20.2.tar.gz [root@server6 ~]# cd nginx-1.20.2/ [root@server6 nginx-1.20.2]# ls [root@server6 nginx-1.20.2]# ./configure --with-http_stub_status_module --with-http_ssl_module checking for OS + Linux 3.10.0-957.el7.x86_64 x86_64 checking for C compiler ... not found ./configure: error: C compiler cc is not found [root@server6 nginx-1.20.2]# yum install -y gcc pcre-devel openssl-devel [root@server6 nginx-1.20.2]# ./configure --with-http_stub_status_module --with-http_ssl_module [root@server6 nginx-1.20.2]# make && make install
[root@server6 nginx-1.20.2]# cd /usr/local/nginx/
[root@server6 nginx]# ls
conf html logs sbin
[root@server6 nginx]# cd conf/
[root@server6 conf]# ls
[root@server6 conf]# vim nginx.conf
location /status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
[root@server6 conf]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
[root@server6 conf]# nginx -t
[root@server6 conf]# nginx
[root@server6 conf]# curl localhost/status ##配置成功
Active connections: 1
server accepts handled requests
1 1 1
Reading: 0 Writing: 1 Waiting: 0
写一个配置文件来检索想要的结果
[root@server6 conf]# cd /etc/zabbix/zabbix_agentd.d/
[root@server6 zabbix_agentd.d]# ls
[root@server6 zabbix_agentd.d]# cp /usr/share/doc/zabbix-agent-5.0.18/userparameter_mysql.conf /etc/zabbix/zabbix_agentd.d/
[root@server6 zabbix_agentd.d]# ls
userparameter_mysql.conf
[root@server6 zabbix_agentd.d]# mv userparameter_mysql.conf userparameter_nginx.conf
[root@server6 zabbix_agentd.d]# ls
userparameter_nginx.conf
[root@server6 zabbix_agentd.d]# vim userparameter_nginx.conf
UserParameter=nginx.active,curl -s localhost/status | grep Active | awk '{print $3}'
UserParameter=nginx.accept,curl -s localhost/status | awk NR==3 |awk '{print $1}'
UserParameter=nginx.handle,curl -s localhost/status | awk NR==3 |awk '{print $2}'
UserParameter=nginx.request,curl -s localhost/status | awk NR==3 |awk '{print $3}'
[root@server6 zabbix_agentd.d]# systemctl restart zabbix-agent.service
在server端可以看到结果:
[root@server5 ~]# yum install -y zabbix-get.x86_64 [root@server5 ~]# zabbix_get -s 172.25.52.6 -p 10050 -k "nginx.active" 1 [root@server5 ~]# zabbix_get -s 172.25.52.6 -p 10050 -k "nginx.accept" 3 [root@server5 ~]# zabbix_get -s 172.25.52.6 -p 10050 -k "nginx.handle" 4 [root@server5 ~]# zabbix_get -s 172.25.52.6 -p 10050 -k "nginx.request" 5
然后去网页添加模板(http://172.25.52.5/zabbix/)
创建监控项
注意:键值要和文件中的键值相同
创建图形
添加监控项:选择刚刚添加的监控项nginx-active
同理添加其余几个监控项
可以给每一个监控项都添加一个只显示这个监控项的图形:
预览图形
这样就有4个监控图形:
也可以添加一个nginx-status的图形,将所有监控项都添加进去:
图形预览:
导入现有模板
删除刚刚添加的模板
添加nginx模板
修改配置文件:
[root@server6 zabbix_agentd.d]# cd /usr/local/nginx/conf/
[root@server6 conf]# ls
[root@server6 conf]# vim nginx.conf
location /basic_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
[root@server6 conf]# cd /etc/zabbix/zabbix_agentd.d/
[root@server6 zabbix_agentd.d]# ls
userparameter_nginx.conf
[root@server6 zabbix_agentd.d]# mv userparameter_nginx.conf /mnt/ ## 将刚刚的模板移除
[root@server6 zabbix_agentd.d]# ls
[root@server6 zabbix_agentd.d]# systemctl restart zabbix-agent.service
导入新的模板
[root@server5 ~]# rpm -ivh percona-zabbix-templates-1.1.8-1.noarch.rpm [root@server5 ~]# cd /var/lib/zabbix/percona/scripts [root@server5 scripts]# ls get_mysql_stats_wrapper.sh ss_get_mysql_stats.php [root@server5 scripts]# cd .. [root@server5 percona]# ls scripts templates [root@server5 percona]# pwd /var/lib/zabbix/percona [root@server5 percona]# cd templates/ [root@server5 templates]# ls userparameter_percona_mysql.conf zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.8.xml [root@server5 templates]# cp userparameter_percona_mysql.conf /etc/zabbix/zabbix_agentd.d/ [root@server5 templates]# cd /etc/zabbix/zabbix_agentd.d/ [root@server5 zabbix_agentd.d]# ls userparameter_percona_mysql.conf [root@server5 zabbix_agentd.d]# wc -l userparameter_percona_mysql.conf 190 userparameter_percona_mysql.conf [root@server5 zabbix_agentd.d]# cd .. [root@server5 zabbix]# ls web zabbix_agentd.conf zabbix_agentd.d zabbix_server.conf [root@server5 zabbix]# systemctl restart zabbix-agent.service
添加软链接
修改配置文件
[root@server5 scripts]# ls get_mysql_stats_wrapper.sh ss_get_mysql_stats.php [root@server5 scripts]# vim ss_get_mysql_stats.php [root@server5 scripts]# /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gg 47 [root@server5 scripts]# cd /tmp/ [root@server5 tmp]# ls localhost-mysql_cacti_stats.txt systemd-private-799012a6b0eb4b01bad417bb5e217190-httpd.service-e99yPH systemd-private-799012a6b0eb4b01bad417bb5e217190-rh-php72-php-fpm.service-oPm1nM [root@server5 tmp]# ll localhost-mysql_cacti_stats.txt -rw-r--r-- 1 root root 1391 Dec 22 12:17 localhost-mysql_cacti_stats.txt [root@server5 tmp]# cat localhost-mysql_cacti_stats.txt ##查看生成的文件
[root@server5 tmp]# rm -fr localhost-mysql_cacti_stats.txt
在网页中导入模板
将模板加入监控主机:
可以看到Zabbix-server端的监控项增加到305个



