To start monitoring MongoDB with Foglight, perform the following installation instructions in the given order:
The agent requires a user with specific privileges to execute system queries. Additional steps are required to allow a TLS/SSL connection with the agent if the database server is configured to support or require this. In order to collect profiled operation data, profiling must be enabled on each MongoDB instance on a per-database basis. To collect slow operation entries from the database log, an SSH user must be created on the database host (available for Linux database hosts only).
The MongoDB agent requires database user credentials with certain minimum privileges in order to be able to fully monitor the server and cluster. All user authorizations must be for the admin database. The roles needed for the user will vary depending on the MongoDB version. Note that when monitoring a sharded cluster, a database user will need to be created separately on each component replica set, i.e. on the config server replica set as well as on each shard. The code examples for creating a database user assume the default authentication method. For non-default authentication methods (such as x.509 and LDAP, for which the user is created on the $external database) these examples may need to be modified.
For MongoDB 3.0+ the following roles on the admin database are required: clusterMonitor, readAnyDatabase.
User creation script sample:
db.getSiblingDB('admin').createUser({
user: '<xxx>',
pwd: '<yyy>',
roles: ['clusterMonitor', 'readAnyDatabase']
})
For versions prior to MongoDB 3.0 the following roles are required: clusterAdmin, dbAdminAnyDatabase, readAnyDatabase.
User creation script sample:
db.getSiblingDB('admin').addUser({
user: '<xxx>',
pwd: '<yyy>',
roles: ['clusterAdmin', 'dbAdminAnyDatabase', 'readAnyDatabase']
})
Additional privileges are required for monitoring users and roles, which is available starting with MongoDB 2.6. Specifically, the viewUser and viewRole privilege actions are required on any databases for which user and role data is to be collected. The following sample script creates a new role that enables monitoring users and roles on all databases and then grants it to a user called foglightAgent:
db.getSiblingDB('admin').createRole({
role: 'viewAllUsersAndRoles',
privileges: [{ resource: { db: '', collection: '' }, actions: ['viewUser', 'viewRole'] }],
roles: []
})
db.getSiblingDB('admin').grantRolesToUser('foglightAgent', ['viewAllUsersAndRoles'])
Finally, ensure that all firewalls and network configurations allow the machine running the FglAM to access the host of each monitored server at its configured port.
The below instructions cover common steps used to configure a TLS/SSL connection from the MongoDB Agent client. For full information on secure connections and server-side configuration, refer to the TLS/SSL Configuration for Clients section of the MongoDB documentation for your database version.
A full treatment of TLS/SSL keys, certificates, and certificate authorities (CA) is beyond the scope of this document. The following instructions assume familiarity with TLS/SSL concepts and tools. Client and certificate authority certificates must be available prior to proceeding.
In order to use SSL, your MongoDB server must include SSL support and allow SSL connections. There are various configurations options for client connections. Refer to the MongoDB documentation and verify that the current MongoDB server configuration parameters support the desired authentication.
The Foglight agent, in its capacity as a database client, requires access to a private key, its signed certificate, and the signing CA’s certificate. The client key and certificate must be imported into a keystore, and the CA certificate must be imported into a separate truststore.
One example method for generating a JKS keystore for use with Foglight utilizes openssl and keytool. Set the key and certificate filenames, alias name, and keystore password as appropriate.
openssl pkcs12 \
-export \
-in ${CERT_NAME}.crt \
-inkey ${CERT_NAME}.key \
-name $CERT_NAME \
-out temp-keystore.p12 \
-passout pass:${KEYPASS}
keytool -importkeystore \
-srckeystore temp-keystore.p12 \
-srcstoretype PKCS12 \
-srcstorepass $KEYPASS \
-destkeystore keystore \
-deststoretype JKS \
-deststorepass $KEYPASS
Regardless of how the keystore is constructed, it must list the client certificate as a ‘PrivateKeyEntry’, indicating that it also contains the private key, not just the signed certificate.
Separately, import the CA certificate into a truststore:
keytool -importcert \
-keystore truststore \
-alias $CA_NAME \
-file ${CA_NAME}.crt \
-keypass $TRUSTPASS \
-storepass $TRUSTPASS \
-storetype JKS \
-noprompt
Next, edit the baseline.jvmargs.config file in the <FglAM-install-root>/state/default/config directory and add the following parameters with file paths and passwords appropriate for your system.
vmparameter.0 = "-Djavax.net.ssl.keyStore=/path/to/keystore";
vmparameter.1 = "-Djavax.net.ssl.keyStorePassword=changeit";
vmparameter.2 = "-Djavax.net.ssl.trustStore=/path/to/truststore";
vmparameter.3 = "-Djavax.net.ssl.trustStorePassword=changeit";
Escape any quotes with a doubled backslash (’\’). On an Agent Manager installed on Windows, use forward slashes in the file paths, like so:
vmparameter.0 = "-Djavax.net.ssl.keyStore=\\"C:/path/to/keystore\\"";
Then, restart the FglAM and continue with the agent configuration, setting the “Use TLS/SSL?” option in the Agent Properties to true. If the client certificate is not configured specifically for the FglAM host, you can also set the “Allow Invalid Cert Hostname?” option to true to allow the certificate to be used anyway.
If the monitoring target is hosted in the MongoDB Atlas environment, the host address and port to use when setting up the Foglight agent must first be retrieved from the monitoring target’s local database. Do not use an address copied from the options in the “Connect” dialog on the Atlas web dashboard. Instead, connect to the MongoDB instance, e.g. via mongosh, and execute the following JavaScript code to retrieve a valid host address and port.
function getMonitoringAddress() {
var theDb = db.getMongo().getDB('local')
for (var member of theDb.system.replset.find().next().members) {
if (!member.arbiterOnly) {
var hostPort = member.host .split(':')
return {
'host': hostPort[0],
'port': hostPort[1],
}
}
}
return 'Unable to find non-arbiter member of replica set'
}
getMonitoringAddress()
The script returns output in the following format:
{ host: 'atlas-xxx-shard-nn-nn.xxx.mongodb.net', port: '27017' }
Take note of the host and port values for use when configuring the agent. See Setting Agent Properties.
Operation profiling is disabled by default on MongoDB servers. If profiling information is desired for monitoring purposes, profiling must be enabled in MongoDB. Profiling is enabled on an instance-by-instance and database-by-database basis. So if, for example, profiling for a specific database is desired across a three instance replica set, profiling can be enabled on the primary only or on all three instances, as desired. Profiling on MongoDB does impact database performance, and should be used judiciously in production environments. For more information see Database Profiling Overhead.
To enable profiling, first connect to the MongoDB instance with the mongo shell. The database user must be assigned a role with the enableProfiler privilege action on the respective database. An example of a built-in role that permits profiler enabling on non-system databases is dbAdmin.
Switch to the database for which profiling should be enabled. Replace <db-name> below with the name of the database:
use <db-name>;
To check the current profiling status, execute:
db.getProfilingStatus()
Modify the profiling level by executing the setProfilingLevel command. For example, the following command will enable profiling for all operations taking longer than 250 milliseconds:
db.setProfilingLevel(1, { slowms: 250 })
This is only an example of using the setProfilingLevel command and should not be taken as a recommendation for your database. For full details and options for using this command, see setProfilingLevel.
The simplest way to create a new agent is to navigate to the Databases dashboard, click on the Monitor button, and select MongoDB. Following the instructions in the Agent Installer Wizard popup to create a new Foglight for MongoDB agent.
Foglight for MongoDB provides a graphic, intuitive method for creating and configuring agents. To run the instance installation wizard:
To check on the status of the newly created agent, navigate to Administration > Agents > Agent Status and click the icon in the Log File column for the relevant agent.
The Agent Status dashboard can be used to create new agents and configure and manage existing agents. To access the page from the navigation pane, select Administration > Agents > Agent Status.
To create a new agent instance:
To modify the properties of an existing agent, skip to step 3, deactivate the agent, update the configuration, and then reactivate it.
For new agents, the default values can be accepted for most fields. Database connections must be configured.
Add one row in the connections list for each mongos query server, config server, and mongod data server in the sharded cluster or replica set to be monitored. Do not add rows for arbiters.
The Collection Period fields in the agent properties are used to set the sample frequencies. Before you modify the periods for an agent, make a copy of the default period list and modify the copy.