-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathget_server_metrics.py
More file actions
39 lines (31 loc) · 904 Bytes
/
get_server_metrics.py
File metadata and controls
39 lines (31 loc) · 904 Bytes
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
from __future__ import annotations
from datetime import datetime, timedelta, timezone
from os import environ
from hcloud import Client
from hcloud.images import Image
from hcloud.server_types import ServerType
assert (
"HCLOUD_TOKEN" in environ
), "Please export your API token in the HCLOUD_TOKEN environment variable"
token = environ["HCLOUD_TOKEN"]
client = Client(
token=token,
application_name="examples",
application_version="unknown",
)
server = client.servers.get_by_name("my-server")
if server is None:
response = client.servers.create(
name="my-server",
server_type=ServerType(name="cx23"),
image=Image(name="ubuntu-24.04"),
)
server = response.server
end = datetime.now(timezone.utc)
start = end - timedelta(hours=1)
response = server.get_metrics(
type=["cpu", "network"],
start=start,
end=end,
)
print(response.metrics)