返回列表 发帖

Zabbix使用Agent监控CentOS7/Redhat7

笺注:CentOS7的安装可参考 最小化安装CentOS7.8


被监控主机的信息:
[root@ser1 ~]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
[root@ser1 ~]#
[root@ser1 ~]# uname -r
3.10.0-1127.el7.x86_64

[root@ser1 ~]# hostname
ser1.zhuohua.store
[root@ser1 ~]# cat /etc/hostname
ser1.zhuohua.store


被监控主机查看本机的网卡的IP地址:
[root@ser1 ~]# yum -y install net-tools

[root@ser1 ~]# ifconfig ens33 |grep netmask |awk '{print $2}'
192.168.168.190



下载CentOS7/Redhat7的zabbix-agent:https://pan.baidu.com/s/1keBgeoAE95M-gyiN3Qgw2w  提取码:m9zi

被监控主机安装zabbix-agent :
rpm -ivh zabbix-agent-4.4.5-2.el7.x86_64.rpm
图片1.png



被监控主机上会自动创建一个组zabbix:
[root@ser1 ~]# tail -1 /etc/group
zabbix:x:995:

被监控主机上会自动创建一个用户zabbix:
[root@ser1 ~]# cat /etc/passwd |tail -1
zabbix:x:997:995:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin



查看zabbix-agent的相关文件、目录:
[root@ser1 ~]# rpm -ql zabbix-agent
/etc/logrotate.d/zabbix-agent
/etc/zabbix/zabbix_agentd.conf
/etc/zabbix/zabbix_agentd.d
/usr/lib/systemd/system/zabbix-agent.service
/usr/lib/tmpfiles.d/zabbix-agent.conf
/usr/sbin/zabbix_agentd
/usr/share/doc/zabbix-agent-4.4.5
/usr/share/doc/zabbix-agent-4.4.5/AUTHORS
/usr/share/doc/zabbix-agent-4.4.5/COPYING
/usr/share/doc/zabbix-agent-4.4.5/ChangeLog
/usr/share/doc/zabbix-agent-4.4.5/NEWS
/usr/share/doc/zabbix-agent-4.4.5/README
/usr/share/doc/zabbix-agent-4.4.5/userparameter_mysql.conf
/usr/share/man/man8/zabbix_agentd.8.gz
/var/log/zabbix
/var/run/zabbix
[root@ser1 ~]#


被监控主机修改自己的Agent配置文件:
[root@ser1 ~]# vi /etc/zabbix/zabbix_agentd.conf
Server=127.0.0.1
修改为:
Server=192.168.168.154  ( Zabbix server ip )

ServerActive=127.0.0.1
修改为:
ServerActive=192.168.168.154  ( Zabbix server ip )

Hostname=Zabbix server
修改为:
Hostname=ser1.zhuohua.store  ( Hostname of client system )


保存好配置文件后,启动zabbix-agent:
[root@ser1 ~]# systemctl start zabbix-agent
[root@ser1 ~]#


开机自动启动zabbix-agent:
[root@ser1 ~]# 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@ser1 ~]#

确认开机自动启动zabbix-agent:
[root@ser1 ~]# systemctl is-enabled zabbix-agent
enabled


检测zabbix-agent的进程zabbix_agentd是否运行中:(运行时的效果)
[root@ser1 ~]# ps -ef | grep zabbix_agentd |grep -v grep
zabbix    10139      1  0 18:38 ?        00:00:00 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
zabbix    10140  10139  0 18:38 ?        00:00:00 /usr/sbin/zabbix_agentd: collector [idle 1 sec]
zabbix    10141  10139  0 18:38 ?        00:00:00 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
zabbix    10142  10139  0 18:38 ?        00:00:00 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
zabbix    10143  10139  0 18:38 ?        00:00:00 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]
zabbix    10144  10139  0 18:38 ?        00:00:00 /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
[root@ser1 ~]#
[root@ser1 ~]# echo $?
0

zabbix-agent使用TCP 10050端口:
[root@ser1 ~]# netstat -anp |grep 10050
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      10139/zabbix_agentd
tcp6       0      0 :::10050                :::*                    LISTEN      10139/zabbix_agentd
[root@ser1 ~]#
[root@ser1 ~]# echo $?
0



停止zabbix-agent:
[root@ser1 ~]# systemctl stop zabbix-agent
[root@ser1 ~]#


检测zabbix-agent的进程zabbix_agentd是否运行中:(没有运行时的效果)
[root@ser1 ~]# ps -ef | grep zabbix_agentd |grep -v grep
[root@ser1 ~]#
[root@ser1 ~]# echo $?
1

[root@ser1 ~]# netstat -anp |grep 10050
[root@ser1 ~]#
[root@ser1 ~]# echo $?
1


