-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkStatus.py
63 lines (51 loc) · 2.18 KB
/
checkStatus.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
import sys
sys.path.append('py-libs')
import requests, json, argparse
parser = argparse.ArgumentParser(description='Checks the status of Go agents.')
parser.add_argument('--hostname', help='The hostname for the Go server', required=True)
parser.add_argument('--port', help='The port for the Go server', required=False, default=80)
parser.add_argument('--username', help='The username to use for API requests', required=False, default="")
parser.add_argument('--password', help='The password to use for API requests', required=False, default="")
parser.add_argument('--minSpace', help='The minimum space for Go agents before they are disabled', required=False, default=1)
args = vars(parser.parse_args())
goHostname = args["hostname"]
goPort = args["port"]
goUser = args["username"]
goPass = args["password"]
minAgentSpace = args["minSpace"]
exitCode = 0
def isAgentLowOnSpace(agentSpace, minSpace):
agentSpace = agentSpace.replace(" ","").replace("GB","")
if "MB" in agentSpace:
return True
else:
try:
return float(agentSpace) < float(minSpace)
except ValueError:
return False
def print_err(*args):
sys.stderr.write(' '.join(map(str,args)) + '\n')
goAgentsUrl = "http://" + goHostname + ":" + str(goPort) + "/go/api/agents"
if (goUser != "" and goPass != ""):
response = requests.get(goAgentsUrl, auth=(goUser, goPass))
else:
response = requests.get(goAgentsUrl)
response.raise_for_status()
agentsList = response.json()
for agent in agentsList:
agentName = agent["agent_name"]
agentIp = agent["ip_address"]
agentFreeSpace = agent["free_space"]
agentUuid = agent["uuid"]
if isAgentLowOnSpace(agentFreeSpace, minAgentSpace):
exitCode = 1
print "Agent " + agentName + " has " + agentFreeSpace + " free space which is too low - disabling it."
disableAgentUrl = "http://" + goHostname + ":" + str(goPort) + "/go/api/agents/" + agentUuid + "/disable"
if (goUser != "" and goPass != ""):
response = requests.post(disableAgentUrl, auth=(goUser, goPass))
else:
response = requests.post(disableAgentUrl)
if (response.status_code != requests.codes.ok):
exitCode = 1
print_err("Error while trying to disable " + agentName, "using URL " + disableAgentUrl, response.text)
exit(exitCode)