inserted jobInfo object into service
This commit is contained in:
parent
88638dcede
commit
cf3791a3a9
3 changed files with 45 additions and 38 deletions
|
|
@ -46,12 +46,10 @@ class Job:
|
||||||
self.log.d(self.service)
|
self.log.d(self.service)
|
||||||
try:
|
try:
|
||||||
jobInfo = JobInfo(self.host, self.service)
|
jobInfo = JobInfo(self.host, self.service)
|
||||||
result = self.service._execute(self.host)
|
self.service._execute(jobInfo)
|
||||||
jobInfo.set_result(result)
|
|
||||||
jobInfo.set_execution_successful(self.service.was_execution_successful())
|
|
||||||
jobInfo.set_execution_end()
|
jobInfo.set_execution_end()
|
||||||
|
|
||||||
self.handle_threshold(self.service.get_threshold(), self.service.was_execution_successful())
|
self.handle_threshold(self.service.get_threshold(), jobInfo.was_execution_successful())
|
||||||
|
|
||||||
self.jobInfos.append(jobInfo)
|
self.jobInfos.append(jobInfo)
|
||||||
|
|
||||||
|
|
@ -59,12 +57,18 @@ class Job:
|
||||||
self.log.d(e)
|
self.log.d(e)
|
||||||
|
|
||||||
|
|
||||||
class JobInfo:
|
class JobInfo(object):
|
||||||
def __init__(self, host, service):
|
def __init__(self, host, service):
|
||||||
self.id = generateId()
|
self.id = generateId()
|
||||||
self.host = host
|
self.host = host
|
||||||
self.service = service
|
self.service = service
|
||||||
self.executionBegin = datetime.now()
|
self.executionBegin = datetime.now()
|
||||||
|
self._errorcode = -1
|
||||||
|
self._message = None
|
||||||
|
self._executionSuccess = False
|
||||||
|
|
||||||
|
def get_host(self):
|
||||||
|
return self.host
|
||||||
|
|
||||||
def set_result(self, result):
|
def set_result(self, result):
|
||||||
self.result = result
|
self.result = result
|
||||||
|
|
@ -73,4 +77,19 @@ class JobInfo:
|
||||||
self.executionEnd = datetime.now()
|
self.executionEnd = datetime.now()
|
||||||
|
|
||||||
def set_execution_successful(self, successful):
|
def set_execution_successful(self, successful):
|
||||||
self.executionSuccess = successful
|
self._executionSuccess = successful
|
||||||
|
|
||||||
|
def was_execution_successful(self):
|
||||||
|
return self._executionSuccess
|
||||||
|
|
||||||
|
def set_message(self, msg):
|
||||||
|
self._message = msg
|
||||||
|
|
||||||
|
def get_message(self):
|
||||||
|
return self._message
|
||||||
|
|
||||||
|
def set_errorcode(self, errcode):
|
||||||
|
self._errorcode = errcode
|
||||||
|
|
||||||
|
def get_errorcode(self):
|
||||||
|
return self._errorcode
|
||||||
|
|
@ -37,8 +37,7 @@ class Service(object):
|
||||||
if KEY_PERIODS in kwargs:
|
if KEY_PERIODS in kwargs:
|
||||||
self.add_periods(kwargs[KEY_PERIODS])
|
self.add_periods(kwargs[KEY_PERIODS])
|
||||||
|
|
||||||
self.errorcode = 0
|
|
||||||
self.errormessage = None
|
|
||||||
|
|
||||||
def add_arguments(self, args):
|
def add_arguments(self, args):
|
||||||
for key, val in args.items():
|
for key, val in args.items():
|
||||||
|
|
@ -98,42 +97,31 @@ class Service(object):
|
||||||
def needs_arguments(self):
|
def needs_arguments(self):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def set_execution_successful(self, successful):
|
def _execute(self, jobInfo):
|
||||||
self.executionSuccessful = successful
|
|
||||||
|
|
||||||
def was_execution_successful(self):
|
|
||||||
return self.executionSuccessful
|
|
||||||
|
|
||||||
def _execute(self, host):
|
|
||||||
try:
|
try:
|
||||||
|
self.pre_execute(jobInfo)
|
||||||
|
|
||||||
self.set_execution_successful(True)
|
self.execute(jobInfo)
|
||||||
self.pre_execute(host)
|
self.parse_result(jobInfo)
|
||||||
|
self.post_execute(jobInfo)
|
||||||
result = self.execute(host)
|
|
||||||
parseResult = self.parse_result(result)
|
|
||||||
self.post_execute(parseResult)
|
|
||||||
return parseResult
|
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
self.set_execution_successful(False)
|
self.set_execution_successful(False)
|
||||||
self._threshold -= 1
|
self._threshold -= 1
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
def execute(self, host):
|
def execute(self, jobInfo):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def pre_execute(self, host):
|
def pre_execute(self, jobInfo):
|
||||||
self.errorcode = 0
|
|
||||||
self.errormessage = None
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def parse_result(self, executionResult):
|
def parse_result(self, jobInfo):
|
||||||
|
|
||||||
result = []
|
result = []
|
||||||
for parser in self.get_parser():
|
for parser in self.get_parser():
|
||||||
result.append(parser.parse(executionResult))
|
result.append(parser.parse(jobInfo))
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def post_execute(self, parseResult):
|
def post_execute(self, jobInfo):
|
||||||
pass
|
pass
|
||||||
|
|
|
||||||
|
|
@ -23,25 +23,25 @@ class TcpconnectService(Service):
|
||||||
def needs_arguments(self):
|
def needs_arguments(self):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def execute(self, host):
|
def execute(self, jobInfo):
|
||||||
try:
|
try:
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
except socket.error, msg:
|
except socket.error, msg:
|
||||||
#log.w("%s\n" % msg[1])
|
#log.w("%s\n" % msg[1])
|
||||||
self.errorcode = 1
|
jobInfo.set_errorcode(1)
|
||||||
self.errormessage = "Could not create socket."
|
jobInfo.set_message("Could not create socket.")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
sock.connect((host, self.port))
|
sock.connect((jobInfo.get_host(), self.port))
|
||||||
except socket.error, msg:
|
except socket.error, msg:
|
||||||
#log.w("%s\n" % msg[1])
|
#log.w("%s\n" % msg[1])
|
||||||
self.errorcode = 2
|
jobInfo.set_errorcode(2)
|
||||||
self.errormessage = "Could not establish connection."
|
jobInfo.set_message("Could not establish connection.")
|
||||||
|
|
||||||
print(self.errorcode)
|
if jobInfo.get_errorcode() == -1:
|
||||||
print(self.errormessage)
|
jobInfo.set_execution_successful(True)
|
||||||
sock.close()
|
sock.close()
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
def create(kwargs):
|
def create(kwargs):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue