基于zabbix3.2对mongodb的监控
脚本:
calendar timegm time gmtime urllib.parse pymongo MongoClient, errors MongoDB(): (): .mongo_host = .mongo_port = .mongo_db = [, ] .mongo_user = urllib.parse.quote_plus() .mongo_password = urllib.parse.quote_plus() .__conn = .__dbnames = .__metrics = [] connect(): .__conn : .mongo_user : : .__conn = MongoClient(% (.mongo_host, .mongo_port)) errors.PyMongoError py_mongo_error: (% (py_mongo_error)) : : .__conn = MongoClient(% (.mongo_user, .mongo_password, .mongo_host, .mongo_port)) errors.PyMongoError py_mongo_error: (% (py_mongo_error)) add_metrics(, k, v): dict_metrics = {} dict_metrics[] = k dict_metrics[] = v .__metrics.append(dict_metrics) print_metrics(): metrics = .__metrics metric metrics: zabbix_item_key = (metric[]) zabbix_item_value = (metric[]) (+ zabbix_item_key + + zabbix_item_value) get_db_names(): .__conn : .connect() db_handler = .__conn[.mongo_db[]] master = db_handler.command()[] dict_metrics = {} dict_metrics[] = master: dict_metrics[] = db_names = .__conn.database_names() .__dbnames = db_names : dict_metrics[] = .__metrics.append(dict_metrics) get_mongo_db_lld(): .__dbnames : db_names = .get_db_names() : db_names = .__dbnames dict_metrics = {} db_list = [] dict_metrics[] = dict_metrics[] = {: db_list} db_names : db_name db_names: dict_lld_metric = {} dict_lld_metric[] = db_name db_list.append(dict_lld_metric) dict_metrics[] = {: db_list} .__metrics.insert(, dict_metrics) get_oplog(): .__conn : .connect() db_handler = .__conn[] coll = db_handler.oplog.rs op_first = (coll.find().sort(, ).limit()) op_last = (coll.find().sort(, -).limit()) op_first.count() > op_last.count() > : op_fst = (op_first.next())[].time op_last_st = op_last[][] op_lst = (op_last.next())[].time status = ((op_lst - op_fst), ) .add_metrics(, status) current_time = timegm(gmtime()) oplog = ((((op_last_st).split())[].split())[]) .add_metrics(, (current_time - oplog)) get_maintenance(): .__conn : .connect() db_handler = .__conn fsync_locked = (db_handler.is_locked) .add_metrics(, fsync_locked) : config = db_handler.admin.command(, ) connstring = (.mongo_host + + (.mongo_port)) i (, (config[][])): connstring config[][][i][]: priority = config[][][i][] hidden = (config[][][i][]) .add_metrics(, priority) .add_metrics(, hidden) errors.PyMongoError: () get_server_status_metrics(): .__conn : .connect() db_handler = .__conn[.mongo_db[]] ss = db_handler.command() .add_metrics(, ss[]) .add_metrics(, ss[][]) .add_metrics(, (ss[])) .add_metrics(, (ss[])) k, v ss[].items(): .add_metrics(+ k, v) k, v ss[].items(): .add_metrics(+ k, v) k [, , , ]: .add_metrics(+ k, ss[][k]) k, v ss[].items(): .add_metrics(+ k, v) k, v ss[].items(): .add_metrics(+ k, v) .add_metrics(, ss[][]) ss[][] == : .add_metrics(, ss[][] []) .add_metrics(, ss[][] []) .add_metrics(, ss[][] []) lock_total_time = ss[][] .add_metrics(, lock_total_time) k, v ss[][].items(): .add_metrics(+ k, v) k, v ss[][].items(): .add_metrics(+ k, v) get_db_stats_metrics(): .__conn : .connect() .__dbnames : .get_db_names() .__dbnames : mongo_db .__dbnames: db_handler = .__conn[mongo_db] dbs = db_handler.command() k, v dbs.items(): k [, , , , , , , , , , ]: .add_metrics(+ k + + mongo_db + , (v)) close(): .__conn : .__conn.close() __name__ == : mongodb = MongoDB() mongodb.get_db_names() mongodb.get_mongo_db_lld() mongodb.get_oplog() mongodb.get_maintenance() mongodb.get_server_status_metrics() mongodb.get_db_stats_metrics() mongodb.print_metrics() mongodb.close()
#!/bin/bash
get_MongoDB_metrics(){
python /usr/local/zabbix/scripts/zabbix-mongodb.py
}
# Send the results to zabbix server by using zabbix sender
result=$(get_MongoDB_metrics | /usr/local/zabbix/bin/zabbix_sender -vv -c /usr/local/zabbix/conf/zabbix_agentd.conf -i - 2>&1)
response=$(echo "$result" | awk -F ';' '$1 ~ /^info/ && match($1,/[0-9].*$/) {sum+=substr($1,RSTART,RLENGTH)} END {print sum}')
if [ -n "$response" ]; then
echo "$response"
else
echo "$result"
fi
模版:
Template MongoDB
The amount of mapped memory, in megabytes (MB), including the memory used for journaling. This value will always be twice the value of mem.mapped. This field is only included if journaling is enabled.
The scale argument affects this value. Only present when using the mmapv1 storage engine.
Only present when using the mmapv1 storage engine.
and {Template MongoDB:mongodb.hidden.last()}=1
and {Template MongoDB:mongodb.priority.last()}=0
and {Template MongoDB:mongodb.hidden.last()}=1
and {Template MongoDB:mongodb.priority.last()}=1
and {Template MongoDB:mongodb.hidden.last()}=0
and {Template MongoDB:mongodb.priority.last()}=0
and {Template MongoDB:mongodb.hidden.last()}=0
and {Template MongoDB:mongodb.priority.last()}=1
and {Template MongoDB:mongodb.hidden.last()}=1
and {Template MongoDB:mongodb.priority.last()}=0
and {Template MongoDB:mongodb.hidden.last()}=1
and {Template MongoDB:mongodb.priority.last()}=1
and {Template MongoDB:mongodb.hidden.last()}=0
and {Template MongoDB:mongodb.priority.last()}=0
zabbix_config:
# mongodb stats
UserParameter=mongodb.zabbix.sender, /usr/local/zabbix/scripts/mongodb-stats.sh
注释:
python脚本中引用的Module pymongo 3.6版本,注意不同版本可能引发的方法变更