天天看点

Zabbix监控之低级自动发现以及MySQL多实例

概述

Zabbix的网络发现是指zabbix server通过配置好的规则,自动添加host,group,template

Zabbix的主动注册刚好和网络发现是相反的,功能基本一致。zabbix agent主动联系zabbix server,server自动添加host,group,template

以上两种方式都是发现host,添加host,而low-level discovery(低级自动发现)更加底层,用于发现item,trigger,graph等等。

2.MySQL多实例的低级自动发现

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

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

<code>(1)配置3307、3308的实例</code>

<code>[root@linux-node2 ~]# mkdir /data/{3307,3308} -p</code>

<code>[root@linux-node2 ~]# chown -R mysql.mysql /data</code>

<code>[root@linux-node2 ~]# cp /etc/my.cnf /etc/my3307.cnf</code>

<code>[root@linux-node2 ~]# vim /etc/my3307.cnf </code>

<code>[mysqld]</code>

<code>datadir=/data/3307</code>

<code>socket=/data/3307/mysql.sock</code>

<code># Disabling symbolic-links is recommended to prevent assorted security risks</code>

<code>symbolic-links=0</code>

<code>port=3307</code>

<code># Settings user and group are ignored when systemd is used.</code>

<code># If you need to run mysqld under a different user or group,</code>

<code># customize your systemd unit file for mariadb according to the</code>

<code># instructions in http://fedoraproject.org/wiki/Systemd</code>

<code>[mysqld_safe]</code>

<code>log-error=/data/3307/mariadb.log</code>

<code>pid-file=/data/3307/mariadb.pid</code>

<code>#</code>

<code># include all files from the config directory</code>

<code>!includedir /etc/my.cnf.d</code>

<code>[root@linux-node2 ~]# cp /etc/my3307.cnf /etc/my3308.cnf</code>

<code>[root@linux-node2 ~]# vim /etc/my3308.cnf </code>

<code>datadir=/data/3308</code>

<code>socket=/data/3308/mysql.sock</code>

<code>port=3308</code>

<code>log-error=/data/3308/mariadb.log</code>

<code>pid-file=/data/3308/mariadb.pid</code>

<code>(2)初始化数据库</code>

<code>[root@linux-node2 ~]# mysql_install_db --user=mysql --defaults-file=/etc/my3307.cnf </code>

<code>Installing MariaDB/MySQL system tables in '/data/3307' ...</code>

<code>171218 12:01:02 [Note] /usr/libexec/mysqld (mysqld 5.5.56-MariaDB) starting as process 4804 ...</code>

<code>OK</code>

<code>Filling help tables...</code>

<code>171218 12:01:02 [Note] /usr/libexec/mysqld (mysqld 5.5.56-MariaDB) starting as process 4813 ...</code>

<code>To start mysqld at boot time you have to copy</code>

<code>support-files/mysql.server to the right place for your system</code>

<code>PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !</code>

<code>To do so, start the server, then issue the following commands:</code>

<code>'/usr/bin/mysqladmin' -u root password 'new-password'</code>

<code>'/usr/bin/mysqladmin' -u root -h linux-node2 password 'new-password'</code>

<code>Alternatively you can run:</code>

<code>'/usr/bin/mysql_secure_installation'</code>

<code>which will also give you the option of removing the test</code>

<code>databases and anonymous user created by default.  This is</code>

<code>strongly recommended for production servers.</code>

<code>See the MariaDB Knowledgebase at http://mariadb.com/kb or the</code>

<code>MySQL manual for more instructions.</code>

<code>You can start the MariaDB daemon with:</code>

<code>cd '/usr' ; /usr/bin/mysqld_safe --datadir='/data/3307'</code>

<code>You can test the MariaDB daemon with mysql-test-run.pl</code>

<code>cd '/usr/mysql-test' ; perl mysql-test-run.pl</code>

<code>Please report any problems at http://mariadb.org/jira</code>

<code>The latest information about MariaDB is available at http://mariadb.org/.</code>

<code>You can find additional information about the MySQL part at:</code>

<code>http://dev.mysql.com</code>

<code>Consider joining MariaDB's strong and vibrant community:</code>

<code>https://mariadb.org/get-involved/</code>

