Getting Data In

Python script with Universal forwarder

jangid
Builder

Here is my script

from urllib2 import sys, Request, urlopen, URLError, HTTPError
if len(sys.argv) <= 1:
  sys.exit(0)
url = sys.argv[1] + "/messages=SplunkTestMessage&outputFormat=splunk"

req = Request(url)
try:
  response = urlopen(req)
except HTTPError, e:
  print "The server couldn\'t fulfill the request. ",
  print "error_code=", e.code, " status=2"
except URLError, e:
  print "Failed to reach server. ",
  print "error_code=", e.reason, " status=3"
else:
  html = response.read()
  print html, " status=1"

Same script perfectly works with Splunk main instance.

How do I run python script with Universal forwarder?
There is no problem with bat file and other exe. I am forwarding script output from Windows and AIX machine.

Tags (3)
1 Solution

dshpritz
SplunkTrust
SplunkTrust

Note that the Universal Forwarder does not include bundled Python:

Introducing the Universal Forwarder - Scripted inputs and Python

If you need to run a Python script on a Universal Forwarder, you can install a local version of Python and run the script by using a .bat file. Otherwise you will need to use a full version of Splunk configured as a Lightweight Forwarder.

HTH,

Dave

View solution in original post

linu1988
Champion

bat file should contain the command then, include the bat file in the script stanza.

0 Karma

dshpritz
SplunkTrust
SplunkTrust

Note that the Universal Forwarder does not include bundled Python:

Introducing the Universal Forwarder - Scripted inputs and Python

If you need to run a Python script on a Universal Forwarder, you can install a local version of Python and run the script by using a .bat file. Otherwise you will need to use a full version of Splunk configured as a Lightweight Forwarder.

HTH,

Dave

jangid
Builder

This script perfectly works manually and main splunk instance using it

0 Karma

jangid
Builder

Sorry I forgot to mention here is inputs.conf

[script://$SPLUNK_HOME\etc\system\bin\util.py http://127.0.0.1:8085]
index = main
interval = 30
sourcetype = sys_health
disabled = 0

0 Karma

linu1988
Champion

You can mention in the script stanza of inputs.conf file. But i am not sure how the script will run. For the script to execute it should have the compiler/interpreter. May be you need to set the script to run through python.

See if the script runs manually, then try setting in script stanza OR Manually set the path and run through a bat file. Thanks.

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!

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 ...

Step into “Hunt the Insider: An Splunk ES Premier Mystery” to catch a cybercriminal ...

After a whole week of being on call, you fell asleep on your keyboard, and you hit a sequence of buttons that ...