查看目录结构
[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
[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配置文件
[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配置文件
[root@iZbp14ipzpabr30yllskn4Z salt]# more discovery_port/files/userparameter_discovery_port.conf
UserParameter=tcpportlisten,/opt/discovery_port.sh "$1"
[root@iZbp14ipzpabr30yllskn4Z salt]#
获取服务器端口脚本
[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权限
[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]#
执行
测试
[root@iZbp14ipzpabr30yllskn4Z salt]# salt 10.208.2.54 state.highstate test=True
执行
[root@iZbp14ipzpabr30yllskn4Z salt]# salt 10.208.2.54 state.highstate
See also