zabbix
文章目录
zabbix简介
- zabbix 是一款开源的功能强大的分布式监控系统,一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案。
- zabbix server可以通过SNMP,zabbix agent,ping,端口监视等方法提供对远程服务器/网络状态的监视。
- zabbix是由2部分构成,分别是zabbix server(监控端)与zabbix agent(被监控端)。
- zabbix server可以通过SNMP,zabbix agent,ping,端口监视等方法提供对远程服务器/网络状态的监视,数据收集等功能,它可以运行在Linux,Ubuntu,Solaris,HP-UX,AIX,Free BSD,Open BSD,OS X等平台上。
- zabbix agent需要安装在被监视的目标服务器上,它主要完成对硬件信息或与操作系统有关的内存,CPU等信息的收集。
- zabbix server 可以单独监视远程服务器的服务状态;同时也可以与zabbix agent配合,可以轮询zabbix agent主动接收监视数据(agent方式),同时还可被动接收zabbix agent发送的数据(trapping方式)。
zabbix主要功能
- CPU负荷
- 内存使用
- 磁盘使用
- 网络状况
- 端口监视
- 日志监视
zabbix的主要特点
- 安装与配置简单,学习成本低
- 支持多语言(包括中文)
- 免费开源
- 自动发现服务器与网络设备
- 分布式监视以及WEB集中管理功能
- 可以无agent监视
- 用户安全认证和柔软的授权方式
- 通过WEB界面设置或查看监视结果
- email等通知功能
zabbix组件
- zabbix server:负责接收agent发送的报告信息的核心组件,所有配置,统计数据及操作数据均由其组织进行
- zabbix database:用户存储所有配置信息,以及存储由zabbix server收集到的数据
- zabbix web:zabbix的interface接口,通常与Server运行在同一台主机上
- zabbix agent:部署在被监控主机上,负责收集本地数据发往server端或proxy端
- zabbix proxy:常用于分布监控环境中,代理Server收集部分被监控的监控数据并统一发往Server端(通常大于500台主机才需要使用)
zabbix原理
zabbix agent安装在被监控的主机上,负责定期收集被监控端的本地各项数据,并发送至zabbix server端,zabbix server收到数据,将数据存储到数据库( zabbix database)中,用户基于zabbix web的可视化界面可以看到前端数据图像。当zabbix监控某个具体的项目时,该项目会设置一个触发器阈值,当被监控的指标超过该触发器设定的阈值,会进行一些必要的动作,动作包括:发送信息(邮件、微信、短信)、发送命令(SHELL 命令、Reboot、Restart、Install等)。
普通的监控软件
- cacti:擅长画图,流量图
- nagios:擅长告警,配置比较麻烦
- zabbix:结合cacti与nagios的优点,有web界面,结合grafna
- promethues:擅长监控容器
zabbix监控指标
- 系统监控
- 内存使用率
- 硬盘使用率
- CPU使用率
- 网络监控
- 流量
- 网卡
- 硬件监控 IPMI
- 电源
- CPU温度
- 业务监控
- 进程
- 日志
zabbix配置文件
zabbix配置文件有两种:
- 服务器端配置文件(/usr/local/etc/zabbix_server.conf)
- 客户端配置文件(/usr/local/etc/zabbix_agentd.conf)
- zabbix代理配置文件(/usr/local/etc/zabbix_proxy.conf)
服务器端配置文件zabbix_server.conf常用配置参数:
参数 | 作用 |
---|---|
LogFile | 设置服务端日志文件存放路径 |
ListenIP | 设置服务端监听IP |
ListenPort | 设置服务端监听的端口号 |
PidFile | 设置服务端进程号文件存放路径 |
DBHost | 指定zabbix的数据库服务器IP |
DBName | 指定zabbix使用的数据库库名 |
DBUser | 指定zabbix数据库登录用户 |
DBPassword | 指定zabbix数据库登录密码 |
DBPort | 指定zabbix数据库端口号 |
User | 设置zabbix以什么用户的身份运行 |
AlertScriptsPath | 设置告警脚本存放路径 |
ExternalScripts | 外部脚本存放路径 |
客户端配置文件zabbix_agentd.conf常用配置参数:
参数 | 作用 |
---|---|
Server | 指定zabbix服务器的IP或域名 |
ServerActive | 指定zabbix服务器的IP或域名 |
Hostname | 指定本机的主机名,此项必须与web界面配置项一致 |
UnsafeUserParameters | 是否启用自定义监控项,可选值为{1 | 0} |
UserParameter | 指定自定义监控脚本参数 |
LogFile | 设置客户端日志文件存放路径 |
部署lamp
配置yum源
进入目录添加yum源仓库
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- 0 0 0 0 0 0 0 0 --:--:-- --:--:-- 100 2495 100 2495 0 0 5788 0 --:--:-- --:--:-- --:--:-- 5775
[root@localhost yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
清除建立缓存
[root@localhost yum.repos.d]# dnf clean all
Failed to set locale, defaulting to C.UTF-8
Repository AppStream is listed more than once in the configuration
13 files removed
[root@localhost yum.repos.d]# dnf makecache
Failed to set locale, defaulting to C.UTF-8
Repository AppStream is listed more than once in the configuration
CentOS-8.5.2111 - Base - mirror 513 kB/s | 4.6 MB 00:09
CentOS-8.5.2111 - Extras - mirr 34 kB/s | 10 kB 00:00
CentOS-8.5.2111 - AppStream - m 415 kB/s | 8.4 MB 00:20
baseos 41 MB/s | 2.5 MB 00:00
Metadata cache created.
安装三个软件包
安装httpd
[root@localhost ~]# dnf install -y httpd
安装mysql
[root@localhost ~]# dnf -y module install mariadb:10.5
安装php
[root@localhost ~]# dnf -y module install php:7.4
配置apache
配置虚拟主机文件
[root@localhost ~]# cp /usr/share/doc/httpd/httpd-vhosts.conf /etc/httpd/conf.d/
[root@localhost ~]# vim /etc/httpd/conf.d/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/var/www/html/zabbix.example.com"
ServerName
ErrorLog "/var/log/httpd/zabbix.example.com-error_log"
CustomLog "/var/log/httpd/zabbix.example.com-access_log" commonzabbix.example.com
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/zabbix.example.com/$1
<Directory "/var/www/html/zabbix.example.com">
Options none
AllowOverride none
Require all granted
</VirtualHost>
修改httpd主配置文件
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
167 DirectoryIndex index.php index.html 加index.php
288 AddType application/x-httpd-php .php
289 AddType application/x-httpd-php-source .phps 添加此两行配置
配置html网页文件
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# mkdir zabbix.example.com
[root@localhost html]# ls
zabbix.example.com
[root@localhost html]# cd zabbix.example.com/
[root@localhost zabbix.example.com]# vim index.php
<?php
phpinfo();
?>
[root@localhost zabbix.example.com]# cd
[root@localhost ~]#
设置开机自启动
[root@localhost ~]# systemctl enable --now httpd
[root@localhost ~]# systemctl enable --now mariadb
[root@localhost ~]# systemctl enable --now php-fpm
修改数据库密码
[root@localhost ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.5.9-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> set password = password ('yqz123!');
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> quit
Bye
修改php-fpm.d配置文件
[root@localhost ~]# vim /etc/php-fpm.d/www.conf
38 listen = 0.0.0.0:9000 修改此行
重启php
[root@localhost ~]# systemctl restart php-fpm
查看端口
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:9000 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
[root@localhost ~]#
关闭防火墙
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled
访问
部署zabbix
环境说明:
环境 | IP | 要安装的应用 |
---|---|---|
服务器 | 172.16.12.128 | lamp架构 zabbix server zabbix agent |
客户端 | 172.16.12.129 | zabbix agent |
因为zabbix
是用php
语言开发的,所以必须先部署lamp
架构,使其能够支持运行php
网页
zabbix服务端安装
安装依赖包
[root@localhost ~]# dnf install -y net-snmp-devel libevent-devel gcc gcc-c++ make libxml2-devel libcurl-devel pcre-devel openssl openssl-devel mysql-devel php-bcmath php-gd php-mysqlnd wget
下载zabbix 6.2包
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://cdn.zabbix.com/zabbix/sources/stable/6.2/zabbix-6.2.2.tar.gz
解压
[root@localhost src]# tar xf zabbix-6.2.2.tar.gz
创建用户
[root@localhost src]# useradd -r -d /usr/lib/zabbix -s /sbin/nologin -c "Zabbix Monitoring System" zabbix
创建目录并改权限
[root@localhost src]# mkdir -p /usr/lib/zabbix
[root@localhost src]# chmod 770 /usr/lib/zabbix
[root@localhost src]# chown -R zabbix.zabbix /usr/lib/zabbix
配置数据库
[root@localhost src]# mysql -uroot -p123com
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.5.9-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database zabbix character set utf8mb4 collate utf8mb4_bin;
Query OK, 1 row affected (0.002 sec)
MariaDB [(none)]> create user 'zabbix'@'localhost' identified by 'zabbix123!';
Query OK, 0 rows affected (0.002 sec)
MariaDB [(none)]> grant all privileges on zabbix.* to 'zabbix'@'localhost';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> SET GLOBAL log_bin_trust_function_creators = 1;
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> quit
Bye
[root@localhost src]#
导入表文件
[root@localhost src]# cd zabbix-6.2.2/database/mysql/
[root@localhost mysql]# mysql -uroot -pyqz123! zabbix < schema.sql
[root@localhost mysql]# mysql -uroot -pyqz123! zabbix < images.sql
[root@localhost mysql]# mysql -uroot -pyqz123! zabbix < data.sql
测试看是否可以登录到zabbix
[root@localhost mysql]# mysql -uroot -pyqz123!
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.5.9-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
编译安装
[root@localhost mysql]# cd
[root@localhost ~]# cd /usr/src/zabbix-6.2.2
[root@localhost zabbix-6.2.2]# ./configure --enable-server --enable-agent --with-mysql --with-libcurl --with-libxml2
[root@localhost zabbix-6.2.2]# make install
添加配置文件
[root@localhost zabbix-6.2.2]# cd /usr/local/etc/
[root@localhost etc]# vim zabbix_server.conf DBPassword=zabbix123!
启动
[root@localhost etc]# zabbix_server
[root@localhost etc]# zabbix_agentd
[root@localhost etc]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 128 0.0.0.0:10051 0.0.0.0:*
LISTEN 0 128 0.0.0.0:9000 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
部署web界面
[root@localhost etc]# cd
[root@localhost ~]# cd /usr/src/zabbix-6.2.2
[root@localhost zabbix-6.2.2]# cp -a ui/* /var/www/html/zabbix.example.com/
cp: overwrite '/var/www/html/zabbix.example.com/index.php'? y
[root@localhost zabbix-6.2.2]# chown -R apache.apache /var/www/html
设置参数
[root@localhost zabbix-6.2.2]# sed -ri 's/(post_max_size =).*/\1 16M/g' /etc/php.ini
[root@localhost zabbix-6.2.2]# sed -ri 's/(max_execution_time =).*/\1 300/g' /etc/php.ini
[root@localhost zabbix-6.2.2]# sed -ri 's/(max_input_time =).*/\1 300/g' /etc/php.ini
[root@localhost zabbix-6.2.2]# sed -i '/;date.timezone/a date.timezone = Asia/Shanghai' /etc/php.ini
[root@localhost zabbix-6.2.2]# systemctl restart php-fpm
安装三个包
[root@localhost zabbix-6.2.2]# dnf -y install php-bcmath
[root@localhost zabbix-6.2.2]# dnf -y install php-gd
[root@localhost zabbix-6.2.2]# dnf -y install php-mysqlnd
访问测试