Skip to content

Instantly share code, notes, and snippets.

@ejber-ozkan
Last active February 18, 2019 11:22
Show Gist options
  • Save ejber-ozkan/15d4504033b4ae0d8633e34a3ed66bc9 to your computer and use it in GitHub Desktop.
Save ejber-ozkan/15d4504033b4ae0d8633e34a3ed66bc9 to your computer and use it in GitHub Desktop.
A few bash functions for MongoDB Atlas API querying.
# A bunch of simple functions to expand and use to query the MongoDB Atlas API.
export TOKEN="[create your mongodb atlas api token and enter it here]"
export GROUPID="[enter groupid or project id]"
export API="https://cloud.mongodb.com/api/atlas/v1.0"
export USERNAME="[enter user]"
# groupid = projectid
# https://cloud.mongodb.com/v2/$GROUPID#clusters
# ideally python and jq is required.
# get cluster details for this project (groupid) , with jq and python example to extract further...
function get_clusters {
curl -X GET -u "$USERNAME:$TOKEN" --digest "$API/groups/$GROUPID/clusters" | python -m json.tool | jq -r '[.results[] | { mongoURI:.mongoURI , ClusterName:.name }]'
}
# get all events
function get_all_events {
curl -X GET -u "$USERNAME:$TOKEN" --digest "$API/groups/$GROUPID/events" | python -m json.tool
}
# get all alerts
function get_all_alerts {
curl -X GET -u "$USERNAME:$TOKEN" --digest "$API/groups/$GROUPID/alerts" | python -m json.tool
}
# get all alert configs
function get_all_alert_configs {
curl -X GET -u "$USERNAME:$TOKEN" --digest "$API/groups/$GROUPID/alertConfigs" | python -m json.tool
}
# get audit options , not clear on usefullness yet
function get_audit_options {
curl -u "$USERNAME:$TOKEN" --digest \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--request GET "$API/groups/$GROUPID/auditLog?pretty=true"
}
# get logs on cluster (full hostname required )
export HOSTNAME="[get cluster hostname list from mongoDB API]"
# get logs on cluster (full hostname required ) # poll and digest every 5 minutes
function get_cluster_logs {
curl -u "$USERNAME:$TOKEN" --digest \
--header 'Accept: application/gzip' \
--request GET "$API/groups/$GROUPID/clusters/$HOSTNAME/logs/mongodb.gz" \
--output "mongodb.gz"
}
# NETWORK_PERMISSION_ENTRY_ADDED
# NETWORK_PERMISSION_ENTRY_UPDATED
# NETWORK_PERMISSION_ENTRY_REMOVED
export EVENT_TYPE_NAME="NETWORK_PERMISSION_ENTRY_ADDED"
# Setup up alert config
function enable_alert {
curl -X POST -u "$USERNAME:$TOKEN" --digest "$API/groups/$GROUPID/alertConfigs" \
-H "Content-Type: application/json" --data '
{
"eventTypeName" : "'$EVENT_TYPE_NAME'",
"enabled" : true,
"notifications" : [ {
"typeName" : "GROUP",
"intervalMin" : 5,
"delayMin" : 0,
"smsEnabled" : false,
"emailEnabled" : true
} ]
}'
}
# example :
enable_alert $EVENT_TYPE_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment