In this blog, we will see how to configure Percona Monitoring and Management (PMM) monitoring for a MongoDB cluster. It’s very simple, like adding a replica set or standalone instances to PMM Monitoring.
For this example, I have used docker to create PMM Server and MongoDB sharded cluster containers. If you want the steps I used to create the MongoDB® cluster environment using docker, I have shared them at the end of this blog. You can refer to this if you would like to create the same set up.
Configuring PMM Clients
For PMM installations, you can check these links for PMM installation and pmm-client setup. The following are the members of the MongoDB cluster:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
mongos: mongos1 config db: (replSet name - mongors1conf) mongocfg1 mongocfg2 mongocfg3 Shard1: (replSet name - mongors1) mongors1n1 mongors1n2 mongors1n3 Shard1: (replSet name - mongors2) mongors2n1 mongors2n2 mongors2n3 |
In this setup, I installed the pmm-client on mongos1 server. Then I added an agent to monitor MongoDB metrics with cluster option as shown in the next code block. I named the cluster “mongoClusterPMM” (Note: you have to be root user or need sudo access to execute the pmm-admin command):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
root@90262f1360a0:/# pmm-admin add mongodb --cluster mongoClusterPMM [linux:metrics] OK, now monitoring this system. [mongodb:metrics] OK, now monitoring MongoDB metrics using URI localhost:27017 [mongodb:queries] OK, now monitoring MongoDB queries using URI localhost:27017 [mongodb:queries] It is required for correct operation that profiling of monitored MongoDB databases be enabled. [mongodb:queries] Note that profiling is not enabled by default because it may reduce the performance of your MongoDB server. [mongodb:queries] For more information read PMM documentation (https://www.percona.com/doc/percona-monitoring-and-management/conf-mongodb.html). root@90262f1360a0:/# pmm-admin list pmm-admin 1.11.0 PMM Server | 172.17.0.2 (password-protected) Client Name | 90262f1360a0 Client Address | 172.17.0.4 Service Manager | unix-systemv ---------------- ------------- ----------- -------- ---------------- ------------------------ SERVICE TYPE NAME LOCAL PORT RUNNING DATA SOURCE OPTIONS ---------------- ------------- ----------- -------- ---------------- ------------------------ mongodb:queries 90262f1360a0 - YES localhost:27017 query_examples=true linux:metrics 90262f1360a0 42000 YES - mongodb:metrics 90262f1360a0 42003 YES localhost:27017 cluster=mongoClusterPMM root@90262f1360a0:/# |
As you can see, I used the pmm-admin add mongodb [options] command which enables monitoring for system, MongoDB metrics and queries. You need to enable profiler to monitor MongoDB queries. Use the next command to enable it at database level:
1 2 |
use db_name db.setProfilingLevel(1) |
Check this blog to know more about QAN setup and details. If you want to enable only MongoDB metrics, rather than queries to be monitored, then you can use the command pmm-admin add mongodb:metrics [options] . After this, go to the PMM homepage (in my case localhost:8080) in your browser and select MongoDB Cluster Summary from the drop down list under the MongoDB option. The below screenshot shows the MongoDB Cluster—“mongoClusterPMM” statistics— collected by the agent that we added in mongos1 server.
Did we miss something here? And do you see any metrics in the dashboard above except “Balancer Enabled” and “Chunks Balanced”?
No. This is because, PMM doesn’t have enough data to show in the dashboard. The shards are not added to the cluster yet and as you can see it displays 0 under shards. Let’s add two shard replica sets mongors1 and mongors2 in the mongos1 instance, and enable sharding to the database to complete the cluster setup as follows:
1 2 3 4 |
mongos> sh.addShard("mongors1/mongors1n1:27017,mongors1n2:27017,mongors1n3:27017") { "shardAdded" : "mongors1", "ok" : 1 } mongos> sh.addShard("mongors2/mongors2n1:27017,mongors2n2:27017,mongors2n3:27017") { "shardAdded" : "mongors2", "ok" : 1 } |
Now, I’ll add some data, collection and shard keys, and enable sharding so that we can see some statistics in the dashboard:
1 2 3 4 5 6 7 |
use vinodh db.setProfilingLevel(1) db.testColl.insertMany([{id1:1,name:"test insert”},{id1:2,name:"test insert"},{id1:3,name:"insert"},{id1:4,name:"insert"}]) db.testColl.ensureIndex({id1:1}) sh.enableSharding("vinodh") sh.shardCollection("vinodh.testColl", {id1:1}) |
At last! Now you can see statistics in the graph for the MongoDB cluster:
Full Cluster Monitoring
We are not done yet. We have just added an agent to monitor mongos1 instance, and so only the cluster related statistics and QAN are collected through this node. For monitoring all nodes in the MongoDB cluster, we need to configure all nodes in PMM under “mongoClusterPMM” cluster. This will tell PMM that the configured nodes are part of the same cluster. We could also monitor the replica set related metrics for the members in config DBs and shards. Let’s add the monitoring agents in mongos1 server to monitor all MongoDB instances remotely. We’ll use these commands:
1 2 3 4 5 6 7 8 9 |
pmm-admin add mongodb:metrics --uri "mongodb://mongocfg1:27017/admin?replicaSet=mongors1conf" mongocfg1replSet --cluster mongoClusterPMM pmm-admin add mongodb:metrics --uri "mongodb://mongocfg2:27017/admin?replicaSet=mongors1conf" mongocfg2replSet --cluster mongoClusterPMM pmm-admin add mongodb:metrics --uri "mongodb://mongocfg3:27017/admin?replicaSet=mongors1conf" mongocfg3replSet --cluster mongoClusterPMM pmm-admin add mongodb:metrics --uri "mongodb://mongors1n1:27017/admin?replicaSet=mongors1" mongors1replSetn1 --cluster mongoClusterPMM pmm-admin add mongodb:metrics --uri "mongodb://mongors1n2:27017/admin?replicaSet=mongors1" mongors1replSetn2 --cluster mongoClusterPMM pmm-admin add mongodb:metrics --uri "mongodb://mongors1n3:27017/admin?replicaSet=mongors1" mongors1replSetn3 --cluster mongoClusterPMM pmm-admin add mongodb:metrics --uri "mongodb://mongors2n1:27017/admin?replicaSet=mongors2" mongors2replSetn1 --cluster mongoClusterPMM pmm-admin add mongodb:metrics --uri "mongodb://mongors2n2:27017/admin?replicaSet=mongors2" mongors2replSetn2 --cluster mongoClusterPMM pmm-admin add mongodb:metrics --uri "mongodb://mongors2n3:27017/admin?replicaSet=mongors2" mongors2replSetn3 --cluster mongoClusterPMM |
Once you have added them, you can check the agent’s (mongodb-exporter) status as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
root@90262f1360a0:/# pmm-admin list pmm-admin 1.11.0 PMM Server | 172.17.0.2 (password-protected) Client Name | 90262f1360a0 Client Address | 172.17.0.4 Service Manager | unix-systemv ---------------- ------------------ ----------- -------- ----------------------- -- |