線上實戰zabbix監控mongodb詳細步驟
**Zabbix 監控mongodb做後整理**
系統運維工程師 李超
因為我安全問題我們只能呢個通過mongo客戶端去遠程監控mongo服務端。
1、 如果你不是在mongo服務器上操作請參考我下邊的部分,如果是直接在mongodb服務器上安裝的zabbix_agnetd請參考網上其他文章。(直接在mongodb服務端安裝zabbix客戶端的文檔非常多)。
2、 下邊首先安裝mongo客戶端:我的系統是:ubuntu16.04,安裝mongo客戶端直接apt-get安裝即可(這裏不多做介紹,但是要註意apt一般都是較低的版本,要4.0版本的可以更新apt這裏不做介紹,網上可以搜索到相關內容)。
/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" : "------------------------------------------------\nMALLOC: 4007000 ( 3.8 MiB) Bytes in use by application\nMALLOC: + 36864 ( 0.0 MiB) Bytes in page heap freelist\nMALLOC: + 3283112 ( 3.1 MiB) Bytes in central cache freelist\nMALLOC: + 5451808 ( 5.2 MiB) Bytes in transfer cache freelist\nMALLOC: + 5583584 ( 5.3 MiB) Bytes in thread cache freelists\nMALLOC: + 8577216 ( 8.2 MiB) Bytes in malloc metadata\nMALLOC: ------------\nMALLOC: = 26939584 ( 25.7 MiB) Actual memory used (physical + swap)\nMALLOC: + 3090604032 ( 2947.4 MiB) Bytes released to OS (aka unmapped)\nMALLOC: ------------\nMALLOC: = 3117543616 ( 2973.1 MiB) Virtual address space used\nMALLOC:\nMALLOC: 2014 Spans in use\nMALLOC: 115 Thread heaps in use\nMALLOC: 4096 Tcmalloc page size\n------------------------------------------------\nCall ReleaseFreeMemory() to release freelist memory to the OS (via madvise()).\nBytes released to the OS take up virtual address space but no physical memory.\n"
}
},
"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詳細步驟