<code>[root@linux-node2 ~]# mysqld_safe --defaults-file=/etc/my3307.cnf &amp;</code>

<code>[1] 4825</code>

<code>[root@linux-node2 ~]# 171218 12:01:53 mysqld_safe Logging to '/data/3307/mariadb.log'.</code>

<code>171218 12:01:53 mysqld_safe Starting mysqld daemon with databases from /data/3307</code>

<code>[root@linux-node2 ~]# mysql_install_db --user=mysql --defaults-file=/etc/my3308.cnf </code>

<code>Installing MariaDB/MySQL system tables in '/data/3308' ...</code>

<code>171218 12:03:48 [Note] /usr/libexec/mysqld (mysqld 5.5.56-MariaDB) starting as process 5041 ...</code>

<code>171218 12:03:48 [Note] /usr/libexec/mysqld (mysqld 5.5.56-MariaDB) starting as process 5049 ...</code>

<code>cd '/usr' ; /usr/bin/mysqld_safe --datadir='/data/3308'</code>

<code>[root@linux-node2 ~]# mysqld_safe --defaults-file=/etc/my3308.cnf &amp;</code>

<code>[2] 5061</code>

<code>[root@linux-node2 ~]# 171218 12:03:56 mysqld_safe Logging to '/data/3308/mariadb.log'.</code>

<code>171218 12:03:56 mysqld_safe Starting mysqld daemon with databases from /data/3308</code>

<code>[root@linux-node2 ~]# netstat -tulnp </code>

<code>Active Internet connections (only servers)</code>

<code>Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    </code>

<code>tcp        0      0 0.0.0.0:3307            0.0.0.0:*               LISTEN      4972/mysqld         </code>

<code>tcp        0      0 0.0.0.0:3308            0.0.0.0:*               LISTEN      5207/mysqld         </code>

<code>tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      8237/nginx: master  </code>

<code>tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      866/sshd            </code>

<code>tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2235/master         </code>

<code>tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      3263/zabbix_agentd  </code>

<code>tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      4645/mysqld         </code>

<code>tcp6       0      0 :::22                   :::*                    LISTEN      866/sshd            </code>

<code>tcp6       0      0 ::1:25                  :::*                    LISTEN      2235/master         </code>

<code>tcp6       0      0 :::10050                :::*                    LISTEN      3263/zabbix_agentd</code>

<code>[root@linux-node2 ~]# netstat -tulnp |grep mysql|awk -F "[ :]+" '{print $5}'</code>

<code>3307</code>

<code>3308</code>

<code>3306</code>

<code>(3)编写获取端口号的shell脚本</code>

<code>[root@linux-node2 scripts]# vim discovery_mysql.sh </code>

<code>#!/bin/bash</code>

<code>#mysql low-lever discovery</code>

<code>res=`netstat -lntp|grep mysql |awk -F "[ :]+" '{print $5}'`</code>

<code>port=($res)</code>

<code>printf '{'</code>

<code>printf '"data":['</code>

<code>for key in ${!port[@]}</code>

<code>do</code>

<code>        </code><code>if [[ "${#port[@]}" -gt 1 &amp;&amp; "${key}"  -ne "$((${#port[@]}-1))" ]];then</code>

<code>                </code><code>printf '{'</code>

<code>                </code><code>printf "\"{#MYSQLPORT}\":\"${port[${key}]}\"},"</code>

<code>        </code><code>else [[ "${key}" -eq "((${#port[@]}-1))" ]]</code>

<code>                </code><code>printf "\"{#MYSQLPORT}\":\"${port[${key}]}\"}"</code>

<code>        </code><code>fi</code>

<code>done</code>

<code>printf ']'</code>

<code>printf '}'</code>

<code>[root@linux-node2 scripts]# sh discovery_mysql.sh |python -m json.tool    #采用json格式显示</code>

<code>{</code>

<code>    </code><code>"data": [</code>

<code>        </code><code>{</code>

<code>            </code><code>"{#MYSQLPORT}": "3307"</code>

<code>        </code><code>},</code>

<code>            </code><code>"{#MYSQLPORT}": "3308"</code>

<code>            </code><code>"{#MYSQLPORT}": "3306"</code>

<code>        </code><code>}</code>

<code>    </code><code>]</code>

