nagios的安装路径为 /usr/local/nagios
1.下载插件
cd /usr/local/nagios/libexec
wget --no-check-certificate https://github.com/mzupan/nagios-plugin-mongodb/archive/master.zip
unzip master
mv nagios-plugin-mongodb-master nagios-plugin-mongodb
chown -R nagios.nagios nagios-plugin-mongodb/
2. 因为插件是用python写的,所以需要安装python 并安装mongo python的驱动
我的python的版本是:Python 2.7.3
wget --no-check-certificate https://github.com/mongodb/mongo-python-driver/archive/master.zip
unzip master.zip

cd master
python setup.py install
3. 如果以上两步都成功后 说明已经安装完成,接下来看如何使用
以下是插件的帮助文档
/usr/local/nagios/libexec/nagios-plugin-mongodb/check_mongodb.py --help
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
  -a AUTHDB, --authdb=AUTHDB
                        The database you want to authenticate against
这里面着重说下-A 选项 ,就是因为这个选项 才能灵活的监控mongodb。如下是-A后可以跟的一些选项
option -A:  (choose from 'connect', 'connections', 'replication_lag', 'replication_lag_percent', 'replset_state', 'memory', 'memory_mapped', 'lock', 'flushing', 'last_flush_time', 'index_miss_ratio', 'databases', 'collections', 'database_size', 'database_indexes', 'collection_indexes', 'collection_size', 'collection_storageSize', 'queues', 'oplog', 'journal_commits_in_wl', 'write_data_files', 'journaled', 'opcounters', 'current_lock', 'replica_primary', 'page_faults', 'asserts', 'queries_per_second', 'page_faults', 'chunks_balance', 'connect_primary', 'collection_state', 'row_count', 'replset_quorum')
简单介绍如下:
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. 了解上面的命令结构后 然后是在nagios里配置  这里我就简单配置了一项 其它配置类似
vim /usr/local/nagios/etc/objects/commands.cfg 在最后加入如下信息
#check mongo  definition
define command{
       command_name check_mongodb
       command_line $USER1$/nagios-plugin-mongodb/check_mongodb.py -H $ARG1$ -P $ARG2$ -u $ARG3$ -p $ARG4$ -d  $ARG5$ -A $ARG6$ -W $ARG7$ -C $ARG8$    
       }
简单说下 nagios 用两个$符来包含变量,$USER1$ 是命令插件的路径 此处为/usr/local/nagios/libexec/ ,$ARGn$ 是需要传递的参数
vim services.cfg
define service{
        use                     local-service
        host_name               mongo(在hosts.cfg中定义的被监控mongo主机名)
        service_description     check mongoconnects(自定义的一个名称)
        check_command           check_mongodb!127.0.0.1!27017!mongodb1!mongodb2!mongodb3!connections!90!95
        }
此处check_command要和上面commands.cfg里定义的命令相对应,nagios用叹号来间隔传递的参数
127.0.0.1 对应 ARG1 依次类推。
然后重启nagios
service nagios restart 打开监控页面 看看是否有相应的监控服务。


10-10 04:03