A script to iterate through all databases in a CosmosDB MongoDB API's cluster.
This script will pull storage usage, index usage, total requests for the last 30 days and collections information including the number of indexes and documents from an Azure account. It will also collect Request Units and MongoDB Charge requests as saved in Azure.
You must have a valide Azure account in order to access the data from Azure CosmosDB.
Once you have an account, follow these steps on a computer running Mac OS (this was successfully tested on v11.6):
- Install Powershell via Homebrew or the Visual Studio Code extensions
brew install --cask powershell
- If using Visual Studio Code, you should also install the following extension:
- Azure Account -
Required
- Azure Database -
Required
- Azure Resource Manager (ARM) Tools -
Optional
- Bicep -
Optional
- Connect to Powershell in Visual Studio Code's terminal or your preferred terminal application
pwsh
- Once in Powershell, install Azure, CosmosDB and MongoDB dependencies
Install-Module -Name Az
Install-Module -Name Az.CosmosDB
Install-Module -Name Mdbc [Mdbc Github page](https://github.com/nightroman/Mdbc)
- Update the settings.json file with the Atlas connection string to your cluster and the database that will capture the metrics
- Connect to your Azure account (this will redirect you to the browser to authenticate)
Connect-AzAccount
- Run the script To display the results as plain text grouped per database in the terminal window
./cosmos-sizing-script.ps1
To push the combined results of all databases as a document to a MongoDB Atlas cluster over API
./cosmos-sizing-script-json.ps1
Notes
These commands assume that you are in the same folder as the script file
Due to the way Azure services behave, in some instances the script will return 0 for some metrics. This is due to the fact that the DB is in a sleep/cold state. It is therefore recommended to run the script twice to be sure to return metrics for all items