Alerting

How to call Node.js instead of python or perl in Alert Script to fire results of the alert to an external system?

tdepuy
Path Finder

Hello,
I am writing an Alert Script to fire results of the Alert to an external system via REST API over HTTPS. Since python doesn't seem to be compiled with HTTPS support, I am looking at other options. It looks like there is a Node.js executable in /opt/splunk/bin which I tested:

boba@splunk:/opt/splunk/bin$
boba@splunk:/opt/splunk/bin$ pwd
/opt/splunk/bin
boba@splunk:/opt/splunk/bin$ ./node --version
v0.8.14
boba@splunk:/opt/splunk/bin$ ./node
> var https = require( 'https' )
undefined
>
undefined
> https
{ Server: { [Function: Server] super_: { [Function: Server] super_: [Object] } },
  createServer: [Function],
  globalAgent:
   { domain: null,
     _events: { free: [Function] },
     _maxListeners: 10,
     options: {},
     requests: {},
     sockets: {},
     maxSockets: 5,
     createConnection: [Function: createConnection] },
  Agent:
   { [Function: Agent]
     super_:
      { [Function: Agent]
        super_: [Function: EventEmitter],
        defaultMaxSockets: 5 } },
  request: [Function],
  get: [Function] }
>

However, when I try to call my "/opt/splunk/bin/scripts/test.js" script:


boba@splunk:/opt/splunk/bin/scripts$ cat test.js

!/opt/splunk/bin/node

"use strict"
var https = require( 'https' );
var fs = require( 'fs' );
var LOGFILE = "my.log"
fs.appendFile( LOGFILE, "Parms: " + process.argv + "\n" )

from the Alert, the splunkd.log prints this:

splunkd.log:10-16-2014 17:28:28.304 -0700 ERROR script - command="runshellscript", Cannot find script at /opt/splunk/bin/scripts/test.js

Despite this:


boba@splunk:/opt/splunk/bin/scripts$ file /opt/splunk/bin/scripts/test.js
/opt/splunk/bin/scripts/test.js: a /opt/splunk/bin/node script, ASCII text executable

0 Karma
1 Solution

tdepuy
Path Finder

Having a python wrapper script seems to work. This hard codes the path, but demonstrates the functionality.

#!/opt/splunk/bin/python

from subprocess import call
import sys

call( ["/opt/splunk/bin/node", "/opt/splunk/bin/scripts/test.js"] + sys.argv )

View solution in original post

0 Karma

jrodman
Splunk Employee
Splunk Employee

Alert script invocation supports #! scripts, so you can use an executable textfile that looks like #!/path/to/node on the first line.

Errrr, this should work anyway. I'll try it.

Yes, this error makes little sense.

In the current code, this is, in etc/apps/search/bin/runshellscript.py:

if not os.path.exists(script):
    results = splunk.Intersplunk.generateErrorResults('Cannot find script at ' +script)

So this message indeed means that path isn't available. Something else must be going on in the environment, or you may be using a version where this code is different.

0 Karma

tdepuy
Path Finder

Having a python wrapper script seems to work. This hard codes the path, but demonstrates the functionality.

#!/opt/splunk/bin/python

from subprocess import call
import sys

call( ["/opt/splunk/bin/node", "/opt/splunk/bin/scripts/test.js"] + sys.argv )
0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Agent Mode Engaged! Enchaining Agentic Operations with Splunk AI Assistant 2.0

    Are you ready to transform how your team handles complex data requests? We invite you to our upcoming ...

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

Modernize your Splunk Apps – Introducing Python 3.13 in Splunk

We are excited to announce that the upcoming releases of Splunk Enterprise 10.2.x and Splunk Cloud Platform ...