-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcheckCronJobRunningTime.py
executable file
·77 lines (70 loc) · 3.59 KB
/
checkCronJobRunningTime.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##
# Tart Monitoring
#
# Copyright (c) 2013, Tart İnternet Teknolojileri Ticaret AŞ
#
# Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby
# granted, provided that the above copyright notice and this permission notice appear in all copies.
#
# The software is provided "as is" and the author disclaims all warranties with regard to the software including all
# implied warranties of merchantability and fitness. In no event shall the author be liable for any special, direct,
# indirect, or consequential damages or any damages whatsoever resulting from loss of use, data or profits, whether
# in an action of contract, negligence or other tortious action, arising out of or in connection with the use or
# performance of this software.
##
from __future__ import with_statement
from __future__ import print_function
from datetime import datetime, timedelta
import argparse
fatalString = "PHP Fatal error"
beginString = "CLI_CONTROLLER_STARTED"
endString = "CLI_CONTROLLER_FINISHED"
optionParser = argparse.ArgumentParser(description='This Python script checks whether a running action begins or ends in a given time period.')
optionParser.add_argument("-c", "--critical", type = int, dest = "criticalLimit", help = "Critical time limit for the actions.")
optionParser.add_argument("-w", "--warning", type = int, dest = "warningLimit", help = "Warning time limit for the actions.")
optionParser.add_argument("-f", "--file", type = str, dest = "logFile", help = "Input log file for checking")
options = optionParser.parse_args()
def checkTimeDifference(row, missingAction):
words = row.split()
controllerName = words[3]
actionName = words[4]
lastTime = words[0] + " " + words[1]
lastTime = lastTime[1:-1]
formattedLastTime = datetime.strptime(lastTime, '%Y-%m-%d %H:%M:%S')
currentTime = datetime.now()
timeDifference = currentTime - formattedLastTime
if (timeDifference >= timedelta(minutes=options.warningLimit) and timeDifference < timedelta(minutes=options.criticalLimit)):
print('warning: ' + controllerName + " controller's " + actionName + " action has a problem. it didn't " + missingAction + " for " + str(timeDifference.seconds/60) + " minutes.")
return 1
elif (timeDifference >= timedelta(minutes=options.warningLimit) and timeDifference >= timedelta(minutes=options.criticalLimit)):
print('critical: ' + controllerName + " controller's " + actionName + " action has a problem. it didn't " + missingAction + " for " + str(timeDifference.seconds/60) + " minutes.")
return 2
else :
print('ok: ' + controllerName + " controller's " + actionName + " cron has been running ok for " + str(timeDifference.seconds/60) + " minutes.")
return 0
def check():
returnValue = 0
checkFlag = 0
fatalLogs = ''
with open(options.logFile,"r") as f:
file = reversed(list(f))
for row in file:
if fatalString in row:
fatalLogs += row
returnValue = max(returnValue, 1)
if beginString in row:
if checkFlag == 1:
break
checkValue = checkTimeDifference(row, "finished")
returnValue = max(checkValue, returnValue)
break
if endString in row:
checkFlag = 1
checkValue = checkTimeDifference(row, "started")
returnValue = max(checkValue, returnValue)
print(fatalLogs,end='')
return returnValue
if __name__ == '__main__':
exit(check())