-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.py
172 lines (149 loc) · 5.12 KB
/
repository.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import logging
from dataclasses import dataclass, field
from datetime import datetime, timedelta
from typing import Dict, List, Union
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
TIME_FMT = r"%Y-%m-%d %H:%M:%S"
@dataclass
class Service:
name: str # format "{host}-{service}"
protocol: str # https / ssh / ...
port: str # 443 / 8000 / ...
@dataclass
class Host:
ip: str # NOTE: unique
name: str = "?"
metrics: dict = field(default_factory=dict)
last_update: datetime = field(
default_factory=datetime.now,
init=False,
)
class Repository:
interval: int = 5 * 60 # 5min
_hosts: Dict[str, Host] = {} # {ip: host}
def __init__(self, *args, **kwargs):
for key, value in kwargs:
setattr(self, key, value)
def auto_check_and_deregister(self):
now = datetime.now()
for ip, host in self._hosts.items():
# check last update is 2 times interval
if now > host.last_update + timedelta(seconds=2 * self.interval):
logger.info(f"{host.name} is outedated, deregister it now!")
self.deregister(ip)
@property
def services(self) -> List[Service]:
"""Aggregate services
Returns:
List[Service]: All services
"""
raise NotImplementedError()
svcs = []
for _, host in self._hosts.items():
svcs.extend()
return svcs
@property
def metrics(
self,
) -> Dict[str, Dict[str, Union[str, Dict[str, Union[str, float, int]]]]]:
"""Aggregate metrics
Returns:
Dict[str, Dict[str, Union[str, Dict[str, Union[str, float, int]]]]]:
{
"網站": {
"os": "Ubuntu 20.04.1 LTS",
"ip": "140.118.9.222",
"cpu": { "total": 100, "used": 6.6, "unit": "%" },
"ram": {
"total": 3.7190513610839844,
"used": 2.2864608764648438,
"unit": "GB"
},
"disks": {
"/": {
"total": 907.6402473449707,
"used": 31.06873321533203,
"unit": "GB"
}
},
"vram": {},
"net": { "total": 20, "used": 0.21386544965207577, "unit": "GB" },
"domain": "cgm.im"
},
"旗艦": {
"os": "Ubuntu 18.04.4 LTS",
"ip": "140.118.110.127",
"cpu": { "total": 100, "used": 74.2, "unit": "%" },
"ram": {
"total": 31.228492736816406,
"used": 3.3500404357910156,
"unit": "GB"
},
"disks": {
"/": {
"total": 116.37757873535156,
"used": 61.42502212524414,
"unit": "GB"
},
"/mnt/sdb1": {
"total": 931.5107383728027,
"used": 340.91260528564453,
"unit": "GB"
}
},
"vram": {
"0: GeForce RTX 2080 Ti": {
"total": 10.7578125,
"used": 0.025390625,
"unit": "GB"
}
},
"net": { "total": 20, "used": 0.021168168634176254, "unit": "GB" },
"domain": "flagship.cgm.im"
},
"大考": {
"os": "Windows 10.0.19041",
"ip": "140.118.109.84",
"cpu": { "total": 100, "used": 0.6, "unit": "%" },
"ram": {
"total": 11.909080505371094,
"used": 5.724323272705078,
"unit": "GB"
},
"disks": {
"C:\\": {
"total": 930.4313163757324,
"used": 121.89962387084961,
"unit": "GB"
}
},
"vram": {
"0: GeForce GTS 450": { "total": 1, "used": 0.1025390625, "unit": "GB" }
},
"net": { "total": 20, "used": 0.014969858340919018, "unit": "GB" },
"domain": "ceec.cgm.im"
}
}
"""
return {
h.ip: {**h.metrics, "lastUpdate": h.last_update.strftime(TIME_FMT)}
for _, h in self._hosts.items()
}
@property
def hosts(self) -> List[Host]:
return list(self._hosts.keys())
def get_host(self, ip: str) -> Host:
return self.hosts[ip]
def is_host_exist(self, ip: str) -> bool:
return ip in self._hosts.keys()
def register(self, host: Host):
self._hosts[host.ip] = host
def deregister(self, ip: str):
del self._hosts[ip]
def update(self, ip: str, metrics: dict):
self._hosts[ip].metrics = metrics
self._hosts[ip].last_update = datetime.now()
# NOTE: frontend need to know some fixed tabs
if __name__ == "__main__":
repo = Repository()