重启zabbix-agent:
[root@ser1 ~]# systemctl restart zabbix-agent
[root@ser1 ~]#



添加防火墙(Firewalld)规则: (打开TCP 10050端口)
[root@ser1 ~]# firewall-cmd --zone=public --add-port=10050/tcp --permanent
success
[root@ser1 ~]# firewall-cmd --reload
success
[root@ser1 ~]#

查看Firewalld的配置文件:
[root@ser1 ~]# cat /etc/firewalld/zones/public.xml
<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="ssh"/>
  <service name="dhcpv6-client"/>
  <port protocol="tcp" port="10050"/>
</zone>
[root@ser1 ~]#



关闭SELinux:
[root@ser1 ~]# setenforce 0
[root@ser1 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
[root@ser1 ~]#

查看SELinux的配置文件:
[root@ser1 ~]# cat /etc/selinux/config |grep -v ^$
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@ser1 ~]#





################## Zabbix添加被监控主机:

配置》主机》创建主机:(主机名称无需与被监控主机的真实主机名一致;群组可以随便选;IP地址那里要输入被监控主机的IP地址;端口为10050)
图片2.png
页面太长,紧接的是下半部分:
图片3.png

主机c7添加成功:
图片4.png


给主机c7添加模板:(  Template OS Linux by Zabbix agent  )
图片5.png

主机c7添加模板成功:
图片6.png

图片7.png



######

假如主机源于模板的应用集、监控项、触发器、图形等迟迟不出现,可以手动检查一下自动发现规则:
图片8.png

图片9.png

手动检查自动发现规则后,主机的应用集、监控项、触发器、图形等会增加一些:
图片10.png



###

查看模板Template OS Linux by Zabbix agent自带的图形:(要等一段时间才会出现)

监测》图形:(查看主机c7的图形)

查看CPU使用率:(可以选择时间范围)
图片11.png

图片12.png
注释:图形中的数据线是从右往左延伸的。

图片13.png



CPU usage:
图片14.png

图片15.png

图片16.png



内存使用率:
图片17.png

图片18.png

图片19.png



Memory usage:
图片20.png

图片21.png

图片22.png



网卡流量:
图片23.png

图片24.png

图片25.png
单位:
bps: 比特/秒
Kbps: 千比特/秒
Mbps: 兆比特/秒



第一块硬盘的读写速度:( sda: Disk read/write rates )
图片26.png

图片27.png

图片28.png



被监控主机的硬盘分区使用情况:
lsblk
df -hl

图片29.png


查看根分区的使用情况:
图片30.png

图片31.png

可以看到根分区的大小、已使用量、使用率:
图片32.png



查看分区/home的使用情况:
图片33.png

图片34.png

可以看到分区/home的大小、已使用量、使用率:
图片35.png





######

模板Template OS Linux by Zabbix agent默认自带的监控系统时间的触发器:
名称: System time is out of sync (diff with Zabbix server > {$SYSTEM.FUZZYTIME.MAX}s)
严重性: 警告
表达式: {c7:system.localtime.fuzzytime({$SYSTEM.FUZZYTIME.MAX})}=0
图片36.png
本页拖下去:(默认就是已启用)
图片37.png



当主机c7的系统时间与Zabbix服务器的系统时间相差大于60秒时,仪表板的告警效果如下:(显示对应触发器的名称)
System time is out of sync (diff with Zabbix server > 60s)
图片38.png

图片39.png



###

被监控主机(CentOS7)进行时间同步:(这里假设被监控主机的系统时间不正确)
[root@ser1 ~]# yum -y install ntpdate

[root@ser1 ~]# find / -name ntpdate
/etc/sysconfig/ntpdate
/usr/sbin/ntpdate

[root@ser1 ~]# /usr/sbin/ntpdate time.windows.com
2 Nov 08:42:51 ntpdate[2038]: step time server 20.189.79.72 offset 1721.230464 sec
[root@ser1 ~]#


仪表板的告警自动消失:
图片40.png



###

CentOS7使用Chrony实现自动同步时间:

[root@ser1 ~]# vi /etc/chrony.conf

默认的NTP时间服务器:(可以注释掉不用)
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst

插入:(自定义进行时间同步的NTP时间服务器)
server ntp1.aliyun.com

如下图:
图片41.png


[root@ser1 ~]# systemctl restart chronyd
[root@ser1 ~]#


