[Cacti] cacti監控mongodb效能實戰
前言:
為了更好的使用mongodb,需要監控出mongodb的一些基礎使用情況,比如Flush數、連線數、記憶體使用率、Index操作,Slave延遲等等,這些可以通過配置cacti監控mongodb的模板來完成。
在計算機本地,下載此tgz包:http://mysql-cacti-templates.googlecode.com/files/better-cacti-templates-1.1.8.tar.gz
解壓到任意目錄,然後開啟cacti的web介面,選擇"Import/Export" — "Improt Templates",點選"選擇檔案",將cacti_host_template_x_mongodb_server_ht_0.8.6i-sver1.1.8.xml,匯入到cacti裡,這樣,cacti的圖形模板裡就可以選擇使用mongodb的圖形專案了。
[[email protected] soft]# tar zxvf better-cacti-templates-1.1.8.tar.gz
[[email protected] soft]# cd better-cacti-templates-1.1.8
[[email protected] better-cacti-templates-1.1.8]# cp scripts/ss_get_by_ssh.php /var/www/html/cacti/scripts/ss_get_by_ssh.php
[[email protected] better-cacti-templates-1.1.8]# chown –R apache /var/www/html/cacti/scripts/ss_get_by_ssh.php
修改監控指令碼ss_get_by_ssh.php
[[email protected] better-cacti-templates-1.1.8]# vim /var/www/html/cacti/scripts/ss_get_by_ssh.php
$ssh_user = 'root'; # SSH username
$ssh_port = 22; # SSH port
$ssh_iden = '-i /var/www/html/cacti/id_rsa'; # SSH identity
這裡mongodb
function mongodb_cmdline ( $options ) {
return “echo \”db._adminCommand({serverStatus:1, repl:2})\” | /usr/local/mongo/mongodb/bin/mongo --port 30000″;
}
之後,重啟cacti服務
[[email protected] templates]# service httpd restart
停止 httpd: [確定]
正在啟動 httpd: [確定]
[[email protected] templates]#
3,使用SSH的方式登入監控(1):建立SSH遠曾登入賬號
使用ssh-keygen命令生成祕鑰,回車後輸入key的路徑/var/www/html/cacti/id_rsa,然後2個回車,密碼為空,如下:
[[email protected] squid-2 soft]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /var/www/html/cacti/id_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /var/www/html/cacti/id_rsa.
Your public key has been saved in /var/www/html/cacti/id_rsa.pub.
The key fingerprint is:
73:8e:77:45:a0:54:ef:c8:a4:2d:62:50:8d:da:d8:e4 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
| .o..o |
| .o... o |
| .B . . o |
| o.E = + |
| S + + o |
| . * . . |
| . o . |
| . . |
| |
+-----------------+
把key檔案許可權改成cacti的使用使用者可以訪問的許可權:
[[email protected] squid-2 soft]# chown -R cacti_user /var/www/html/cacti/id_rsa*
[[email protected] squid-2 soft]#
將key加入到遠端mongodb伺服器上,也可在遠端伺服器建立一個cacti_user使用者,將key加上到cactiuser目錄下
[[email protected] squid-2 soft]# ssh-copy-id -i /var/www/html/cacti/id_rsa [email protected]
(2):配置各種引數
修改ss_get_by_ssh.php,設定好私鑰檔案路徑:
$ssh_user = 'root'; # SSH username
$ssh_port = 22; # SSH port
$ssh_iden = '-i /var/www/html/cacti/id_rsa'; # SSH identity
如果mongodb不使用預設埠,需要修改下面一段,把埠加上即可:
function mongodb_cmdline ( $options ) {
return “echo \”db._adminCommand({serverStatus:1, repl:2})\” | /usr/local/mongo/mongodb/bin/mongo --port 30000″;
}
(3)檢測監控指令碼
[[email protected] templates]# php /var/www/html/cacti/scripts/ss_get_by_ssh.php --type mongodb --host 10.254.3.62 --items dc,de,df,dg,dh,di,dj,dk,dl,dm,dn,do,dp,dq,dr,ds,dt,du
dc:57 de:0 df:401604608 dg:-1 dh:-1 di:-1 dj:-1 dk:-1 dl:-1 dm:-1 dn:-1 do:46268320 dp:5245 dq:0 dr:6 ds:52 dt:46697435 du:-1[[email protected] templates]#
[[email protected] templates]#
看到有資料了,檢測OK。
4.1. 修改mongodb_cmdline函式,通過授權帳號密碼、指定埠進行登入。
function mongodb_cmdline ( $options ) {
// return "echo \"db._adminCommand({serverStatus:1, repl:2})\" | mongo";
return "echo \"db._adminCommand({serverStatus:1, repl:2})\" | /usr/local/mongodb/bin/mongo admin --port $options[port2] --username pluser --password !378zm13yt";
}
4.2. 修改get_command_result函式,採用遠端登入的方式,而非預設SSH的方式。(資料庫本來就可以遠端連線), 大概在558行
// $final_cmd = $use_ssh ? "$ssh '$cmd'" : $cmd;
$final_cmd = "$cmd --host $options[host]";
4.3 修改Data Input Methods
選擇“Console”,在左側選單欄選擇“Data Input Methonds”,再選擇mongodb監控項連線“X Get Mongodb Stats…”,在中間的“Input String”後面的輸入框裡面,帶上引數符合,如下所示:
<path_php_binary> -q <path_cacti>/scripts/ss_get_by_ssh.php --host <hostname> --type mongodb –port2 30000 --username pluser --password !378zm13yt --items dk,dl,dm,dn
4.4. 執行指令碼進行測試,結果如下:
[[email protected] ~]# php ss_get_by_ssh.php --type mongodb --host 10.254.3.62 --port2 30000 --items jc,jd
PHP Parse error: syntax error, unexpected T_STRING in /var/www/html/cacti/scripts/ss_get_by_ssh.php on line 1265
[[email protected] ~]#
指令碼有全形亂碼,去掉,再執行指令碼進行測試,結果如下:
[@cacti scripts]# php ss_get_by_ssh.php --type mongodb --host 10.254.3.62 --port2 30000 --items jc,jd
jc:4 jd:5497683968
5,生成監控圖在介面,選擇“Device”,選擇mongodb伺服器連線“3.X2_mongodb”,再選擇右上角的“Create Graphs for this Host”,在“Graph Types”後的下拉框裡面選擇Mongodb服務項,就會看到列表中有mongodb監控服務,然後打勾選上,點選右下角的“Create”按鈕,為主機新增進監控圖,如下所示:
PS:在新增Device裝置的時候,如果勾選了MongoDB的模板,需要指定Port2引數。
6,檢視mongodb監控影象
新增完畢之後,會有一下系列監控圖,如下所示:
(1)MongoDB Background Flushes GT
(2)MongoDB Commands GT
(3)MongoDB Connections GT
(4)MongoDB Index Ops GT
(5)MongoDB Memory GT
(6) MongoDB Slave Lag GT
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------