-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_retry
More file actions
81 lines (68 loc) · 2.22 KB
/
check_retry
File metadata and controls
81 lines (68 loc) · 2.22 KB
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
77
78
79
80
81
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
import sys
import os
import subprocess
import time
#-------------------------------------------------------------------------------
_candebug = False
def candebug():
global _candebug
return _candebug
def setcandebug(value):
global _candebug
_candebug = value
def infomsg(msg):
if candebug() == True:
print(msg, flush=True)
def mapstatustoexitcode(status):
if status=="OK":
exitcode = 0
elif status=="WARNING":
exitcode = 1
elif status=="CRITICAL":
exitcode = 2
elif status=="UNKNOWN":
exitcode = 3
else:
exitcode = 4
return exitcode
def exitnagios(status,message):
exitcode = mapstatustoexitcode(status)
print(status+": "+message, flush=True)
sys.exit(exitcode)
#-------------------------------------------------------------------------------
def doretrycall(cmdline):
counter = 0
retries = 3
while True:
counter = counter+1
infomsg(str(cmdline))
completedproc = subprocess.run(cmdline,capture_output=True)
output = completedproc.stdout.decode("utf-8").strip()
errors = completedproc.stderr.decode("utf-8").strip()
exitcode = completedproc.returncode
if exitcode == 0:
if output!="":
print(output, file=sys.stdout, flush=True, end="")
if errors!="":
print(errors, file=sys.stderr, flush=True, end="")
exit(0)
elif counter>=retries:
if output!="":
print(output, file=sys.stdout, flush=True, end="")
if errors!="":
print(errors, file=sys.stderr, flush=True, end="")
exit(exitcode)
else:
time.sleep(10)
#-------------------------------------------------------------------------------
def main(forcedargs=None):
if forcedargs==None:
forcedargs = sys.argv[1:]
cmdline = forcedargs
doretrycall(cmdline)
#-------------------------------------------------------------------------------
if __name__ == "__main__":
main()
#-------------------------------------------------------------------------------