api cant add a new contactgroup

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
sigmainformatique
Posts: 74
Joined: Mon Apr 23, 2018 8:11 am

api cant add a new contactgroup

Post by sigmainformatique »

Hi,

I'm trying to add contactgroups using the API, but it doesn't seem to be working :
The following command were gathered from the help page http://127.0.0.1/nagiosxi/help/

curl -XPOST "http://127.0.0.1/nagiosxi/api/v1/config ... N&pretty=1" -d "contactgroup_name=testcontactgroup&alias=newcontactgroup&applyconfig=1"
{
"success": "Added testcontactgroup to the system. Config applied, Nagios Core was restarted."
}
curl -XGET "http://127.0.0.1/nagiosxi/api/v1/config ... ntactgroup"
[

]
What am I doing wrong ?
we are using nagiosxi-5.8.6 on centos 7.9
Thanks,
User avatar
pbroste
Posts: 1288
Joined: Tue Jun 01, 2021 1:27 pm

Re: api cant add a new contactgroup

Post by pbroste »

Hello @sigmainformatique

Thanks for reaching out; appears from the details that you provided that the POST was a success and applied. Since GET is blank when specifying the contactgroup name, want the command to get the list:

Code: Select all

curl -XGET "http://127.0.0.1/nagiosxi/api/v1/config/contactgroup?apikey=TOKEN&pretty=1&contactgroup_name" -v
Please PM your updated system profile for us to review.

To send us your system profile.
  • Login to the Nagios XI GUI using a web browser.
  • Click the "Admin" > "System Profile" Menu
  • Click the "Download Profile" button
  • Save the profile.zip file and send via Private Message
Please let us know the results.

Thanks,
Perry
sigmainformatique
Posts: 74
Joined: Mon Apr 23, 2018 8:11 am

Re: api cant add a new contactgroup

Post by sigmainformatique »

Hi,

- Listing all groups with curl or through web interface does not show the contactgroup "testcontactgroup".
- Download profile fails:

Code: Select all

PROFILE BUILD FAILED
Array
(
)
CODE: 1
I can see errors in /var/log/httpd/error_log file :

Code: Select all

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

sudo: no tty present and no askpass program specified
The user "apache" is not allow to run any command using sudo.
sigmainformatique
Posts: 74
Joined: Mon Apr 23, 2018 8:11 am

Re: api cant add a new contactgroup

Post by sigmainformatique »

I have the same issue if I try to add a contact :

Code: Select all

curl -XPOST "http://127.0.0.1/nagiosxi/api/v1/config/contact?apikey=TOKEN&pretty=1" -d "contact_name=testcontact&host_notifications_enabled=1&service_notifications_enabled=1&host_notification_period=24x7&service_notification_period=24x7&host_notification_options=d,r&service_notification_options=w,c,r&host_notification_commands=notify-host-by-email&service_notification_commands=notify-service-by-email&applyconfig=1"
{
    "success": "Added testcontact to the system. Config applied, Nagios Core was restarted."
}
curl -XGET "http://127.0.0.1/nagiosxi/api/v1/config/contact?apikey=TOKEN&pretty=1&contact_name=testcontact"
[
    
]
But the GET API does work :
curl -XGET "http://127.0.0.1/na[code]giosxi/api/v1/ ... agiosadmin"
[
{
"contact_name": "nagiosadmin",
"alias": "Nagios Admin",
],
....
}
][/code]


And so does the PUT calls :

Code: Select all

curl -XPUT  "http://127.0.0.1/nagiosxi/api/v1/config/contacgroup/rrrrrr/?apikey=token&members=msimon&applyconfig=1"
{"success":"Updated rrrrrr in the system. Config applied, Nagios Core was restarted."}
curl  "http://127.0.0.1//nagiosxi/api/v1/config/contacgroupt?apikey=token=rrrrrr"                  
[{"contactgroup_name":"rrrrrr","alias":"rrrrrr","members":["msimon"]}]
curl -XPUT "http://127.0.0.1/nagiosxi/api/v1/config/contactgroup/rrrrrr/?apikey=token&members=msimon,nagiosadmin&applyconfig=1"
{"success":"Updated rrrrrr in the system. Config applied, Nagios Core was restarted."}
curl "http://127.0.0.1/nagiosxi/api/v1/config/contactgroup?apikey=token&contactgroup_name=rrrrrr"                              
[{"contactgroup_name":"rrrrrr","alias":"rrrrrr","members":["msimon","nagiosadmin"]}]


