线上实战zabbix监控mongodb详细步骤
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线上实战zabbix监控mongodb详细步骤相关的知识,希望对你有一定的参考价值。
**Zabbix 监控mongodb做后整理**
系统运维工程师 李超
因为我安全问题我们只能呢个通过mongo客户端去远程监控mongo服务端。
1、 如果你不是在mongo服务器上操作请参考我下边的部分,如果是直接在mongodb服务器上安装的zabbix_agnetd请参考网上其他文章。(直接在mongodb服务端安装zabbix客户端的文档非常多)。
2、 下边首先安装mongo客户端:我的系统是:ubuntu16.04,安装mongo客户端直接apt-get安装即可(这里不多做介绍,但是要注意apt一般都是较低的版本,要4.0版本的可以更新apt这里不做介绍,网上可以搜索到相关内容)。
3、 当mongodb客户端安装完毕以后我们执行下边命令
/bin/echo "db.serverStatus()" |/usr/bin/mongo 192.168.16.111:20001/admin -ulichao -plichao
通过这个命令可以测试知道mongo的系统参数,我们根据这些系统参数进行监控,
执行结果如下:
MongoDB shell version v4.0.0
connecting to: mongodb://192.168.16.111:20001/admin
MongoDB server version: 3.4.9
WARNING: shell and server versions do not match
{
"host" : "iZ2zeem1uivcbfbr01nmnrZ:20000",
"version" : "3.4.9",
"process" : "mongos",
"pid" : NumberLong(10828),
"uptime" : 24441734,
"uptimeMillis" : NumberLong("24441733680"),
"uptimeEstimate" : NumberLong(24441733),
"localTime" : ISODate("2018-08-02T09:34:54.400Z"),
"asserts" : {
"regular" : 0,
"warning" : 0,
"msg" : 0,
"user" : 1763423,
"rollovers" : 0
},
"connections" : {
"current" : 86,
"available" : 52342,
"totalCreated" : 118672
},
"extra_info" : {
"note" : "fields vary by platform",
"page_faults" : 14
},
"network" : {
"bytesIn" : NumberLong("467536962787"),
"bytesOut" : NumberLong("111369070632"),
"physicalBytesIn" : NumberLong("467536962787"),
"physicalBytesOut" : NumberLong("108213593338"),
"numRequests" : NumberLong("2582169303")
},
"opcounters" : {
"insert" : 619611,
"query" : 406777,
"update" : 122426978,
"delete" : 61,
"getmore" : 308,
"command" : 215576153
},
"sharding" : {
"configsvrConnectionString" : "config/iZ2ze7s6pfi7t4x18mx417Z:22000,iZ2ze7s6pfi7t4x18mx418Z:22000,iZ2ze7s6pfi7t4x18mx419Z:22000",
"lastSeenConfigServerOpTime" : {
"ts" : Timestamp(1533202490, 2),
"t" : NumberLong(3)
}
},
"tcmalloc" : {
"generic" : {
"current_allocated_bytes" : 4007000,
"heap_size" : NumberLong("3108966400")
},
"tcmalloc" : {
"pageheap_free_bytes" : 36864,
"pageheap_unmapped_bytes" : NumberLong("3090604032"),
"max_total_thread_cache_bytes" : NumberLong(1073741824),
"current_total_thread_cache_bytes" : 5583584,
"total_free_bytes" : 14318504,
"central_cache_free_bytes" : 3283112,
"transfer_cache_free_bytes" : 5451808,
"thread_cache_free_bytes" : 5583584,
"aggressive_memory_decommit" : 0,
"formattedString" : "------------------------------------------------ MALLOC: 4007000 ( 3.8 MiB) Bytes in use by application MALLOC: + 36864 ( 0.0 MiB) Bytes in page heap freelist MALLOC: + 3283112 ( 3.1 MiB) Bytes in central cache freelist MALLOC: + 5451808 ( 5.2 MiB) Bytes in transfer cache freelist MALLOC: + 5583584 ( 5.3 MiB) Bytes in thread cache freelists MALLOC: + 8577216 ( 8.2 MiB) Bytes in malloc metadata MALLOC: ------------ MALLOC: = 26939584 ( 25.7 MiB) Actual memory used (physical + swap) MALLOC: + 3090604032 ( 2947.4 MiB) Bytes released to OS (aka unmapped) MALLOC: ------------ MALLOC: = 3117543616 ( 2973.1 MiB) Virtual address space used MALLOC: MALLOC: 2014 Spans in use MALLOC: 115 Thread heaps in use MALLOC: 4096 Tcmalloc page size ------------------------------------------------ Call ReleaseFreeMemory() to release freelist memory to the OS (via madvise()). Bytes released to the OS take up virtual address space but no physical memory. "
}
},
"mem" : {
"bits" : 64,
"resident" : 174,
"virtual" : 3426,
"supported" : true
},
"metrics" : {
"cursor" : {
"timedOut" : NumberLong(380),
"open" : {
"multiTarget" : NumberLong(0),
"singleTarget" : NumberLong(0),
"pinned" : NumberLong(0),
"total" : NumberLong(0)
}
},
"commands" : {
"<UNKNOWN>" : NumberLong(1),
"aggregate" : {
"failed" : NumberLong(142),
"total" : NumberLong(8980)
},
"authenticate" : {
"failed" : NumberLong(12),
"total" : NumberLong(12)
},
"buildInfo" : {
"failed" : NumberLong(0),
"total" : NumberLong(635407)
},
"collStats" : {
"failed" : NumberLong(65),
"total" : NumberLong(6827)
},
"count" : {
"failed" : NumberLong(388),
"total" : NumberLong(491985789)
},
"create" : {
"failed" : NumberLong(0),
"total" : NumberLong(10)
},
"createIndexes" : {
"failed" : NumberLong(1),
"total" : NumberLong(69)
},
"createUser" : {
"failed" : NumberLong(6),
"total" : NumberLong(10)
},
"currentOp" : {
"failed" : NumberLong(0),
"total" : NumberLong(15)
},
"dbStats" : {
"failed" : NumberLong(7),
"total" : NumberLong(917)
},
"delete" : {
"failed" : NumberLong(0),
"total" : NumberLong(705)
},
"distinct" : {
"failed" : NumberLong(17),
"total" : NumberLong(5105)
},
"drop" : {
"failed" : NumberLong(0),
"total" : NumberLong(67)
},
"dropDatabase" : {
"failed" : NumberLong(0),
"total" : NumberLong(7)
},
"dropIndexes" : {
"failed" : NumberLong(7),
"total" : NumberLong(23)
},
"enableSharding" : {
"failed" : NumberLong(1),
"total" : NumberLong(2)
},
"explain" : {
"failed" : NumberLong(0),
"total" : NumberLong(13)
},
"find" : {
"failed" : NumberLong(131),
"total" : NumberLong(1764517)
},
"findAndModify" : {
"failed" : NumberLong(1),
"total" : NumberLong(772)
},
"getLastError" : {
"failed" : NumberLong(0),
"total" : NumberLong(1989)
},
"getLog" : {
"failed" : NumberLong(0),
"total" : NumberLong(2)
},
"getMore" : {
"failed" : NumberLong(17),
"total" : NumberLong(1791)
},
"getnonce" : {
"failed" : NumberLong(0),
"total" : NumberLong(38)
},
"insert" : {
"failed" : NumberLong(0),
"total" : NumberLong(207939962)
},
"isMaster" : {
"failed" : NumberLong(0),
"total" : NumberLong(35391424)
},
"killCursors" : {
"failed" : NumberLong(0),
"total" : NumberLong(79)
},
"listCollections" : {
"failed" : NumberLong(0),
"total" : NumberLong(862)
},
"listDatabases" : {
"failed" : NumberLong(0),
"total" : NumberLong(263)
},
"listIndexes" : {
"failed" : NumberLong(0),
"total" : NumberLong(8)
},
"logout" : {
"failed" : NumberLong(0),
"total" : NumberLong(8658)
},
"mapReduce" : {
"failed" : NumberLong(24),
"total" : NumberLong(64)
},
"ping" : {
"failed" : NumberLong(0),
"total" : NumberLong(5980)
},
"profile" : {
"failed" : NumberLong(4),
"total" : NumberLong(4)
},
"renameCollection" : {
"failed" : NumberLong(0),
"total" : NumberLong(2)
},
"repairDatabase" : {
"failed" : NumberLong(0),
"total" : NumberLong(1)
},
"replSetGetStatus" : {
"failed" : NumberLong(88870),
"total" : NumberLong(88870)
},
"rolesInfo" : {
"failed" : NumberLong(0),
"total" : NumberLong(491)
},
"saslContinue" : {
"failed" : NumberLong(4),
"total" : NumberLong(183362)
},
"saslStart" : {
"failed" : NumberLong(8),
"total" : NumberLong(91691)
},
"serverStatus" : {
"failed" : NumberLong(0),
"total" : NumberLong(223111)
},
"shardCollection" : {
"failed" : NumberLong(0),
"total" : NumberLong(3)
},
"update" : {
"failed" : NumberLong(0),
"total" : NumberLong(552681439)
},
"usersInfo" : {
"failed" : NumberLong(0),
"total" : NumberLong(493)
},
"whatsmyuri" : {
"failed" : NumberLong(0),
"total" : NumberLong(54513)
}
}
},
"ok" : 1
}
Bye
上边是我们执行完命令从mongo服务端获取到mongo服务端的参数,下面对他进行监控。
首先建立脚本check_mongo.sh
#!/bin/bash
#mongodb status
#b.serverStatus().ok
#memory status
#Physical memory: db.serverStatus().mem.resident
#Virtual memory: db.serverStatus().mem.virtual
#opcounters status
#insert: db.serverStatus().opcounters.insert
#query: db.serverStatus().opcounters.query
#update: db.serverStatus().opcounters.update
#delete: db.serverStatus().opcounters.delete
#connections status
#current connections: db.serverStatus().connections.current
MONGODBPATH="/usr/bin/mongo"
HOST="47.94.37.221"
PORT="$1"
MONGODB_PA="$MONGODBPATH ${HOST}:${PORT}/admin -uroot -pkeytech2017"if [ $# == 3 ];then
result=$(/bin/echo "db.serverStatus().$2.$3" | $MONGODB_PA --quiet)
echo $result
elif [ $# == 2 ];then
result=$(/bin/echo "db.serverStatus().$2" | $MONGODB_PA --quiet)
echo $result
else
echo "Usage:$0 PORT mem resident"
fi
保存退出!
这里我们可以测试一下我们的脚本可用性:
我们这里的测试是结合上边第一条命令执行后显示的参数来输入的如:
这下明白脚本后边跟的参数的意思了吧$1:端口 $2:主参数 $3: 你要提取的参数
然后我们建立我们想要的key
别问我这个目录时干什么的,如果不知道,请从头开始了解zabbix。
在这个目录下我监理一个.conf文件
这个是我的key文件里边所有内容,但是接上边的内容我们这里只写入
UserParameter=MongoDB.totalCreated,/data/zabbix/scripts/check_mongodb.sh 20001 connections totalCreated
UserParameter=MongoDB.available,/data/zabbix/scripts/check_mongodb.sh 20001 connections available
这两条
剩下的两条后边会用到(最下边会说这两条做什么用)
加完那两条我们保存退出,然后去web上自定义key进行监控
保存,过一会监控图像就出来了,如图:
OK!监控完毕
但是,但是……mongo那么多监控项我们现在只监控了两项吧,如果你觉得上边的方法你吼的住你照着上边自己慢慢的吧所有key都谢出来,下面介绍其他方法,
第二种办法就是我上边说的,直接定义key,在key文件里边加上命令如下
UserParameter=MongoDB.Status[*],/bin/echo "db.serverStatus().$1" |/usr/bin/mongo 192.168.16.111:20001/admin -ulichao -plichao | grep "<$2>" |awk -F : ‘{print $$2}‘ |awk -F , ‘{print $$1}‘
UserParameter=MongoDB.byesOut,/bin/echo "db.serverStatus().network" |/usr/bin/mongo 192.168.16.111:20001/admin -ulichao -plichao | grep "bytesOut" | awk -F : ‘{print $2}‘ | awk -F , ‘{print $1}‘ | awk -F " ‘{print $2}‘
UserParameter=MongoDB.bytesIn,/bin/echo "db.serverStatus().network" |/usr/bin/mongo 192.168.16.111:20001/admin -ulichao -plichao | grep "bytesIn" | awk -F : ‘{print $2}‘ | awk -F , ‘{print $1}‘ | awk -F " ‘{print $2}‘
添加完毕保存保存退出。
然后直接导入模板监控即可。模板在这:https://pan.baidu.com/s/1PFrhealedPUejz7ACI_WVA
添加到web上我么你可以在图形上看到下图
到此mongo监控完毕。
系统运维工程师 李超
以上是关于线上实战zabbix监控mongodb详细步骤的主要内容,如果未能解决你的问题,请参考以下文章