由于MongoDB没有关系型数据中强大的数据字典。因此,如果需要汇总统计一些内部信息,包括数据量,基本元信息,集群架构,状态信息。则需要自己写脚本统计。为日后工作方便,本人在此将一些统计信息通过JavaScript汇总。。使用方式如下:
脚本如下:(还有很多信息待收集统计,先走出第一步)
// 查看mongodb实例信息。 var maxSizeDb = 5; // 看前多少数据库。默认为5 var maxSizeTable = 10; // 前五数据库中表排名前多少。 默认为10. function byteToGb(num){ return (num/1024/1024/1024).toFixed(2); }; function byteToMb(num){ return (num/1024).toFixed(0); }; function statsDbSize(){ var dbArray={}; // 获取数据库信息 dbInfo=db.adminCommand('listDatabases'); // 获取主机信息 hostinfo = db.hostInfo(); dbNameSizeArray = dbInfo.databases; dbDiskSize = db.getSiblingDB(dbNameSizeArray[0].name); dbDiskSizeTotal = dbDiskSize.stats().fsTotalSize; print("=======服务器信息================="); print("CPU核数:", hostinfo.system.numCores); print("内存Size:(MB)", byteToMb(hostinfo.system.memSizeMB)); print("磁盘空间:",byteToGb(dbDiskSizeTotal),"GB"); print("系统版本:",hostinfo.os.name); print("内核版本:",hostinfo.os.version); print("最大文件打开数:",hostinfo.extra.maxOpenFiles); print("是否启用numa:",hostinfo.system.numaEnabled); print("================================== ") print("=======实例概览=======") print("磁盘空间:",byteToGb(dbDiskSizeTotal),"GB",' ||'); print("数据总大小:",byteToGb(dbInfo.totalSize),"GB",' ||'); print("数据库个数:",dbNameSizeArray.length,' ||'); print("=========================== ") // 获取副本集信息 // rsinfo = db.adminCommand({replSetGetStatus:1}); // printjson(rsinfo); rsname = rsinfo.set; print("=======副本集信息======="); print("副本集:",rsname); rsOptimesLastCommittedOpTimeTs = rsinfo.optimes.lastCommittedOpTime.ts rsOptimesReadConcernMajorityOpTimeTs = rsinfo.optimes.readConcernMajorityOpTime.ts rsOptimesAppliedOpTimeTs = rsinfo.optimes.appliedOpTime.ts rsOptimesDurableOpTimeTs = rsinfo.optimes.durableOpTime.ts rslastStableCheckpointTimestamp = rsinfo.lastStableCheckpointTimestamp; // print("最後提交時間:", (Date(rsOptimesLastCommittedOpTimeTs.toLocaleString())).toISOString()) print("最後提交時間:", Date(rsOptimesLastCommittedOpTimeTs.toLocaleString())) print("一致性读時間:", Date(rsOptimesReadConcernMajorityOpTimeTs.toLocaleString())) print("日志应用時間:", Date(rsOptimesAppliedOpTimeTs.toLocaleString())) print("日志持久時間:", Date(rsOptimesDurableOpTimeTs.toLocaleString())) print(" 检查点時間:", Date(rslastStableCheckpointTimestamp.toLocaleString())) print("==========成员=====================") // 打印MongoDB副本集成员信息 rsinfoMember = rsinfo.members //print("===========================================================================================") print('ip:port health state optime syncSourceHost 成员角色'); for(var mem in rsinfoMember){ rsinfoMemberInfo = rsinfoMember[mem]; print('---------------------------------------------------------------------------------------------------------') if(rsinfoMemberInfo.syncSourceHost == ""){ delimiterChar = ' '; } else{ delimiterChar = ' '; } if(rsinfoMemberInfo.optime == null){ print(rsinfoMemberInfo.name,' ' ,rsinfoMemberInfo.health,' ', rsinfoMemberInfo.state,' ', "None", delimiterChar,rsinfoMemberInfo.syncSourceHost, ' ', rsinfoMemberInfo.stateStr);} else{ print(rsinfoMemberInfo.name,' ' ,rsinfoMemberInfo.health,' ', rsinfoMemberInfo.state,' ', rsinfoMemberInfo.optime.ts.getTime(), delimiterChar,rsinfoMemberInfo.syncSourceHost, ' ', rsinfoMemberInfo.stateStr);} } // 获取副本集配置信息 rsconf = db.adminCommand( { replSetGetConfig: 1 } ); rsconfConfig = rsconf.config; // 成员設置 rsconfConfigMember = rsconfConfig.members; // 全局設置 rsconfConfigSettings = rsconfConfig.settings; rsconfprotocolVersion = rsconfConfig.protocolVersion; rsconfwriteConcernMajorityJournalDefault = rsconfConfig.writeConcernMajorityJournalDefault; rsconfConfigSettingschainingAllowed = rsconfConfigSettings.chainingAllowed; rsconfConfigSettingsheartbeatIntervalMillis = rsconfConfigSettings.heartbeatIntervalMillis; rsconfConfigSettingsheartbeatTimeoutSecs = rsconfConfigSettings.heartbeatTimeoutSecs; rsconfConfigSettingselectionTimeoutMillis = rsconfConfigSettings.electionTimeoutMillis; print(" 协议版本( 影响):", rsconfprotocolVersion); print(" 默认写策略:", rsconfwriteConcernMajorityJournalDefault); print("是否允许链式复制:", rsconfConfigSettingschainingAllowed); print("心跳检测间隔时间:", rsconfConfigSettingsheartbeatIntervalMillis); print("心跳检测超时时间:", rsconfConfigSettingsheartbeatTimeoutSecs); print(" 选举超时时间:", rsconfConfigSettingselectionTimeoutMillis); print("ip:port, arbiterOnly, buildIndexes, 影藏节点, 权重, 标签, 延时同步, 投票节点"); for(var i in rsconfConfigMember){ print('------------------------------------------------------------------------------------------------------') var rsmember = rsconfConfigMember[i]; if(rsmember.tags = {}){ rsmember.tags = 'None'; } print(rsmember.host, ' ',rsmember.arbiterOnly,' ', rsmember.buildIndexes,' ', rsmember.hidden,' ', rsmember.priority, ' ',rsmember.tags,' ', rsmember.slaveDelay,' ', rsmember.votes) } // printjson(rsconfConfigMember); // printjson(rsconfConfigSettings); // print("=========================== ") // for(var i=0; i<dbNameSizeArray.length; i++){ dbname = dbNameSizeArray[i].name; dbvalue = byteToGb(dbNameSizeArray[i].sizeOnDisk); dbArray[dbname] = dbvalue; } // 将时间戳转换成时间 // //printjson(dbArray); // order by dbsize ,Get the top five(maxSizeDb) database names in the occupied space var items = Object.keys(dbArray).map(function(key){ return [key, dbArray[key]];}); var sdbArray = items.sort(function(first, second){return second[1] - first[1]}); // maxSizeDbName = sdbArray.slice(0, maxSizeDb); print("=========Overview Database=========") print("库名"," 大小:GB ||") for(var i in sdbArray){ //print(sdbArray[i][0],' ',sdbArray[i][1],' '); printjson(sdbArray[i],"||") } print("=========================== ") // 可以在这个里面加上前十大的集合 for(var j in sdbArray.slice(0, maxSizeDb)){ db1 = db.getSiblingDB(sdbArray[j][0]); // printjson(db1.stats()) db2 = db.getSiblingDB(db1); tableName = db2.getCollectionNames(); // print("集合 "," ,大小 行数") for(var i in tableName){ tn = tableName[i]; // print(db1+'.'+tn); } } } statsDbSize()