1.安装nagios-plugin-mongodb插件
cd /usr/local/src
wget https://github.com/mzupan/nagios-plugin-mongodb/archive/master.zip
unzip master.zip
cd nagios-plugin-mongodb-master/
mv check_mongodb.py /usr/local/nagios/libexec/
cd /usr/local/nagios/libexec/
chown -R chown nagios.nagios check_mongodb.py
2.安装pymongo驱动
yum install pymongo -y
3.命令使用
[root@nagios ~]# /usr/local/nagios/libexec/check_mongodb.py -h
Usage: check_mongodb.py [options]
This Nagios plugin checks the health of mongodb.
Options:
-h, --help show this help message and exit
-H HOST, --host=HOST The hostname you want to connect to
-P PORT, --port=PORT The port mongodb is running on
-u USER, --user=USER The username you want to login as
-p PASSWD, --pass=PASSWD
The password you want to use for that user
-W WARNING, --warning=WARNING
The warning threshold you want to set
-C CRITICAL, --critical=CRITICAL
The critical threshold you want to set
-A ACTION, --action=ACTION
The action you want to take
--max-lag Get max replication lag (for replication_lag action
only)
--mapped-memory Get mapped memory instead of resident (if resident
memory can not be read)
-D, --perf-data Enable output of Nagios performance data
-d DATABASE, --database=DATABASE
Specify the database to check
--all-databases Check all databases (action database_size)
-s, --ssl Connect using SSL
-r, --replicaset Connect to replicaset
-q QUERY_TYPE, --querytype=QUERY_TYPE
The query type to check
[query|insert|update|delete|getmore|command] from
queries_per_second
-c COLLECTION, --collection=COLLECTION
Specify the collection to check
-T SAMPLE_TIME, --time=SAMPLE_TIME
Time used to sample number of pages faults
-M MONGO_VERSION, --mongoversion=MONGO_VERSION
The MongoDB version you are talking with, either 2 or
3
其中:
connect – 默认动作. 检查连接
connections – 检查打开的数据库连接的百分比
memory – 检测内存使用量
memory_mapped – 检查映射内存的使用情况
lock – 检查锁定时间的百分比
flushing – 检查平均flush时间(以微秒)
last_flush_time – 检查上次刷新时间(以微秒)
index_miss_ratio – 检查索引命中失败率
databases – 检查数据库的总数
collections – 检查集合的总数
database_size – 检查特定数据库的大小
database_indexes – 检查特定数据库的索引大小
collection_indexes – 检查一个集合的索引大小
replication_lag – 检查复制延迟(以秒为单位)
replication_lag_percent – 检查复制延迟(以百分比表示)
replset_state – 检查副本集的状态
replica_primary – 检查副本集的主服务器
queries_per_second – 检查每秒查询量
connect_primary – 检查连接在一组中的主服务器
collection_state – 检查数据库中特定集合的状态
4.配置监控命令
vim command.cfg
define command {
command_name check_mongodb
command_line $USER1$/check_mongodb.py -H $HOSTADDRESS$ -P $ARG1$ -A $ARG2$ -W $ARG3$ -C $ARG4$ -D
}
define command {
command_name check_mongodb_query
command_line $USER1$/check_mongodb.py -H $HOSTADDRESS$ -P $ARG1$ -A $ARG2$ -W $ARG3$ -C $ARG4$ -q $ARG5$ -D
}
5.配置监控项
##每秒钟查询量
define service{
use local-service
host_name usvr-111
service_description Mongo DB 每秒钟查询量 #27017
check_command check_mongodb_query!27017!queries_per_second!200!150!query
service_groups mongodb_services
notifications_enabled 1
notification_interval 0
contact_groups admin
}
###锁表百分比
define service{
use local-service
host_name usvr-111
service_description Mongo DB 锁表百分比#27017
check_command check_mongodb!27017!lock!5!10
service_groups mongodb_services
notifications_enabled 1
notification_interval 0
contact_groups admin
}
##连接数##
define service{
use local-service
host_name usvr-111
service_description Mongo DB 连接数#27017
check_command check_mongodb!27017!connections!70!80
service_groups mongodb_services
notifications_enabled 1
notification_interval 0
contact_groups admin
}
##连接超时##
define service{
use local-service
host_name usvr-111
service_description Mongo DB 连接超时 #27017
check_command check_mongodb!27017!connect!2!4
service_groups mongodb_services
notifications_enabled 1
notification_interval 0
contact_groups admin
}