分布式监控系统Zabbix--完整安装记录-批量添加主机和自动发现端口

简介:

一、Zabbix-3.0.3批量添加主机的配置如下:

0)被监控机上要安装zabbix_agent,并配置好zabbix_agentd.conf (如下172.29.8.50是zabbix_server机器的ip地址)

1
2
3
4
5
可批量修改zabbix_agentd.conf,如下:
sed  -i  's/Server=127.0.0.1/Server=172.29.8.50/g'  /etc/zabbix/zabbix_agentd .conf
sed  -i  's/ServerActive=127.0.0.1/ServerActive=172.29.8.50/g'  /etc/zabbix/zabbix_agentd .conf
grep  "Server"  /etc/zabbix/zabbix_agentd .conf |  grep  - v  '^#'
service zabbix-agent restart

1)创建自动发现规则。"配置"->"自动发现"->"创建自动发现"

如下“检查”的配置为:选择“Zabbix agent”检测类型、端口10050、Key填写“system.uname”  (ip范围可以多种方式填写,比如:172.29.8.21-27,172.29.33.21-29,172.29.34.34)

2)创建Action动作(事件源是“自动发现”)

上面“操作”里的“默认接收人”和“默认信息”都是默认的配置,不用修改,只需要添加下面的操作即可

3)等一会儿,就会发现已批量添加了上面配置的网段的机器

上面采用"自动发现"批量添加主机后,显示的名称是主机的ip地址。即添加的客户端主机的Host name是以IP地址命名的,不够人性化。
要想实现自定义主机名,方法是:
在zabbix_server主机的/etc/hosts里面做好客户端的主机名和IP映射

1
2
3
4
5
6
7
8
[root@IDC-Admin-02 ~] # cat /etc/hosts
......
LX-DB-8_21-172.29.8.21    172.29.8.21
LX-DB-8_21-172.29.8.23    172.29.8.23
LX-DB-8_21-172.29.8.24    172.29.8.24
LX-DB-8_21-172.29.8.25    172.29.8.25
LX-DB-8_21-172.29.8.26    172.29.8.26
LX-DB-8_21-172.29.8.27    172.29.8.27

如上配置后,删除首次"自动发现"批量创建的主机,再次创建后,批量创建的主机的显示名称就是其主机名了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
------------------------------------批量添加主机到zabbix监控中---------------------------------
将新上线的172.29.32.0 /24 网段的服务器添加到zabbix监控中,zabbix_agent一键安装脚本为:https: //pan .baidu.com /s/1o8wpFpo      提取密码:7qsi
 
操作步骤记录(下面是在zabbix server端操作的):
[root@zabbix ~] # nmap -sn 172.29.32.0/24
 