<code>}</code>

<code>[root@linux-node2 zabbix_agentd.d]# vim discovery_mysql.conf    #编辑自定义key</code>

<code>UserParameter=discovery_mysql,/bin/bash /etc/zabbix/zabbix_agentd.d/scripts/discovery_mysql.sh</code>

<code>[root@linux-node1 ~]# zabbix_get -s linux-node2 -k discovery_mysql    #测试server端获取数据</code>

<code>(Not all processes could be identified, non-owned process info</code>

<code> </code><code>will not be shown, you would have to be root to see it all.)</code>

<code>{"data":[]}</code>

<code>#这里报错是因为netstat -tulnp的参数-p在执行时需要root的权限,这里使用suid的方式进行授权</code>

<code>[root@linux-node2 ~]# chmod u+s `which netstat`</code>

<code>[root@linux-node2 ~]# ll `which netstat`</code>

<code>-rwsr-xr-x. 1 root root 155000 8月   3 17:17 /usr/bin/netstat</code>

<code>[root@linux-node1 ~]# zabbix_get -s linux-node2 -k discovery_mysql</code>

<code>{"data":[{"{#MYSQLPORT}":"3307"},{"{#MYSQLPORT}":"3308"},{"{#MYSQLPORT}":"3306"}]}</code>

<code>(4)编辑自定义key,获取每个端口的状态数据</code>

<code>[root@linux-node2 zabbix_agentd.d]# cat userparameter_mysql.conf</code>

<code>UserParameter=mysql.status[*],echo "show global status where Variable_name='$2';" | HOME=/var/lib/zabbix mysql -uroot -p123456 -h 127.0.0.1 -P $1 -N | awk '{print $$2}'</code>

<code>修改不同端口的mysql密码:</code>

<code>[root@linux-node2 zabbix_agentd.d]# mysqladmin -h 127.0.0.1 -uroot password '123456' -P3306</code>

<code>[root@linux-node2 zabbix_agentd.d]# mysqladmin -h 127.0.0.1 -uroot password '123456' -P3307</code>

<code>[root@linux-node2 zabbix_agentd.d]# mysqladmin -h 127.0.0.1 -uroot password '123456' -P3308</code>

<code>测试是否正常</code>

<code>[root@linux-node2 zabbix_agentd.d]# mysql -uroot -p123456 -h 127.0.0.1 -P3306 -e "use mysql;show GLOBAL VARIABLES like 'port';"</code>

<code>+---------------+-------+</code>

<code>| Variable_name | Value |</code>

<code>| port          | 3306  |</code>

<code>[root@linux-node2 zabbix_agentd.d]# mysql -uroot -p123456 -h 127.0.0.1 -P3307 -e "use mysql;show GLOBAL VARIABLES like 'port';"</code>

<code>| port          | 3307  |</code>

<code>[root@linux-node2 zabbix_agentd.d]# mysql -uroot -p123456 -h 127.0.0.1 -P3308 -e "use mysql;show GLOBAL VARIABLES like 'port';"</code>

<code>| port          | 3308  |</code>

<code>zabbix_get测试是否能正常获取数据:</code>

<code>[root@linux-node1 ~]# zabbix_get -s linux-node2 -k mysql.status[3306,Bytes_sent]</code>

<code>1808</code>

3.创建发现规则

这里直接在linux-node2上进行创建

<code>"Configuration"</code><code>--&gt;</code><code>"Host"</code><code>--&gt;</code><code>"linux-node2"</code><code>--&gt;</code><code>"Discovery"</code><code>--&gt;</code><code>"Create discovery rule"</code><code>--&gt;</code><code>"Add"</code>

创建 Item prototypes,其实就是一个创建一个 item

Item prototypes (0)&gt;&gt;create Item prototypes,按照官方模板来配置。这里配5个展示

再查看Host中的item项,会多出以下监控项:

查看监控最新数据图表,即可看到3306、3307、3308的数据库状态信息:

版权声明:原创作品,谢绝转载。否则将追究法律责任

本文转自 IT_外卖小哥  51CTO博客,原文链接:http://blog.51cto.com/jinlong/2051703

继续阅读