How can i get an alarm when an index created?

Hello;

We have authorized developers to create indexes in our system, but we want to be informed when they create an index.

Do we have an option to get an alarm when an index is created on the database?

I’m open to your suggestions on this subject.

Thaks

Hi @Sercan_Ersan - Welcome to the community :wave:

Can you provide the following information:

  1. Is the deployment on-prem or hosted on Atlas?
  2. Your main concern with the index creation being attempted (i.e. Performance, unmanaged/untracked indexes, etc.)

Regards,
Jason

Hello @Jason_Tran, thank you for your answer.

  1. Our deployment is on Atlas.
  2. To have information about indexes created in Development and Production environments. By following the indexes, to check if there is an index that is really suitable for the wishes of the person who created it.

Regards,
Sercan

@Jason_Tran

I think there is no development where we can get alerts in this way, at least for mongodb 4.4… versions.

I also wanted to do a work like this, but for now I have a problem there too.


db = db.getSiblingDB("admin");
dbs = db.runCommand({ "listDatabases": 1}).databases;


dbs.forEach(function(database) {
db = db.getSiblingDB(database.name);
cols = db.getCollectionNames();

cols.forEach(function(col) {

     indexes = db[col].getIndexes();

     indexes.forEach(function(idx) {
        print("Database:" + database.name + " | Collection:" +col+ " | Index:" + idx.name);
        printjson(indexes);
         });


    });

});

With the above code block, we can see the indexes in all databases under the cluster.

Our query output is like this;
Capture

Is there a way for me to insert this output into a collection?

Hi @Sercan_Ersan,

Thanks for clarifying those details. There currently isn’t an Atlas alert that can be created for when an index creation is submitted. If you would like this feature to be added, I would suggest you to file a feature request via feedback.mongodb.com being sure to include all your use case details. From that platform, you will be able to interact with our Product Management team, keep track of the progress of your request, and make this visible to other users.

Is there a way for me to insert this output into a collection?

One idea based off what you have provided from the output screenshot is to log the namespace details & index details (from the output) on the application side and insert them to a collection so that you can use to monitor indexes. This could then be made to be a CRON / scheduled job so that you can check every now and then to verify the appropriate indexes are in place. Additionally, you can consider limiting access to this collection to only certain database users. Please see Configure Custom Database Roles for more information on this.

However, this leads to another problem - How or when to know when an index is removed?

  1. To have information about indexes created in Development and Production environments. By following the indexes, to check if there is an index that is really suitable for the wishes of the person who created it.

I believe it may be better to solve this from a workflow perspective. One suggestion could be:

  1. Allowing your developers to create indexes in the dev environment and have some auditing to pick up when a new index is created. Since you’re on MongoDB Atlas, you could Set up Database Auditing to audit createIndex and dropIndex actions although this feature is only available on M10+ tier clusters. You can then retrieve the mongodb-audit-log’s and filter for if these actions have occurred periodically.

    Example:

  2. Disallow creating indexes in the production environment outside of an approved deployment process. I.e. Only indexes that you’ve approved (through an internal process) from Dev can be created on Production.

Of course, all the above would depend on your use case and requirements. Would you be able to advise if the indexes are being created manually or via code?

Regards,
Jason

2 Likes