Splunk Search

Using splunklib.modularinput without making a class

jwhughes58
Contributor

I've code that looks like this

#!/usr/bin/env python
#
#########################################################################
# Program    : verodin_get_jobs.py
# Purpose    :
# Programmer : Joe Hughes
#
#########################################################################

from __future__ import print_function

#########################################################################
# Imports for adding Splunk paths
#########################################################################
#
import os
import sys

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "lib"))

#########################################################################
# Froms
#########################################################################
#
from splunklib.modularinput import *

#########################################################################
# Imports
#########################################################################
#
import json
import logging
import optparse
import re
import requests
import splunk.entity as entity
import time

try:
    from splunk.clilib.bundle_paths import make_splunkhome_path
except ImportError:
    from splunkappserver.mrsparkle.lib.util import make_splunkhome_path

formatter = logging.Formatter('%(asctime)s %(name)s: PID=%(process)s [%(threadName)s] %(levelname)s: - %(message)s', "%Y-%m-%d %H:%M:%S")
log = logging.getLogger('verodin_get_jobs')

#########################################################################
# Constants
#########################################################################
#

#########################################################################
# Globals
#########################################################################
#
USER='joseph.hughes@kp.org'

#########################################################################
# Functions
#########################################################################

################
# getCredentials
################
#
def getCredentials(sessionKey, user):
    verodin = 'TA-kp_verodin'

    print(user, sessionKey)

    try:
      user=inputs.inputs.values()[0]['apiUser']
    except:
      log.error(" "+instance_name+" Unable to pull apiUser from inputs.conf, exiting")
      raise Exception(" Unable to pull apiUser from inputs.conf")

    try:
        entities = entity.getEntities(['admin', 'passwords'], namespace=verodin, owner='nobody', sessionKey=sessionKey)
    except Exception, e:
        raise Exception(" Could not get %s credentials from splunk. Error %s" % (verodin, str(e)))

    for i, c in entities.items():
        if c['username'] == user:
            return c['username'], c['clear_password']

    raise Exception(" No credentials found for user. Check that apiUser in inputs.conf matches the user in passwords.conf.")


###########
# main
###########
#
def main():
    print("Starting")
    sessionKey = _input_definition.metadata["session_key"]
    user = USER
    getCredentials(sessionKey, user)
    print("Ending")


#########################################################################
# Main Program
#########################################################################
#
if __name__ == '__main__':
    main()

What I haven't figured out how to do is call _input_definition.metadata["session_key"] without turning this into a class which I'm trying to avoid. I might be tripping up over python syntax. Any thoughts?

TIA,
Joe

0 Karma
1 Solution

jwhughes58
Contributor

From an email exchange with Splunk Development.

You’ve defined a scripted input, which is not compatible with the modular inputs framework - https://docs.splunk.com/Documentation/Splunk/latest/AdvancedDev/ScriptedInputsIntro
The Splunk SDKs are not designed to work with scripted inputs, so we are not able to assist with questions about scripted inputs.
Please read the above and other documentation on docs.splunk.com.

And I also got this link which is the best explanation of why.

http://dev.splunk.com/view/python-sdk/SP-CAAAER3

View solution in original post

0 Karma

jwhughes58
Contributor

From an email exchange with Splunk Development.

You’ve defined a scripted input, which is not compatible with the modular inputs framework - https://docs.splunk.com/Documentation/Splunk/latest/AdvancedDev/ScriptedInputsIntro
The Splunk SDKs are not designed to work with scripted inputs, so we are not able to assist with questions about scripted inputs.
Please read the above and other documentation on docs.splunk.com.

And I also got this link which is the best explanation of why.

http://dev.splunk.com/view/python-sdk/SP-CAAAER3

0 Karma
Get Updates on the Splunk Community!

Introducing Splunk Enterprise 9.2

WATCH HERE! Watch this Tech Talk to learn about the latest features and enhancements shipped in the new Splunk ...

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...