User avatar
pbroste
Posts: 1288
Joined: Tue Jun 01, 2021 1:27 pm

Re: api cant add a new contactgroup

Post by pbroste »

Hello @sigmainformatique

Thanks for following up with the details; appears that the user apache does not have enough permissions to run the System Profile script. Please run as root and then send vi Private Message:

Code: Select all

su -l root
rm -rf /usr/local/nagiosxi/var/components/profile.zip
/usr/local/nagiosxi/scripts/components/getprofile.sh SUPPORT

Then send the resulting /usr/local/nagiosxi/var/components/profile.zip​ file via Private Message.

Thanks,
Perry
sigmainformatique
Posts: 74
Joined: Mon Apr 23, 2018 8:11 am

Re: api cant add a new contactgroup

Post by sigmainformatique »

I had to patch getprofile.sh to get it working :

Code: Select all

--- getprofile.sh.org	2021-10-08 10:20:08.836068760 +0200
+++ getprofile.sh	2021-10-08 10:42:20.244000000 +0200
@@ -47,9 +47,10 @@
 ver="${version%%.*}"
 
 # Make a clean folder (but save profile.html)
-rm -rf "/usr/local/nagiosxi/var/components/profile/$folder/"
+[ -d "/usr/local/nagiosxi/var/components/profile/$folder/" ] && rm -rf "/usr/local/nagiosxi/var/components/profile/$folder/"
+[ -f "/usr/local/nagiosxi/var/components/profile/$folder/" ] && rm -f "/usr/local/nagiosxi/var/components/profile/$folder.zip"
+[ -f "/usr/local/nagiosxi/tmp/profile-$folder.html" ] && mv -f "/usr/local/nagiosxi/tmp/profile-$folder.html" "/usr/local/nagiosxi/var/components/profile/$folder/profile.html"
 mkdir "/usr/local/nagiosxi/var/components/profile/$folder/"
-mv -f "/usr/local/nagiosxi/tmp/profile-$folder.html" "/usr/local/nagiosxi/var/components/profile/$folder/profile.html"
 
 # Create the folder setup
 mkdir -p "/usr/local/nagiosxi/var/components/profile/$folder/nagios-logs"
@@ -132,7 +133,34 @@
     php -r '
         define("CFG_ONLY", 1);
         require_once($argv[1]);
