zabbix
文章目录
一、基础
- 部署教程地址:https://www.zabbix.com/download?zabbix=5.0&os_distribution=centos&os_version=7&components=server_frontend_agent&db=mysql&ws=apache
###### 配置yum源
[root@localhost ~]# rpm -Uvh centos-release-scl-*
warning: centos-release-scl-2-3.el7.centos.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:centos-release-scl-rh-2-3.el7.cen################################# [ 50%]
2:centos-release-scl-2-3.el7.centos################################# [100%]
[root@localhost ~]# ll /etc/yum.repos.d/
total 20
-rw-r--r--. 1 root root 998 Dec 11 2018 CentOS-SCLo-scl.repo
-rw-r--r--. 1 root root 971 Oct 29 2018 CentOS-SCLo-scl-rh.repo
-rw-r--r--. 1 root root 358 Jan 13 03:37 redhat.repo
-rw-r--r--. 1 root root 60 Jan 13 03:37 rhel7.repo
-rw-r--r--. 1 root root 853 May 10 2020 zabbix.repo
[root@localhost ~]# yum clean all
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Cleaning repos: rhel7 zabbix zabbix-non-supported
Other repos take up 1.6 k of disk space (use --verbose for details)
##### 安装软件包
[root@localhost ~]# yum install zabbix-server-mysql zabbix-agent
##### 再配置yum源,下载地址https://developer.aliyun.com/packageSearch?word=centos-release-scl
[root@localhost ~]# rpm -Uvh centos-release-scl-*
warning: centos-release-scl-2-3.el7.centos.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:centos-release-scl-rh-2-3.el7.cen################################# [ 50%]
2:centos-release-scl-2-3.el7.centos################################# [100%]
[root@localhost ~]# ll /etc/yum.repos.d/
total 20
-rw-r--r--. 1 root root 998 Dec 11 2018 CentOS-SCLo-scl.repo
-rw-r--r--. 1 root root 971 Oct 29 2018 CentOS-SCLo-scl-rh.repo
-rw-r--r--. 1 root root 358 Jan 13 03:37 redhat.repo
-rw-r--r--. 1 root root 60 Jan 13 03:37 rhel7.repo
-rw-r--r--. 1 root root 853 May 10 2020 zabbix.repo
[root@localhost ~]# vim /etc/yum.repos.d/zabbix.repo
########### 安装软件包
[root@localhost ~]# yum install zabbix-web-mysql-scl zabbix-apache-conf-scl
########### 从rpm包安装mysql5.7,下载地址:https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.33-1.el7.x86_64.rpm-bundle.tar
[root@localhost ~]# yum install -y mysql-community-client-5.7.33-1.el7.x86_64.rpm mysql-community-common-5.7.33-1.el7.x86_64.rpm mysql-c ommunity-libs-5.7.33-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.33-1.el7.x86_64.rpm mysql-community-server-5.7.33-1.el7.x86_64.rpm
########## 开启MySQL server
[root@localhost ~]# systemctl enable --now mysqld
[root@localhost ~]# grep 'password' /var/log/mysqld.log
2023-01-15T10:07:10.876920Z 1 [Note] A temporary password is generated for root @localhost: ::t!=ZVM
[root@localhost ~]# mysql_secure_installation
############### 修改管理员密码进入mysql,按照教程建立zabbix数据库及用户
[root@localhost ~]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.33 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
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 'xxxxxx';
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)
mysql> set global log_bin_trust_function_creators = 1;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
######### 导入官方提供的SQL语句
[root@localhost ~]# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | my sql -uzabbix -p zabbix
Enter password:
########## 关闭log_bin_trust_function_creators
[root@localhost ~]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.33 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> set global log_bin_trust_function_creators = 0;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
############ 按照教程修改配置
[root@localhost ~]# vim /etc/zabbix/zabbix_server.conf
[root@localhost ~]# vim /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
############ 启动zabbix服务
[root@localhost ~]# systemctl enable --now zabbix-server zabbix-agent httpd
- 访问http://192.168.147.142/zabbix/setup.php,进一步配置
###### 这里主要检查依赖是否都通过,给DB的管理密码
二、添加zabbix client
-
默认ZBX,监控普通应用
-
SNMP监控路由设备
-
JMX监控java应用
-
IPMI监控硬件
-
安装zabbix-agent
# 修改yum源配置
[root@localhost ~]# vim /etc/yum.repos.d/zabbix.repo
:%s/gpgcheck=1/gpgcheck=0
[root@localhost ~]# yum install -y zabbix-agent
# 修改zabbix_agentd.conf
[root@localhost ~]# vim /etc/zabbix/zabbix_agentd.conf
Server=192.168.147.142 # 指定zabbix server的地址
ServerActive=192.168.147.142
Hostname=server3 # 指定本机的hostname
# 启动zabbix-agent服务
[root@localhost ~]# systemctl enable --now zabbix-agent.service
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.
1. 手动添加
2. 自动搜索添加
3. 触发器自动注册
4. 使用zabbix API批量添加
# API登录到管理员账号,获取到token
[root@localhost ~]# curl -X POST http://192.168.147.142/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
}'
{"jsonrpc":"2.0","result":"c9777c1d30be7277ab6cfa5c8445db59","id":1}[root@localhost ~]#
# 获取主机列表
[root@localhost ~]# curl -X POST http://192.168.147.142/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": 2,
"auth": "c9777c1d30be7277ab6cfa5c8445db59"
}' | python -m json.tool
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 514 0 223 100 291 3986 5201 --:--:-- --:--:-- --:--:-- 5290
{
"id": 2,
"jsonrpc": "2.0",
"result": [
{
"host": "Zabbix server",
"hostid": "10084",
"interfaces": [
{
"interfaceid": "1",
"ip": "127.0.0.1"
}
]
},
{
"host": "server2",
"hostid": "10437",
"interfaces": [
{
"interfaceid": "2",
"ip": "192.168.147.138"
}
]
}
]
}
# 发现主机
[root@localhost ~]# curl -X POST http://192.168.147.142/zabbix/api_jsonrpc.php -H 'Content-Type: application/json-rpc' -d '
{
"jsonrpc": "2.0",
"method": "dhost.get",
"params": {
"output": "extend",
"selectDServices": "extend",
"druleids": "2"
},
"auth": "c9777c1d30be7277ab6cfa5c8445db59",
"id": 1
}' | python -m json.tool
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 928 0 703 100 225 16411 5252 --:--:-- --:--:-- --:--:-- 16738
{
"id": 1,
"jsonrpc": "2.0",
"result": [
{
"dhostid": "1",
"druleid": "2",
"dservices": [
{
"dcheckid": "2",
"dhostid": "1",
"dns": "server2",
"dserviceid": "1",
"ip": "192.168.147.138",
"lastdown": "0",
"lastup": "1673787802",
"port": "10050",
"status": "0",
"value": "Linux server2 3.10.0-957.el7.x86_64 #1 SMP Thu Oct 4 20:48:51 UTC 2018 x 86_64"
}
],
"lastdown": "0",
"lastup": "1673787802",
"status": "0"
},
{
"dhostid": "2",
"druleid": "2",
"dservices": [
{
"dcheckid": "2",
"dhostid": "2",
"dns": "server3",
"dserviceid": "2",
"ip": "192.168.147.143",
"lastdown": "0",
"lastup": "1673787811",
"port": "10050",
"status": "0",
"value": "Linux server3 3.10.0-957.el7.x86_64 #1 SMP Thu Oct 4 20:48:51 UTC 2018 x 86_64"
}
],
"lastdown": "0",
"lastup": "1673787811",
"status": "0"
}
]
}
三、使用zabbix监控nginx和mysql、percona
- 在server2上安装nginx
[root@server2 nginx-1.22.1]# ./configure --with-http_ssl_module --with-http_stub_status_module # 需要添加这个参数
# 修改配置文件,添加一个location
location = /basic_status {
stub_status;
}
- 在server3上安装mysql
# 确保同时安装mysql和zabbix-agent
# 拷贝template_db_mysql.conf到/etc/zabbix/zabbix_agentd.d/
[root@server1 ~]# find / -name *mysql.conf
/usr/lib/tmpfiles.d/mysql.conf
/usr/share/doc/zabbix-agent-5.0.30/userparameter_mysql.conf
[root@server1 ~]# cp /usr/share/doc/zabbix-agent-5.0.30/userparameter_mysql.conf /etc/zabbix/zabbix_agentd.d/
# 进入数据库,创建专用于监控的用户
mysql> CREATE USER 'zbx_monitor'@localhost IDENTIFIED BY 'xxxxxxxxxxxxxxxxx';
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT REPLICATION CLIENT,PROCESS,SHOW DATABASES,SHOW VIEW ON *.* TO 'zbx_monitor'@localhost;
Query OK, 0 rows affected (0.00 sec)
# 在zabbix agent的主目录新建.my.cnf写入数据库配置
[root@server1 ~]# mkdir /var/lib/zabbix/
[root@server1 ~]# cd /var/lib/zabbix/
[root@server1 ~]# vim .my.cnf
[client]
user=zbx_monitor
password=xxxxxxxxxxx
# 重启zabbix-agent
systemctl restart zabbix-agentd.service
zabbix监控nginx配置
zabbix监控mysql配置