mongodb的监控

1.MongoDB自带的监控

2.MongoDB一些参数监控

2.1 db.serverStatus指令

2.2 db.stats指令

2.3 配置zabbix的mongodb_status.conf

3.zabbix监控

3.1 zabbix-get 工具检测

3.2zabbix创建模板,并在模板中创建item和graph

4.python脚本操作mongodb


1.MongoDB自带的监控

[root@bfd-ango-01 shell]# mongostat -h 47.105.115.143:27017 -udolphin -pk8DDg9sd --authenticationDatabase admin
insert query update delete getmore command flushes mapped vsize   res faults qrw arw net_in net_out conn                time
    *0    *0     *0     *0       0    12|0       0     0B  272M 22.0M      0 0|0 0|0  1.61k   24.1k    6 Jan 14 15:29:31.648
    *0    *0     *0     *0       0    26|0       0     0B  272M 22.0M      0 0|0 0|0  3.84k   39.0k    6 Jan 14 15:29:32.642
    *0    *0     *0     *0       0    12|0       0     0B  272M 22.0M      0 0|0 0|0  1.69k   23.3k    6 Jan 14 15:29:33.643
    *0    *0     *0     *0       0    24|0       0     0B  272M 22.0M      0 0|0 0|0  3.59k   37.7k    6 Jan 14 15:29:34.663
    *0    *0     *0     *0       0    11|0       0     0B  272M 22.0M      0 0|0 0|0  1.51k   22.7k    6 Jan 14 15:29:35.675
    *0    *0     *0     *0       0    12|0       0     0B  272M 22.0M      0 0|0 0|0  1.55k   23.1k    6 Jan 14 15:29:36.666
    *0    *0     *0     *0       0    25|0       0     0B  272M 22.0M      0 0|0 0|0  3.75k   39.4k    6 Jan 14 15:29:37.642
    *0    13     *0     *0       0    12|0       0     0B  272M 22.0M      0 0|0 0|0  5.76k   36.3k    6 Jan 14 15:29:38.641
    *0    *0     *0     *0       0    11|0       0     0B  272M 22.0M      0 0|0 0|0  1.53k   22.9k    6 Jan 14 15:29:39.642
    *0    *0     *0     *0       0    11|0       0     0B  272M 22.0M      0 0|0 0|0  1.53k   22.9k    6 Jan 14 15:29:40.644

mongostat是mongdb自带的状态检测工具,在命令行下使用。它会间隔固定时间获取mongodb的当前运行状态,并输出。如果你发现数据库突然变慢或者有其他问题的话,你第一手的操作就考虑采用mongostat来查看mongo的状态。

它的输出有以下几列:

  • inserts/s 每秒插入次数
  • query/s 每秒查询次数
  • update/s 每秒更新次数
  • delete/s 每秒删除次数
  • getmore/s 每秒执行getmore次数
  • command/s 每秒的命令数,比以上插入、查找、更新、删除的综合还多,还统计了别的命令
  • flushs/s 每秒执行fsync将数据写入硬盘的次数。
  • mapped/s 所有的被mmap的数据量,单位是MB,
  • vsize 虚拟内存使用量,单位MB
  • res 物理内存使用量,单位MB
  • faults/s 每秒访问失败数(只有Linux有),数据被交换出物理内存,放到swap。不要超过100,否则就是机器内存太小,造成频繁swap写入。此时要升级内存或者扩展
  • locked % 被锁的时间百分比,尽量控制在50%以下吧
  • idx miss % 索引不命中所占百分比。如果太高的话就要考虑索引是不是少了
  • q t|r|w 当Mongodb接收到太多的命令而数据库被锁住无法执行完成,它会将命令加入队列。这一栏显示了总共、读、写3个队列的长度,都为0的话表示mongo毫无压力。高并发时,一般队列值会升高。
  • conn 当前连接数
  • time 时间戳

2.MongoDB一些参数监控

这里我们不会很详细描述每一步操作,只是告诉如何去监控这些指标,实际情况根据自己的需要去监控,首先我们看看mongodb有那些指标

2.1 db.serverStatus指令

 

