Assigning an API key to a user

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
optionstechnology
Posts: 234
Joined: Thu Nov 17, 2016 11:26 am

Assigning an API key to a user

Post by optionstechnology »

I have multiple Nagios servers with the same user on each setup to run reports

But because I cannot specify an API key to use - it means I have to track the API key of each server separately

Is there a way of putting the same API key for this user into the database on all of my Nagios boxes?
User avatar
pbroste
Posts: 1288
Joined: Tue Jun 01, 2021 1:27 pm

Re: Assigning an API key to a user

Post by pbroste »

Hello @optionstechnology

Thanks for reaching out, there are possibly other ways or workarounds but this is what I tested on my test VM's:

Logged in to the Nagios XI web console on Server A to copy the api key fH4CoSSE05J7cPqnIhiWVbqceLuKFsaX92YJPhFA9vIC9EFVWdS3p9kAoeJFYRlN to distribute.

On Server B (receiving the updated distributed api key):

Code: Select all

mysqldump -uroot -pnagiosxi nagiosxi > nagiosxi.sql
Verified the original key is found on Server B before making the update:

Code: Select all

grep -o 'rVFc7FAkabP8dcqhhj62VBeG5n5E8pP2ZC9MUsbdBr9eDWpfs9qVABdfIO0iiRri' nagiosxi.sql
Updating the api key on Server B:

Code: Select all

sed -i 's/rVFc7FAkabP8dcqhhj62VBeG5n5E8pP2ZC9MUsbdBr9eDWpfs9qVABdfIO0iiRri/fH4CoSSE05J7cPqnIhiWVbqceLuKFsaX92YJPhFA9vIC9EFVWdS3p9kAoeJFYRlN/g' nagiosxi.sql
Verified that the key was written to server B:

Code: Select all

grep -o 'fH4CoSSE05J7cPqnIhiWVbqceLuKFsaX92YJPhFA9vIC9EFVWdS3p9kAoeJFYRlN' nagiosxi.sql
Writing the database:

Code: Select all

mysql -uroot -pnagiosxi nagiosxi < nagiosxi.sql
Restart services:

Code: Select all

systemctl restart nagios mysqld httpd
Make sure that you have backed up your server instance before making any changes.

Thanks,
Perry
optionstechnology
Posts: 234
Joined: Thu Nov 17, 2016 11:26 am

Re: Assigning an API key to a user

Post by optionstechnology »

Hey this is great - one minor tweek - is it possible to do by username instead of find/replacing the actual token string?

Reason I ask is ill be automating it on newly built servers so I don't want to have to manually go in every time and get the current API key just to replace it
User avatar
pbroste
Posts: 1288
Joined: Tue Jun 01, 2021 1:27 pm

Re: Assigning an API key to a user

Post by pbroste »

Hello @optionstechnology

Thanks for following up, technically this is going outside of the spectrum of our support and into customizing.

Ran through some trials but the results are not what I expect and want to toss this out to the forum for a possible workaround. Here is what I tried but please bear in mind that the results did not work as intended. Either tweaks are needed or someone might have an idea that goes off in a different direction.

Tried the following and please make any adjustments:

Code: Select all

grep -i 'usernamehere' nagiosxidump.sql | awk -F "," '{print $6}'
or

Code: Select all

grep -Ei 'usernamehere' nagiosxidump.sql | cut -d , -f 6
The idea to grep for the 'username' and six comma-delimited data placement indexes gets us over to the "apikey". Where you could then use sed to find, using a regular expression to /replace.

Thanks,
Perry
optionstechnology
Posts: 234
Joined: Thu Nov 17, 2016 11:26 am

Re: Assigning an API key to a user

Post by optionstechnology »

Just in case anyone is interested in doing this I achieved it with:

Code: Select all

mysql -uroot -pnagiosxi nagiosxi -e "UPDATE xi_users SET api_key = 'bigapikeygoeshere' WHERE user_id=1;"
Then just apply config in nagios
User avatar
pbroste
Posts: 1288
Joined: Tue Jun 01, 2021 1:27 pm

Re: Assigning an API key to a user

Post by pbroste »

Hello @optionstechnology

Excellent solution, and thanks for sharing. I will go ahead and lock this, please let us know if there is anything further.

Thanks,
Perry
Locked