<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Using splunklib.modularinput without making a class in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Using-splunklib-modularinput-without-making-a-class/m-p/490814#M194163</link>
    <description>&lt;P&gt;I've code that looks like this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;#!/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()
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;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?&lt;/P&gt;

&lt;P&gt;TIA,&lt;BR /&gt;
Joe&lt;/P&gt;</description>
    <pubDate>Wed, 30 Sep 2020 02:21:13 GMT</pubDate>
    <dc:creator>jwhughes58</dc:creator>
    <dc:date>2020-09-30T02:21:13Z</dc:date>
    <item>
      <title>Using splunklib.modularinput without making a class</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-splunklib-modularinput-without-making-a-class/m-p/490814#M194163</link>
      <description>&lt;P&gt;I've code that looks like this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;#!/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()
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;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?&lt;/P&gt;

&lt;P&gt;TIA,&lt;BR /&gt;
Joe&lt;/P&gt;</description>
      <pubDate>Wed, 30 Sep 2020 02:21:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-splunklib-modularinput-without-making-a-class/m-p/490814#M194163</guid>
      <dc:creator>jwhughes58</dc:creator>
      <dc:date>2020-09-30T02:21:13Z</dc:date>
    </item>
    <item>
      <title>Re: Using splunklib.modularinput without making a class</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-splunklib-modularinput-without-making-a-class/m-p/490815#M194164</link>
      <description>&lt;P&gt;From an email exchange with Splunk Development.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;You’ve defined a scripted input, which is not compatible with the modular inputs framework - &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/AdvancedDev/ScriptedInputsIntro"&gt;https://docs.splunk.com/Documentation/Splunk/latest/AdvancedDev/ScriptedInputsIntro&lt;/A&gt;&lt;BR /&gt;
The Splunk SDKs are not designed to work with scripted inputs, so we are not able to assist with questions about scripted inputs.&lt;BR /&gt;
Please read the above and other documentation on docs.splunk.com.&lt;/STRONG&gt;&lt;/P&gt;

&lt;P&gt;And I also got this link which is the best explanation of why.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;&lt;A href="http://dev.splunk.com/view/python-sdk/SP-CAAAER3"&gt;http://dev.splunk.com/view/python-sdk/SP-CAAAER3&lt;/A&gt;&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Oct 2019 20:32:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-splunklib-modularinput-without-making-a-class/m-p/490815#M194164</guid>
      <dc:creator>jwhughes58</dc:creator>
      <dc:date>2019-10-01T20:32:45Z</dc:date>
    </item>
  </channel>
</rss>

