Calling curl from inside script library?

Started by graeChris, April 27, 2023, 12:08:57 AM

Previous topic - Next topic

graeChris

I'm trying to build a series of scripts that send curl requests via command line to an external API. I've gone through the Docs multiple times but I haven't been able to find any documentation on how to use NSXL to send a curl command. I was able to get it to work under Actions Configuration, but I need this to occur during Hook::AlarmStateChange. 

For Curl to work via Actions I had to specify C:\\Windows\\System32\\curl.exe

Is it possible to call this using NSXL?
If it is not possible, should we be calling the server action instead? 


Filipp Sudanov

First of all, there is built-in support for web services in NetXMS. It's possible to set up DCIs that get data from web services, but there's also ability to use them from NXSL scripts https://www.netxms.org/documentation/nxsl-latest/#class-webservice

It's probably not a good idea to have long operations in hook scrips, so I would call PostEvent() from NXSL and then have a rule in EPP that catches that event and calls server action which calls NXSL script.

graeChris

Hi Filipp,

Thank you for your help! I was able to call postevent() during the hook using the following:

sub main()
{
    eventserver = FindNodeObject($node, 100);
    global alarmstate = $alarm->state;
    switch(alarmstate)
    {
/* Alarm State is Outstanding */
        case "0":
            break;
/* Alarm State is Acknowledged */
        case "1":
            PostEvent(eventserver, "Xms_Alarm_Ack");
            break;
/* Alarm State is Resolved */
        case "2":
            PostEvent(eventserver, "Xms_Alarm_Resolve");
            break;
/* Alarm State is Sticky Acknowledged */
        case "17":
            PostEvent(eventserver, "Xms_Alarm_StickyAck");
            break;
    }
}


I then created the following webservice with the following parameters:

URL: https://api.opsgenie.com/v2/alerts/%[OGAckAlarmID]/acknowledge?identifierType=alias
HTTP request method: POST
Request data:
[
{
"user": "netXMS Admin",
"source\\": "netXMS",
"note": "Acknowledged via Alert API from netXMS - Time: %t"
}
]

I added the Authentication and the Content-Type:application/json in the Headers.

Now the problem I am running into- The EPP script does not want to run the Webservice without the data argument being passed. I tried to use a variety of Macro schemes to pull the data from the webservice configuration data field but I keep coming up empty. using trace() provided no information either.

Here are my questions:
  • Can I call a script inside the url field of the webservice configuration?
  • How do I reference the "Request Data" field in the webservice configuration when calling the post function?

I am attempting to call the post() function found here: https://www.netxms.org/documentation/nxsl-latest/#class-webservice

Filipp Sudanov

Yes, URL field supports macros, so %[OGAckAlarmID] should work. I am not sure how you are getting alarm ID in that script, may be you should pass the ID as parameter in your PostEvent() calls.

Request Data field is employed when web service is used as data source for a DCI. When using post() method of web service object, that field is ignored, so you have to pass the whole request in post() call.