-
Notifications
You must be signed in to change notification settings - Fork 9
/
KSC.py
123 lines (109 loc) · 4.01 KB
/
KSC.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
from kscore.session import get_session
import json
import time
import logging
from datetime import datetime, timedelta
from cloud2falcon import PERIOD
def get_one_metric(namespace, region, metricname, period, id):
s = get_session()
client = s.create_client("monitor", region, use_ssl=True)
now = datetime.now()
start = datetime.now() - timedelta(minutes=PERIOD)
ISOFORMAT = "%Y-%m-%dT%XZ"
m = client.get_metric_statistics(
InstanceID=id,
Namespace=namespace,
MetricName=metricname,
StartTime=start.strftime(ISOFORMAT),
EndTime=now.strftime(ISOFORMAT),
Period='60',
Aggregate='Average'
)
return json.dumps(m, sort_keys=True, indent=4)
def get_metric_data(period, namespace, name, id_list,
metricname, ak, sk, region):
metric_data = []
ISOFORMAT = "%Y-%m-%dT%XZ"
for id in id_list:
if metricname == 'BandWidth':
ts = time.time()
t = int(ts)
v = id['BandWidth'] * 1000 * 1000
data = {"id": id['l'], "ip": id['d'], "region": region['site'], "metric": metricname,
"time": t, "value": v}
metric_data.append(data)
continue
response = json.loads(
get_one_metric(
name,
region['name'],
metricname,
period,
id['l']))
try:
metric_list = response['getMetricStatisticsResult']['datapoints']['member']
for metric in metric_list:
ts = time.strptime(metric['timestamp'], ISOFORMAT)
t = int(time.mktime(ts))
data = {"id": id['l'], "ip": id['d'], "region": region['site'], "metric": metricname,
"time": t, "value": metric['average']}
metric_data.append(data)
except BaseException:
logging.error('responce from ksc error')
metric_data.sort(key=lambda x: x["time"])
return metric_data
def get_id(resource, ak, sk, region):
if resource == "elb":
return elb(ak, sk, region)
elif resource == "eip":
return eip(ak, sk, region)
elif resource == "nat":
return nat(ak, sk, region)
elif resource == "connect":
return connect(ak, sk, region)
def elb(ak, sk, region):
id_list = []
s = get_session()
region = region
eipClient = s.create_client("eip", region, use_ssl=True)
allEips = eipClient.describe_addresses(
**{'Filter.1.Name': 'instance-type', 'Filter.1.Value.1': 'Slb'})
for item in allEips['AddressesSet']:
id_list.append({"l": item['InstanceId'],
"d": item['PublicIp'],
"BandWidth": item['BandWidth']})
return id_list
def eip(ak, sk, region):
id_list = []
s = get_session()
region = region
eipClient = s.create_client("eip", region, use_ssl=True)
allEips = eipClient.describe_addresses(
**{'Filter.1.Name': 'instance-type', 'Filter.1.Value.1': 'Ipfwd'})
for item in allEips['AddressesSet']:
id_list.append({"l": item['AllocationId'],
"d": item['PublicIp'],
"BandWidth": item['BandWidth']})
return id_list
def nat(ak, sk, region):
s = get_session()
id_list = []
region = region
vpcClient = s.create_client("vpc", region, use_ssl=True)
allVpcs = vpcClient.describe_nats()
for item in allVpcs['NatSet']:
id_list.append({"l": item['NatId'],
"d": item['NatName'],
"BandWidth": item['BandWidth']})
return id_list
def connect(ak, sk, region):
s = get_session()
id_list = []
region = region
vpcClient = s.create_client("vpc", region, use_ssl=True)
allConnect = vpcClient.describe_direct_connect_gateways()
for item in allConnect['DirectConnectGatewaySet']:
id_list.append({"l": item['DirectConnectGatewayId'],
"d": item['DirectConnectGatewayName'],
"BandWidth": item['BandWidth']})
return id_list