Starting Nmap 5.51 ( http: //nmap .org ) at 2017-09-05 09:29 CST
Nmap scan report  for  172.29.32.1
Host is up (0.0020s latency).
Nmap scan report  for  172.29.32.2
Host is up (0.00071s latency).
Nmap scan report  for  172.29.32.3
Host is up (0.00092s latency).
Nmap scan report  for  usercenter01.WANG.cn (172.29.32.21)
Host is up (0.00064s latency).
Nmap scan report  for  usercenter02.WANG.cn (172.29.32.22)
Host is up (0.00042s latency).
Nmap scan report  for  passport01.WANG.cn (172.29.32.23)
Host is up (0.00061s latency).
Nmap scan report  for  passport02.WANG.cn (172.29.32.24)
Host is up (0.00068s latency).
..........
 
[root@zabbix ~] # nmap -sn 172.29.32.0/24|grep 172.29.32|awk '{print $6}'|cut -d"(" -f2|cut -d ")" -f1
 
 
 
172.29.32.21
172.29.32.22
172.29.32.23
172.29.32.24
172.29.32.25
172.29.32.26
172.29.32.27
172.29.32.28
 
172.29.32.30
172.29.32.31
172.29.32.32
172.29.32.33
172.29.32.34
........
 
将ip读取到一个列表文件中 /mnt/a .txt
[root@zabbix ~] # nmap -sn 172.29.32.0/24|grep 172.29.32|awk '{print $6}'|cut -d"(" -f2|cut -d ")" -f1 > /mnt/a.txt
[root@zabbix ~] # cat /mnt/a.txt
--------------------------------------------------------------------------------------------------------------------
或者这里也可以使用造数命令:
[root@localhost ~] # for i in `seq 21 64`;do echo 172.29.32.$i >> /mnt/a.txt;done
[root@localhost ~] # cat /mnt/a.txt 
--------------------------------------------------------------------------------------------------------------------
 
参考http: //www .cnblogs.com /kevingrace/p/5900303 .html,配置到这些被监控主机的信任关系
 
安装expect步骤这里省略~
[root@zabbix ~] # cd /mnt/
[root@zabbix mnt] # ls
a.txt  noscp.exp sshkey.exp  sshkey.sh
 
这些被监控主机的 ssh 端口都是22222,密码都是默认的WANG-@@@-12345(如果端口和密码是其他的,就按照实际情况进行脚本修改)
[root@zabbix mnt] # cat noscp.exp
#!/usr/bin/expect
 
#noscp.exp
 
if  {$argc<4} {
puts stderr  "Usage: $argv0 localfile remotefile user passwd "
exit  1
}
 
set  localfile [ lindex $argv 0 ]
set  remotefile [ lindex $argv 1 ]
set  user [ lindex $argv 2 ]
set  pwd  [ lindex $argv 3 ]
 
set  timeout 30
 
spawn  scp  -P 22222 ${localfile} ${user}@${remotefile}
 
expect {
"*yes/no"  { send  "yes\r" ; exp_continue }
"*password:"  { send  "$pwd\r"  }
}
 
expect eof
 
[root@zabbix mnt] # cat sshkey.exp
#!/usr/bin/expect
 
#sshkey.exp
 
if  {$argc<3} {
puts stderr  "Usage: $argv0 host user passwd "
exit  1
}
 
set  host [ lindex $argv 0 ]
set  user [ lindex $argv 1 ]
set  pwd  [ lindex $argv 2 ]
 
set  timeout 30
 
#spawn ssh ${user}@${host} "rm -rf ~/.ssh/id_rsa*"
#
#expect {
# "*yes/no" { send "yes\r"; exp_continue }
# "*password:" { send "$pwd\r"; exp_continue }
#}
 
spawn  ssh  -p22222 ${user}@${host}  "ssh-keygen -t rsa"
 
expect {
"*yes/no"  { send  "yes\r" ; exp_continue }
"*password:"  { send  "$pwd\r" ; exp_continue }
"Enter file in which to save the key*"  { send  "\n\r" ; exp_continue }
"Overwrite*"  { send  "y\n" ; exp_continue }
"Enter passphrase (empty for no passphrase):"  { send  "\n\r" ; exp_continue }
"Enter same passphrase again:"  { send  "\n\r"  }
}
 
spawn  ssh  -p22222 ${user}@${host}  "cat ~/.ssh/id_rsa.pub"
 
expect {
"*yes/no"  { send  "yes\r" ; exp_continue }
"*password:"  { send  "$pwd\r"  }
}
 
expect eof
 
[root@zabbix mnt] # cat sshkey.sh
#!/bin/bash
for  ip  in  ` cat  /mnt/a .txt`
do
/mnt/sshkey .exp $ip root WANG-@@@-12345 | grep  ssh -rsa >> ~/. ssh /authorized_keys
/mnt/noscp .exp ~/. ssh /authorized_keys  $ip:~/. ssh  root WANG-@@@-12345
done
 
接着直接运行脚本进行信任关系设置:
[root@zabbix mnt] # sh -x sshkey.sh
 
执行完之后,验证下 ssh 无密码信任关系是否都成功设置了:
[root@zabbix mnt] # for i in `cat /mnt/a.txt`;do echo $i;ssh -p22222 $i "hostname";done
 
然后批量进行被监控主机的zabbix_agent安装及设置(使用zabbix_agent一键脚本)。
线上的话,我是把一键安装脚本放到内网下载地址里的
[root@zabbix mnt] # for i in `cat /mnt/a.txt`;do echo $i;ssh -p22222 $i "wget http://yum.wang-inc.com/software/zabbix_agentd_install.sh && sh -x zabbix_agentd_install.sh";done
[root@zabbix mnt] # for i in `cat /mnt/a.txt`;do echo $i;ssh -p22222 $i "lsof -i:10050";done
 
接着就可以在zabbix的web界面里批量添加主机了(自动发现)。为了自动发现后能识别出主机名,先提前在zabbix server端做host。
[root@zabbix mnt] # for i in `cat /mnt/a.txt`;do echo $i >> /opt/ip.list;done
[root@zabbix mnt] # for i in `cat /mnt/a.txt`;do ssh -p22222 $i "hostname" >> /opt/hostname.list;done
[root@zabbix mnt] # paste /opt/ip.list /opt/hostname.list >> /etc/hosts
 
最后就安装上面 "自动发现" 实现批量添加主机.......

二、Zabbix-3.0.3自动发现端口的配置如下:

1)编写自动发现端口的脚本discovertcpport.sh。配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@IDC-Admin-02 ~] # cd /data/zabbix/monitor_scripts/
[root@IDC-Admin-02 monitor_scripts] # cat discovertcpport.sh
#!/bin/bash
portarray=(` sudo  netstat  -tnlp| egrep  -i  "$1" | awk  { 'print $4' }| awk  -F ':'  '{if ($NF~/^[0-9]*$/) print $NF}' | sort | uniq `)
length=${ #portarray[@]}
printf  "{\n"
printf   '\t' "\"data\":["
for  ((i=0;i<$length;i++))
   do
      printf  '\n\t\t{'
      printf  "\"{#TCP_PORT}\":\"${portarray[$i]}\"}"
      if  [ $i -lt $[$length-1] ]; then
                 printf  ','
      fi
   done
printf   "\n\t]\n"
printf  "}\n"
 
脚本说明:
使用 netstat 命令输出端口号;-tnlp=Tcp协议+不显示别名+listen状态+显示程序名称;$1~$9表示输出的第几个参数;
awk  { 'print $4' }表示输出第4个参数(如0.0.0.0:80); awk  -F ':'  '{ if  ($NF~/^[0-9]*$/) print $NF}
表示截取冒号后面的值,且只能是0~9的数字;| sort | uniq 表示排序和去重。
 
[root@IDC-Admin-02 monitor_scripts] # chmod 755 discovertcpport.sh
[root@IDC-Admin-02 monitor_scripts] # cd /data/zabbix/etc/zabbix_agentd.conf.d/
[root@IDC-Admin-02 zabbix_agentd.conf.d] # cat discovertcp.conf
UserParameter=tcpportlisten, /bin/bash  /data/zabbix/monitor_scripts/discovertcpport .sh  "$1"
 
[root@IDC-Admin-02 zabbix_agentd.conf.d] # /etc/init.d/zabbix_agentd restart

==========在端口自动发现脚本中,也可以自定义发现哪些应用端口,如下===========

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@IDC-Admin-02 monitor_scripts] # cat discovertcpport.sh
#!/bin/bash
portarray=(` sudo  netstat  -tnlp| egrep  -i  "tns|java|php|httpd|xen|mysqld" | awk  { 'print $4' }| awk  -F ':'  '{if ($NF~/^[0-9]*$/) print $NF}' | sort | uniq `)
length=${ #portarray[@]}
printf  "{\n"
printf   '\t' "\"data\":["
for  ((i=0;i<$length;i++))
   do
      printf  '\n\t\t{'
      printf  "\"{#TCP_PORT}\":\"${portarray[$i]}\"}"
      if  [ $i -lt $[$length-1] ]; then
                 printf  ','
      fi
   done
printf   "\n\t]\n"
printf  "}\n"

===============================================================

2)服务端测试,如下测试的返回结果是正常的

