Monitoring Tooling / Command-Line Monitoring with mongotop and mongostat
Sometimes, you may need to shift your focus from monitoring performance at an application level or the database level to the MongoDB instance level.
In this video, we'll monitor our MongoDB database using command line tools like mongo stat and mongo top. We'll also discuss other command line tools like the MongoDB CLI and Atlas CLI tools. Let's dive in.
MongoStat and MongoTop work directly from the command line and provide insights into all databases and collections.
They offer a high level view of your database's health and can be added into scripts and automation workflows.
These command line tools help during deployment processes and for quick spot checks when investigating specific issues.
Mongostat provides a real time snapshot of your MongoDB server showing the number of operations per second, like inserts, updates, and queries.
Think of it as a live dashboard of your database's activity.
Check out the MongoDB documentation for details on how to install this for your operating system.
Once installed, we can execute Mongostat by supplying our connection string as the argument for Mongostat as seen here.
MongoStab pulls the nodes in our cluster every second and displays the information to our terminal.
As you can see, MongoStab returns a pretty extensive output. Our documentation has a full breakdown of what each of these metrics represents. For our purposes, let's look for sudden increases in any of these metrics.
For example, a significant jump in insert operations might correlate with a batch import of new products into your inventory system.
If we notice a corresponding increase in the query or write queues, it could mean the database is struggling to keep up with those operations.
Mongo stat is great for observing trends over time and identifying a baseline performance of our MongoDB instance.
It works complimentary with our Atlas tools and ops manager.
So when we see a spike, we can use those tools to analyze Atlas metrics and find queries that are causing these spikes. While MongoStad gives you an overview of server activity, Mongo top provides insights into read and write operations per collection.
Mongo top helps us identify which collections are the most active in terms of reads and writes. We can execute Mongo top by providing the command name and connection string as we see here.
The output for mongo top is lighter than mongo stat. The metrics are broken down by collection followed by the time spent in operations for that collection.
The time values are broken down into total operational time, time spent on reads, and time spent on writes so we can easily see what our most active collections are.
Collections with consistently high read or write times when compared with our baseline are candidates for further investigation.
For example, after running our performance test, we see this collection frequently shows high read times when compared to our baseline average of single digit times.
This would indicate that queries on this collection are inefficient or that it's accessed very heavily.
Similarly, high write times on this collection suggest an issue with our indexes on that collection.
By using Mago Top to identify the most active collections in terms of reads and writes, we get a clear picture of how well each collection is performing.
High read or write times on a particular collection could indicate the need to refine the indexes used on that collection, change how we structure our queries, or even the need for a full schema review.
So we've seen how Magostat and Magotop provide us with real time visibility into our database.
Now let's explore the MongoDB Atlas CLI and MongoDB CLI.
The MongoDB Atlas CLI and MongoDB CLI allow you to manage your deployments from the command line. These tools are particularly useful for setting up alerts, managing configurations, such as your cluster tier, and performing administrative tasks on databases, like creating or deleting clusters. These CLI tools offer much of the same functionality you'll find in UI based tools, such as the Atlas user interface, cloud manager, and ops manager, as you can see in this table. While these command line tools are useful, it's important to understand their limitations compared to the Atlas tools we explored earlier.
Command line tools like MongoStat and MongoTop provide real time snapshots but don't store historical data for trend analysis.
They also lack the visual representations and query specific insights available in Atlas. When troubleshooting specific performance issues, we often start with Atlas to identify problematic time periods in collections, then complement that with MongoStat and MongoTop for real time monitoring during our investigation and remediation efforts.
Great job. In this video, we shifted our focus from application specific query analysis to a broader database level perspective by exploring command line monitoring tools. We began by understanding how MongoStop provides a real time snapshot of MongoDB server operations.
This helps us observe trends in the number of operations per second and assess overall performance of a MongoDB instance. Then we looked at how Mongo Top offers collection specific insights into read and write operation times.
Finally, we took a quick look at the MongoDB Atlas CLI and Mongo CLI and compared their functionality to other tools in the MongoDB ecosystem.
