分布式监控系统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 ,如需转载请自行联系原作者
相关文章
|
2月前
|
监控 关系型数据库 Linux
|
4月前
|
存储 监控
Zabbix【问题 01】软件包 zabbix-release-6.2-3.el7.noarch (比 zabbix-release-5.0-1.el7.noarch 还要新) 已经安装
Zabbix【问题 01】软件包 zabbix-release-6.2-3.el7.noarch (比 zabbix-release-5.0-1.el7.noarch 还要新) 已经安装
57 0
|
4月前
|
监控 前端开发 应用服务中间件
Zabbix【部署 01】Zabbix企业级分布式监控系统部署配置使用实例(在线安装及问题处理)程序安装+数据库初始+前端配置+服务启动+Web登录
Zabbix【部署 01】Zabbix企业级分布式监控系统部署配置使用实例(在线安装及问题处理)程序安装+数据库初始+前端配置+服务启动+Web登录
411 0
|
4月前
|
监控 数据库连接 数据安全/隐私保护
安装Zabbix Server
安装Zabbix Server
52 1
|
4月前
|
存储 监控 前端开发
zabbix概述及简单的在centos7安装Zabbix5.0及添加监控对象
Zabbix是一种开源的企业级监控解决方案,用于实时监控网络、服务器、应用程序等各种设备和服务的性能和可用性,并提供通知和报警功能。它具有灵活可扩展、可定制化的特点,可以满足不同规模和需求的监控需求。
162 0
|
6月前
|
搜索推荐 应用服务中间件 nginx
67分布式电商项目 - nginx基于端口配置虚拟主机
67分布式电商项目 - nginx基于端口配置虚拟主机
26 0
|
6月前
|
监控 Linux
CentOS 7安装zabbix-agent 5.0报错:依赖检测失败:libpcre.so.0(64bit)获取GPG密钥失败
CentOS 7安装zabbix-agent 5.0报错:依赖检测失败:libpcre.so.0(64bit)获取GPG密钥失败
130 0
|
3月前
|
存储 SQL 监控
修改Zabbix源码实现监控数据双写,满足业务需求!
虽然对接Elasticsearch后有诸多好处,但是它不往数据库写历史数据了,同时还不再计算趋势数据了。有这么一个场景...
修改Zabbix源码实现监控数据双写,满足业务需求!
|
4月前
|
数据采集 监控 数据库
OceanBase社区版可以通过Zabbix监控
OceanBase社区版可以通过Zabbix监控
75 4

热门文章

最新文章

推荐镜像

更多