1
2
3
4
5
6
7
8
9
10
[root@zabbix_server ~] # /data/zabbix/bin/zabbix_get -s 172.29.16.22 -p10050 -k "tcpportlisten"     //其中的172.20.16.22是被监控机的ip
{
    "data" :[
       { "{#TCP_PORT}" : "10050" },
       { "{#TCP_PORT}" : "22222" },
       { "{#TCP_PORT}" : "3306" },
       { "{#TCP_PORT}" : "443" },
       { "{#TCP_PORT}" : "80" }
    ]
}

3)在zabbix监控界面中创建端口自动发现规则

 

注意下面的“键值”要和被监控主机里的监控脚本定义的一致!

接着创建Item监控项原型

接着创建触发器原型:

注意上面“添加”里面的选择项如下

最后就是创建图像原型

查看自动发现的端口监控情况

到此,自动发现端口的监控设置已经完成。下面说下可能遇到的问题:
添加完自动发现规则之后,发现在对应主机的监控图里没有显示端口的监控项,然后发现自动发现规则的后面有问题提示:Value should be a JSON object 
这个错误的解决办法如下:
解决办法是 vim /etc/sudoers
zabbix ALL=(ALL) NOPASSWD: ALL
#Defaults requiretty                                                                //如果有这个配置项,那么就注释掉这个。如果没有,就忽略

