Last active
October 30, 2019 09:53
-
-
Save akshendra/5d6d5960316a598c73ad270ebdc0c012 to your computer and use it in GitHub Desktop.
Mongo info on cloudwatch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import pprint | |
import boto3 | |
from pymongo import MongoClient | |
pp = pprint.PrettyPrinter(indent=2) | |
dbName = os.environ['MONGO_DB_NAME'] | |
dbReplicaSet = os.environ['MONGO_REPLICA_SET'] | |
machineName = os.environ['MACHINE_NAME'] | |
cloudwatch = boto3.client('cloudwatch') | |
client = MongoClient('mongodb://localhost:27017') | |
db = client[dbName] | |
metrics = {} | |
metrics['all'] = {} | |
server_metrics = {} | |
def toGB(val): | |
return float("{0:.2f}".format(int(val) / (1024 * 1024 * 1024))) | |
def make_matrix(name, colName, value, unit): | |
return { | |
'MetricName': name, | |
'Dimensions': [ | |
{ | |
'Name': 'Collection', | |
'Value': colName | |
}, | |
], | |
'Value': value, | |
'Unit': unit, | |
} | |
def make_server_matrix(name, value, unit): | |
return { | |
'MetricName': name, | |
'Dimensions': [ | |
{ | |
'Name': 'Machine', | |
'Value': machineName | |
}, | |
], | |
'Value': value, | |
'Unit': unit, | |
} | |
def send_multi_metrics(metrics, col, unit='Count', | |
namespace='EC2/Mongo'): | |
data = [] | |
for key, value in metrics.items(): | |
data.append(make_matrix(key, col, value, unit)) | |
cloudwatch.put_metric_data( | |
Namespace=namespace, | |
MetricData=data | |
) | |
def send_server_metrics(metrics, unit='Count', | |
namespace='EC2/Mongo'): | |
data = [] | |
for key, value in metrics.items(): | |
data.append(make_server_matrix(key, value, unit)) | |
cloudwatch.put_metric_data( | |
Namespace=namespace, | |
MetricData=data | |
) | |
dbStats = db.command('dbstats') | |
serverStats = db.command('serverStatus') | |
metrics['all']['collections'] = dbStats['collections'] | |
metrics['all']['objects'] = dbStats['objects'] | |
metrics['all']['dataSize'] = toGB(dbStats['dataSize']) | |
metrics['all']['storageSize'] = toGB(dbStats['storageSize']) | |
metrics['all']['indexSize'] = toGB(dbStats['indexSize']) | |
print(metrics) | |
server_metrics['connections'] = serverStats['connections']['current'] | |
collections = db.collection_names() | |
for colName in collections: | |
stats = db.command('collStats', colName) | |
metrics[colName] = {} | |
metrics[colName]['size'] = toGB(stats['size']) | |
metrics[colName]['storageSize'] = toGB(stats['storageSize']) | |
metrics[colName]['totalIndexSize'] = toGB(stats['totalIndexSize']) | |
metrics[colName]['totalIndexes'] = stats['nindexes'] | |
metrics[colName]['count'] = stats['count'] | |
length = len(metrics) | |
for col in metrics: | |
send_multi_metrics(metrics[col], col) | |
send_server_metrics(server_metrics) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment