## Get the Current time variable in resemble to Opsview event time format
curTime = time.time() - options.duration
curTime = time.ctime(curTime)
curTime = time.strptime(curTime, "%a %b  %d %H:%M:%S %Y")
curTime = time.strftime("%Y-%m-%d %H:%M:%S", curTime)
## Get and clean data then analyze it with specific options to put it to ticket.fpt.net
## Get Critical event with host_state is "down" and then with host_state is "up"
print curTime
print options.duration
dataFile = []
try:
    key = Ops.opspostsend(url=options.host, user=options.username, password=options.password)
    print "your session key is : ", key
#    dataFile = ReqEvent.request_rest_serverinfo(token=key, username=options.username, host=options.host, raw=1)
    dataFile = ReqEvent.request_rest_status_hostgroup(token=key, username=options.username, host=options.host, raw=1)
except IOError:
    print IOError
except SyntaxError:
    print SyntaxError
except:
    sys.exit("no data found")
## This block is using to record to log file and send update to Opsview
## This block is using to display data as easy to read by user of Opsview serverinfo
#print dataFile
#ReqEvent.clean_data_rest_serverinfo(dataFile)

def analyze_result_rest_status_hostgroup(rawdata):
    dataFile = json.loads(rawdata)
    if dataFile["summary"] is not None:
        print "summary unhandled alert = "+dataFile["summary"]["unhandled"]
# Main program

## Get the Current time variable in resemble to Opsview event time format
curTime = time.time() - options.duration
curTime = time.ctime(curTime)
curTime = time.strptime(curTime, "%a %b  %d %H:%M:%S %Y")
curTime = time.strftime("%Y-%m-%d %H:%M:%S", curTime)
## Get and clean data then analyze it with specific options to put it to ticket.fpt.net
## Get Critical event with host_state is "down" and then with host_state is "up"
print curTime
print options.duration
dataFile = []
try:
    key = Ops.opspostsend(url=options.host, user=options.username, password=options.password)
    print "your session key is : ", key
    dataFile = ReqEvent.request_rest_event_opsview_hostgroup_hardstate(token=key, username=options.username, params=options.params, host=options.host, host_group=options.host_group, host_state=options.host_state, state_type=options.state_type, raw=0)
except IOError:
    print IOError
except SyntaxError:
    print SyntaxError
except:
    sys.exit("no data found")
## This block is using to record to log file and send update to Opsview
## This block also send out stdout alarms of host down from opsviews

if (dataFile is not None):
    dataMatchCritical = ReqEvent.interpret_rest_event_splunk_all_host_state(dataFile=dataFile, duration=options.duration)

    if options.autologfile:
        ReqEvent.splunk_open_and_write_log("host_state_log_"+options.logserver, dataMatchCritical, path=options.logfolder)
    else:
## Get the Current time variable in resemble to Opsview event time format
curTime = time.time() - options.duration
curTime = time.ctime(curTime)
curTime = time.strptime(curTime, "%a %b  %d %H:%M:%S %Y")
curTime = time.strftime("%Y-%m-%d %H:%M:%S", curTime)
## Get and clean data then analyze it with specific options to put it to ticket.fpt.net
## Get Critical event with host_state is "down" and then with host_state is "up"
print curTime
print options.duration
dataFile = []
try:
    key = Ops.opspostsend(url=options.host, user=options.username, password=options.password)
    print "your session key is : ", key
    dataFile = ReqEvent.request_rest_status(
        token=key, username=options.username, params=options.params, host=options.host, hostgroupid="20"
    )
except IOError:
    print IOError
except SyntaxError:
    print SyntaxError
except:
    sys.exit("no data found")
## This block is using to record to log file and send update to Opsview
## This block also send out stdout alarms of host down from opsviews
print dataFile
if dataFile is not None:
    dataMatchCritical = ReqEvent.interpret_rest_status_service(dataFile=dataFile)

    if options.autologfile:
        ReqEvent.splunk_open_and_write_log(