-----------------------------------------------------------------------------------------------------------------------------

顺便说下开启zabbix监控界面里报警声音的功能

--------------------------------------------------------------------------定制化自动发现端口-----------------------------------------------------------------
上面在自动发现端口的时候,都是默认发现被监控机上所有开启的端口。如果我们要对端口进行过滤发现,即只发现我们想要的端口,配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
如下,只发现mysql、java、php-fpm、httpd、redis、mongo、oracle、node、zookeeper这些程序端口
 
[ test @ ~]$  cat  /data/zabbix/monitor_scripts/discovertcpport .sh
#!/bin/bash
portarray=(` sudo  netstat  -tnlp| egrep  -i  "mysql|java|php-fpm|httpd|redis|mongo|oracle|node|zookeeper" | awk  { 'print $4' }| awk  -F ':'  '{if ($NF~/^[0-9]*$/) print $NF}' | sort | uniq `)
length=${ #portarray[@]}
printf  "{\n"
printf   '\t' "\"data\":["
for  ((i=0;i<$length;i++))
   do
      printf  '\n\t\t{'
      printf  "\"{#TCP_PORT}\":\"${portarray[$i]}\"}"
      if  [ $i -lt $[$length-1] ]; then
                 printf  ','
      fi
   done
printf   "\n\t]\n"
printf  "}\n"
 
 
[ test @ ~]$  cat  /data/zabbix/etc/zabbix_agentd .conf.d /discovertcp .conf
UserParameter=tcpportlisten[*], /data/zabbix/monitor_scripts/discovertcpport .sh  "$1"
 
 
在zabbix server端进行检查(192.168.1.30是被监控机)。
如下说明该被监控机在定制的自动发现端口中只有3829和3830端口在其中
[root@zabbix ~] # /data/zabbix/bin/zabbix_get -s 192.168.1.30 -p10050 -k "tcpportlisten"
{
         "data" :[
                 { "{#TCP_PORT}" : "3829" },
                 { "{#TCP_PORT}" : "3830" }
         ]
}
 
然后在zabbix 的web界面里添加监控配置,配置和上面一模一样!这样就能按照我们自己定制的端口进行自动发现了!
***************当你发现自己的才华撑不起野心时,就请安静下来学习吧***************
本文转自散尽浮华博客园博客,原文链接:http://www.cnblogs.com/kevingrace/p/6003433.html ,如需转载请自行联系原作者
目录
打赏
0
0
0
0
66
分享
相关文章
zabbix7.0.9安装-以宝塔安装形式-非docker容器安装方法-系统采用AlmaLinux9系统-最佳匹配操作系统提供稳定运行环境-安装教程完整版本-优雅草卓伊凡
zabbix7.0.9安装-以宝塔安装形式-非docker容器安装方法-系统采用AlmaLinux9系统-最佳匹配操作系统提供稳定运行环境-安装教程完整版本-优雅草卓伊凡
137 30
|
2月前
|
yum install -y net-snmp-devel 安装不成功 zabbix项目安装,Errors during downloading metadata for repository ‘extras-common’:问题解决方案-优雅草卓伊凡
yum install -y net-snmp-devel 安装不成功 zabbix项目安装,Errors during downloading metadata for repository ‘extras-common’:问题解决方案-优雅草卓伊凡
87 13
yum install -y net-snmp-devel 安装不成功 zabbix项目安装,Errors during downloading metadata for repository ‘extras-common’:问题解决方案-优雅草卓伊凡
|
2月前
|
【02】客户端服务端C语言-go语言-web端PHP语言整合内容发布-优雅草网络设备监控系统-2月12日优雅草简化Centos stream8安装zabbix7教程-本搭建教程非docker搭建教程-优雅草solution
【02】客户端服务端C语言-go语言-web端PHP语言整合内容发布-优雅草网络设备监控系统-2月12日优雅草简化Centos stream8安装zabbix7教程-本搭建教程非docker搭建教程-优雅草solution
107 20
【01】客户端服务端C语言-go语言-web端PHP语言整合内容发布-优雅草网络设备监控系统-硬件设备实时监控系统运营版发布-本产品基于企业级开源项目Zabbix深度二开-分步骤实现预计10篇合集-自营版
【01】客户端服务端C语言-go语言-web端PHP语言整合内容发布-优雅草网络设备监控系统-硬件设备实时监控系统运营版发布-本产品基于企业级开源项目Zabbix深度二开-分步骤实现预计10篇合集-自营版
53 0
Zabbix 5.0 LTS的web界面安装及修改zabbix web管理员的默认密码
这篇文章是关于如何安装Zabbix 5.0 LTS的web界面以及如何修改Zabbix web管理员默认密码的教程。
332 1
如何在 Ubuntu 16.04 上安装和配置 Zabbix 以安全监控远程服务器
如何在 Ubuntu 16.04 上安装和配置 Zabbix 以安全监控远程服务器
80 0
【解锁数据库监控的神秘力量!】OceanBase社区版与Zabbix的完美邂逅 —— 揭秘分布式数据库监控的终极奥秘!
【8月更文挑战第7天】随着OceanBase社区版的普及,企业广泛采用这一高性能、高可用的分布式数据库。为保障系统稳定,使用成熟的Zabbix监控工具对其进行全方位监控至关重要。本文通过实例介绍如何在Zabbix中配置监控OceanBase的方法,包括创建监控模板、添加监控项(如TPS)、设置触发器及图形展示,并提供示例脚本帮助快速上手。通过这些步骤,可以有效监控OceanBase状态,确保业务连续性。
179 0
安装zabbix时报错Could not resolve host: mirrors.huaweicloud.com;Unknown error解决办法
安装zabbix时报错Could not resolve host: mirrors.huaweicloud.com;Unknown error解决办法
红帽 9 zabbix 安装流程
Zabbix是一个监控软件,用于确保企业服务架构的安全运行,具备灵活的告警机制和分布式监控能力。它由Server、Web页面、数据库、Proxy和Agent五个组件组成。工作流程中,Agent在目标设备上收集数据,Server存储和处理数据,Web页面提供监控信息。Zabbix支持主动和被动两种数据收集模式。在Redhat 9.2环境下,安装包括关闭防火墙、设置SELinux、安装MySQL、创建Zabbix数据库和用户、安装Zabbix RPM包及配置服务。完成安装后,通过Web界面使用Admin账号和预设密码zabbix登录。
317 2

推荐镜像

更多
下一篇
oss创建bucket