前言: 为了更好的使用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”按钮,为主机添加进监控图,如下所示:ps:在添加device设备的时候,如果勾选了mongodb的模板,需要指定port2参数。
(2)mongodb commands gt
(3)mongodb connections gt
(4)mongodb index ops gt
(5)mongodb memory gt
(6) mongodb slave lag gt
参考文章地址:http://blog.chinaunix.net/uid-21505614-id-3186756.html
http://c20031776.blog.163.com/blog/static/68471625201341424117630/