connect_to_splunk()恰好接受2个参数(给定1个)

时间:2019-08-04 09:40:30

标签: python

Splunk_WAN_Methods

class Splunk_Methods(object):

    def __init__(self, username, password, hostname, port):
        self.count = 0  # number of devices to get data for. "0" means no limit
        self.HOST = hostname
        self.PORT = port
        self.USERNAME = username
        self.PASSWORD = password

    def connect_to_splunk(self, app):
        '''
        Purpose: Connects to splunk and returns connection_id object handler.
        Input: none
        Output: connection object handler
        '''
        splunk_connection_service = client.connect(
            host=self.HOST,
            port=self.PORT,
            username=self.USERNAME,
            password=self.PASSWORD,
            OWNER="wanro",
            app=app,  # app context has to be defined for get_device_hostname function to work
            verify=False)  # to disable ssl check

        return splunk_connection_service

Main.py

from Splunk_WAN_Methods import Splunk_Methods

if __name__ == '__main__':
    # Connect to splunk
    LOCAL_DIR = os.path.dirname(os.path.realpath(__file__))
    DIR_PATH = os.path.abspath(os.path.join(LOCAL_DIR, os.pardir))
    print DIR_PATH
    sys.path.append(DIR_PATH)

    print LOCAL_DIR
    print DIR_PATH

    with open("%s\environment.conf" % LOCAL_DIR, 'r') as config_file:
        CONFIG_DATA = json.load(config_file)
        print CONFIG_DATA

    ENVIRONMENT = "wan"
    SPLUNK_VARS = CONFIG_DATA[ENVIRONMENT]['splunk']
    #print SPLUNK_VARS

    splunk_api_instance = Splunk_Methods(SPLUNK_VARS['username'],SPLUNK_VARS['password'],SPLUNK_VARS['hostname'],SPLUNK_VARS['port'] )
    #print splunk_api_instance

    splunk_connection = splunk_api_instance.connect_to_splunk('wansa_dashboard_app')
    #print splunk_connection

    splunk_api_instance.get_saved_search_result(splunk_connection, 'ospf_nbrs')
    print splunk_connection
    print SPLUNK_VARS

    class_splunk_methods = Splunk_Methods('username', 'password', 'hostname', 'port')
    try:
        splunk_connection_service = class_splunk_methods.connect_to_splunk()
    except  Exception as e:
        print "Could not connect to Splunk"
        print e
        sys.exit(1)

    #Get todays date
    now = datetime.datetime.now()
    today_date = now.strftime("%Y-%m-%d %H:%M:%S") #2018-09-13 13:02:39

结果有误

C:\Users\U6067069\eclipse-workspace\Int_DESC_NEW\Unit_test_audit\network_compliance_engine1\code
C:\Users\U6067069\eclipse-workspace\Int_DESC_NEW\Unit_test_audit\network_compliance_engine1
{u'wan': {u'splunk': {u'username': u'xxx', u'password': u'xxx', u'hostname': u'xxx', u'xxx': u'xxx'}}}

"uk2-corewan-p3","Bundle-Ether3","ld4-corewan-p1","Bundle-Ether3"
"uk1-corewan-pe3","TenGigE0/1/1/0","uk2-corewan-p3","TenGigE0/0/1/2"
"uk2-corewan-p3","TenGigE0/0/1/2","uk1-corewan-pe3","TenGigE0/1/1/0"
"lo5-corewan-p1","FortyGigE0/0/0/1","uk2-corewan-p3","FortyGigE0/4/0/1"

<splunklib.client.Service object at 0x04074B30>
Could not connect to Splunk
connect_to_splunk() takes exactly 2 arguments (1 given)

1 个答案:

答案 0 :(得分:0)

def __init__(self, username, password, hostname, port):

这意味着在实例化Splunk_Methods()时必须传递所有这些参数,例如

Splunk_Methods('user','pass','google.com',1337)
相关问题