使用salt批量部署zabbix自动发现监控(二)

查看目录结构

1
2
3
4
5
6
7
8
9
10
[root@iZbp14ipzpabr30yllskn4Z salt]# tree discovery_port/
discovery_port/
├── files
│   ├── discovery_port.sh
│   ├── sudoers.sh
│   └── userparameter_discovery_port.conf
└── init.sls
1 directory, 4 files
[root@iZbp14ipzpabr30yllskn4Z salt]#

查看主配置文件init.sls

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
[root@iZbp14ipzpabr30yllskn4Z salt]# more discovery_port/init.sls
### zabbix mysql config
discovery_port:
file.managed:
- name: /etc/zabbix/zabbix_agentd.d/userparameter_discovery_port.conf
- user: root
- mode: 755
- source: salt://discovery_port/files/userparameter_discovery_port.conf
discovery_port_sh:
file.managed:
- name: /opt/discovery_port.sh
- user: root
- mode: 755
- source: salt://discovery_port/files/discovery_port.sh
sudoers_manager:
file.managed:
- name: /opt/sudoers.sh
- user: root
- mode: 755
- source: salt://discovery_port/files/sudoers.sh
cmd.run:
- cwd: /opt/
- names:
- /bin/bash sudoers.sh
- unless: /bin/cat /etc/sudoers| grep zabbix
zabbix_agent:
cmd.run:
- cwd: /
- names:
#- /sbin/service zabbix-agent restart
- /etc/rc.d/init.d/zabbix-agent restart
- watch:
- file: /etc/zabbix/zabbix_agentd.d/userparameter_discovery_port.conf
delete_suders_config:
cmd.run:
- cwd: /opt/
- names:
- /bin/rm /opt/sudoers.sh
[root@iZbp14ipzpabr30yllskn4Z salt]#

files配置文件

1
2
3
4
5
6
7
8
[root@iZbp14ipzpabr30yllskn4Z salt]# tree discovery_port/files/
discovery_port/files/
├── discovery_port.sh
├── sudoers.sh
└── userparameter_discovery_port.conf
0 directories, 3 files
[root@iZbp14ipzpabr30yllskn4Z salt]#

zabbix UserParameter配置文件

1
2
3
[root@iZbp14ipzpabr30yllskn4Z salt]# more discovery_port/files/userparameter_discovery_port.conf
UserParameter=tcpportlisten,/opt/discovery_port.sh "$1"
[root@iZbp14ipzpabr30yllskn4Z salt]#

获取服务器端口脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@iZbp14ipzpabr30yllskn4Z salt]# more discovery_port/files/discovery_port.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"
[root@iZbp14ipzpabr30yllskn4Z salt]#

执行sudo权限脚本,因为zabbix需要有sudo权限

1
2
3
4
5
[root@iZbp14ipzpabr30yllskn4Z salt]# more discovery_port/files/sudoers.sh
/bin/echo "zabbix ALL=(root) NOPASSWD:/bin/netstat" >>/etc/sudoers
/bin/sed -i "s/^Defaults.*.requiretty/#Defaults requiretty/" /etc/sudoers
[root@iZbp14ipzpabr30yllskn4Z salt]#

执行

1
2
3
4
测试
[root@iZbp14ipzpabr30yllskn4Z salt]# salt 10.208.2.54 state.highstate test=True
执行
[root@iZbp14ipzpabr30yllskn4Z salt]# salt 10.208.2.54 state.highstate
OpsNotes wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!