使用saltstack部署zabbix监控mysql主从

思路

1
2
3
4
首先在/srv/salt/下面创建zabbix_mysql目录,然后创建init.sls.
需要注意的前提必须是zabbix agent已经安装部署.
复制我们自己监控创建的监控项目到客户端
重启zabbix agent

查看表结构

1
2
3
4
5
6
7
8
9
10
~/Desktop/salt > tree zabbix_mysql
zabbix_mysql
├── files
│   ├── mysql_grant.sh
│   ├── mysql_low_discovery.sh
│   ├── sudoers.sh
│   └── userparameter_discovery_mysql.conf
└── init.sls
1 directory, 5 files

zabbix discovery

1
2
3
4
5
~/Desktop/salt > cat zabbix_mysql/files/userparameter_discovery_mysql.conf
UserParameter=zabbix_low_discovery[*],/bin/bash /opt/mysql_low_discovery.sh $1
UserParameter=mysql_stats[*],mysql -h localhost -P $1 -uzabbix -pzabbix -e "show global status"|grep "\<$2\>"|cut -f2
UserParameter=mysql_stats_slave[*],mysql -h localhost -P $1 -uzabbix -pzabbix -e "show slave status\G"|grep "\<$2\>"|awk '{if($NF=="Yes") {print 1} else {print 0}}'
~/Desktop/salt >

mysql授权

1
2
3
4
~/Desktop/salt > cat zabbix_mysql/files/mysql_grant.sh
mysql -uroot -nse "GRANT PROCESS, REPLICATION CLIENT ON *.* TO 'zabbix'@'localhost' identified BY 'zabbix';"
mysql -uroot -nse "Flush privileges;"
~/Desktop/salt >

discovery端口

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
~/Desktop/salt > cat zabbix_mysql/files/mysql_low_discovery.sh
#!/bin/bash
#Fucation:mysql low-level discovery
#Script_name mysql_low_discovery.sh
mysql() {
port=($(sudo netstat -tpln | awk -F "[ :]+" '/[m]ysql/ && /0.0.0.0/ {print $5}'))
printf '{\n'
printf '\t"data":[\n'
for key in ${!port[@]}
do
if [[ "${#port[@]}" -gt 1 && "${key}" -ne "$((${#port[@]}-1))" ]];then
socket=`ps aux|grep ${port[${key}]}|grep -v grep|awk -F '=' '{print $10}'|cut -d ' ' -f 1`
printf '\t {\n'
printf "\t\t\t\"{#MYSQLPORT}\":\"${port[${key}]}\"},\n"
else [[ "${key}" -eq "((${#port[@]}-1))" ]]
socket=`ps aux|grep ${port[${key}]}|grep -v grep|awk -F '=' '{print $10}'|cut -d ' ' -f 1`
printf '\t {\n'
printf "\t\t\t\"{#MYSQLPORT}\":\"${port[${key}]}\"}\n"
fi
done
printf '\t ]\n'
printf '}\n'
}
$1
~/Desktop/salt >

sudoer授权文件

1
2
3
4
~/Desktop/salt > cat zabbix_mysql/files/sudoers.sh
/bin/echo "zabbix ALL=(root) NOPASSWD:/bin/netstat" >>/etc/sudoers
/bin/sed -i "s/^Defaults.*.requiretty/#Defaults requiretty/" /etc/sudoers
~/Desktop/salt >

主配置文件如下

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
### zabbix mysql config
userparameter_mysql:
file.managed:
- name: /etc/zabbix/zabbix_agentd.d/userparameter_discovery_mysql.conf
- user: root
- mode: 755
- source: salt://zabbix_mysql/files/userparameter_discovery_mysql.conf
mysql_low_discovery:
file.managed:
- name: /opt/mysql_low_discovery.sh
- user: root
- mode: 755
- source: salt://zabbix_mysql/files/mysql_low_discovery.sh
sudoers_manager:
file.managed:
- name: /opt/sudoers.sh
- user: root
- mode: 755
- source: salt://zabbix_mysql/files/sudoers.sh
cmd.run:
- cwd: /opt/
- names:
- /bin/bash sudoers.sh
- unless: /bin/cat /etc/sudoers| grep zabbix
mysql_grant:
file.managed:
- name: /opt/mysql_grant.sh
- user: root
- mode: 755
- source: salt://zabbix_mysql/files/mysql_grant.sh
cmd.run:
- cwd: /opt/
- names:
- /bin/bash mysql_grant.sh
- watch:
- file: /opt/mysql_grant.sh
zabbix_agent:
cmd.run:
- cwd: /
- names:
- /sbin/service zabbix-agent restart
- watch:
- file: /etc/zabbix/zabbix_agentd.d/userparameter_discovery_mysql.conf

执行

1
2
执行之前先加test=True测试,如果没有问题去掉test=True执行
~/Desktop/salt > salt '10.208.9.216' state.apply test=True
OpsNotes wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!