Zabbix學習之路(九)之低級自動發現以及MySQL多實例


  • 1、概述

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)配置3307、3308的實例
[root@linux-node2 ~]# mkdir /data/{3307,3308} -p
[root@linux-node2 ~]# chown -R mysql.mysql /data
[root@linux-node2 ~]# cp /etc/my.cnf /etc/my3307.cnf
[root@linux-node2 ~]# vim /etc/my3307.cnf 
[mysqld]
datadir=/data/3307
socket=/data/3307/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
port=3307
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/data/3307/mariadb.log
pid-file=/data/3307/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[root@linux-node2 ~]# cp /etc/my3307.cnf /etc/my3308.cnf
[root@linux-node2 ~]# vim /etc/my3308.cnf 
[mysqld]
datadir=/data/3308
socket=/data/3308/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
port=3308
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/data/3308/mariadb.log
pid-file=/data/3308/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

2)初始化數據庫
[root@linux-node2 ~]# mysql_install_db --user=mysql --defaults-file=/etc/my3307.cnf 
Installing MariaDB/MySQL system tables in '/data/3307' ...
171218 12:01:02 [Note] /usr/libexec/mysqld (mysqld 5.5.56-MariaDB) starting as process 4804 ...
OK
Filling help tables...
171218 12:01:02 [Note] /usr/libexec/mysqld (mysqld 5.5.56-MariaDB) starting as process 4813 ...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following commands:

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

Alternatively you can run:
'/usr/bin/mysql_secure_installation'

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the MariaDB Knowledgebase at http://mariadb.com/kb or the
MySQL manual for more instructions.

You can start the MariaDB daemon with:
cd '/usr' ; /usr/bin/mysqld_safe --datadir='/data/3307'

You can test the MariaDB daemon with mysql-test-run.pl
cd '/usr/mysql-test' ; perl mysql-test-run.pl

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

The latest information about MariaDB is available at http://mariadb.org/.
You can find additional information about the MySQL part at:
http://dev.mysql.com
Consider joining MariaDB's strong and vibrant community:
https://mariadb.org/get-involved/
[root@linux-node2 ~]# mysqld_safe --defaults-file=/etc/my3307.cnf &
[1] 4825
[root@linux-node2 ~]# 171218 12:01:53 mysqld_safe Logging to '/data/3307/mariadb.log'.
171218 12:01:53 mysqld_safe Starting mysqld daemon with databases from /data/3307

[root@linux-node2 ~]# mysql_install_db --user=mysql --defaults-file=/etc/my3308.cnf 
Installing MariaDB/MySQL system tables in '/data/3308' ...
171218 12:03:48 [Note] /usr/libexec/mysqld (mysqld 5.5.56-MariaDB) starting as process 5041 ...
OK
Filling help tables...
171218 12:03:48 [Note] /usr/libexec/mysqld (mysqld 5.5.56-MariaDB) starting as process 5049 ...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following commands:

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

Alternatively you can run:
'/usr/bin/mysql_secure_installation'

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the MariaDB Knowledgebase at http://mariadb.com/kb or the
MySQL manual for more instructions.

You can start the MariaDB daemon with:
cd '/usr' ; /usr/bin/mysqld_safe --datadir='/data/3308'

You can test the MariaDB daemon with mysql-test-run.pl
cd '/usr/mysql-test' ; perl mysql-test-run.pl

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

The latest information about MariaDB is available at http://mariadb.org/.
You can find additional information about the MySQL part at:
http://dev.mysql.com
Consider joining MariaDB's strong and vibrant community:
https://mariadb.org/get-involved/

[root@linux-node2 ~]# mysqld_safe --defaults-file=/etc/my3308.cnf &
[2] 5061
[root@linux-node2 ~]# 171218 12:03:56 mysqld_safe Logging to '/data/3308/mariadb.log'.
171218 12:03:56 mysqld_safe Starting mysqld daemon with databases from /data/3308

[root@linux-node2 ~]# netstat -tulnp 
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:3307            0.0.0.0:*               LISTEN      4972/mysqld         
tcp        0      0 0.0.0.0:3308            0.0.0.0:*               LISTEN      5207/mysqld         
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      8237/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      866/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2235/master         
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      3263/zabbix_agentd  
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      4645/mysqld         
tcp6       0      0 :::22                   :::*                    LISTEN      866/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      2235/master         
tcp6       0      0 :::10050                :::*                    LISTEN      3263/zabbix_agentd
[root@linux-node2 ~]# netstat -tulnp |grep mysql|awk -F "[ :]+" '{print $5}'
3307
3308
3306

