Telegraf E! Error in plugin [inputs.mongodb]: not authorized on admin to execute command

telegraf

#1

Hi,

I’ve tried to get metrics with [[inputs.mongodb]] but it seems that I’m missing something.

I’ve read: https://github.com/influxdata/telegraf/tree/master/plugins/inputs/mongodb

This connection uri may be different based on your environment and mongodb setup. If the user doesn’t have the required privilege to execute serverStatus command the you will get this error on telegraf
Error in input [mongodb]: not authorized on admin to execute command { serverStatus: 1, recordStats: 0 }

System information:
CentOS Linux release 7.3.1611 (Core)
Telegraf v1.5.2
MongoDB server version: 3.4.9

Telegraf.conf:
[[inputs.mongodb]]
servers = [“mongodb://admin:admin@localhost:27017/admin”]
gather_perdb_stats = true

MongoDB user: I even created another user named “telegraf” and tried with tis also but no result.
mongo_poc:PRIMARY> show users
{
"_id" : “admin.admin”,
“user” : “admin”,
“db” : “admin”,
“roles” : [
{
“role” : “userAdminAnyDatabase”,
“db” : “admin”
}
]
}
{
"_id" : “admin.telegraf”,
“user” : “telegraf”,
“db” : “admin”,
“roles” : [
{
“role” : “read”,
“db” : “admin”
},
{
“role” : “dbAdmin”,
“db” : “admin”
}
]
}

Telegraf output:

  • Plugin: inputs.mongodb, Collection 1
    2018-03-10T18:56:58Z E! Error in plugin [inputs.mongodb]: not authorized on admin to execute command { serverStatus: 1, recordStats: 0 }
  • Plugin: inputs.mongodb, Collection 2
    2018-03-10T18:56:59Z E! Error in plugin [inputs.mongodb]: not authorized on admin to execute command { serverStatus: 1, recordStats: 0 }

Kindly let me know what do you think or I must do in order to fix this?
How is meant to work this plugin in case when I have like now 3 MongoDB nodes in Cluster environment with 1 master and 2 slaves, I must put in telegraf.conf the details for [[inputs.mongodb]] only on the master?

Kind Regards,


#2

I’ve found a way after many hours of research.
It seems the only way for telegraf to read the [[inputs.mongodb]] is to set the root user from mongodb in telegraf.conf.

Telegraf.conf:
[[inputs.mongodb]]
servers = [“mongodb://root:root@localhost:27017/admin”]
gather_perdb_stats = true

“But still remains the question regarding if is needed to configure [[inputs.mongodb]] on a MongoDB cluster configuration on all 3 servers (2 slaves and 1 master) I think is made to run only on MongoDB Master?”

Kind Regards


#3

Here is how I created a user for monitoring:

db.createUser({user: "telegraf", pwd: "telegraf", roles: [{role:"clusterMonitor",db:"admin"}]})

The connection string was then:

servers = ["mongodb://telegraf:telegraf@example.org:27017/admin"]

I haven’t personally ran a cluster, but my guess is that it is reasonable to do it either way depending on your situation. Much of the data should be similar between nodes, but each node would give you it’s unique view of the data.


#4

Thank you Daniel.
I’ll come with a feedback.


#5

Looks OK in both ways, but I’ll follow your recommendation.
I have another question regarding “gather_perdb_stats = true” how is better to be on true vs false?

–==Scenario 1 - telegraf user==–
Telegraf “1.5.2”:
[[inputs.mongodb]]
servers = [“mongodb://telegraf:t3l3graf@localhost:27017/admin”]
gather_perdb_stats = true

MongoDB “3.4.9” User “telegraf”:
{
“_id” : “admin.root”,
“user” : “root”,
“db” : “admin”,
“roles” : [
{
“role” : “root”,
“db” : “admin”
}
]
}
{
“_id” : “admin.telegraf”,
“user” : “telegraf”,
“db” : “admin”,
“roles” : [
{
“role” : “clusterMonitor”,
“db” : “admin”
}
]
}

[root@server1 ~]# /usr/bin/telegraf --config /etc/telegraf/telegraf.conf --test | grep -i “mongodb_”

mongodb_db_stats,hostname=localhost:27017,host=server1,db_name=admin collections=2i,storage_size=53248i,index_size=69632i,ok=1i,objects=6i,data_size=1305i,num_extents=0i,avg_obj_size=217.5,type=“db_stat”,indexes=3i 1521378971000000000
mongodb_db_stats,host=server1,db_name=data,hostname=localhost:27017 storage_size=12288i,avg_obj_size=0,collections=3i,objects=0i,data_size=0i,type=“db_stat”,num_extents=0i,indexes=9i,index_size=36864i,ok=1i 1521378971000000000
mongodb_db_stats,host=server1,db_name=local,hostname=localhost:27017 index_size=98304i,type=“db_stat”,avg_obj_size=96.00562223991538,data_size=117807635i,storage_size=38236160i,indexes=5i,collections=6i,objects=1227091i,num_extents=0i,ok=1i 1521378971000000000

–==Scenario 2 - root user==–
Telegraf “1.5.2”:
[[inputs.mongodb]]
servers = [“mongodb://root:root@localhost:27017/admin”]
gather_perdb_stats = true

MongoDB “3.4.9” User “root”:
{
“_id” : “admin.root”,
“user” : “root”,
“db” : “admin”,
“roles” : [
{
“role” : “root”,
“db” : “admin”
}
]
}
{
“_id” : “admin.telegraf”,
“user” : “telegraf”,
“db” : “admin”,
“roles” : [
{
“role” : “clusterMonitor”,
“db” : “admin”
}
]
}

[root@server1 ~]# /usr/bin/telegraf --config /etc/telegraf/telegraf.conf --test | grep -i “mongodb_”

mongodb_db_stats,hostname=localhost:27017,host=server1,db_name=admin type=“db_stat”,indexes=3i,storage_size=53248i,num_extents=0i,ok=1i,objects=6i,avg_obj_size=217.5,data_size=1305i,index_size=69632i,collections=2i 1521379039000000000
mongodb_db_stats,hostname=localhost:27017,host=server1,db_name=data index_size=36864i,ok=1i,type=“db_stat”,collections=3i,storage_size=12288i,num_extents=0i,indexes=9i,objects=0i,avg_obj_size=0,data_size=0i 1521379039000000000
mongodb_db_stats,db_name=local,hostname=localhost:27017,host=server1 collections=6i,objects=1227098i,avg_obj_size=96.00562220784322,num_extents=0i,indexes=5i,storage_size=38236160i,type=“db_stat”,data_size=117808307i,index_size=98304i,ok=1i 1521379039000000000


#6

What difference are you trying to show in those examples?


#7

1. role: clusterMonitor can be used on the scenario where you monitor the cluster activity installing telegraf on the master node only, if you will install on the slaves nodes using the same role and same user per that role you will have the cluster info not slaves info.
2. role: root user is not recommended to use due to the fact that has all permissions and is not a good approach, the difference is that with root you can monitor per instance the activity: master, slaves.

I’m using now after your recommendation the role: clusterMonitor.

Thank you.