def request_rest_event_splunk(token, username, host, params, service_state, host_group, raw):
    if params == "":
        if not host_group or not service_state:
            print "you don't have startTime and host_group information for request"
            sys.exit()
        else:
            for each in ["ok", "warning", "critical", "unknown"]:
                if service_state == each:
                    params += "service_state=" + service_state + "&"
                else:
                    continue
            params += "host_group=" + str(host_group) + "&"
    else:
        pass

    params += "cols=state&cols=hostname&cols=time&cols=output"
    url = "https://" + host + "/rest/event?" + params + "&format_datetime=1"
    print "request url is", url
    if raw == 0:
    # get data from API as dictionary format
        dataFile = Ops.opsgetsend(url, user=username, key=token)
    elif raw == 1:
    # get data from API as json raw format
        dataFile = Ops.opsgetsend_rawjson(url, user=username, key=token)
        #print dataFile
    return dataFile
def request_rest_event_splunk_all(token, username, host, params, raw):
    params += "cols=state&cols=hostname&cols=time&cols=output"
    url = "https://" + host + "/rest/event?" + params + "&format_datetime=1"
    print "request url is", url
    if raw == 0:
    # get data from API as dictionary format
        dataFile = Ops.opsgetsend(url, user=username, key=token)
    elif raw == 1:
    # get data from API as json raw format
        dataFile = Ops.opsgetsend_rawjson(url, user=username, key=token)
        #print dataFile
    return dataFile
def getHostIP(host, hostid, username, token):
    url = "https://" + host + "/rest/config/host/"+hostid
    print url
    dataFile = Ops.opsgetsend_rawjson(url=url, user=username, key=token)
    dataFile = json.loads(dataFile)
    print dataFile["object"]["ip"]
    return(dataFile["object"]["ip"])
def request_rest_event_splunk_host_state(token, username, host, params, host_state, state_type, raw):
    if not host_state or not state_type:
        print "you have to input host_state in function request_rest_event_splunk_host_state"
        sys.exit()
    else:
        params += "host_state=" + host_state + "&"+"state_type="+state_type+"&"
    params += "cols=state&cols=hostname&cols=time&cols=output"
    url = "https://" + host + "/rest/event?" + params + "&format_datetime=1"
    print "request url is", url
    if raw == 0:
    # get data from API as dictionary format
        dataFile = Ops.opsgetsend(url, user=username, key=token)
    elif raw == 1:
    # get data from API as json raw format
        dataFile = Ops.opsgetsend_rawjson(url, user=username, key=token)
        #print dataFile
    return dataFile
def request_rest_status(token, username, params, state, servicetype, host, hostgroupid):
    reqservicetype = ""
    if params == "":
        if servicetype == 0:
            reqservicetype = "status/service"
            if not state and not hostgroupid:
                print "you don't have state and hostgroupid information for request"
                sys.exit()
            else:
                params = "state="+str(state)+"&"+"hostgroupid="+str(hostgroupid)
        elif servicetype == 1:
            reqservicetype = "status/host"
            if not state and not hostgroupid:
                print "you don't have state and hostgroupid information for request"
                sys.exit()
            else:
                params = "state="+str(state)+"&"+"hostgroupid="+str(hostgroupid)
        elif servicetype == 2:
            reqservicetype = "status/hostgroup"
            if not state and not hostgroupid:
                print "you don't have state and hostgroupid information for request"
                sys.exit()
            else:
                params = "state="+str(state)+"&"+"hostgroupid="+str(hostgroupid)
        elif servicetype == 3:
            reqservicetype = "status/viewport"
            if not state and not hostgroupid:
                print "you don't have state and hostgroupid information for request"
                sys.exit()
            else:
                params = "state="+str(state)+"&"+"hostgroupid="+str(hostgroupid)
        elif servicetype == 4:
            reqservicetype = "status/performancemetric"
            if not state and not hostgroupid:
                print "you don't have state and hostgroupid information for request"
                sys.exit()
            else:
                params = "state="+str(state)+"&"+"hostgroupid="+str(hostgroupid)
        else:
            print "error params"
            sys.exit()
    else:
        pass
    url = "https://"+host+"/rest/"+reqservicetype+"?"+params+"&format_datetime=1"
    print "request url is", url
    dataFile = Ops.opsgetsend(url, user=username, key=token)
    #print dataFile
    if servicetype == 0:
        interpret_rest_status_service(dataFile)
    elif servicetype == 1:
        interpret_rest_status_host(dataFile)
    elif servicetype == 2:
        interpret_rest_status_hostgroup(dataFile)
    else:
        sys.exit("Service Type is not support by program")
Exemplo n.º 6
0
#options.params = "state=2&hostgroupid=505"
options.state = 2
options.hostgroupid = 462
options.type = 0
options.service_state = "critical"
options.host_group = 462

# Check for required options
for option in ('host', 'username', 'password'):
    if not getattr(options, option):
        print 'Option %s not specified' % option
        parser.print_help()
        sys.exit()

# Main program
key = Ops.opspostsend(url=options.host, user=options.username, password=options.password)
print "your session key is : ", key
#ReqStatus.request_rest_status(token=key,username=options.username,params=options.params,state=options.state,servicetype=options.type,host=options.host,hostgroupid=options.hostgroupid)
# binding data and interpret as dictionary format
if key is not None:
    dataFile = ReqEvent.request_rest_event(token=key, username=options.username, params=options.params, service_state=options.service_state, host=options.host, host_group=options.host_group, raw=0)
#ReqEvent.interpret_rest_event(dataFile=dataFile)
    data_frame = ReqEvent.interpret_rest_event_pandas_to_dataFrame(dataFile=dataFile)
else:
    sys.exit("there is no key to get data")
#print data_frame.groupby(['hostname', 'servicename'])
# some specific dataframe view information and group data
# ex:  print data_frame
# ex:  print data_frame.head(n=10)
# ex:  print data_frame.groupby('hostname').apply(lambda subf: subf['servicename'])
# ex:  print data_frame.groupby('hostname').apply(lambda subf: subf['time'])