Building for the Splunk Platform

With the Splunk Python SDK, how do I reload deploy client with 2.7?

daniel333
Builder

All,

I am just the worst at Python, so forcing myself to use it more. I can make a connection and list the apps per the SDK example. But that is as far as I have gotten.

Looking to call my deployment server and reload deploy class
1) How do I make that API?
2) How would I find the name of the method for reloading the deploy class

0 Karma
1 Solution

harsmarvania57
SplunkTrust
SplunkTrust

Hi @daniel333,

You can try below script to reload deployment server using Python SDK.

import sys
sys.path.append('splunk-sdk-python-1.6.5')
import splunklib.six as six
import urllib
from xml.etree import ElementTree
import getpass

HOST = raw_input("Enter splunk server hostname/ip: ")
PORT = 8089
splunkUser = raw_input("Enter Splunk Admin Username: ")
splunkPassword = getpass.getpass("Enter Splunk Admin Password: ")
sc = raw_input("Enter Serverclass name to reload OR provide hit Enter to reload all server classes: ")

connection = six.moves.http_client.HTTPSConnection(HOST, PORT)
body = urllib.urlencode({'username': splunkUser, 'password': splunkPassword})
headers = {'Content-Type': "application/x-www-form-urlencoded",
           'Host': HOST
          }

connection.request("POST", "/services/auth/login", body, headers)
response = connection.getresponse()
content = response.read()
connection.close()

session_key = ElementTree.XML(content).findtext("./sessionKey")

connection = six.moves.http_client.HTTPSConnection(HOST, PORT)
headers = {'Content-Type': "application/x-www-form-urlencoded",
           'Host': HOST,
           'Authorization': "Splunk %s" % session_key
          }

if not sc:
    body = ''
else:
    body =  urllib.urlencode({"serverclass": sc})

connection.request("POST", "/services/deployment/server/config/_reload", body, headers)

response = connection.getresponse()
content = response.read()
connection.close()

if response.status == 200:
    if not sc:
        print 'Deployment Server reloaded successfully'
    else:
        print 'Deployment server for class ' + sc + ' reloaded successfully'
else:
    print 'Deployment Server reload failed'
    print content

if you want to reload whole deployment server then just Hit Enter while asking for Serverclass name otherwise provide serverclass name

For example:

Enter splunk server hostname/ip: mysplunkserver
Enter Splunk Admin Username: admin
Enter Splunk Admin Password:
Enter Serverclass name to reload OR provide hit Enter to reload all server classes: myServerClass

Above example reload myServerClass server class.

View solution in original post

harsmarvania57
SplunkTrust
SplunkTrust

Hi @daniel333,

You can try below script to reload deployment server using Python SDK.

import sys
sys.path.append('splunk-sdk-python-1.6.5')
import splunklib.six as six
import urllib
from xml.etree import ElementTree
import getpass

HOST = raw_input("Enter splunk server hostname/ip: ")
PORT = 8089
splunkUser = raw_input("Enter Splunk Admin Username: ")
splunkPassword = getpass.getpass("Enter Splunk Admin Password: ")
sc = raw_input("Enter Serverclass name to reload OR provide hit Enter to reload all server classes: ")

connection = six.moves.http_client.HTTPSConnection(HOST, PORT)
body = urllib.urlencode({'username': splunkUser, 'password': splunkPassword})
headers = {'Content-Type': "application/x-www-form-urlencoded",
           'Host': HOST
          }

connection.request("POST", "/services/auth/login", body, headers)
response = connection.getresponse()
content = response.read()
connection.close()

session_key = ElementTree.XML(content).findtext("./sessionKey")

connection = six.moves.http_client.HTTPSConnection(HOST, PORT)
headers = {'Content-Type': "application/x-www-form-urlencoded",
           'Host': HOST,
           'Authorization': "Splunk %s" % session_key
          }

if not sc:
    body = ''
else:
    body =  urllib.urlencode({"serverclass": sc})

connection.request("POST", "/services/deployment/server/config/_reload", body, headers)

response = connection.getresponse()
content = response.read()
connection.close()

if response.status == 200:
    if not sc:
        print 'Deployment Server reloaded successfully'
    else:
        print 'Deployment server for class ' + sc + ' reloaded successfully'
else:
    print 'Deployment Server reload failed'
    print content

if you want to reload whole deployment server then just Hit Enter while asking for Serverclass name otherwise provide serverclass name

For example:

Enter splunk server hostname/ip: mysplunkserver
Enter Splunk Admin Username: admin
Enter Splunk Admin Password:
Enter Serverclass name to reload OR provide hit Enter to reload all server classes: myServerClass

Above example reload myServerClass server class.

prakhersinghal
Explorer

Thanks. It was helpful.

0 Karma

daniel333
Builder

Wow! Exactly what I was trying to build.

1) I see you're not importing the SDK, does that not offer the functionality ?
2) I see you used this endpoint,how would I have found that on my own? /services/deployment/server/config/_reload

0 Karma

harsmarvania57
SplunkTrust
SplunkTrust

Hi,

  1. I am importing Splunk SDK for Python in my script, see sys.path.append('splunk-sdk-python-1.6.5')
  2. To use different Splunk REST API start with this documentation , if you will not able to find required REST API in documentation then run test splunk instance and do your work using CLI or Splunk Web and then check $SPLUNK_HOME/var/log/splunk/splunkd_access.log to find which REST API Splunk fired, you will get API but what parameter you need to pass for those API is trail and error method.
0 Karma
Get Updates on the Splunk Community!

Splunk Training for All: Meet Aspiring Cybersecurity Analyst, Marc Alicea

Splunk Education believes in the value of training and certification in today’s rapidly-changing data-driven ...

The Splunk Success Framework: Your Guide to Successful Splunk Implementations

Splunk Lantern is a customer success center that provides advice from Splunk experts on valuable data ...

Investigate Security and Threat Detection with VirusTotal and Splunk Integration

As security threats and their complexities surge, security analysts deal with increased challenges and ...