参考文档
https://www.cnblogs.com/clsn/p/8214194.html#auto_id_0
监控数据:mongostat,记录mongodb实例运行的状态,如图:
insert:每秒插入数(带*的表示复制集的从库,下同)
query:每秒查询数
update:每秒更新数
delete:每秒删除数
getmore:每秒返回的游标数
command:每秒的命令数(其中从库用管道符|将本地数据和复制数据隔开) 。
dirty: wiredtiger特有的参数,记录wiredtiger缓存中脏数据的比例。
used: wiredtiger特有的参数,记录wiredtiger缓存使用百分比。
flusher: 每个查询间隔之间的checkpoint触发次数。
Vsize:虚拟内存使用量。
res:物理内存使用量。
qrw: 客户端等待从MongoDB实例读写数据的队列长度,|隔开。
arw:执行读写操作的活跃客户端数量,|隔开。
netIn:MongoDB实例的网络进流量。
netOut:MongoDB实例的网络出流量。
conn: 连接数。
set:复制集名称。
repl:复制集中的状态。
TestRepSet:SECONDARY> db.serverStatus().mem { "bits" : 64, "resident" : 98, "virtual" : 1357, "supported" : true, "mapped" : 0, "mappedWithJournal" : 0 } TestRepSet:SECONDARY> db.serverStatus().connections { "current" : 3, "available" : 51197, "totalCreated" : 4 } TestRepSet:SECONDARY> db.serverStatus().connections.current 3 TestRepSet:SECONDARY> db.serverStatus().globalLock.currentQueue { "total" : 0, "readers" : 0, "writers" : 0 } TestRepSet:SECONDARY> db.serverStatus().opcounters { "insert" : 0, "query" : 2, "update" : 0, "delete" : 0, "getmore" : 0, "command" : 6630 } TestRepSet:SECONDARY>
单机版安装
设置yum源
vim /etc/yum.repos.d/mongodb-org-4.0.repo
[mongodb-org-4.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/7Server/mongodb-org/4.0/x86_64/ gpgcheck=0 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
yum安装
yum makecache yum install -y mongodb-org rpm -ql mongodb-org-server
/etc/mongod.conf
/lib/systemd/system/mongod.service
/usr/bin/mongod
/usr/share/doc/mongodb-org-server-4.0.6
/usr/share/doc/mongodb-org-server-4.0.6/LICENSE-Community.txt
/usr/share/doc/mongodb-org-server-4.0.6/MPL-2
/usr/share/doc/mongodb-org-server-4.0.6/README
/usr/share/doc/mongodb-org-server-4.0.6/THIRD-PARTY-NOTICES
/usr/share/man/man1/mongod.1
/var/lib/mongo
/var/log/mongodb
/var/log/mongodb/mongod.log
/var/run/mongodb
查看配置文件
vim /etc/mongod.conf
启动服务与查看
systemctl enable mongod systemctl start mongod systemctl status mongod
查看DB状态
mongostat
mongostat insert query update delete getmore command dirty used flushes vsize res qrw arw net_in net_out conn time *0 *0 *0 *0 0 1|0 0.0% 0.0% 0 1.03G 70.0M 0|0 1|0 157b 62.5k 1 Mar 20 23:19:44.070 *0 *0 *0 *0 0 1|0 0.0% 0.0% 0 1.03G 70.0M 0|0 1|0 157b 62.5k 1 Mar 20 23:19:45.071 *0 *0 *0 *0 0 2|0 0.0% 0.0% 0 1.03G 70.0M 0|0 1|0 158b 62.8k 1 Mar 20 23:19:46.067
两台集群配置(复制集群)
egrep -v "^$|^#" /etc/mongod.conf
systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log storage: dbPath: /var/lib/mongo journal: enabled: true processManagement: fork: true # fork and run in background pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile timeZoneInfo: /usr/share/zoneinfo net: port: 27017 bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting. setParameter: enableLocalhostAuthBypass: false replication: replSetName: TestRepSet
连接其中一个mongod,设置复制集的成员信息
mongo
config = { _id: "TestRepSet", members: [ {_id: 0, host: "10.10.10.80:27017",priority:1}, {_id: 1, host: "10.10.10.198:27017",priority:2}] }
rs.initiate(config)
{ "ok" : 1, "operationTime" : Timestamp(1553103106, 1), "$clusterTime" : { "clusterTime" : Timestamp(1553103106, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } }
TestRepSet:SECONDARY> rs.status()
{ "set" : "TestRepSet", "date" : ISODate("2019-03-20T17:32:10.820Z"), "myState" : 1, "term" : NumberLong(1), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1553103119, 1), "t" : NumberLong(1) }, "readConcernMajorityOpTime" : { "ts" : Timestamp(1553103119, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1553103119, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1553103119, 1), "t" : NumberLong(1) } }, "lastStableCheckpointTimestamp" : Timestamp(1553103118, 1), "members" : [ { .....
创建相关用户 (必须在上面第5这步完成后才能做)
use admin db.createUser({user:"admin",pwd:"123456",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})
MongoDB规定,创建的第一个用户一定要是在admin数据库、并且角色是userAdminAnyDatabase。
创建完第一个角色,用这个角色登录来创建其他角色。
use admin db.auth("admin","123456") db.createUser({user:"test",pwd:"123456",roles:[{role:"clusterAdmin",db:"admin"}]}) #这个角色可以进行复制集的管理(添加成员、查看状态等等)
最后
把集群中所有主机的配置文件的security.authorization:true注释去掉,重启服务
systemctl restart mongod
mongo
rs.status();
TestRepSet:SECONDARY> rs.status(); { "set" : "TestRepSet", "date" : ISODate("2019-03-20T18:37:32.576Z"), "myState" : 2, "term" : NumberLong(3), "syncingTo" : "10.10.10.198:27017", "syncSourceHost" : "10.10.10.198:27017", "syncSourceId" : 1, "heartbeatIntervalMillis" : NumberLong(2000), "optimes" :
......
db.serverStatus().repl.primary
10.10.10.198:27017
zabbix monitor mongodb
yum install epel-release -y yum install jq -y
编写监控shell脚本
vim /usr/local/zabbix/share/zabbix/externalscripts/mongodb.sh
#!/bin/bash ################################################## # Description: zabbix mongodb monitor # Note: Zabbix 3.2 # Date: Fri Apr 21 2017 # Verion: 1.0 # Requirments: mongo, jq, sudo access to mongo_conf.sh # # Based on Noe <netkiller@msn.com> script # # Change log: # Mon Apr 24, 2017 # - Added checks for mongo and jq # - User --eval to fectch data # - fixed json output to comform # - Use jq to parse json https://stedolan.github.io/jq/ # - upated index to handle space or comma betwee values # ################################################## source /etc/profile DB_HOST=127.0.0.1 DB_PORT=27017 DB_USERNAME=test DB_PASSWORD=123456 MONGO=`which mongo` JQ=`which jq` EXIT_ERROR=1 EXIT_OK=0 if [ ! -x "$MONGO" ] ; then echo "mongo not found" exit $EXIT_ERROR elif [ ! -x "$JQ" ] ; then echo "jq not found" exit $EXIT_ERROR elif [ $# -eq 0 ] ; then echo "No values pass" exit $EXIT_ERROR fi index=.$(echo $@ | sed 's/[ ,]/./g') MONGO_CMD="$MONGO --host ${DB_HOST:-localhost} --port ${DB_PORT:-27017} --authenticationDatabase admin --quiet" [[ "$DB_USERNAME" ]] && MONGO_CMD="${MONGO_CMD} --username ${DB_USERNAME}" [[ "$DB_PASSWORD" ]] && MONGO_CMD="${MONGO_CMD} --password ${DB_PASSWORD}" output=$( $MONGO_CMD <<< "db.runCommand( { serverStatus: 1} )" | sed -e 's/NumberLong((.*))/1/ s/ISODate((.*))/1/ s/ObjectId((.*))/1/ s/Timestamp(.*)/"&"/ s/(BinData(.*)"(.*)")/"12)"/ s/"([0-9]*)"/1/' ) mongo_status=${PIPESTATUS[0]} if [ $mongo_status -ne $EXIT_OK ] ; then echo "mongo exec error" exit $EXIT_ERROR fi value=$(echo $output | jq $index 2>/dev/null) #jq_status=$? echo $value
vim /etc/zabbix/zabbix_agentd.conf
UnsafeUserParameters=1
UserParameter=mongodb.serverStatus[*],/usr/local/zabbix/share/zabbix/externalscripts/mongodb.sh $1 $2 $3 $4 $
重启zabbix agent服务
systemctl restart zabbix-agent.service
zabbix server 端测试
bash-4.3# zabbix_get -s 10.10.10.80 -k 'mongodb.status[mem,virtual]' 1354 bash-4.3# zabbix_get -s 10.10.10.80 -k 'mongodb.status[opcounters,query]' 2
导入zabbix 模板
mongodb rs.status() repl set 监控(python脚本)
/usr/local/zabbix/share/zabbix/externalscripts/mongo.py
#!/usr/bin/python3 #coding:utf-8 import pymongo from pymongo import MongoClient class check_mongo_repl(): def __init__(self): self.conn = pymongo.MongoClient('mongodb://test:123456@127.0.0.1:27017/') self.db = self.conn.admin self.rs = self.db.command('replSetGetStatus') def get_rs_num(self): list = "Value : %s" % self.rs.values() get_list = list.split(',') a = 0 if " 'stateStr': 'PRIMARY'" in get_list: a = 1 if " 'stateStr': 'SECONDARY'" in get_list: a += 1 if " 'stateStr': 'ARBITER'" in get_list: a += 1 return a if __name__ == "__main__": mongo = check_mongo_repl() print (mongo.get_rs_num())
zabbix agent 上加入自定义参数
vim /etc/zabbix/zabbix_agentd.conf
UserParameter=mongodb.repl.set[*],/usr/local/zabbix/share/zabbix/externalscripts/mongodb.py
重启zabbix agent服务
systemctl restart zabbix-agent.service
zabbix server端测试
bash-4.3# zabbix_get -s 10.10.10.80 -k "mongodb.repl.set" 2
根据实际情况加入trigger, graph 监控完成.