[root@bfd-ango-01 shell]# echo "db.serverStatus()"|mongo 47.105.115.143:27017/app -udolphin -pk8DDg9sd --authenticationDatabase admin
MongoDB shell version v4.0.4
connecting to: mongodb://47.105.115.143:27017/app
Implicit session: session { "id" : UUID("49e995c3-4836-4b51-a52a-7b3b4900a7c8") }
MongoDB server version: 4.0.4
{
	"host" : "bfd-ango-01",
	"version" : "4.0.4",
	"process" : "mongos",
	"pid" : NumberLong(675154),
	"uptime" : 3534927,
	"uptimeMillis" : NumberLong("3534927010"),
	"uptimeEstimate" : NumberLong(3534927),
	"localTime" : ISODate("2019-01-14T07:35:20.453Z"),
	"asserts" : {
		"regular" : 0,
		"warning" : 0,
		"msg" : 0,
		"user" : 241,
		"rollovers" : 0
	},
	"connections" : {
		"current" : 6,
		"available" : 524274,
		"totalCreated" : 158240
	},
	"extra_info" : {
		"note" : "fields vary by platform",
		"page_faults" : 4
	},
	"logicalSessionRecordCache" : {
		"activeSessionsCount" : 8,
		"sessionsCollectionJobCount" : 11784,
		"lastSessionsCollectionJobDurationMillis" : 12,
		"lastSessionsCollectionJobTimestamp" : ISODate("2019-01-14T07:34:56.902Z"),
		"lastSessionsCollectionJobEntriesRefreshed" : 1,
		"lastSessionsCollectionJobEntriesEnded" : 90,
		"lastSessionsCollectionJobCursorsClosed" : 0,
		"transactionReaperJobCount" : 0,
		"lastTransactionReaperJobDurationMillis" : 0,
		"lastTransactionReaperJobTimestamp" : ISODate("2018-12-04T09:39:55.494Z"),
		"lastTransactionReaperJobEntriesCleanedUp" : 0
	},
	"network" : {
		"bytesIn" : NumberLong(470734593),
		"bytesOut" : NumberLong("2771880916"),
		"physicalBytesIn" : NumberLong(470734593),
		"physicalBytesOut" : NumberLong("2771880916"),
		"numRequests" : NumberLong(2826205),
		"compression" : {
			"snappy" : {
				"compressor" : {
					"bytesIn" : NumberLong(1124249067),
					"bytesOut" : NumberLong(973585235)
				},
				"decompressor" : {
					"bytesIn" : NumberLong(1332237452),
					"bytesOut" : NumberLong(1968198165)
				}
			}
		},
		"serviceExecutorTaskStats" : {
			"executor" : "passthrough",
			"threadsRunning" : 6
		}
	},
	"opcounters" : {
		"insert" : 10459,
		"query" : 71434,
		"update" : 132,
		"delete" : 261,
		"getmore" : 4,
		"command" : 2754551
	},
	"sharding" : {
		"configsvrConnectionString" : "bfd/127.0.0.1:28017,127.0.0.1:28018,127.0.0.1:28019",
		"lastSeenConfigServerOpTime" : {
			"ts" : Timestamp(1547451317, 1),
			"t" : NumberLong(3)
		},
		"maxChunkSizeInBytes" : NumberLong(67108864)
	},
	"shardingStatistics" : {
		"catalogCache" : {
			"numDatabaseEntries" : NumberLong(3),
			"numCollectionEntries" : NumberLong(3),
			"countStaleConfigErrors" : NumberLong(0),
			"totalRefreshWaitTimeMicros" : NumberLong(21910182),
			"numActiveIncrementalRefreshes" : NumberLong(0),
			"countIncrementalRefreshesStarted" : NumberLong(11783),
			"numActiveFullRefreshes" : NumberLong(0),
			"countFullRefreshesStarted" : NumberLong(3),
			"countFailedRefreshes" : NumberLong(0)
		}
	},
	"tcmalloc" : {
		"generic" : {
			"current_allocated_bytes" : 7116552,
			"heap_size" : 19681280
		},
		"tcmalloc" : {
			"pageheap_free_bytes" : 3538944,
			"pageheap_unmapped_bytes" : 430080,
			"max_total_thread_cache_bytes" : NumberLong(1073741824),
			"current_total_thread_cache_bytes" : 2162368,
			"total_free_bytes" : 8595704,
			"central_cache_free_bytes" : 1690840,
			"transfer_cache_free_bytes" : 4742496,
			"thread_cache_free_bytes" : 2162368,
			"aggressive_memory_decommit" : 0,
			"pageheap_committed_bytes" : 19251200,
			"pageheap_scavenge_count" : 2050,
			"pageheap_commit_count" : 4809,
			"pageheap_total_commit_bytes" : 944230400,
			"pageheap_decommit_count" : 2050,
			"pageheap_total_decommit_bytes" : 924979200,
			"pageheap_reserve_count" : 16,
			"pageheap_total_reserve_bytes" : 19681280,
			"spinlock_total_delay_ns" : 1462,
			"formattedString" : "------------------------------------------------\nMALLOC:        7117128 (    6.8 MiB) Bytes in use by application\nMALLOC: +      3538944 (    3.4 MiB) Bytes in page heap freelist\nMALLOC: +      1690840 (    1.6 MiB) Bytes in central cache freelist\nMALLOC: +      4742496 (    4.5 MiB) Bytes in transfer cache freelist\nMALLOC: +      2161792 (    2.1 MiB) Bytes in thread cache freelists\nMALLOC: +      1204480 (    1.1 MiB) Bytes in malloc metadata\nMALLOC:   ------------\nMALLOC: =     20455680 (   19.5 MiB) Actual memory used (physical + swap)\nMALLOC: +       430080 (    0.4 MiB) Bytes released to OS (aka unmapped)\nMALLOC:   ------------\nMALLOC: =     20885760 (   19.9 MiB) Virtual address space used\nMALLOC:\nMALLOC:           1266              Spans in use\nMALLOC:             26              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"
		}
	},
	"transportSecurity" : {
		"1.0" : NumberLong(0),
		"1.1" : NumberLong(0),
		"1.2" : NumberLong(0),
		"1.3" : NumberLong(0),
		"unknown" : NumberLong(0)
	},
	"mem" : {
		"bits" : 64,
		"resident" : 22,
		"virtual" : 272,
		"supported" : true
	},
	"metrics" : {
		"cursor" : {
			"timedOut" : NumberLong(0),
			"open" : {
				"multiTarget" : NumberLong(0),
				"singleTarget" : NumberLong(0),
				"pinned" : NumberLong(0),
				"total" : NumberLong(0)
			}
		},
		"commands" : {
			"<UNKNOWN>" : NumberLong(23),
			"aggregate" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(3445)
			},
			"authenticate" : {
				"failed" : NumberLong(2),
				"total" : NumberLong(2)
			},
			"buildInfo" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(315869)
			},
			"collStats" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(103)
			},
			"connectionStatus" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(12)
			},
			"copydb" : {
				"failed" : NumberLong(4),
				"total" : NumberLong(4)
			},
			"count" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(33)
			},
			"create" : {
				"failed" : NumberLong(1),
				"total" : NumberLong(9)
			},
			"createIndexes" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(12)
			},
			"createUser" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(3)
			},
			"currentOp" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(1)
			},
			"dbStats" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(53)
			},
			"delete" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(261)
			},
			"drop" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(2)
			},
			"dropDatabase" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(1)
			},
			"endSessions" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(158152)
			},
			"find" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(71434)
			},
			"findAndModify" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(73)
			},
			"getLastError" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(120)
			},
			"getLog" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(12)
			},
			"getMore" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(4)
			},
			"getnonce" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(2)
			},
			"hostInfo" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(10)
			},
			"insert" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(915)
			},
			"isMaster" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(1169116)
			},
			"listCollections" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(67)
			},
			"listDatabases" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(20)
			},
			"listIndexes" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(53)
			},
			"logout" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(116)
			},
			"ping" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(8)
			},
			"replSetGetStatus" : {
				"failed" : NumberLong(315878),
				"total" : NumberLong(315878)
			},
			"saslContinue" : {
				"failed" : NumberLong(6),
				"total" : NumberLong(316146)
			},
			"saslStart" : {
				"failed" : NumberLong(7),
				"total" : NumberLong(158083)
			},
			"serverStatus" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(157958)
			},
			"update" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(132)
			},
			"usersInfo" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(17)
			},
			"whatsmyuri" : {
				"failed" : NumberLong(0),
				"total" : NumberLong(157863)
			}
		}
	},
	"ok" : 1,
	"operationTime" : Timestamp(1547451317, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1547451317, 1),
		"signature" : {
			"hash" : BinData(0,"DFAR9j+CHFQiZazFoItzOenSw9g="),
			"keyId" : NumberLong("6631044199212908573")
		}
	}
}

 