可以看到阿里云的NTP服务器的IP地址:
[root@ser1 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^? 120.25.115.20                 2   6     1    53   +110ms[ +110ms] +/-  129ms
[root@ser1 ~]#


至此,本服务器每一次重启系统后,都会自动和阿里云的NTP服务器进行时间同步:
[root@ser1 ~]# reboot















############
############

被监控主机(CentOS7)安装vsftpd服务:
[root@ser1 ~]# yum -y install vsftpd

[root@ser1 ~]# rpm -q vsftpd
vsftpd-3.0.2-27.el7.x86_64


启动vsftpd服务:
[root@ser1 ~]# systemctl start vsftpd


开机自动启动vsftpd服务:
[root@ser1 ~]# systemctl enable vsftpd
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.

确认开机自动启动vsftpd服务:
[root@ser1 ~]# systemctl is-enabled vsftpd
enabled


查看vsftpd服务的主配置文件:
[root@ser1 ~]# cat /etc/vsftpd/vsftpd.conf |grep -v '^#' |grep -v '^$'
anonymous_enable=YES
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=NO
listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
[root@ser1 ~]#

默认就开启了FTP匿名用户登录:
[root@ser1 ~]# cat /etc/vsftpd/vsftpd.conf |grep nonymous_enable
anonymous_enable=YES
[root@ser1 ~]#


FTP匿名用户的默认根目录:( /var/ftp
rpm -ql vsftpd |tail -2
ls -ld /var/ftp
ll /var/ftp
图片42.png



备注:想要让客户端通过服务器的防火墙(Firewalld)访问vsftpd服务,单单打开TCP 21端口是不够的!!

还需要以下操作:
[root@ser1 ~]# echo 'pasv_min_port=61001' >> /etc/vsftpd/vsftpd.conf
[root@ser1 ~]# echo 'pasv_max_port=62000' >> /etc/vsftpd/vsftpd.conf
[root@ser1 ~]#
[root@ser1 ~]# tail -2 /etc/vsftpd/vsftpd.conf
pasv_min_port=61001
pasv_max_port=62000



重启vsftpd服务:
[root@ser1 ~]# systemctl restart vsftpd



打开防火墙(Firewalld)以下端口:( TCP 21、61001-62000 )
[root@ser1 ~]# firewall-cmd --zone=public --add-port=21/tcp --permanent
success
[root@ser1 ~]# firewall-cmd --zone=public --add-port=61001-62000/tcp --permanent
success
[root@ser1 ~]# firewall-cmd --reload
success


查看防火墙(Firewalld)所有打开的端口:
[root@ser1 ~]# firewall-cmd --zone=public --list-ports
10050/tcp 21/tcp 61001-62000/tcp


查看Firewalld的配置文件:
[root@ser1 ~]# cat /etc/firewalld/zones/public.xml
<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="ssh"/>
  <service name="dhcpv6-client"/>
  <port protocol="tcp" port="10050"/>
  <port protocol="tcp" port="21"/>
  <port protocol="tcp" port="61001-62000"/>

</zone>



确认SELinux已经关闭:
[root@ser1 ~]# getenforce
Disabled
[root@ser1 ~]#

查看SELinux的配置文件:
[root@ser1 ~]# cat /etc/selinux/config |grep -v ^$ |grep -v ^#
SELINUX=disabled
SELINUXTYPE=targeted
[root@ser1 ~]#



######

Windows客户端测试被监控主机的vsftpd服务:

软件WinSCP的下载链接:https://pan.baidu.com/s/1lXa21_RrPjSPnxALZbPhGw

WinSCP是常用的FTP、SFTP客户端软件,这里使用的文件协议为 FTP
图片43.png

图片44.png
备注:
FTP匿名用户是直接登录到/var/ftp,且不能离开这个目录;
FTP匿名用户默认是允许从FTP服务器下载文件和文件夹的;
FTP匿名用户无法创建文件和文件夹;不能上传东西,也不能删除东西;



######

在Zabbix服务器上使用命令zabbix_get检测被监控主机的进程是否运行中:(vsftpd服务的进程vsftpd运行时)
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.168.190 -k proc.num[vsftpd]
3

在Zabbix服务器上使用命令zabbix_get检测被监控主机的TCP端口是否处于侦听状态:(TCP 21端口正在侦听时)
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.168.190 -k net.tcp.listen[21]
1

在Zabbix服务器上使用命令zabbix_get检测被监控主机的FTP服务是否运行中:(FTP 服务(端口默认为TCP 21)运行时)
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.168.190 -k net.tcp.service[ftp]
1

[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.168.190 -k net.tcp.service[ftp,,21]
1



被监控主机停止vsftpd服务:
[root@ser1 ~]# systemctl stop vsftpd
[root@ser1 ~]#
[root@ser1 ~]# netstat -anp |grep vsftpd
[root@ser1 ~]# echo $?
1


在Zabbix服务器上使用命令zabbix_get检测被监控主机的进程是否运行中:(vsftpd服务的进程vsftpd没有运行时)
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.168.190 -k proc.num[vsftpd]
0

在Zabbix服务器上使用命令zabbix_get检测被监控主机的TCP端口是否处于侦听状态:(TCP 21端口未侦听时)
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.168.190 -k net.tcp.listen[21]
0

在Zabbix服务器上使用命令zabbix_get检测被监控主机的FTP服务是否运行中:(FTP 服务(端口默认为TCP 21)没有运行时)
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.168.190 -k net.tcp.service[ftp]
0

[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.168.190 -k net.tcp.service[ftp,,21]
0



被监控主机重启vsftpd服务:
[root@ser1 ~]# systemctl restart vsftpd
[root@ser1 ~]#
[root@ser1 ~]# netstat -anp |grep vsftpd
tcp6       0      0 :::21                   :::*                    LISTEN      3158/vsftpd         
tcp6       0      0 192.168.168.190:21      192.168.168.174:1703    ESTABLISHED 3165/vsftpd         
unix  2      [ ]         DGRAM                    32858    3158/vsftpd         
unix  3      [ ]         STREAM     CONNECTED     32908    3165/vsftpd         
unix  3      [ ]         STREAM     CONNECTED     32909    3167/vsftpd         
[root@ser1 ~]# echo $?
0



######

在主机c7中创建监控项:( 监控被监控主机的TCP 21端口的侦听状态 )
自定义名称: Check TCP Port 21
键值: net.tcp.listen[21]
信息类型: 浮点数
图片45.png
备注:其他地方保持默认,点击页底“添加”按钮;


给主机c7的监控项“Check TCP Port 21”创建触发器:(被监控主机的TCP 21端口处于未侦听状态时就告警)
自定义名称: TCP Port 21 is down.
严重性: 灾难
表达式: {c7:net.tcp.listen[21].last()}=0
图片46.png
备注:
触发器的表达式要用到监控项中的键值;
其他地方保持默认,点击页底“添加”按钮;



当主机c7的TCP 21端口处于未侦听状态时,在仪表板上看到的效果:(显示对应触发器的名称)
TCP Port 21 is down.
图片47.png

图片48.png
笺注:
当被监控主机的vsftpd服务已停止时,对应的TCP 21端口就处于未侦听状态。
被监控主机关机、重启都不会触发主机c7的触发器TCP Port 21 is down.



当主机c7的TCP 21端口处于侦听状态时,在仪表板上的告警会马上消失,如下图:
图片49.png
笺注:当被监控主机的vsftpd服务正在运行时,对应的TCP 21端口就处于侦听状态。





监测》最新数据:(查看主机c7的所有监控项的当前值)
图片50.png


截图有省略:
图片51.png

图片52.png



监测》最新数据:(只查看主机c7的监控项“Check TCP Port 21”的当前值)
图片53.png


图片54.png

图片55.png



查看主机c7的监控项“Check TCP Port 21”的图形:
图片56.png

选择时间范围,查看监控项“Check TCP Port 21”的图形:
图片57.png

无需特意给监控项创建图形,也会以监控项为名称自动创建一个图形:
图片58.png
注释:图形中的数据线是从右往左延伸的。

图片59.png





############
############

被监控主机卸载zabbix-agent:
[root@ser1 ~]# rpm -e zabbix-agent
警告:/etc/zabbix/zabbix_agentd.conf 已另存为 /etc/zabbix/zabbix_agentd.conf.rpmsave

卸载成功:
[root@ser1 ~]# rpm -qi zabbix-agent
未安装软件包 zabbix-agent

[root@ser1 ~]# rpm -ql zabbix-agent
未安装软件包 zabbix-agent


但组zabbix不会被删除:
[root@ser1 ~]# grep zabbix /etc/group
zabbix:x:995:

但用户zabbix不会被删除:
[root@ser1 ~]# grep zabbix /etc/passwd
zabbix:x:997:995:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin


文档和软件适用于Redhat7、CentOS7





相关文章:
Zabbix使用预编译Agents监控CentOS7
Zabbix5.0.12_使用Agent监控CentOS7(RPM包)

Zabbix使用Agent监控CentOS6/Redhat6
Zabbix使用Agent监控Linux的TCP+UDP端口
Zabbix使用Agent监控Linux进程+最新数据

Zabbix使用SNMP监控CentOS7/Redhat7
CentOS6_RPM软件包管理器(screen+mailx)

CentOS8_vsftpd匿名用户
CentOS8使用Chrony进行时间同步





#################################
#################################
亲,学习研究也要劳逸结合哦,来我微店逛逛,买点东西好好犒劳犒劳自己和家人吧^_^^_^

电蚊拍可充电式LED灭苍蝇拍大号网面强力灭蚊拍电蚊子拍
wenpai.png

苏泊尔榨汁机杯家用全自动果蔬多功能迷你学生小型便携式炸果汁机
shuiguo.png

返回列表