-        print(@$cfg["db_info"]["ndoutils"]["dbserver"] . "\n");
+        print(@$cfg["db_info"]["nagiosxi"]["dbserver"] . "\n");
+    ' \
+        '/usr/local/nagiosxi/html/config.inc.php' 2>/dev/null |
+    tail -1
+)
+db_user=$(
+    php -r '
+        define("CFG_ONLY", 1);
+        require_once($argv[1]);
+        print(@$cfg["db_info"]["nagiosxi"]["user"] . "\n");
+    ' \
+        '/usr/local/nagiosxi/html/config.inc.php' 2>/dev/null |
+    tail -1
+)
+db_pwd=$(
+    php -r '
+        define("CFG_ONLY", 1);
+        require_once($argv[1]);
+        print(@$cfg["db_info"]["nagiosxi"]["pwd"] . "\n");
+    ' \
+        '/usr/local/nagiosxi/html/config.inc.php' 2>/dev/null |
+    tail -1
+)
+db_db=$(
+    php -r '
+        define("CFG_ONLY", 1);
+        require_once($argv[1]);
+        print(@$cfg["db_info"]["nagiosxi"]["db"] . "\n");
     ' \
         '/usr/local/nagiosxi/html/config.inc.php' 2>/dev/null |
     tail -1
@@ -168,21 +196,21 @@
     else
 
         echo "Getting xi_users..."
-        echo 'select * from xi_users;' | mysql -u root -pnagiosxi nagiosxi -t > "/usr/local/nagiosxi/var/components/profile/$folder/xi_users.txt"
+        echo 'select * from xi_users;' | mysql -u ${db_user} -p"${db_pass}" ${db_db} -t > "/usr/local/nagiosxi/var/components/profile/$folder/xi_users.txt"
 
         echo "Getting xi_usermeta..."
-        echo 'select * from xi_usermeta;' | mysql -u root -pnagiosxi nagiosxi -t > "/usr/local/nagiosxi/var/components/profile/$folder/xi_usermeta.txt"
+        echo 'select * from xi_usermeta;' | mysql -u ${db_user} -p"${db_pass}" ${db_db} -t > "/usr/local/nagiosxi/var/components/profile/$folder/xi_usermeta.txt"
 
         echo "Getting xi_options(mail)..."
-        echo 'select * from xi_options;' | mysql -t -u root -pnagiosxi nagiosxi | grep mail > "/usr/local/nagiosxi/var/components/profile/$folder/xi_options_mail.txt"
+        echo 'select * from xi_options;' | mysql -t -u ${db_user} -p"${db_pass}" ${db_db} | grep mail > "/usr/local/nagiosxi/var/components/profile/$folder/xi_options_mail.txt"
 
         echo "Getting xi_otions(smtp)..."
-        echo 'select * from xi_options;' | mysql -t -u root -pnagiosxi nagiosxi | grep smtp > "/usr/local/nagiosxi/var/components/profile/$folder/xi_options_smtp.txt"
+        echo 'select * from xi_options;' | mysql -t -u ${db_user} -p"${db_pass}" ${db_db} | grep smtp > "/usr/local/nagiosxi/var/components/profile/$folder/xi_options_smtp.txt"
 
     fi
 
     if which mysqladmin >/dev/null 2>&1; then
-        errlog=$(mysqladmin -u root -pnagiosxi variables | grep log_error)
+        errlog=$(mysqladmin -u ${db_user} -p"${db_pass}" variables | grep log_error)
         if [ $? -eq 0 ] && [ -f "$errlog" ]; then
             /usr/bin/tail -n500 "$errlog" > "/usr/local/nagiosxi/var/components/profile/$folder/logs/database_errors.txt"
         fi
@@ -234,12 +262,12 @@
 
 spool_perfdata_location=$(cat /usr/local/nagios/etc/pnp/npcd.cfg | sed -n -e 's/^perfdata_spool_dir = //p')
 echo "Total files in $spool_perfdata_location" > "/usr/local/nagiosxi/var/components/profile/$folder/file_counts.txt"
-ls -al "$spool_perfdata_location" | wc -l >> "/usr/local/nagiosxi/var/components/profile/$folder/file_counts.txt"
+ls -al "$spool_perfdata_location" 2>/dev/null| wc -l >> "/usr/local/nagiosxi/var/components/profile/$folder/file_counts.txt"
 echo "" >> "/usr/local/nagiosxi/var/components/profile/$folder/file_counts.txt"
 
 spool_xidpe_location=$(cat /usr/local/nagios/etc/commands.cfg | sed -n -e 's/\$TIMET\$.perfdata.host//p' | sed -n -e 's/\s*command_line\s*\/bin\/mv\s//p' | sed -n -e 's/.*\s//p')
 echo "Total files in $spool_xidpe_location" >> "/usr/local/nagiosxi/var/components/profile/$folder/file_counts.txt"
-ls -al "$spool_xidpe_location" | wc -l >> "/usr/local/nagiosxi/var/components/profile/$folder/file_counts.txt"
+ls -al "$spool_xidpe_location"  2>/dev/null| wc -l >> "/usr/local/nagiosxi/var/components/profile/$folder/file_counts.txt"
 echo "" >> "/usr/local/nagiosxi/var/components/profile/$folder/file_counts.txt"
 
 echo "Counting MRTG Files..."
User avatar
pbroste
Posts: 1288
Joined: Tue Jun 01, 2021 1:27 pm

Re: api cant add a new contactgroup

Post by pbroste »

Hello @sigmainformatique

Thanks for following up and send over the results. Want to go ahead and rule out permissions, to run the API command with root user permissions.

Code: Select all

su -l root
curl -XGET "http://127.0.0.1/nagiosxi/api/v1/config/contactgroup?apikey=TOKEN&pretty=1&contactgroup_name" -v --show-error -k
Some housekeeping; we see that when executing reconfigure_nagios.sh it is trying to import cfg from the /usr/local/nagios/etc/import/. Please review this import and if everything is okay remove files from the import directory. Then re-run the 'reconfigure_nagios.sh' and check for anything that failed.

Verify that the host and services look good in pre-flight with no errors in core by:
  • Code: Select all

    /usr/local/nagios/bin/nagios -vvv /usr/local/nagios/etc/nagios.cfg
Let me know how things look.

Thanks,
Perry
sigmainformatique
Posts: 74
Joined: Mon Apr 23, 2018 8:11 am

Re: api cant add a new contactgroup

Post by sigmainformatique »

Hi Perry,

What I see through curl is what I see through theweb interface. Here is the output of the curl ran as root (output is the same if run from another host) :

Code: Select all

* About to connect() to 127.0.0.1 port 80 (#0)
*   Trying 127.0.0.1...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET /nagiosxi/api/v1/config/contactgroup?apikey=8vs6PWilJifCZ7Gn23CCNuNsnVAMihlpQQOFM7ZLIvDXFgkWidulKC7ntHpOe4t4&pretty=1&contactgroup_name HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 127.0.0.1
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Mon, 11 Oct 2021 06:13:45 GMT
< Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips mod_fcgid/2.3.9 PHP/5.4.16
< X-Powered-By: PHP/5.4.16
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT
< Transfer-Encoding: chunked
< Content-Type: application/json
< 
{ [data not shown]
[
    {
        "contactgroup_name": "aaaaaaaaaaaa",
        "alias": "aaaaaaaaaaaa"
    },
    {
        "contactgroup_name": "admins",
        "alias": "Nagios Administrators",
        "members": [
            "nagiosadmin"
        ]
    },
...
]
There was no issue in /usr/local/nagios/etc/import, but just to rule this out, we removed all files in /usr/local/nagios/etc/import.
Output of: /usr/local/nagios/bin/nagios -vvv /usr/local/nagios/etc/nagios.cfg

Nagios Core 4.

Code: Select all

4.6
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 2020-04-28
License: GPL

Website: https://www.nagios.org
Reading configuration data...
  Read main config file okay...
	Checked 92929 services.
	Checked 12752 hosts.
	Checked 4644 host groups.
	Checked 21 service groups.
	Checked 656 contacts.
	Checked 283 contact groups.
	Checked 853 commands.
	Checked 412 time periods.
	Checked 0 host escalations.
	Checked 0 service escalations.
  Checking for circular paths...
	Checked 12752 hosts
	Checked 0 service dependencies
	Checked 0 host dependencies
	Checked 412 timeperiods
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...

Total Warnings: 4330
Total Errors:   0
Warnings are about notification periode or interval.
sigmainformatique
Posts: 74
Joined: Mon Apr 23, 2018 8:11 am

Re: api cant add a new contactgroup

Post by sigmainformatique »

Perry,

We are trying to upgrade from 5.5.8 to 5.8.6. Could this issue we're having be related to the upgrade ?

Here how we proceed to upgrade :
- initial : xi 5.5.8 + nagios core 4.2
- upgrade to xi 5.5.9 (using upgrade script)
- upgrade to xi 5.5.11 (using upgrade script)
- upgrade to xi 5.6.14 (using upgrade script)
- upgrade to xi 5.7.5 (using upgrade script)
- upgrade to xi 5.8.6 (using upgrade script)

Ronan
User avatar
pbroste
Posts: 1288
Joined: Tue Jun 01, 2021 1:27 pm

Re: api cant add a new contactgroup

Post by pbroste »

Hello Ronan,

What do we see in the 'upgrade.log' located '/usr/local/nagiosxi/tmp/upgrade.log'?

Let us know,
Perry
Locked