Page 1 of 1

Raw Import API via Powershell

Posted: Tue Jan 29, 2019 6:47 am
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?

Re: Raw Import API via Powershell

Posted: Tue Jan 29, 2019 3:18 pm
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

Re: Raw Import API via Powershell

Posted: Thu Jan 31, 2019 5:49 am
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

Re: Raw Import API via Powershell

Posted: Thu Jan 31, 2019 5:58 am
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?

Re: Raw Import API via Powershell

Posted: Thu Jan 31, 2019 2:36 pm
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

Re: Raw Import API via Powershell

Posted: Fri Feb 01, 2019 10:29 am
by optionstechnology
Yip, that did the trick, thanks

Re: Raw Import API via Powershell

Posted: Fri Feb 01, 2019 1:10 pm
by cdienger
Glad to hear. Are we good to close the thread at this point?

Re: Raw Import API via Powershell

Posted: Mon Feb 04, 2019 5:04 am
by optionstechnology
yip

Re: Raw Import API via Powershell

Posted: Mon Feb 04, 2019 9:56 am
by cdienger
locking.