-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
122 lines (100 loc) · 3.22 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import click
import requests
import pandas as pd
import random
import time
from tabulate import tabulate
@click.group()
def cli():
pass
def retry(max_attempts=3, wait_seconds=5):
"""
:param max_attempts:
:param wait_seconds:
:return:
retry operation with a decorator implemented so that the retry operation can be referenced with ease.
"""
def decorator(func):
def wrapper(*args, **kwargs):
attempt = 1
while attempt <= max_attempts:
try:
return func(*args, **kwargs)
except Exception as e:
click.echo(f"Attempt {attempt}/{max_attempts} failed with error: {e}")
attempt += 1
time.sleep(wait_seconds)
click.echo(f"Function failed after {max_attempts} attempts.")
return None
wrapper.__name__ = func.__name__
return wrapper
return decorator
@cli.command()
@click.option('--port', type=int, default=80)
@retry()
def get_status(port):
"""
retrieve the details of all servers
:param port:
:return:
"""
servers = requests.get(f'http://0.0.0.0:{port}/servers').json()
status = []
for server in servers:
server = requests.get(f'http://0.0.0.0:{port}/{server}').json()
status.append(server)
df = pd.DataFrame(status)
table = tabulate(df, headers='keys', tablefmt='psql')
click.echo(table)
return status
@cli.command()
@click.option('--port', type=int, default=80)
@retry()
def unhealthy_status(port):
"""
retrieve all servers with unhealthy status
:param port:
:return:
"""
servers = requests.get(f'http://0.0.0.0:{port}/servers').json()
status = []
for server in servers:
server = requests.get(f'http://0.0.0.0:{port}/{server}').json()
server['cpu'] = f'{random.randint(0, 100)}%'
server['memory'] = f'{random.randint(0, 100)}%'
status.append(server)
click.echo(status)
service_stats = {}
for stat in status:
if stat['status'] == 'Unhealthy':
continue
service_name = stat['service']
if service_name not in service_stats:
service_stats[service_name] = []
service_stats[service_name].append(stat['ip'])
services_with_few_health = {}
for service_name, ips in service_stats.items():
healthy_instances = len(ips)
if healthy_instances <= 2:
services_with_few_health[service_name] = ips
df = pd.DataFrame(services_with_few_health)
table = tabulate(df, headers='keys', tablefmt='psql')
click.echo(table)
@cli.command()
@click.option('--port', type=int, default=80)
@click.option('--interval', type=int, default=5)
@retry()
def current_status(port, interval):
while True:
servers = requests.get(f'http://0.0.0.0:{port}/servers').json()
status = []
for server in servers:
server_status = requests.get(f'http://0.0.0.0:{port}/{server}').json()
status.append(server_status)
df = pd.DataFrame(status)
table = tabulate(df, headers='keys', tablefmt='psql')
click.clear()
click.echo(table)
time.sleep(interval)
if __name__ == '__main__':
cli()