Raw Import API via Powershell

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

Raw Import API via Powershell

Post by optionstechnology »

I am trying to do Raw Import to add a contact to nagios via the API in powershell, i seem to be missing a part of the command and cant figure out how to do it-

The curl command is

Code: Select all

curl -XPOST "https://nagiosserver/nagiosxi/api/v1/config/import?apikey=bigkey&pretty=1" -d '
define contact {
        contact_name test
        host_notifications_enabled 0
        service_notifications_enabled 0
        host_notification_period none
        service_notification_period none
        host_notification_options n
        service_notification_options n
        host_notification_commands xi_host_notification_handler
        service_notification_commands xi_service_notification_handler
}'
I've tested this and it does correctly add the contact

This is the same command in Powershell-

Code: Select all

        $bodycontact = @{
        contact_name="powershelltest"
        host_notifications_enabled=0
        service_notifications_enabled=0
        host_notification_period="none"
        service_notification_period="none"
        host_notification_options="n"
        service_notification_options="n"
        host_notification_commands="xi_host_notification_handler"
        service_notification_commands="xi_service_notification_handler"
        }

Invoke-RestMethod -Method Post -Uri "http://nagiosserver/nagiosxi/api/v1/config/import?apikey=bigkey&define=contact" -Body $bodycontact
This reports success-

success
-------
Imported configuration data. Config imported but not yet applied.

but doesn't actually add the data.

The key bit I am missing is the 'define contact' line - but I'm not sure how to turn that into powershell syntax, anyone any idea?
User avatar
cdienger
Support Tech
Posts: 5045
Joined: Tue Feb 07, 2017 11:26 am

Re: Raw Import API via Powershell

Post by cdienger »

Try:

Code: Select all

$bodycontact = @"
define contact {
contact_name powershelltest
host_notifications_enabled 0
service_notifications_enabled 0
host_notification_period none
service_notification_period none
host_notification_options n
service_notification_options n
host_notification_commands xi_host_notification_handler
service_notification_commands xi_service_notification_handler
}
"@

Invoke-RestMethod -Method Post -Uri "http://nagiosserver/nagiosxi/api/v1/config/import?apikey=bigkey" -Body $bodycontact
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
optionstechnology
Posts: 234
Joined: Thu Nov 17, 2016 11:26 am

Re: Raw Import API via Powershell

Post by optionstechnology »

Yes! that worked

One thing to note for anyone else trying this - you weirdly cant put any whitespace between the -
}
"@

one I removed all the indents it worked fine

You can also use-

use templatename

To add it to a template without any of the other options being required i.e.

Code: Select all

$bodycontact = @"
define contact {
contact_name powershelltest
use xi_contactgroup_all
}
"@
Thanks
optionstechnology
Posts: 234
Joined: Thu Nov 17, 2016 11:26 am

Re: Raw Import API via Powershell

Post by optionstechnology »

One other thing, I'm also trying to create a contact group to add all the users too - can I specify that from the contact? Or do I have to use 'define contactgroup'?

I used-

Code: Select all

$bodygroup = @"
define contactgroup {
    contactgroup_name   NOC Users
    alias               NOC View
    members             powershelltest
}
"@
Which works perfectly, however - once I create it once I cannot add new users to it by running it again... Nor can I delete it

Any idea how to get around this?
User avatar
cdienger
Support Tech
Posts: 5045
Joined: Tue Feb 07, 2017 11:26 am

Re: Raw Import API via Powershell

Post by cdienger »

contactgroups is a valid directive for the contact object so this should work:

Code: Select all

$bodycontact = @"
define contact {
contact_name powershelltest
contactgroups powershellgroup
host_notifications_enabled 0
service_notifications_enabled 0
host_notification_period none
service_notification_period none
host_notification_options n
service_notification_options n
host_notification_commands xi_host_notification_handler
service_notification_commands xi_service_notification_handler
}
"@

Invoke-RestMethod -Method Post -Uri "http://nagiosserver/nagiosxi/api/v1/config/import?apikey=bigkey" -Body $bodycontact
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
optionstechnology
Posts: 234
Joined: Thu Nov 17, 2016 11:26 am

Re: Raw Import API via Powershell

Post by optionstechnology »

Yip, that did the trick, thanks
User avatar
cdienger
Support Tech
Posts: 5045
Joined: Tue Feb 07, 2017 11:26 am

Re: Raw Import API via Powershell

Post by cdienger »

Glad to hear. Are we good to close the thread at this point?
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
optionstechnology
Posts: 234
Joined: Thu Nov 17, 2016 11:26 am

Re: Raw Import API via Powershell

Post by optionstechnology »

yip
User avatar
cdienger
Support Tech
Posts: 5045
Joined: Tue Feb 07, 2017 11:26 am

Re: Raw Import API via Powershell

Post by cdienger »

locking.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
Locked