您好,登錄后才能下訂單哦!
下文內(nèi)容主要給大家?guī)韟abbix是如何實(shí)現(xiàn)監(jiān)控MySQL,所講到的知識,與書籍不同,都是億速云專業(yè)技術(shù)人員在與用戶接觸過程中,總結(jié)出來的,具有一定的經(jīng)驗(yàn)分享價(jià)值,希望給廣大讀者帶來幫助。
一、linux環(huán)境下監(jiān)控MySQL
Zabbix Server自帶了MySQL插件來監(jiān)控mysql數(shù)據(jù)庫的模板,只需要配置好agent客戶端,然后在web端給主機(jī)增加模板就行了
監(jiān)控項(xiàng)目:
Com_update: mysql執(zhí)行的更新個(gè)數(shù)
Com_select: mysql執(zhí)行的查詢個(gè)數(shù)
Com_insert: mysql執(zhí)行插入的個(gè)數(shù)
Com_delete: 執(zhí)行刪除的個(gè)數(shù)
Com_rollback: 執(zhí)行回滾的操作個(gè)數(shù)
Bytes_received: 接受的字節(jié)數(shù)
Bytes_sent: 發(fā)送的字節(jié)數(shù)
Slow_queries: 慢查詢語句的個(gè)數(shù)
Com_commit: 確認(rèn)的事物個(gè)數(shù)
Com_begin: 開始的事物個(gè)數(shù)
Uptime: 云服務(wù)器已啟動(dòng)的秒數(shù)
Questions: 客戶端發(fā)送到服務(wù)器的語句個(gè)數(shù)
監(jiān)控模板下載地址:http://www.zabbix.org/wiki/Zabbix_Templates#External_template_resources
腳本下載地址:https://github.com/itnihao/zabbix-book
1)創(chuàng)建zabbix鏈接MySQL的用戶名,密碼并授予權(quán)限。
mysql> grant all on *.* to zabbix@'localhost' identified by "123456”; mysql> flush privileges;
2)在zabbix_agent服務(wù)目錄下創(chuàng)建 .my.cnf 連接文件
cd /usr/local/zabbix/etc/ vim .my.cnf [client] user=zabbix password=123456
注意:
如果在數(shù)據(jù)庫grant授權(quán)時(shí),針對的是localhost,這個(gè).my.cnf里面就不用加host參數(shù)了【如上配置】
但如果grant授權(quán)時(shí)針對的是本機(jī)的ip(如192.168.1.25),那么在.my.cnf文件里就要加上host參數(shù)進(jìn)行指定:host=192.168.1.25
3)配置MySQL的key文件
這個(gè)可以從zabbix安裝時(shí)的解壓包里拷貝過來:
cp /usr/local/src/zabbix3.0.3/conf/ zabbix_agentd/userparameter_mysql.conf /usr/local/zabbix/etc/zabbix_agentd.conf.d/
4)替換zabbix安裝路徑,注意如果MySQL沒配置好環(huán)境變量可能找不到MySQL命令,可以用MySQL全路徑
看到類似 HOME=/var/lib/zabbix 的路徑設(shè)置,把路徑全都替換為 /usr/local/zabbix/etc/,也就是上面的.my.cnf文件所在的目錄路徑。
cd /usr/local/zabbix/etc/zabbix_agentd.conf.d/ vim userparameter_mysql.conf # For all the following commands HOME should be set to the directory that has .my.cnf file with password information. # Flexible parameter to grab global variables. On the frontend side, use keys like mysql.status[Com_insert]. # Key syntax is mysql.status[variable]. UserParameter=mysql.status[*],echo "show global status where Variable_name='$1';" | HOME=/usr/local/zabbix/etc/ mysql -N | awk '{print $$2}' # Flexible parameter to determine database or table size. On the frontend side, use keys like mysql.size[zabbix,history,data]. # Key syntax is mysql.size[<database>,<table>,<type>]. # Database may be a database name or "all". Default is "all". # Table may be a table name or "all". Default is "all". # Type may be "data", "index", "free" or "both". Both is a sum of data and index. Default is "both". # Database is mandatory if a table is specified. Type may be specified always. # Returns value in bytes. # 'sum' on data_length or index_length alone needed when we are getting this information for whole database instead of a single table UserParameter=mysql.size[*],bash -c 'echo "select sum($(case "$3" in both|"") echo "data_length+index_length";; data|index) echo "$3_length";; free) echo "data_free";; esac)) from information_schema.tables$([[ "$1" = "all" || ! "$1" ]] || echo " where table_schema=\"$1\"")$([[ "$2" = "all" || ! "$2" ]] || echo "and table_name=\"$2\"");" | HOME=/usr/local/zabbix/etc/ mysql -N' UserParameter=mysql.ping,HOME=/usr/local/zabbix/etc/ mysqladmin ping | grep -c alive UserParameter=mysql.version,mysql -V %s#/var/lib/zabbix#/usr/local/zabbix/etc/# #用命令替換
也可以用這個(gè)shell腳本監(jiān)控,包括主從監(jiān)控:
### MySQL DB Infomation UserParameter=mysql.status[*],echo"show global status where Variable_name='$1';"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf -N|awk '{print $$2}' UserParameter=mysql.variables[*],echo"show global variables where Variable_name='$1';"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf -N|awk '{print $$2}' UserParameter=mysql.ping,mysqladmin--defaults-file=/usr/local/zabbix/etc/.my.cnf ping|grep -c alive UserParameter=mysql.version,echo"select version();"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf -N #### MySQL Master Information UserParameter=mysql.master.Slave_count,echo"show slave hosts;"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf -N|wc -l UserParameter=mysql.master.Binlog_file,echo"show master status;"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf -N|awk '{print $1}'|awk -F.'{print $1}' UserParameter=mysql.master.Binlog_number,echo"show master status;"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf -N|awk '{print $1}'|awk -F.'{print $2}' UserParameter=mysql.master.Binlog_position,echo"show master status;"|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf-N|awk '{print $2}' UserParameter=mysql.master.Binlog_count,echo"show binary logs;"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf -N|wc -l UserParameter=mysql.master.Binlog_total_size,echo"show binary logs;"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf -N|awk '{sum+=$NF}END{print sum}' #### MySQL Slave Information UserParameter=mysql.slave.Seconds_Behind_Master,echo"show slave status\G"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf|grep "Seconds_Behind_Master"|awk'{print $2}' UserParameter=mysql.slave.Slave_IO_Running,echo"show slave status\G"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf|grep"Slave_IO_Running"|awk '{print $2}' UserParameter=mysql.slave.Slave_SQL_Running,echo"show slave status\G"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf|grep"Slave_SQL_Running"|awk '{print $2}' UserParameter=mysql.slave.Relay_Log_Pos,echo"show slave status\G"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf|grep"Relay_Log_Pos"|awk '{print $2}' UserParameter=mysql.slave.Exec_Master_Log_Pos,echo"show slave status\G"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf|grep"Exec_Master_Log_Pos"|awk '{print $2}' UserParameter=mysql.slave.Read_Master_Log_Pos,echo"show slave status\G"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf|grep"Read_Master_Log_Pos"|awk '{print $2}'
5)修改zabbix_agentd.conf配置文件,開啟額外加載,就是去掉前面的#號
vim zabbix_agentd.conf Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/
6)重啟zabbix_agentd服務(wù)
/etc/init.d/zabbix_agentd restart
7)測試看能否獲取到數(shù)據(jù)
# zabbix_get -s 127.0.0.1 -p 10050 -k mysql.status[Com_select] 200661
8)登陸zabbix監(jiān)控界面,在“配置”里為主機(jī)添加模板,完成監(jiān)控。
二、windows下的MySQL監(jiān)控
要想在Windows上取得MySQL的狀態(tài)數(shù)據(jù),可以用vbs腳本運(yùn)行mysql命令
1)在d:\Zabbix\Scripts\目錄下新建兩個(gè)腳本文件內(nèi)容如下:
mysql_ping.vbs
Set objFS =CreateObject("Scripting.FileSystemObject") Set objArgs = WScript.Arguments str1 = getCommandOutput("D:\SOFT_PHP_PACKAGE\mysql\bin\mysqladmin-ucactiuser -pcactiuser ping") //修改對應(yīng)數(shù)據(jù)庫路徑,用戶名和密碼 If Instr(str1,"alive") > 0Then WScript.Echo 1 Else WScript.Echo 0 End If Function getCommandOutput(theCommand) Dim objShell, objCmdExec Set objShell =CreateObject("WScript.Shell") Set objCmdExec = objshell.exec(thecommand) getCommandOutput =objCmdExec.StdOut.ReadAll end Function
MYSQL-status.vbs
Set objFS = CreateObject("Scripting.FileSystemObject") Set objArgs = WScript.Arguments str1 = getCommandOutput("D:\SOFT_PHP_PACKAGE\mysql\bin\mysqladmin-u cactiuser -pcactiuser extended-status") //修改對應(yīng)數(shù)據(jù)庫路徑,用戶名和密碼 Arg = objArgs(0) str2 = Split(str1,"|") For i = LBound(str2) to UBound(str2) If Trim(str2(i)) = Arg Then WScript.Echo TRIM(str2(i+1)) Exit For End If next Function getCommandOutput(theCommand) Dim objShell, objCmdExec Set objShell =CreateObject("WScript.Shell") Set objCmdExec = objshell.exec(thecommand) getCommandOutput =objCmdExec.StdOut.ReadAll end Function
2)修改windows上的zabbix_agentd.comf文件,設(shè)置key值。在UserParameter下面加兩句;
UserParameter=mysql.status[*], cscript/nologo d:\Zabbix\Scripts\MySQL_Ext-Status_Script.vbs $1
UserParameter=mysql.ping, cscript /nologo d:\Zabbix\Scripts\MySql_Ping.vbs
3)重啟zabbix_agentd,并給主機(jī)添加MySQL模版,查看items狀態(tài)。
對于以上關(guān)于zabbix是如何實(shí)現(xiàn)監(jiān)控MySQL,如果大家還有更多需要了解的可以持續(xù)關(guān)注我們億速云的行業(yè)推新,如需獲取專業(yè)解答,可在官網(wǎng)聯(lián)系售前售后的,希望該文章可給大家?guī)硪欢ǖ闹R更新。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。