文章目录
一、Zabbix由来
场景
运维工程师除了搭建架构环境,配置管理外,还需要保证业务的稳定运行。不稳定的情况包括很多方面,
如:
-
CPU负载过大
-
内存不够
-
磁盘空间满了
-
网络很卡
-
服务不能被访问
等等各种问题。我们运维工程师无法做到时刻盯着服务器查看各类状态,所以需要建立一套完善的自动化监控系统,将所有需要监控的服务器及其各种需要的状态数据都实时地收集, 图形展示,报警。
1、认识监控
监控的目的
实时收集数据并图形展示, 通过报警及时发现问题与处理问题。
为架构优化也提供依据。
监控的目标
生活中的监控:
那么请问linux系统中的监控主要监控什么?
任何你所想要监控的数据, 如cpu负载,cpu的idle时间,内存使用量,内存利用率,io,network等等。
现在很多开源监控方案已经把常见的监控做成了模板,我们可以直接套用
大型公司会有更多的监控需求, 那么就需要专业的开发人员来做监控开发(运维人员也可以开发)
2、主流的开源监控平台介绍
-
mrtg
(Multi Router Traffic Grapher)通过snmp协议得到设备的流量信息,并以包含PNG格式的
图形的HTML文档方式显示给用户。 -
cacti
(仙人掌) 用php语言实现的一个软件,它的主要功能是用snmp服务获取数据,然后用
rrdtool储存和更新数据。官网地址: https://www.cacti.net/ -
abbix
跨平台,画图,多条件告警,多种API接口。用户基数大。官网地址: https://www.zabbix.co m/
-
prometheus
基于时间序列的数值数据的容器监控解决方案。官网地址: https://prometheus.io/
3、zabbix基础概念初探
1. 主机(host)和主机群组(host group) 主机指被监控的一个设备(服务器,交换机等),当被监控的主机数量巨大时,就需要分组
2. zabbix用户(user)与用户群组(group)
zabbix可以多个用户登录管理(和Linux操作系统一样有管理员和普通管理者)
3. 监控项(item)与应用集(application)
监控的需求太多了,就拿监控cpu平均负载来说,就有监控1分钟内,5分钟内,15分钟内等三个常见的监控参 数。
监控项(item)是从收集数据或监控的一个最小单位。把cpu1分钟内的平均负载就可以做成一个监控项。 应用集就是多个监控项的组。
4. 图形
监控项收集的数据需要用图形直观地展示出来。
5. 触发器和报警
当监控项收集的数据达到一个临界点时,就要触发报警通知管理人员。 如: 当根分区使用率超过80%时, 就通过发报警信息到管理人员。
6. 模板
模板主要包括监控项,图形,触发器等概念,相当于是把要监控的东西做成一个合集。
二、监控环境准备
zabbix服务器结构图
先在阿里云官网上安装centos8和epel软件包
[root@localhost yum.repos.d]# rm -rf *
[root@localhost yum.repos.d]# ls
[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
100 2495 100 2495 0 0 2991 0 --:--:-- --:--:-- --:--:-- 2991
[root@localhost yum.repos.d]# ls
CentOS-Base.repo
[root@localhost yum.repos.d]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
CentOS-8.5.2111 - Base - mirrors.aliyun.com 355 kB/s | 4.6 MB 00:13
CentOS-8.5.2111 - Extras - mirrors.aliyun.com 11 kB/s | 10 kB 00:00
CentOS-8.5.2111 - AppStream - mirrors.aliyun.co 417 kB/s | 8.4 MB 00:20
epel-release-latest-8.noarch.rpm 45 kB/s | 24 kB 00:00
Installed:
epel-release-8-17.el8.noarch
Complete!
[root@localhost yum.repos.d]# ls
CentOS-Base.repo epel.repo epel-testing.repo
epel-modular.repo epel-testing-modular.repo
三、搭建LAMP架构
1、yum安装httpd
[root@localhost yum.repos.d]# yum -y install httpd
Last metadata expiration check: 0:10:30 ago on Thu 01 Sep 2022 11:32:47 AM CST.
Package httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@localhost yum.repos.d]# systemctl restart httpd
[root@localhost yum.repos.d]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@localhost yum.repos.d]# 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 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
<IfModule dir_module>
DirectoryIndex index.html **index.php** 添加其后缀以web服务提供PHP页面测试
</IfModule>
[root@localhost ~]# systemctl restart httpd
2、安装数据库
[root@localhost yum.repos.d]# yum -y install mariadb-server mariadb
Last metadata expiration check: 0:05:03 ago on Thu 01 Sep 2022 11:32:47 AM CST.
Package mariadb-server-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64 is already installed.
Package mariadb-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
开启数据库
[root@localhost yum.repos.d]# systemctl restart mariadb.service
[root@localhost yum.repos.d]# systemctl enable mariadb.service
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
初始化数据库
[root@localhost yum.repos.d]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y //输入密码
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
3、安装php
安装其依赖环境
[root@localhost yum.repos.d]# yum -y install php-xml.x86_64 php-json.x86_64 php-mysqlnd.x86_64 php-common.x86_64 php-fpm.x86_64 php-bcmath.x86_64 php-cli.x86_64 php.x86_64 php-gd.x86_64 php-pdo.x86_64 php-devel.x86_64 --allowerasing
Last metadata expiration check: 0:11:31 ago on Thu 01 Sep 2022 11:32:47 AM CST.
Package php-xml-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64 is already installed.
Package php-json-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64 is already installed.
Package php-mysqlnd-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64 is already installed.
Package php-common-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64 is already installed.
Package php-fpm-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64 is already installed.
Package php-bcmath-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64 is already installed.
Package php-cli-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64 is already installed.
Package php-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64 is already installed.
Package php-gd-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64 is already installed.
Package php-pdo-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64 is already installed.
Package php-devel-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
//修改默认时间
[root@localhost ~]# vim /etc/php.ini
; http://php.net/date.timezone
date.timezone = Asia/Shanghai //将此处时区注释打开并设置为上海,以便为zabbix提供时间服务
写入网页测试页面
[root@localhost ~]# vim /var/www/html/index.php
<?php
phpinfo();
?>
[root@localhost ~]# systemctl start php-fpm
[root@localhost ~]# systemctl enable php-fpm
正常访问
三、zabbix监控日志安装
配置yum仓库,分别为阿里云和清华镜像仓库
1、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 | 设置服务端监听IPListenPort |
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 |
UserParameter | 指定自定义监控脚本参数 |
LogFile | 设置客户端日志文件存放路径 |
2、 YUM安装
配置yum仓库,分别为阿里云和清华镜像仓库
[root@localhost yum.repos.d]# vim zabbix.repo
[aliyun]
name=aliyun
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/8/x86_64/
enable=1
gpgcheck=0
[qinghua]
name=Zabbix Official Repository - $basearch
#baseurl=http://repo.zabbix.com/zabbix/3.4/rhel/7/$basearch/
baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.4/rhel/8/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591 //定义密钥文件
[root@localhost yum.repos.d]# yum clean all
[root@localhost yum.repos.d]# yum list all
//安装管理工具服务
[root@localhost yum.repos.d]# dnf -y install zabbix-server-mysql zabbix-web-mysql zabbix-apache-conf zabbix-agent
Last metadata expiration check: 0:04:57 ago on Thu 01 Sep 2022 12:05:00 PM CST.
Package zabbix-server-mysql-4.4.10-1.el8.x86_64 is already installed.
Package zabbix-web-mysql-4.4.10-1.el8.noarch is already installed.
Package zabbix-apache-conf-4.4.10-1.el8.noarch is already installed.
Package zabbix-agent-4.4.10-1.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
数据库授权zabbix用户
[root@server ~]# mysql
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.001 sec) //创建用户,并开启uth8(允许数据库表中有中文存在)
MariaDB [(none)]> grant all on zabbix.* to zabbix@'localhost' identified by
-> '123'; //仅主机登录,密码为123
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> flush privileges; //刷新配置
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> exit
Bye
将源有安装的zabbix数据服务的数据导入到数据库中,便于管理
[root@localhost yum.repos.d]# zcat /usr/share/doc/zabbix-server-mysql/create.sql.gz | mysql -u zabbix -p123 zabbix
修改其注释信息
[root@localhost ~]# vim /etc/zabbix/zabbix_server.conf
ListenPort=10051 //开启默认端口
DBHost=localhost //默认主机访问
DBName=zabbix
DBUser=zabbix
DBPassword=redhat #打开注释并修改连接mysql的密码,在124行
DBSocket=/var/lib/mysql/mysql.sock //开启数据日志套接字
ListenIP=0.0.0.0 //允许任何ip访问
[root@localhost ~]# systemctl restart zabbix-server.service
[root@localhost ~]# systemctl enable --now zabbix-server.service
Created symlink /etc/systemd/system/multi-user.target.wants/zabbix-server.service → /usr/lib/systemd/system/zabbix-server.service.
[root@localhost ~]# 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:10051 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
[root@localhost ~]# systemctl restart httpd