Zabbix-架构-安装-基本操作-自定义监控
一、zabbix
1.1 zabbix架构
c/s
b/s
参考博客:zabbix实现原理及架构详解 - 茁壮的小草 - 博客园 (cnblogs.com)
重要组件说明:
1)**zabbix server:**负责接收agent发送的报告信息的核心组件,所有配置、统计数据及操作数据都由它组织进行;
2)**database storage:**专用于存储所有配置信息,以及由zabbix收集的数据;
3)**web interface:**zabbix的GUI接口;
4)**proxy:**可选组件,常用于监控节点很多的分布式环境中,代理server收集部分数据转发到server,可以减轻server的压力;
5)**agent:**部署在被监控的主机上,负责收集主机本地数据如cpu、内存、数据库等数据发往server端或proxy端;
另外,zabbix server、proxy、agent都有自己的配置文件以及log文件,重要的参数需要在这里配置,后面会详细说明。
一个监控系统运行的大概的流程是这样的:
agentd需要安装到被监控的主机上,它负责定期收集各项数据,并发送到zabbix server端,zabbix server将数据存储到数据库中,zabbix web根据数据在前端进行展现和绘图。这里agentd收集数据分为主动和被动两种模式:
**主动:**agent请求server获取主动的监控项列表,并主动将监控项内需要检测的数据提交给server/proxy
被动:server向agent请求获取监控项的数据,agent返回数据。
【主动监测】通信过程如下:
zabbix首先向ServerActive配置的IP请求获取active items,获取并提交active tiems数据值server或者proxy。很多人会提出疑问:zabbix多久获取一次active items?它会根据配置文件中的RefreshActiveChecks的频率进行,如果获取失败,那么将会在60秒之后重试。分两个部分:
获取ACTIVE ITEMS列表
- Agent打开TCP连接(主动检测变成Agent打开)
- Agent请求items检测列表
- Server返回items列表
- Agent 处理响应
- 关闭TCP连接
- Agent开始收集数据
主动检测提交数据过程如下:
- Agent建立TCP连接
- Agent提交items列表收集的数据
- Server处理数据,并返回响应状态
- 关闭TCP连接
【被动监测】通信过程如下:
- Server打开一个TCP连接
- Server发送请求agent.ping\n
- Agent接收到请求并且响应
1 - Server处理接收到的数据1
- 关闭TCP连接
这里,有人可以看出来,被动模式每次都需要打开一个tcp连接,这样当监控项越来越多时,就会出现server端性能问题了。
还有人会问,那实际监控中是用主动的还是被动的呢?这里主要涉及两个地方:
1、新建监控项目时,选择的是zabbix代理还是**zabbix端点代理程式(主动式),**前者是被动模式,后者是主动模式。
2、agentd配置文件中StartAgents参数的设置,如果为0,表示禁止被动模式,否则开启。一般建议不要设置为0,因为监控项目很多时,可以部分使用主动,部分使用被动模式。
常用的监控架构平台
1、server-agentd模式:
这个是最简单的架构了,常用于监控主机比较少的情况下。
2、server-proxy-agentd模式:
这个常用于比较多的机器,使用proxy进行分布式监控,有效的减轻server端的压力。
1.2 安装zabbix
官方网站:https://www.zabbix.com/cn/
下载Zabbix 5.0 LTS for CentOS 7, MySQL, Nginx
Install and configure Zabbix for your platform
a. Install Zabbix repository
# rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
# yum clean all
b. 安装Zabbix server,Web前端,agent
# yum install zabbix-server-mysql zabbix-agent -y
c. Install Zabbix frontend
Enable Red Hat Software Collections
# yum install centos-release-scl -y
编辑配置文件 /etc/yum.repos.d/zabbix.repo and enable zabbix-frontend repository.
[zabbix-frontend]
...
enabled=1
...
Install Zabbix frontend packages.
# yum install zabbix-web-mysql-scl zabbix-nginx-conf-scl -y
d. 创建初始数据库
Make sure you have database server up and running.
安装mariadb
yum install mariadb mariadb-server -y
启动mariadb
service mariadb start
设置开机自启
systemctl enable mariadb.service
在数据库主机上运行以下代码。
# mysql -uroot -p
password
mysql> create database zabbix character set utf8 collate utf8_bin;
mysql> create user zabbix@localhost identified by 'zkj123456';
mysql> grant all privileges on zabbix.* to zabbix@localhost;
mysql> quit;
# 建库
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| zabbix |
+--------------------+
5 rows in set (0.00 sec)
# 创建用户
MariaDB [(none)]> create user zabbix@localhost identified by 'zkj123456';
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)
导入初始架构和数据,系统将提示您输入新创建的密码。
# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix
进入数据库检查
[root@zabbix zabbix-server-mysql-5.0.27]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.68-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)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| zabbix |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> use zabbix;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [zabbix]> show tables;
+----------------------------+
| Tables_in_zabbix |
+----------------------------+
| acknowledges |
| actions |
| alerts |
| application_discovery |
| application_prototype |
| application_template |
| applications |
| auditlog |
| auditlog_details |
| autoreg_host |
| conditions |
| config |
| config_autoreg_tls |
| corr_condition |
| corr_condition_group |
| corr_condition_tag |
| corr_condition_tagpair |
| corr_condition_tagvalue |
| corr_operation |
| correlation |
| dashboard |
| dashboard_user |
| dashboard_usrgrp |
| dbversion |
| dchecks |
| dhosts |
| drules |
| dservices |
| escalations |
| event_recovery |
| event_suppress |
| event_tag |
| events |
| expressions |
| functions |
| globalmacro |
| globalvars |
| graph_discovery |
| graph_theme |
| graphs |
| graphs_items |
| group_discovery |
| group_prototype |
| history |
| history_log |
| history_str |
| history_text |
| history_uint |
| host_discovery |
| host_inventory |
| host_tag |
| hostmacro |
| hosts |
| hosts_groups |
| hosts_templates |
| housekeeper |
| hstgrp |
| httpstep |
| httpstep_field |
| httpstepitem |
| httptest |
| httptest_field |
| httptestitem |
| icon_map |
| icon_mapping |
| ids |
| images |
| interface |
| interface_discovery |
| interface_snmp |
| item_application_prototype |
| item_condition |
| item_discovery |
| item_preproc |
| item_rtdata |
| items |
| items_applications |
| lld_macro_path |
| lld_override |
| lld_override_condition |
| lld_override_opdiscover |
| lld_override_operation |
| lld_override_ophistory |
| lld_override_opinventory |
| lld_override_opperiod |
| lld_override_opseverity |
| lld_override_opstatus |
| lld_override_optag |
| lld_override_optemplate |
| lld_override_optrends |
| maintenance_tag |
| maintenances |
| maintenances_groups |
| maintenances_hosts |
| maintenances_windows |
| mappings |
| media |
| media_type |
| media_type_message |
| media_type_param |
| module |
| opcommand |
| opcommand_grp |
| opcommand_hst |
| opconditions |
| operations |
| opgroup |
| opinventory |
| opmessage |
| opmessage_grp |
| opmessage_usr |
| optemplate |
| problem |
| problem_tag |
| profiles |
| proxy_autoreg_host |
| proxy_dhistory |
| proxy_history |
| regexps |
| rights |
| screen_user |
| screen_usrgrp |
| screens |
| screens_items |
| scripts |
| service_alarms |
| services |
| services_links |
| services_times |
| sessions |
| slides |
| slideshow_user |
| slideshow_usrgrp |
| slideshows |
| sysmap_element_trigger |
| sysmap_element_url |
| sysmap_shape |
| sysmap_url |
| sysmap_user |
| sysmap_usrgrp |
| sysmaps |
| sysmaps_elements |
| sysmaps_link_triggers |
| sysmaps_links |
| tag_filter |
| task |
| task_acknowledge |
| task_check_now |
| task_close_problem |
| task_data |
| task_remote_command |
| task_remote_command_result |
| task_result |
| timeperiods |
| trends |
| trends_uint |
| trigger_depends |
| trigger_discovery |
| trigger_tag |
| triggers |
| users |
| users_groups |
| usrgrp |
| valuemaps |
| widget |
| widget_field |
+----------------------------+
166 rows in set (0.00 sec)
e. 为Zabbix server配置数据库
编辑配置文件 /etc/zabbix/zabbix_server.conf
DBPassword=password
[root@zabbix zabbix-server-mysql-5.0.27]# cd /etc/zabbix
[root@zabbix zabbix]# ls
web zabbix_agentd.conf zabbix_agentd.d zabbix_server.conf
[root@zabbix zabbix]# vim zabbix_server.conf
f. 为Zabbix前端配置PHP
编辑配置文件 /etc/opt/rh/rh-nginx116/nginx/conf.d/zabbix.conf uncomment and set ‘listen’ and ‘server_name’ directives.
# listen 80;
# server_name example.com;
[root@zabbix zabbix]# cd /etc/opt/rh/rh-nginx116/nginx/conf.d
[root@zabbix conf.d]# ls
zabbix.conf
[root@zabbix conf.d]# vim zabbix.conf
编辑配置文件 /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf add nginx to listen.acl_users directive.
listen.acl_users = apache,nginx
[root@zabbix conf.d]# cd /etc/opt/rh/rh-php72/php-fpm.d
[root@zabbix php-fpm.d]# ls
www.conf zabbix.conf
[root@zabbix php-fpm.d]# vim zabbix.conf
- php-fpm:是php语言的解释器 ,php-fpm会去执行php程序代码
- user --> nginx --> php页面 --> php-fpm --> MySQL
Then uncomment and set the right timezone for you.
; php_value[date.timezone] = Europe/Riga
查看时区
[root@zabbix php-fpm.d]# timedatectl
Local time: 六 2022-09-03 11:46:08 CST
Universal time: 六 2022-09-03 03:46:08 UTC
RTC time: 六 2022-09-03 03:46:08
Time zone: Asia/Shanghai (CST, +0800) # 时区
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
关闭防火墙,selinux
# 关闭防火墙
[root@zabbix php-fpm.d]# service firewalld stop
Redirecting to /bin/systemctl stop firewalld.service
# 关闭selinux
[root@zabbix php-fpm.d]# getenforce
Enforcing
# 临时关闭
[root@zabbix php-fpm.d]# setenforce 0
# 永久关闭
[root@zabbix php-fpm.d]# vim /etc/selinux/config
g. 启动Zabbix server和agent进程
启动Zabbix server和agent进程,并为它们设置开机自启:
# systemctl restart zabbix-server zabbix-agent rh-nginx116-nginx rh-php72-php-fpm
# systemctl enable zabbix-server zabbix-agent rh-nginx116-nginx rh-php72-php-fpm
检查:
[root@zabbix php-fpm.d]# netstat -anplut | grep zabbix
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 2427/zabbix_agentd
tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 2434/zabbix_server
tcp6 0 0 :::10050 :::* LISTEN 2427/zabbix_agentd
tcp6 0 0 :::10051 :::* LISTEN 2434/zabbix_server
[root@zabbix php-fpm.d]# netstat -anplut | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2476/nginx: master
tcp6 0 0 :::80 :::* LISTEN 2476/nginx: master
1.3 访问web启动zabbix
访问后为什么报如下错误?
原因:
在nginx的配置文件里使用了域名www.sc.com,访问这个域名就会访问到zabbix web服务器里
如何解决?
方法一:
修改配置文件里的端口号,不用80端口。
然后重启服务
systemctl restart zabbix-server zabbix-agent rh-nginx116-nginx rh-php72-php-fpm
方法二:
修改hosts文件
需要在windows的hosts文件里添加之前配置的域名记录
访问www.sc.com 成功!
接下来按照步骤走
登陆
默认的用户名为:Admin 密码:zabbix
看到如下界面,表示安装成功~
1.4 安装zabbix-Agent
1.下载zabbix仓库文件
rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
2.安装zabbix Agent
yum install zabbix-agent -y
3.启动zabbix Agent
systemctl restart zabbix-agent
systemctl enable zabbix-agent
4.关闭防火墙、selinux
[root@zabbix-agent ~]# systemctl restart zabbix-agent
[root@zabbix-agent ~]# systemctl enable zabbix-agent
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.
[root@zabbix-agent ~]# service firewalld stop
Redirecting to /bin/systemctl stop firewalld.service
[root@zabbix-agent ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@zabbix-agent ~]# getenforce
Enforcing
[root@zabbix-agent ~]# setenforce 0
[root@zabbix-agent ~]# vim /etc/selinux/config
5.查看zabbix-agent是否启动
[root@zabbix-agent ~]# ps aux | grep zabbix
zabbix 1826 0.0 0.0 79096 1256 ? S 15:01 0:00 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
zabbix 1827 0.0 0.0 79096 1508 ? S 15:01 0:00 /usr/sbin/zabbix_agentd: collector [idle 1 sec]
zabbix 1828 0.0 0.0 79096 1828 ? S 15:01 0:00 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
zabbix 1829 0.0 0.0 79096 1828 ? S 15:01 0:00 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
zabbix 1830 0.0 0.0 79096 1828 ? S 15:01 0:00 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]
zabbix 1831 0.0 0.1 79096 2204 ? S 15:01 0:00 /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
root 1927 0.0 0.0 112824 980 pts/0 S+ 15:04 0:00 grep --color=auto zabbix
[root@zabbix-agent ~]# netstat -auplut | grep zabbix
tcp 0 0 0.0.0.0:zabbix-agent 0.0.0.0:* LISTEN 1826/zabbix_agentd
tcp 0 36 zabbix-agent:ssh 192.168.40.1:54340 ESTABLISHED 1575/sshd: root@pts
tcp6 0 0 [::]:zabbix-agent [::]:* LISTEN 1826/zabbix_agentd
6.修改zabbix-agent的主配置文件
修改为允许来采集数据的服务器ip,不然下面的zabbix_get命令不能去采集数据
[root@zabbix-agent ~]# cd /etc/zabbix/
[root@zabbix-agent zabbix]# ls
zabbix_agentd.conf zabbix_agentd.d
[root@zabbix-agent zabbix]# vim zabbix_agentd.
7.刷新zabbix agent服务
[root@zabbix-agent zabbix]# service zabbix-agent restart
Redirecting to /bin/systemctl restart zabbix-agent.service
8.在server端使用zabbix_get命令测试
先安装zabbix-get软件
[root@zabbix zabbix]# yum install zabbix-get.x86_64 -y
[root@zabbix zabbix]# zabbix_get -s 192.168.40.140 -p 10050 -k "system.cpu.load[all,avg1]"
# -s 192.168.40.140 -- 指定被监控服务器的ip
# -p -- 指定访问的端口号 port
# -k "system.cpu.load[all,avg1]" -- 指定获取的数据的指标 -- key(关键字)--> 获取监控主机的项目名字
9.在web zabbix中测试
设置中文语言
首先创建被监控的主机
然后配置主机、模板
10.中文乱码问题解决
第一步:修改默认的字体
[root@zabbix include]# vim /usr/share/zabbix/include/defines.inc.php
第二步:
上传字体文件,到windows里的C:\Windows\Fonts找到simkai.ttf – 新宋体常规
将字体文件放入linux里存放字体的路径 /usr/share/zabbix/assets/fonts
[root@zabbix zabbix]# cd /usr/share/zabbix/assets/fonts
[root@zabbix fonts]# ls
graphfont.ttf
[root@zabbix fonts]# ls
graphfont.ttf simkai.ttf
回到zabbix,检测主机,成功!
11.安装mariadb,测试模板有无监控到它
[root@zabbix-agent zabbix]# yum install mariadb mariadb-server -y
[root@zabbix-agent zabbix]# service mariadb start
Redirecting to /bin/systemctl start mariadb.service
失败,应该是模板 不匹配
1.5 专有名词解释
监控项:
https://www.cnblogs.com/smail-bao/p/6109882.html
监控的一个项目 ==》 被监控的一个具体的对象。
例如:cpu的使用率,内存的使用率,磁盘的使用率等。
应用集:
都是集中在某个应用方向的,很多监控项的集合。
某种类型的应用的集合,里面包含了很多的监控项。
例如:3个与cpu相关的监控项
模板:
包含了很多应用集,直接使用某个模板,里面自动会帮我们配置很多经常使用的应用集。
触发器:
当某个监控项达到某个阈值的时候,就去执行某个动作,例如告警。
对某个监控项的数据进行监控,一旦获得不到相应的数据,就触发一个通知事情,告诉我们某个监控项出现问题了,赶紧去处理。
图形:
图形展示的数据来自监控项
图形不会自动出现,需要我们添加的
某些模板自带图形–》会自动创建图形
自动发现:
zabbix可以根据某些模板里的应用集里的监控项,自动去扫描被监控的主机,哪些可以添加进来,会给予提醒
就会增加一些监控项,例如:网卡流量
1.6 自定义监控项
为什么需要自定义监控项?
zabbix自带的模板不能满足公司的需求
自定义监控脚本–ssh服务
步骤:
在zabbix-agent上执行
1.在zbbix-agent上编写监控脚本,监控sshd进程是否启动
[root@zabbix-agent zabbix]# pwd
/etc/zabbix
[root@zabbix-agent zabbix]# ls
zabbix_agentd.conf zabbix_agentd.d
[root@zabbix-agent zabbix]# cd zabbix_agentd.d
[root@zabbix-agent zabbix]# ls
[root@zabbix-agent zabbix_agentd.d]# vim monitor_sshd.sh
[root@zabbix-agent zabbix_agentd.d]# cat monitor_sshd.sh
#!/bin/bash
#统计sshd进程的数量
num=$( ps aux|grep /usr/sbin/sshd|wc -l)
echo $num
脚本的含义:
只要这个两个进程存在,就表示ssh服务运行正常
综上,进行统计,有2行就正常
给脚本可执行权限
[root@zabbix-agent zabbix_agentd.d]# chmod +x monitor_sshd.sh
[root@zabbix-agent zabbix_agentd.d]# ls
monitor_sshd.sh
[root@zabbix-agent zabbix_agentd.d]# bash monitor_sshd.sh
2
配置文件,用来告诉zabbix监控ssh调用哪个脚本
[root@zabbix-agent zabbix_agentd.d]# vim userparameter_ssh.conf
[root@zabbix-agent zabbix_agentd.d]# cat userparameter_ssh.conf
UserParameter=sc.ssh.status,/etc/zabbix/zabbix_agentd.d/monitor_sshd.sh
# sc.ssh.status -- 自定义个一个key(监控项)名字
# /etc/zabbix/zabbix_agentd.d/monitor_sshd.sh -- 执行的脚本
在zabbix-agent执行
[root@zabbix-agent zabbix_agentd.d]# service zabbix-agent restart
Redirecting to /bin/systemctl restart zabbix-agent.service
在zabbix-server执行
[root@kafka-1 ~]# zabbix_get -s 192.168.40.140 -p 10050 -k sc.ssh.status
sh: /etc/zabbix/zabbix_agentd.d/monitor_sshd.sh: # 如果没给脚本可执行文件,会报错权限不够
[root@zabbix fonts]# zabbix_get -s 192.168.40.140 -p 10050 -k sc.ssh.status
2
在zabbix-server上添加应用集–》监控项–》出图–》聚合图形里
web zabbix进行测试
不用模板哦~
创建主机 -->创建应用集–>创建监控项–>创建图形–>添加触发器
创建应用集
创建监控项
创建图形
测试一下
可以看见,图中的值为2,表示正常
手动关闭ssh服务,再来看监控,可以看见变为了1
[root@zabbix-agent zabbix_agentd.d]# service sshd stop
Redirecting to /bin/systemctl stop sshd.service
添加触发器,再进行测试
重启ssh服务
[root@zabbix-agent zabbix_agentd.d]# service sshd restart
Redirecting to /bin/systemctl restart sshd.service
[root@zabbix-agent zabbix_agentd.d]# service sshd stop
Redirecting to /bin/systemctl stop sshd.service
再关闭ssh服务
看仪表板
至此,自定义监控差不多测试完毕,还差一个报警短信功能没使用。
可以参考博文:
https://blog.youkuaiyun.com/whell_scl/article/details/106722721
(84条消息) Zabbix实现钉钉群告警_夏诗曼CharmaineXia的博客-优快云博客_zabbix 钉钉告警
1.7 聚合图形
将各个监控项对于的图形集合到一个大的图形里,将自己想要得到的监控项聚集到一起
刚刚只监控了sshd,我们再监控一个mysqld
编辑脚本:统计mysql进程数量
按照ssh的步骤配置主机
新建聚合图形
聚合成功