Splunk Dev

Help needed with Custom Command

bloodstrife
Engager

Hi there, I have just started working on Splunk and is totally new to Python.
For my situation, I would like to create a custom command just like any other commands(e.g. Top/ Rare), I would like my custom command to display the current system time.

here is my Python script:
CurrentTime.py

import time;

localtime = time.localtime(time.time())
print "Local current time :", localtime

And here is my commands.conf

[CurrentTime]
type = python
filename = CurrentTime.py
streaming = false
generating = false

Not sure if the above codes are correct, but if they were functional, what should I do to actually get it to display the current system time on my Splunk app?

Tags (2)
0 Karma

jonuwz
Influencer

Or you could just do this to get the exact time the event was processed (different for each event)

| eval current_time=time()

Or this to get the time the search was kicked off:

| eval current_time=now()

0 Karma

MuS
SplunkTrust
SplunkTrust

Hi bloodstrife

after you put your script into etc/apps/YourApp/bin folder and placed the commands.conf into etc/apps/YourApp/default folder, restart Splunk and switch to YourApp. There simply enter the following search command:

  | CurrentTime

and this will run your command.

But I tested your script and the output is none. Here is a quick and dirty rewrite to get it printing current time in csv like output that Splunk likes:

import time;
mytime = time.localtime(time.time())
print "my_year ,my_mon ,my_mday ,my_hour ,my_min ,my_sec ,my_wday ,my_yday" 
print "%s, %s, %s ,%s ,%s ,%s ,%s ,%s" % (mytime.tm_year, mytime.tm_mon, mytime.tm_mday, mytime.tm_hour, mytime.tm_min, mytime.tm_sec, mytime.tm_wday, mytime.tm_yday)

I'm pretty sure there are better ways to do this in python 😉
But nevertheless hope that helps ....

Cheers, MuS

HiroshiSatoh
Champion

It is output by a screen if I make modifications in this way.
Please learn it to see a document and a sample. I think that it is simple and can refer to uniq.py of the search.

CurrentTime.py

import time
from splunk.Intersplunk import getOrganizedResults, outputResults, getKeywordsAndOptions
results, dummy, settings = getOrganizedResults()

#localtime = time.localtime(time.time())
localtime = time.asctime(time.localtime())

results = []
event = {}
event['message'] = "Local current time :" + localtime
results.append(event)
outputResults(results)

alt text

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!

Index This | What travels the world but is also stuck in place?

April 2026 Edition  Hayyy Splunk Education Enthusiasts and the Eternally Curious!   We’re back with this ...

Discover New Use Cases: Unlock Greater Value from Your Existing Splunk Data

Realizing the full potential of your Splunk investment requires more than just understanding current usage; it ...

Continue Your Journey: Join Session 2 of the Data Management and Federation Bootcamp ...

As data volumes continue to grow and environments become more distributed, managing and optimizing data ...