2.2 db.stats指令

[root@bfd-ango-01 shell]# echo "db.stats()"|mongo 47.105.115.143:27017/app -udolphin -pk8DDg9sd --authenticationDatabase admin
MongoDB shell version v4.0.4
connecting to: mongodb://47.105.115.143:27017/app
Implicit session: session { "id" : UUID("f980ae05-e0ee-4bfe-9ed4-90ed36e0589a") }
MongoDB server version: 4.0.4
{
	"raw" : {
		"shard3/127.0.0.1:29015,127.0.0.1:29018" : {
			"db" : "app",
			"collections" : 0,
			"views" : 0,
			"objects" : 0,
			"avgObjSize" : 0,
			"dataSize" : 0,
			"storageSize" : 0,
			"numExtents" : 0,
			"indexes" : 0,
			"indexSize" : 0,
			"fileSize" : 0,
			"fsUsedSize" : 0,
			"fsTotalSize" : 0,
			"ok" : 1
		},
		"shard1/127.0.0.1:29013,127.0.0.1:29016" : {
			"db" : "app",
			"collections" : 0,
			"views" : 0,
			"objects" : 0,
			"avgObjSize" : 0,
			"dataSize" : 0,
			"storageSize" : 0,
			"numExtents" : 0,
			"indexes" : 0,
			"indexSize" : 0,
			"fileSize" : 0,
			"fsUsedSize" : 0,
			"fsTotalSize" : 0,
			"ok" : 1
		},
		"shard2/127.0.0.1:29014,127.0.0.1:29017" : {
			"db" : "app",
			"collections" : 5,
			"views" : 0,
			"objects" : 5052,
			"avgObjSize" : 87.85550277117973,
			"dataSize" : 443846,
			"storageSize" : 311296,
			"numExtents" : 0,
			"indexes" : 7,
			"indexSize" : 282624,
			"fsUsedSize" : 7236730880,
			"fsTotalSize" : 27510337536,
			"ok" : 1
		}
	},
	"objects" : 5052,
	"avgObjSize" : 87,
	"dataSize" : 443846,
	"storageSize" : 311296,
	"numExtents" : 0,
	"indexes" : 7,
	"indexSize" : 282624,
	"fileSize" : 0,
	"extentFreeList" : {
		"num" : 0,
		"totalSize" : 0
	},
	"ok" : 1,
	"operationTime" : Timestamp(1547451364, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1547451368, 2),
		"signature" : {
			"hash" : BinData(0,"L6gkXKxh9FgoJzf2PWP785YMkEc="),
			"keyId" : NumberLong("6631044199212908573")
		}
	}
}

