[MongoDB]: To Check fragmentation using a JavaScript

 MongoDB Fragmentation Alerting script  :-

1) Script will consider below aspects and prompt us “POSSIBLE FRAGMENTATION” & “REVIEW SCHEMA” issues.

dataSize
indexSize
storageSize
fileSize

2) It has 6GB minimum threshold dataSize for the database as thumb rule to apply the script.

[Dev root @ dbversitydotcom /opt/mongodb/bin]# cat getMongoFragmentation.js

print(“– THIS SCRIPT ‘getMongoFragmentation.js’ IS NOT FOR PRODUCTION USE, IT IS FOR DEMONSTRATION PURPOSES ONLY –“);

currentNodeDetails = db.getSisterDB(“local”).startup_log.findOne();
currentHostname = currentNodeDetails[‘hostname’];
currentCommandLine = currentNodeDetails[‘cmdLine’];
currentHostport = currentCommandLine[‘port’];

if (currentHostport == undefined)
{
currentHostport = 27017;
}
hostDetails = currentHostname + “:” + currentHostport;
isMaster = db.isMaster();
if (isMaster.ismaster) {
print(“\n** Connected to primary: ” + hostDetails);
}
else if (isMaster.secondary) {
print(“\n** Connected to secondary: ” + hostDetails);
rs.slaveOk();
}
else {
print(“\n** Connected : ” + hostDetails);
}

replicaSetConfig = null;
try {
replicaSetConfig = db.getSisterDB(“local”).system.replset.findOne();
} catch(err) {
if (err == null) {
print(“\n** Standalone node:”);
}
else
{
print(err);
}
}

if ( replicaSetConfig != null )
{
print(“\n** Replica set config:”);
printjson(replicaSetConfig);
}

print (“\n** Checking for Fragmentation Started **”);
dbs = db.adminCommand(‘listDatabases’);
dbs[‘databases’].forEach(function(mydb) {
indb = db.getSiblingDB(mydb.name);

if (indb == ‘local’ || indb == ‘config’ || indb == ‘admin’) {}
else {
currentstats = (indb.stats(1024*1024));

SixGBsInMBs = 6144;
if (currentstats[‘dataSize’]<=SixGBsInMBs)
{
newstats = (indb.stats(1024*1024*1024));
print(“\nUNABLE to apply rules of thumb to the database ‘” + indb + “‘ (dataSize ~” + newstats[‘dataSize’] + “GB/~” + currentstats[‘dataSize’] + “MB) as it is smaller than 6GB.”);
}
else
{
print(“\n** Database: ” + indb + ” – Statistics listed in MB”);
print(“\ndataSize: ” + currentstats[‘dataSize’]);
print(“indexSize: ” + currentstats[‘indexSize’]);
print(“storageSize: ” + currentstats[‘storageSize’]);
print(“fileSize: ” + currentstats[‘fileSize’]);
lowerIndexSizeToDataSize = currentstats[‘dataSize’] / 4;
upperIndexSizeToData = currentstats[‘dataSize’];
if ( currentstats[‘indexSize’] >= lowerIndexSizeToDataSize && currentstats[‘indexSize’] < upperIndexSizeToData )
{
print(“\n–WARNING– Database: ” + indb + ” — Index size is between 25% to 100% of data size, REVIEW SCHEMA.”);
}
else if ( currentstats[‘indexSize’] >= upperIndexSizeToData )
{
print(“\n–WARNING– Database: ” + indb + ” — Index size is equal or larger than data size, REVIEW SCHEMA.”);
}
else
{
print(“\nPropotion of index to data size looks adequate for Database: ” + indb);
}
twiceDataSize = currentstats[‘dataSize’]*2;
if ( currentstats[‘storageSize’] >= twiceDataSize )
{
print(“\n–WARNING– Database: ” + indb + ” — Storage size is twice or greater than data size, POSSIBLE FRAGMENTATION.”);
}
storagePlusIndexSize = currentstats[‘storageSize’] + currentstats[‘indexSize’];
sevenFivePercentOfFileSize = (currentstats[‘fileSize’] / 4) * 3;
SevenGBsinMBs = 7168;
if ( storagePlusIndexSize <= sevenFivePercentOfFileSize )
{
print(“\n–WARNING– Database: ” + indb + ” — Storage size plus Index Size is less than 75% of File Size, POSSIBLE FRAGMENTATION.”);
}
else if ( storagePlusIndexSize >= (currentstats[‘fileSize’] + SevenGBsinMBs) )
{
print(“\n–WARNING– Database: ” + indb + ” — Storage size plus Index Size is greater than File Size plus 7GB, POSSIBLE FRAGMENTATION.”);
}
else
{
print(“\nPropotion of storage and index sizes to file size looks adequate for Database: ” + indb);
}
}
}
});
print (“\n** Checking for Fragmentation Completed **”);

[Dev root @ dbversitydotcom /opt/mongodb/bin]#
[Dev root @ dbversitydotcom /opt/mongodb/bin]#
[Dev root @ dbversitydotcom /opt/mongodb/bin]#
[Dev root @ dbversitydotcom /opt/mongodb/bin]# ./mongo getMongoFragmentation.js >> getMongoFragmentation.log
[Dev root @ dbversitydotcom /opt/mongodb/bin]#
[Dev root @ dbversitydotcom /opt/mongodb/bin]# cat getMongoFragmentation.log

