欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

[Cacti]cacti监控mongodb性能实战

程序员文章站 2022-06-16 14:17:56
...

前言: 为了更好的使用mongodb,需要监控出mongodb的一些基础使用情况,比如Flush数、连接数、内存使用率、Index操作,Slave延迟等等,这些可以通过配置cacti监控mongodb的模板来完成。 1,在cacti界面导入模板 在计算机本地,下载此tgz包:http://mysql-cact

前言:

为了更好的使用mongodb,需要监控出mongodb的一些基础使用情况,比如Flush数、连接数、内存使用率、Index操作,Slave延迟等等,这些可以通过配置cacti监控mongodb的模板来完成。

1,在cacti界面导入模板

在计算机本地,下载此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的图形项目了。

2,在cacti服务器端配置监控脚本

[root@squid-2 soft]# wget http://mysql-cacti-templates.googlecode.com/files/better-cacti-templates-1.1.8.tar.gz

[root@squid-2 soft]# tar zxvf better-cacti-templates-1.1.8.tar.gz

[root@squid-2 soft]# cd better-cacti-templates-1.1.8

[root@squid-2 better-cacti-templates-1.1.8]# cp scripts/ss_get_by_ssh.php /var/www/html/cacti/scripts/ss_get_by_ssh.php

[root@squid-2 better-cacti-templates-1.1.8]# chown –R apache /var/www/html/cacti/scripts/ss_get_by_ssh.php

修改监控脚本ss_get_by_ssh.php

[root@squid-2 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由于使用了30000端口不是默认端口27018,所以还需要修改端口配置

function mongodb_cmdline ( $options ) {

return “echo \”db._adminCommand({serverStatus:1, repl:2})\” | /usr/local/mongo/mongodb/bin/mongo --port 30000″;

}

之后,重启cacti服务

[root@squid-2 templates]# service httpd restart

停止 httpd: [确定]

正在启动 httpd: [确定]

[root@squid-2 templates]#

3,使用SSH的方式登录监控

(1):创建SSH远曾登录账号

使用ssh-keygen命令生成秘钥,回车后输入key的路径/var/www/html/cacti/id_rsa,然后2个回车,密码为空,如下:

[root@ 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 root@name01

The key's randomart image is:

+--[ RSA 2048]----+

| .o..o |

| .o... o |

| .B . . o |

| o.E = + |

| S + + o |

| . * . . |

| . o . |

| . . |

| |

+-----------------+

把key文件权限改成cacti的使用用户可以访问的权限:

[root@ squid-2 soft]# chown -R cacti_user /var/www/html/cacti/id_rsa*

[root@ squid-2 soft]#

将key加入到远程mongodb服务器上,也可在远程服务器建立一个cacti_user用户,将key加上到cactiuser目录下

[root@ squid-2 soft]# ssh-copy-id -i /var/www/html/cacti/id_rsa root@10.254.3.62

(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)检测监控脚本

[root@squid-2 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[root@squid-2 templates]#

[root@squid-2 templates]#

看到有数据了,检测OK。

4,使用Mongodb远程连接的方式来监控

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”后面的输入框里面,带上参数符合,如下所示:

-q /scripts/ss_get_by_ssh.php --host --type mongodb –port2 30000 --username pluser --password !378zm13yt --items dk,dl,dm,dn

4.4. 执行脚本进行测试,结果如下:

[root@squid-2 ~]# 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

[root@squid-2 ~]#

脚本有全角乱码,去掉,再执行脚本进行测试,结果如下:

[@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”按钮,为主机添加进监控图,如下所示:
[Cacti]cacti监控mongodb性能实战PS:在添加Device设备的时候,如果勾选了MongoDB的模板,需要指定Port2参数。

[Cacti]cacti监控mongodb性能实战

(2)MongoDB Commands GT

[Cacti]cacti监控mongodb性能实战

(3)MongoDB Connections GT

[Cacti]cacti监控mongodb性能实战

(4)MongoDB Index Ops GT

[Cacti]cacti监控mongodb性能实战

(5)MongoDB Memory GT

[Cacti]cacti监控mongodb性能实战

(6) MongoDB Slave Lag GT

[Cacti]cacti监控mongodb性能实战


[Cacti]cacti监控mongodb性能实战参考文章地址:
http://blog.chinaunix.net/uid-21505614-id-3186756.html

http://c20031776.blog.163.com/blog/static/68471625201341424117630/