generated from atsign-company/archetype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_send_data.py
executable file
·50 lines (41 loc) · 1.95 KB
/
docker_send_data.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
#!/usr/bin/env python3
from google.cloud import monitoring_v3
import time
import os
import shutil
import requests
metadata_server = "http://metadata/computeMetadata/v1/"
metadata_flavor = {'Metadata-Flavor' : 'Google'}
#gce_id = requests.get(metadata_server + 'instance/id', headers = metadata_flavor).text
gce_name = requests.get(metadata_server + 'instance/hostname', headers = metadata_flavor).text
gce_project = requests.get(metadata_server + 'project/project-id', headers = metadata_flavor).text
split_gce_name=gce_name.split(".",2)
client = monitoring_v3.MetricServiceClient()
project_id = gce_project
project_name = f"projects/{project_id}"
load_series = monitoring_v3.TimeSeries()
load_series.metric.type = "custom.googleapis.com/at_swarm_node_load"
load_series.resource.type = "gce_instance"
load_series.resource.labels["instance_id"] = split_gce_name[0]
load_series.resource.labels["zone"] = split_gce_name[1]
du_series = monitoring_v3.TimeSeries()
du_series.metric.type = "custom.googleapis.com/at_swarm_node_root_volume_usage"
du_series.resource.type = "gce_instance"
du_series.resource.labels["instance_id"] = split_gce_name[0]
du_series.resource.labels["zone"] = split_gce_name[1]
while True:
load1, load5, load15 = os.getloadavg()
root_total, root_used, root_free = shutil.disk_usage("/")
now = time.time()
seconds = int(now)
nanos = int((now - seconds) * 10 ** 9)
interval = monitoring_v3.TimeInterval(
{"end_time": {"seconds": seconds, "nanos": nanos}}
)
load_point = monitoring_v3.Point({"interval": interval, "value": {"double_value": load5}})
load_series.points = [load_point]
client.create_time_series(request={"name": project_name, "time_series": [load_series]})
du_point = monitoring_v3.Point({"interval": interval, "value": {"double_value": root_used/root_total}})
du_series.points = [du_point]
client.create_time_series(request={"name": project_name, "time_series": [du_series]})
time.sleep(60)