Пример #1
0
import splunklib.client as client

fargs = {'tpw': '', u'spw': u'pass1', u'cmdtype': u'CLI', u'cmd': u'ls | less', 'tuser': '', u'suser': u'root', u'nodelist': u'nl1.csv'}
sargs = {u'user': u'admin', u'authtoken': u'TV9Y7tmzNilhL^4vWPmhR6HRMRHjmIvw3tSIlJEceDc2r0OW8bMqrIOtx1R2jA3jYD1uGGWQSZkEOWLr1yLNhXlKz^rCXdLOm2ExNKnEMAMpc3kJpI3YOyqpcV9k3LUtMKgO'}

ppid = os.getpid()
# Create configuration
objcfg = configconf(fargs, sargs, ppid)
if objcfg.status != 'Success':
    logging.debug('Error, creating pid object, err = ' + str(objcfg.status))
gc = objcfg.getconfig()

#pdb.set_trace()

# If getconfig unsuccessful, log errmsg and exit script
if gc != 'Success':
    logging.debug('Error, getconfig function, err = ' + str(gc))
    sys.exit(1)

logging.debug('endpoint: objcfg.hosts[0] = ' + str(objcfg.hosts[0]))


binding_service = binding.connect(token=sargs['authtoken'])
client_service = client.connect(token=sargs['authtoken'])
sk = cliauto_kvstore(binding_service)

sc = sshcom(fargs, sargs, ppid, sk, binding_service, client_service, objcfg)
r = sc.process_cmd()
print ("sc.response = " + str(sc.response))

def get_any_item(items):
    for item in items:
        return item

fargs = {'tpw': '', u'spw': u'spw2', u'cmdtype': u'CLI', u'cmd': u'uptime', 'tuser': '', u'suser': u'su2', u'nodelist': u'nl2'}
sargs = {u'user': u'admin', u'authtoken': u'S6A1zabz^k4vP1jZB9RaVvDQXITZmjMos4b7YMPME3JDc7lwr9prD9qvqfhhZM6WW2AQaJgA356o_PFfmhfKIsLd0BeOnaXSbl9SCk30NysExL1ZgRIRZorHPNfX'}
cur_parallel_ssh_results = {}
cur_parallel_ssh_results['127.0.0.1'] = {'resultstatus': 'Test', 'iteration': 1, 'host': 'Splunk_Training', 'ip_address': '127.0.0.1', 'result': 'test', 'resultraw': 'total 8\r\n-rw-------. 1 root root 1575 Sep 19 09:24 anaconda-ks.cfg\r\ndrwxr-xr-x. 2 root root    6 Sep 19 09:28 Desktop\r\ndrwxr-xr-x. 2 root root    6 Sep 19 09:28 Documents\r\ndrwxr-xr-x. 4 root root  199 Sep 19 12:02 Downloads\r\n-rw-r--r--. 1 root root 1623 Sep 19 09:27 initial-setup-ks.cfg\r\ndrwxr-xr-x. 2 root root    6 Sep 19 09:28 Music\r\ndrwxr-xr-x. 2 root root    6 Sep 19 09:28 Pictures\r\ndrwxr-xr-x. 2 root root    6 Sep 19 09:28 Public\r\ndrwxr-xr-x. 2 root root    6 Sep 19 09:28 Templates\r\ndrwxr-xr-x. 2 root root    6 Sep 19 09:28 Videos\r\n'}

print str(type(cur_parallel_ssh_results))
print (str(type(cur_parallel_ssh_results['127.0.0.1'])))
print("cur_parallel_ssh_results[host] = " + str(cur_parallel_ssh_results['127.0.0.1']))

pdb.set_trace()
serviceb = binding.connect(token=sargs['authtoken'])
sk = cliauto_kvstore(serviceb)
gk = sk.get_items("/servicesNS/nobody/cliauto/storage/collections/data/cliauto_pid_collection")
print ("gk[0] = " + str(gk[0]))
#print ("gk[1] = " + str(gk[1]))
item = get_any_item(gk[1])

