Splunk Dev

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
Ultra Champion

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
Ultra Champion

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
Ultra Champion

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!

Built-in Service Level Objectives Management to Bridge the Gap Between Service & ...

Wednesday, May 29, 2024  |  11AM PST / 2PM ESTRegister now and join us to learn more about how you can ...

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer at Splunk .conf24 ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

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