MongoDB shell version: 2.6.5
connecting to: test

— THIS SCRIPT ‘getMongoFragmentation.js’ IS NOT FOR PRODUCTION USE, IT IS FOR DEMONSTRATION PURPOSES ONLY —

** Connected to primary: dbversitydotcom:27017

** Checking for Fragmentation Started **

UNABLE to apply rules of thumb to the database ‘dbversity’ (dataSize ~0GB/~0MB) as it is smaller than 6GB.

** Checking for Fragmentation Completed **

[Dev root @ dbversitydotcom /opt/mongodb/bin]

Various workouts & results :-

[root@dbversity ~]$ /tmp/mongodb/bin/mongo –port 27010 MongoDB_Fragmentation_Estimator.sh
MongoDB shell version: 2.4.5
connecting to: 127.0.0.1:27010/test
— THIS SCRIPT ‘getMongoFragmentation.js’ IS NOT FOR PRODUCTION USE, IT IS FOR DEMONSTRATION PURPOSES ONLY —

** Connected to primary: vm-5cc5-bad6:27010

** Replica set config:
{
“_id” : “rs2”,
“version” : 3,
“members” : [
{
“_id” : 0,
“host” : “dbversity:27010”
},
{
“_id” : 1,
“host” : “dbversity:27011”
},
{
“_id” : 2,
“host” : “dbversity:27012”
}
]
}

** Checking for Fragmentation Started **

** Database: test – Statistics listed in MB

dataSize: 2595
indexSize: 1245
storageSize: 3416
fileSize: 8125

–WARNING– Database: test — Index size is between 25% to 100% of data size, REVIEW SCHEMA.

–WARNING– Database: test — Storage size plus Index Size is less than 75% of File Size, POSSIBLE FRAGMENTATION.

** Checking for Fragmentation Completed **
[root@dbversity ~]$
————————————————————————————-
[root@dbversity ~]$ /tmp/mongodb/bin/mongo –port 27011 MongoDB_Fragmentation_Estimator.sh
MongoDB shell version: 2.4.5
connecting to: 127.0.0.1:27011/test
— THIS SCRIPT ‘getMongoFragmentation.js’ IS NOT FOR PRODUCTION USE, IT IS FOR DEMONSTRATION PURPOSES ONLY —

** Connected to secondary: dbversity:27011

** Replica set config:
{
“_id” : “rs2”,
“version” : 3,
“members” : [
{
“_id” : 0,
“host” : “dbversity:27010”
},
{
“_id” : 1,
“host” : “dbversity:27011”
},
{
“_id” : 2,
“host” : “dbversity:27012”
}
]
}

** Checking for Fragmentation Started **

** Checking for Fragmentation Completed **
[root@dbversity ~]$
———————————————————————————————————————————————–
[root@dbversity ~]$ /tmp/mongodb/bin/mongo –port 27010 MongoDB_Fragmentation_Estimator.sh
MongoDB shell version: 2.4.5
connecting to: 127.0.0.1:27010/test
— THIS SCRIPT ‘getMongoFragmentation.js’ IS NOT FOR PRODUCTION USE, IT IS FOR DEMONSTRATION PURPOSES ONLY —

** Connected to primary: dbversity:27010

** Replica set config:
{
“_id” : “rs2”,
“version” : 3,
“members” : [
{
“_id” : 0,
“host” : “dbversity:27010”
},
{
“_id” : 1,
“host” : “dbversity:27011”
},
{
“_id” : 2,
“host” : “dbversity:27012”
}
]
}

** Checking for Fragmentation Started **

UNABLE to apply rules of thumb to the database ‘test’ (dataSize ~0GB/~0MB) as it is smaller than 6GB.

UNABLE to apply rules of thumb to the database ‘splitter’ (dataSize ~0GB/~0MB) as it is smaller than 6GB.

** Checking for Fragmentation Completed **
[root@dbversity ~]$

—————————————————————————————

[root@dbversity ~]$
[root@dbversity ~]$ /tmp/mongodb/bin/mongo –port 27011 MongoDB_Fragmentation_Estimator.sh
MongoDB shell version: 2.4.5
connecting to: 127.0.0.1:27011/test
— THIS SCRIPT ‘getMongoFragmentation.js’ IS NOT FOR PRODUCTION USE, IT IS FOR DEMONSTRATION PURPOSES ONLY —

** Connected to secondary: dbversity:27011

** Replica set config:
{
“_id” : “rs2”,
“version” : 3,
“members” : [
{
“_id” : 0,
“host” : “dbversity:27010”
},
{
“_id” : 1,
“host” : “dbversity:27011”
},
{
“_id” : 2,
“host” : “dbversity:27012”
}
]
}

** Checking for Fragmentation Started **

** Database: test – Statistics listed in MB

dataSize: 151
indexSize: 68
storageSize: 219
fileSize: 960

–WARNING– Database: test — Index size is between 25% to 100% of data size, REVIEW SCHEMA.

–WARNING– Database: test — Storage size plus Index Size is less than 75% of File Size, POSSIBLE FRAGMENTATION.

** Checking for Fragmentation Completed **
[root@dbversity ~]$

 

 

 

  • Ask Question