2.3 配置zabbix的mongodb_status.conf

 

# 到zabbix_agentd.d目录下,创建mongodb_status.conf文件,内容如下图所示
cd /etc/zabbix/zabbix_agentd.d

这是zabbix自定义监控,根据上面的内容可以看出,是根据脚本执行返回的结果进行监控,所以我们还要写shell脚本,脚本内容如下:

 

#!/bin/bash
MONGODBPATH="/opt/mongodb/bin/mongo"
HOST="47.105.115.143"
PORT="27017"

echo "$1()"|$MONGODBPATH $HOST:$PORT/app -udolphin -pk8DDg9sd --authenticationDatabase admin |grep -w $2|awk -F ':' '{print $2}'|head -n 1| sed "s#,##"

 

3.zabbix监控

3.1 zabbix-get 工具检测

这时候一切都配置好了,我们重启zabbix_agent,例用之前安装的zabbix-get 工具进行检查一下,能够获取到值,则表示ok

 

[root@bfd-ango-01 shell]# zabbix_get -s 47.105.115.143 -k mongodb_stats[db.serverStatus,totalCreated]
158287

 

3.2zabbix创建模板,并在模板中创建item和graph

3.2.1 配置item

3.2.2 配置graph

3.2.3 打开graph

注意:

1. 这里其实根据数据量分成多个graph,因为有的值在0-100以内,有的值在几千到几万之间,所有的item都在一个图中,页面显示不友好,根据自己实际情况按照上图显示分成多个graph即可,这里只是演示作用。

2. 我们没有创建触发器,原因是这个是根据实际情况去配置,到一步后,后面也就简单了。

4.python脚本操作mongodb

import pymongo
client = pymongo.MongoClient( '127.0.0.1', 27017 )
bfd=client.bfd
myuser = bfd.myuser
for item in myuser.find().limit(5):
print(item)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值