data = str(cur_parallel_ssh_results['127.0.0.1']['result'])
#data = "test1"
print("data = " + str(data))
service = client.connect(token=sargs['authtoken'])
pdb.set_trace()
#ci = cliauto_index(service, "main", sargs)
ci = cliauto_index(service, "main")
if ci.status != "Success":
    print("Unable to create cliauto_index to write results to index")
Пример #3
0
    def __init__(self, fargs, sargs, cargs, ppid, objcfg):

        # Processing the request
        try:
            self.response = json.dumps({ 'payload': 'Error, Default pid response',
                                         'status': 400  # HTTP status code
                                      })

            self.fargs = fargs
            self.sargs = sargs
            self.cargs = cargs
            self.ppid = ppid
            self.pids = []
            self.objcfg = objcfg

            logging.info('Starting processing request...')

            # Development - cmdtype=LRP is to gain understanding of Splunk REST API python process life cycle
            if self.fargs['cmdtype'].lower() == 'lrp':
                logging.info('Success, long running process disabled')
                self.response = json.dumps({ 'payload': 'Success, long running process disabled',
                                             'status': 200  # HTTP status code
                                          })
                return None

                #host_list = ['h1', 'h2']
                #r = self.parallel_lrp(host_list, self.fargs, self.sargs)
                #logging.info('Success, started long running process')
                #self.response = json.dumps({ 'payload': 'Success, started long running process',
                #                             'status': 200  # HTTP status code
                #                          })
                #return None

            # Process cmdtype = status
            if self.fargs['cmdtype'].lower() == 'status':
                binding_service = binding.connect(token=self.sargs['authtoken'])
                self.sk = cliauto_kvstore(binding_service)

                if self.sk.status != 'Success':
                    self.response = json.dumps({ 'payload': str(self.sk.status),
                                                 'status': 400  # HTTP status code
                                              })
                    return None

                # Get the PID status
                gps = self.get_pid_status()

                # If the PID Status = Ready or Busy then set response status = 200
                # Else set response status = 400
                if gps[:5] == 'Ready' or gps[:4] == 'Busy':
                    self.response = json.dumps({ 'payload': str(gps),
                                                 'status': 200  # HTTP status code
                                              })
                else:
                    self.response = json.dumps({ 'payload': str(gps),
                                                 'status': 400  # HTTP status code
                                              })

                return None

            # Process cmdtype = cli*
            if self.fargs['cmdtype'][:3].lower() == 'cli':
                client_service = client.connect(token=self.sargs['authtoken'])
                binding_service = binding.connect(token=self.sargs['authtoken'])
                self.sk = cliauto_kvstore(binding_service)

                if self.sk.status != 'Success':
                    self.response = json.dumps({ 'payload': str(self.sk.status),
                                                 'status': 400  # HTTP status code
                                              })
                    return None

                # Get the PID status
                gps = self.get_pid_status()

                # If the PID Status = Ready & cmdtype = CLI then execute requested cmd
                # Else set response to PID status
                if gps == 'Ready':

                    ssc = sshcom(self.fargs, self.sargs, self.cargs, self.ppid, self.sk, binding_service, client_service, self.objcfg)
                    if ssc.status != 'Success':
                        self.response = ssc.response
                        return None

                    pc = ssc.process_cmd()
                    if pc != 'Success':
                        self.response = json.dumps({ 'payload': str(pc),
                                                     'status': 400  # HTTP status code
                                                  })
                        return None

                else:
                    self.response = json.dumps({ 'payload': str(gps),
                                                 'status': 400  # HTTP status code
                                              })
                    return None

                self.response = json.dumps({ 'payload': 'Success, started processing cli command',
                                             'status': 200  # HTTP status code
                                          })
                return None

            # Process unknown cmdtype
            else:
                self.response = json.dumps({ 'payload': 'Unknown cmdtype',
                                             'status': 400  # HTTP status code
                                          })
        except Exception as err:
            logging.error('Error, pid - processing request; err = ' + str(err))
            self.response = json.dumps({ 'payload': 'Error, Processing the request; err = ' + str(err),
                                         'status': 400  # HTTP status code
                                      })