(3)編寫獲取端口號的shell腳本 [root@linux-node2 scripts]# vim discovery_mysql.sh #!/bin/bash #mysql low-lever discovery res=`netstat -lntp|grep mysql |awk -F "[ :]+" '{print $5}'` port=($res) printf '{' printf '"data":[' for key in ${!port[@]} do if [[ "${#port[@]}" -gt 1 && "${key}" -ne "$((${#port[@]}-1))" ]];then printf '{' printf "\"{#MYSQLPORT}\":\"${port[${key}]}\"}," else [[ "${key}" -eq "((${#port[@]}-1))" ]] printf '{' printf "\"{#MYSQLPORT}\":\"${port[${key}]}\"}" fi done printf ']' printf '}' [root@linux-node2 scripts]# sh discovery_mysql.sh |python -m json.tool #采用json格式顯示 { "data": [ { "{#MYSQLPORT}": "3307" }, { "{#MYSQLPORT}": "3308" }, { "{#MYSQLPORT}": "3306" } ] } [root@linux-node2 zabbix_agentd.d]# vim discovery_mysql.conf #編輯自定義key UserParameter=discovery_mysql,/bin/bash /etc/zabbix/zabbix_agentd.d/scripts/discovery_mysql.sh [root@linux-node1 ~]# zabbix_get -s linux-node2 -k discovery_mysql #測試server端獲取數據 (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) {"data":[]} #這里報錯是因為netstat -tulnp的參數-p在執行時需要root的權限,這里使用suid的方式進行授權 [root@linux-node2 ~]# chmod u+s `which netstat` [root@linux-node2 ~]# ll `which netstat` -rwsr-xr-x. 1 root root 155000 8月 3 17:17 /usr/bin/netstat [root@linux-node1 ~]# zabbix_get -s linux-node2 -k discovery_mysql {"data":[{"{#MYSQLPORT}":"3307"},{"{#MYSQLPORT}":"3308"},{"{#MYSQLPORT}":"3306"}]} 4)編輯自定義key,獲取每個端口的狀態數據 [root@linux-node2 zabbix_agentd.d]# cat userparameter_mysql.conf 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}' 修改不同端口的mysql密碼: [root@linux-node2 zabbix_agentd.d]# mysqladmin -h 127.0.0.1 -uroot password '123456' -P3306 [root@linux-node2 zabbix_agentd.d]# mysqladmin -h 127.0.0.1 -uroot password '123456' -P3307 [root@linux-node2 zabbix_agentd.d]# mysqladmin -h 127.0.0.1 -uroot password '123456' -P3308 測試是否正常 [root@linux-node2 zabbix_agentd.d]# mysql -uroot -p123456 -h 127.0.0.1 -P3306 -e "use mysql;show GLOBAL VARIABLES like 'port';" +---------------+-------+ | Variable_name | Value | +---------------+-------+ | port | 3306 | +---------------+-------+ [root@linux-node2 zabbix_agentd.d]# mysql -uroot -p123456 -h 127.0.0.1 -P3307 -e "use mysql;show GLOBAL VARIABLES like 'port';" +---------------+-------+ | Variable_name | Value | +---------------+-------+ | port | 3307 | +---------------+-------+ [root@linux-node2 zabbix_agentd.d]# mysql -uroot -p123456 -h 127.0.0.1 -P3308 -e "use mysql;show GLOBAL VARIABLES like 'port';" +---------------+-------+ | Variable_name | Value | +---------------+-------+ | port | 3308 | +---------------+-------+ zabbix_get測試是否能正常獲取數據: [root@linux-node1 ~]# zabbix_get -s linux-node2 -k mysql.status[3306,Bytes_sent] 1808
  • 3、創建發現規則

這里直接在linux-node2上進行創建

"Configuration"-->"Host"-->"linux-node2"-->"Discovery"-->"Create discovery rule"-->"Add"

 

 創建 Item prototypes,其實就是一個創建一個 item

Item prototypes (0)>>create Item prototypes,按照官方模板來配置。這里配5個展示

 

再查看Host中的item項,會多出以下監控項:

查看監控最新數據圖表,即可看到3306、3307、3308的數據庫狀態信息:

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM