uptime-kuma-api/uptime_kuma_api/api.py

893 lines
26 KiB
Python
Raw Normal View History

2022-07-05 22:12:37 +02:00
import json
2022-07-02 16:00:54 +02:00
import time
import socketio
from . import AuthMethod
from . import MonitorType
from . import NotificationType, notification_provider_options
2022-07-02 20:26:18 +02:00
from . import ProxyProtocol
2022-07-02 21:29:16 +02:00
from . import IncidentStyle
2022-08-02 21:32:28 +02:00
from . import \
convert_from_socket,\
convert_to_socket, \
params_map_monitor, \
params_map_notification,\
params_map_notification_providers, \
get_params_map_notification, \
params_map_proxy, \
params_map_status_page, \
params_map_info, \
params_map_settings
from . import UptimeKumaException
2022-07-05 22:12:37 +02:00
def int_to_bool(data, keys):
2022-07-05 22:12:37 +02:00
if type(data) == list:
for d in data:
int_to_bool(d, keys)
else:
for key in keys:
if key in data:
data[key] = True if data[key] == 1 else False
def _build_monitor_data(
type_: MonitorType,
name: str,
heartbeat_interval: int = 60,
heartbeat_retry_interval: int = 60,
retries: int = 0,
upside_down_mode: bool = False,
tags: list = None,
2022-08-02 11:58:49 +02:00
notification_ids: list = None,
2022-07-05 22:12:37 +02:00
# HTTP, KEYWORD
url: str = None,
certificate_expiry_notification: bool = False,
ignore_tls_error: bool = False,
max_redirects: int = 10,
2022-08-02 11:58:49 +02:00
accepted_status_codes: list = None,
2022-07-05 22:12:37 +02:00
proxy_id: int = None,
http_method: str = "GET",
http_body: str = None,
http_headers: str = None,
auth_method: AuthMethod = AuthMethod.NONE,
auth_user: str = None,
auth_pass: str = None,
auth_domain: str = None,
auth_workstation: str = None,
# KEYWORD
keyword: str = None,
# DNS, PING, STEAM, MQTT
hostname: str = None,
# DNS, STEAM, MQTT
port: int = 53,
# DNS
dns_resolve_server: str = "1.1.1.1",
dns_resolve_type: str = "A",
# MQTT
mqtt_username: str = None,
mqtt_password: str = None,
mqtt_topic: str = None,
mqtt_success_message: str = None,
# SQLSERVER
2022-08-02 11:58:49 +02:00
sqlserver_connection_string: str = "Server=<hostname>,<port>;"
"Database=<your database>;"
"User Id=<your user id>;"
"Password=<your password>;"
"Encrypt=<true/false>;"
"TrustServerCertificate=<Yes/No>;"
"Connection Timeout=<int>",
sqlserver_query: str = None
2022-07-05 22:12:37 +02:00
):
if not accepted_status_codes:
accepted_status_codes = ["200-299"]
dict_notification_ids = {}
if notification_ids:
for notification_id in notification_ids:
dict_notification_ids[notification_id] = True
notification_ids = dict_notification_ids
data = {
"type_": type_,
2022-07-05 22:12:37 +02:00
"name": name,
"heartbeat_interval": heartbeat_interval,
"heartbeat_retry_interval": heartbeat_retry_interval,
"retries": retries,
"notification_ids": notification_ids,
"upside_down_mode": upside_down_mode,
2022-07-05 22:12:37 +02:00
}
if tags:
data.update({
"tags": tags
})
if type_ == MonitorType.KEYWORD:
data.update({
"keyword": keyword,
})
2022-07-07 22:29:31 +02:00
# HTTP, KEYWORD
data.update({
"url": url,
"certificate_expiry_notification": certificate_expiry_notification,
"ignore_tls_error": ignore_tls_error,
"max_redirects": max_redirects,
"accepted_status_codes": accepted_status_codes,
"proxy_id": proxy_id,
"http_method": http_method,
"http_body": http_body,
"http_headers": http_headers,
"auth_method": auth_method,
})
if auth_method in [AuthMethod.HTTP_BASIC, AuthMethod.NTLM]:
2022-07-05 22:12:37 +02:00
data.update({
"auth_user": auth_user,
"auth_pass": auth_pass,
2022-07-05 22:12:37 +02:00
})
if auth_method == AuthMethod.NTLM:
2022-07-05 22:12:37 +02:00
data.update({
"auth_domain": auth_domain,
"auth_workstation": auth_workstation,
2022-07-05 22:12:37 +02:00
})
2022-07-07 22:29:31 +02:00
# DNS, PING, STEAM, MQTT
data.update({
"hostname": hostname,
})
2022-07-07 22:29:31 +02:00
# DNS, STEAM, MQTT
data.update({
"port": port,
})
2022-07-05 22:12:37 +02:00
2022-07-07 22:29:31 +02:00
# DNS
data.update({
"dns_resolve_server": dns_resolve_server,
"dns_resolve_type": dns_resolve_type,
})
2022-07-05 22:12:37 +02:00
2022-07-07 22:29:31 +02:00
# MQTT
data.update({
"mqtt_username": mqtt_username,
"mqtt_password": mqtt_password,
"mqtt_topic": mqtt_topic,
"mqtt_success_message": mqtt_success_message,
})
2022-07-05 22:12:37 +02:00
2022-07-07 22:29:31 +02:00
# SQLSERVER
data.update({
"sqlserver_connection_string": sqlserver_connection_string
})
2022-07-05 22:12:37 +02:00
if type_ == MonitorType.SQLSERVER:
data.update({
"sqlserver_query": sqlserver_query,
2022-07-05 22:12:37 +02:00
})
data = convert_to_socket(params_map_monitor, data)
2022-07-05 22:12:37 +02:00
return data
2022-08-02 11:58:49 +02:00
def _build_notification_data(
name: str,
type_: NotificationType,
default: bool = False,
apply_existing: bool = False,
**kwargs
):
2022-08-02 21:32:28 +02:00
params_map = get_params_map_notification(type_)
type_ = convert_to_socket(params_map, type_)
data = {
2022-07-05 22:12:37 +02:00
"name": name,
"type_": type_,
"default": default,
2022-08-02 11:58:49 +02:00
"apply_existing": apply_existing,
2022-07-05 22:12:37 +02:00
**kwargs
}
2022-08-02 21:32:28 +02:00
data = convert_to_socket(params_map, data)
return data
2022-07-05 22:12:37 +02:00
def _build_proxy_data(
protocol: ProxyProtocol,
host: str,
port: str,
auth: bool = False,
username: str = None,
password: str = None,
active: bool = True,
default: bool = False,
apply_existing: bool = False,
):
data = {
2022-07-05 22:12:37 +02:00
"protocol": protocol,
"host": host,
"port": port,
"auth": auth,
"username": username,
"password": password,
"active": active,
"default": default,
"apply_existing": apply_existing
2022-07-05 22:12:37 +02:00
}
data = convert_to_socket(params_map_proxy, data)
return data
2022-07-02 16:00:54 +02:00
2022-07-06 21:29:40 +02:00
def _build_status_page_data(
slug: str,
# config
id_: int,
title: str,
description: str = None,
theme: str = "light",
2022-07-06 21:29:40 +02:00
published: bool = True,
show_tags: bool = False,
2022-08-02 11:58:49 +02:00
domain_name_list: list = None,
2022-07-06 21:29:40 +02:00
custom_css: str = "",
footer_text: str = None,
show_powered_by: bool = True,
img_data_url: str = "/icon.svg",
monitors: list = None
):
if theme not in ["light", "dark"]:
raise ValueError
2022-07-06 21:29:40 +02:00
if not domain_name_list:
domain_name_list = []
public_group_list = []
if monitors:
public_group_list.append({
"name": "Services",
"monitorList": monitors
})
config = {
"id_": id_,
2022-07-06 21:29:40 +02:00
"slug": slug,
"title": title,
"description": description,
"img_data_url": img_data_url,
"theme": theme,
2022-07-06 21:29:40 +02:00
"published": published,
"show_tags": show_tags,
"domain_name_list": domain_name_list,
"custom_css": custom_css,
"footer_text": footer_text,
"show_powered_by": show_powered_by
2022-07-06 21:29:40 +02:00
}
config = convert_to_socket(params_map_status_page, config)
2022-07-06 21:29:40 +02:00
return slug, config, img_data_url, public_group_list
2022-07-10 18:07:11 +02:00
def _check_missing_arguments(required_params, kwargs, params_map):
2022-07-09 22:15:41 +02:00
missing_arguments = []
for required_param in required_params:
required_param_sock = convert_to_socket(params_map, required_param)
if kwargs.get(required_param_sock) is None:
missing_arguments.append(required_param)
if missing_arguments:
missing_arguments_str = ", ".join([f"'{i}'" for i in missing_arguments])
raise TypeError(f"missing {len(missing_arguments)} required argument: {missing_arguments_str}")
2022-07-10 18:07:11 +02:00
def _check_argument_conditions(valid_params, kwargs, params_map):
for valid_param in valid_params:
valid_param_sock = convert_to_socket(params_map, valid_param)
if valid_param_sock in kwargs:
value = kwargs[valid_param_sock]
conditions = valid_params[valid_param]
min_ = conditions.get("min")
max_ = conditions.get("max")
if min_ is not None and value < min_:
raise ValueError(f"the value of {valid_param} must not be less than {min_}")
if max_ is not None and value > max_:
raise ValueError(f"the value of {valid_param} must not be larger than {max_}")
def _check_arguments_monitor(kwargs):
required_args = [
2022-07-09 22:15:41 +02:00
"type_",
"name",
"heartbeat_interval",
"retries",
"heartbeat_retry_interval"
]
2022-07-10 18:07:11 +02:00
_check_missing_arguments(required_args, kwargs, params_map_monitor)
2022-07-09 22:15:41 +02:00
2022-07-10 18:07:11 +02:00
required_args_by_type = {
2022-07-09 22:15:41 +02:00
MonitorType.HTTP: ["url", "max_redirects"],
MonitorType.PORT: ["hostname", "port"],
MonitorType.PING: ["hostname"],
MonitorType.KEYWORD: ["url", "keyword", "max_redirects"],
MonitorType.DNS: ["hostname", "dns_resolve_server", "port"],
MonitorType.PUSH: [],
MonitorType.STEAM: ["hostname", "port"],
MonitorType.MQTT: ["hostname", "port", "mqtt_topic"],
MonitorType.SQLSERVER: [],
}
2022-07-10 18:07:11 +02:00
type_ = kwargs[convert_to_socket(params_map_monitor, "type")]
required_args = required_args_by_type[type_]
_check_missing_arguments(required_args, kwargs, params_map_monitor)
conditions = {
"heartbeat_interval": {
"min": 20
},
"retries": {
"min": 0
},
"heartbeat_retry_interval": {
"min": 20
},
"max_redirects": {
"min": 0
},
"port": {
"min": 0,
"max": 65535
}
}
_check_argument_conditions(conditions, kwargs, params_map_monitor)
def _check_arguments_notification(kwargs):
required_args = ["type_", "name"]
2022-08-02 21:32:28 +02:00
_check_missing_arguments(required_args, kwargs, params_map_notification)
2022-07-10 18:07:11 +02:00
type_ = kwargs[convert_to_socket(params_map_notification, "type")]
required_args_sock = notification_provider_options[type_]
2022-08-02 21:32:28 +02:00
params_map = get_params_map_notification(type_sock=type_)
required_args = convert_from_socket(params_map, required_args_sock)
_check_missing_arguments(required_args, kwargs, params_map)
2022-07-10 18:07:11 +02:00
provider_conditions = {
'gotify_priority': {
'max': 10,
'min': 0
},
'ntfy_priority': {
'max': 5,
'min': 1
},
'smtp_smtp_port': {
'max': 65535,
'min': 0
}
}
2022-08-02 21:32:28 +02:00
_check_argument_conditions(provider_conditions, kwargs, params_map)
2022-07-09 22:15:41 +02:00
2022-07-10 18:07:11 +02:00
def _check_arguments_proxy(kwargs):
required_args = ["protocol", "host", "port"]
2022-08-02 23:47:56 +02:00
if kwargs.get("auth"):
2022-07-10 18:07:11 +02:00
required_args.extend(["username", "password"])
_check_missing_arguments(required_args, kwargs, params_map_proxy)
conditions = {
"port": {
"min": 0,
"max": 65535
}
}
_check_argument_conditions(conditions, kwargs, params_map_proxy)
2022-07-09 22:15:41 +02:00
2022-07-02 16:00:54 +02:00
class UptimeKumaApi(object):
def __init__(self, url):
self.sio = socketio.Client()
2022-08-02 21:32:28 +02:00
self._event_data: dict = {
2022-07-02 16:00:54 +02:00
"monitorList": None,
"notificationList": None,
"proxyList": None,
"statusPageList": None,
"heartbeatList": None,
"importantHeartbeatList": None,
"avgPing": None,
"uptime": None,
"heartbeat": None,
"info": None,
}
2022-07-05 22:12:37 +02:00
self.sio.on("connect", self._event_connect)
self.sio.on("disconnect", self._event_disconnect)
self.sio.on("monitorList", self._event_monitor_list)
self.sio.on("notificationList", self._event_notification_list)
self.sio.on("proxyList", self._event_proxy_list)
self.sio.on("statusPageList", self._event_status_page_list)
self.sio.on("heartbeatList", self._event_heartbeat_list)
self.sio.on("importantHeartbeatList", self._event_important_heartbeat_list)
self.sio.on("avgPing", self._event_avg_ping)
self.sio.on("uptime", self._event_uptime)
self.sio.on("heartbeat", self._event_heartbeat)
self.sio.on("info", self._event_info)
2022-07-02 16:00:54 +02:00
self.connect(url)
2022-07-05 22:12:37 +02:00
def _get_event_data(self, event):
while self._event_data[event] is None:
2022-07-02 16:00:54 +02:00
time.sleep(0.01)
time.sleep(0.01) # wait for multiple messages
2022-07-05 22:12:37 +02:00
return self._event_data[event]
2022-07-02 16:00:54 +02:00
def _call(self, event, data=None):
r = self.sio.call(event, data)
if type(r) == dict and "ok" in r:
if not r["ok"]:
raise UptimeKumaException(r["msg"])
r.pop("ok")
return r
2022-07-02 16:00:54 +02:00
# event handlers
2022-07-05 22:12:37 +02:00
def _event_connect(self):
2022-07-02 16:00:54 +02:00
pass
2022-07-05 22:12:37 +02:00
def _event_disconnect(self):
2022-07-02 16:00:54 +02:00
pass
2022-07-05 22:12:37 +02:00
def _event_monitor_list(self, data):
self._event_data["monitorList"] = data
2022-07-02 16:00:54 +02:00
2022-07-05 22:12:37 +02:00
def _event_notification_list(self, data):
self._event_data["notificationList"] = data
2022-07-02 16:00:54 +02:00
2022-07-05 22:12:37 +02:00
def _event_proxy_list(self, data):
self._event_data["proxyList"] = data
2022-07-02 16:00:54 +02:00
2022-07-05 22:12:37 +02:00
def _event_status_page_list(self, data):
self._event_data["statusPageList"] = data
2022-07-02 16:00:54 +02:00
2022-07-05 22:12:37 +02:00
def _event_heartbeat_list(self, id_, data, bool_):
if self._event_data["heartbeatList"] is None:
self._event_data["heartbeatList"] = []
self._event_data["heartbeatList"].append({
2022-07-02 16:00:54 +02:00
"id": id_,
"data": data,
"bool": bool_,
})
2022-07-05 22:12:37 +02:00
def _event_important_heartbeat_list(self, id_, data, bool_):
if self._event_data["importantHeartbeatList"] is None:
self._event_data["importantHeartbeatList"] = []
self._event_data["importantHeartbeatList"].append({
2022-07-02 16:00:54 +02:00
"id": id_,
"data": data,
"bool": bool_,
})
2022-07-05 22:12:37 +02:00
def _event_avg_ping(self, id_, data):
if self._event_data["avgPing"] is None:
self._event_data["avgPing"] = []
self._event_data["avgPing"].append({
2022-07-02 16:00:54 +02:00
"id": id_,
"data": data,
})
2022-07-05 22:12:37 +02:00
def _event_uptime(self, id_, hours_24, days_30):
if self._event_data["uptime"] is None:
self._event_data["uptime"] = []
self._event_data["uptime"].append({
2022-07-02 16:00:54 +02:00
"id": id_,
"hours_24": hours_24,
"days_30": days_30,
})
2022-07-05 22:12:37 +02:00
def _event_heartbeat(self, data):
if self._event_data["heartbeat"] is None:
self._event_data["heartbeat"] = []
self._event_data["heartbeat"].append(data)
2022-07-02 16:00:54 +02:00
2022-07-05 22:12:37 +02:00
def _event_info(self, data):
self._event_data["info"] = data
2022-07-02 16:00:54 +02:00
# connection
2022-07-05 22:12:37 +02:00
def connect(self, url: str):
2022-07-02 16:00:54 +02:00
url = url.rstrip("/")
self.sio.connect(f'{url}/socket.io/')
def disconnect(self):
self.sio.disconnect()
# monitors
def get_monitors(self):
r = list(self._get_event_data("monitorList").values())
r = convert_from_socket(params_map_monitor, r)
int_to_bool(r, ["active"])
return r
2022-07-02 16:00:54 +02:00
2022-07-05 22:12:37 +02:00
def get_monitor(self, id_: int):
r = self._call('getMonitor', id_)["monitor"]
r = convert_from_socket(params_map_monitor, r)
int_to_bool(r, ["active"])
return r
2022-07-02 16:00:54 +02:00
2022-07-05 22:12:37 +02:00
def pause_monitor(self, id_: int):
2022-07-09 19:52:21 +02:00
return self._call('pauseMonitor', id_)
2022-07-02 16:00:54 +02:00
2022-07-05 22:12:37 +02:00
def resume_monitor(self, id_: int):
2022-07-09 19:52:21 +02:00
return self._call('resumeMonitor', id_)
2022-07-02 16:00:54 +02:00
2022-07-05 22:12:37 +02:00
def delete_monitor(self, id_: int):
return self._call('deleteMonitor', id_)
2022-07-02 16:00:54 +02:00
def get_monitor_beats(self, id_: int, hours: int):
r = self._call('getMonitorBeats', (id_, hours))["data"]
int_to_bool(r, ["important", "status"])
return r
2022-07-02 16:00:54 +02:00
2022-07-09 22:15:41 +02:00
def add_monitor(self, **kwargs):
data = _build_monitor_data(**kwargs)
2022-07-10 18:07:11 +02:00
_check_arguments_monitor(data)
r = self._call('add', data)
2022-07-09 22:15:41 +02:00
r = convert_from_socket(params_map_monitor, r)
return r
2022-07-02 16:00:54 +02:00
2022-07-05 22:12:37 +02:00
def edit_monitor(self, id_: int, **kwargs):
data = self.get_monitor(id_)
2022-07-09 19:52:21 +02:00
data.update(kwargs)
data = convert_to_socket(params_map_monitor, data)
2022-07-09 22:15:41 +02:00
2022-07-10 18:07:11 +02:00
_check_arguments_monitor(data)
r = self._call('editMonitor', data)
2022-07-09 22:15:41 +02:00
r = convert_from_socket(params_map_monitor, r)
return r
2022-07-02 16:00:54 +02:00
# monitor tags
2022-07-05 22:12:37 +02:00
def add_monitor_tag(self, tag_id: int, monitor_id: int, value=""):
return self._call('addMonitorTag', (tag_id, monitor_id, value))
2022-07-02 16:00:54 +02:00
# editMonitorTag is unused in uptime-kuma
2022-07-05 22:12:37 +02:00
# def edit_monitor_tag(self, tag_id: int, monitor_id: int, value=""):
# return self._call('editMonitorTag', (tag_id, monitor_id, value))
2022-07-02 16:00:54 +02:00
2022-07-05 22:12:37 +02:00
def delete_monitor_tag(self, tag_id: int, monitor_id: int, value=""):
return self._call('deleteMonitorTag', (tag_id, monitor_id, value))
2022-07-02 16:00:54 +02:00
# notifications
def get_notifications(self):
notifications = self._get_event_data("notificationList")
r = []
for notification_raw in notifications:
2022-07-05 22:12:37 +02:00
notification = notification_raw.copy()
config = json.loads(notification["config"])
del notification["config"]
notification.update(config)
2022-08-02 21:32:28 +02:00
notification["type"] = convert_from_socket(params_map_notification_providers, notification["type"])
params_map = get_params_map_notification(notification["type"])
notification = convert_from_socket(params_map, notification)
r.append(notification)
return r
2022-07-05 22:12:37 +02:00
def get_notification(self, id_: int):
notifications = self.get_notifications()
for notification in notifications:
if notification["id"] == id_:
return notification
raise UptimeKumaException("notification does not exist")
2022-07-02 16:00:54 +02:00
2022-07-09 22:15:41 +02:00
def test_notification(self, **kwargs):
data = _build_notification_data(**kwargs)
2022-07-10 18:07:11 +02:00
_check_arguments_notification(data)
return self._call('testNotification', data)
2022-07-02 16:00:54 +02:00
2022-07-09 22:15:41 +02:00
def add_notification(self, **kwargs):
data = _build_notification_data(**kwargs)
2022-07-10 18:07:11 +02:00
_check_arguments_notification(data)
return self._call('addNotification', (data, None))
2022-07-02 16:00:54 +02:00
2022-07-05 22:12:37 +02:00
def edit_notification(self, id_: int, **kwargs):
notification = self.get_notification(id_)
2022-08-02 21:32:28 +02:00
if "type_" in kwargs and kwargs["type_"] != notification["type_"]:
# remove old notification provider options from notification object
for provider in notification_provider_options:
provider_options = notification_provider_options[provider]
2022-08-02 21:32:28 +02:00
params_map = get_params_map_notification(type_sock=provider)
provider_options = convert_from_socket(params_map, provider_options)
if provider != kwargs["type_"]:
for option in provider_options:
if option in notification:
del notification[option]
2022-08-02 21:32:28 +02:00
# convert type from py to sock
kwargs["type_"] = convert_to_socket(params_map_notification_providers, kwargs["type_"])
notification.update(kwargs)
2022-08-02 21:32:28 +02:00
params_map = get_params_map_notification(type_sock=kwargs["type_"])
notification = convert_to_socket(params_map, notification)
2022-07-09 22:15:41 +02:00
2022-07-10 18:07:11 +02:00
_check_arguments_notification(notification)
return self._call('addNotification', (notification, id_))
2022-07-02 16:00:54 +02:00
def delete_notification(self, id_: int):
return self._call('deleteNotification', id_)
2022-07-02 16:00:54 +02:00
def check_apprise(self):
return self._call('checkApprise')
2022-07-02 16:00:54 +02:00
# proxy
def get_proxies(self):
r = self._get_event_data("proxyList")
r = convert_from_socket(params_map_proxy, r)
int_to_bool(r, ["auth", "active", "default", "apply_existing"])
return r
2022-07-05 22:12:37 +02:00
def get_proxy(self, id_: int):
proxies = self.get_proxies()
for proxy in proxies:
if proxy["id"] == id_:
return proxy
raise UptimeKumaException("proxy does not exist")
2022-07-02 16:00:54 +02:00
2022-07-09 22:15:41 +02:00
def add_proxy(self, **kwargs):
data = _build_proxy_data(**kwargs)
2022-07-10 18:07:11 +02:00
_check_arguments_proxy(data)
return self._call('addProxy', (data, None))
2022-07-02 20:26:18 +02:00
2022-07-05 22:12:37 +02:00
def edit_proxy(self, id_: int, **kwargs):
proxy = self.get_proxy(id_)
proxy.update(kwargs)
2022-07-09 19:52:21 +02:00
proxy = convert_to_socket(params_map_proxy, proxy)
2022-07-09 22:15:41 +02:00
2022-07-10 18:07:11 +02:00
_check_arguments_proxy(proxy)
return self._call('addProxy', (proxy, id_))
2022-07-02 20:26:18 +02:00
def delete_proxy(self, id_: int):
return self._call('deleteProxy', id_)
2022-07-02 20:26:18 +02:00
2022-07-02 16:00:54 +02:00
# status page
2022-07-02 20:40:14 +02:00
def get_status_pages(self):
r = list(self._get_event_data("statusPageList").values())
r = convert_from_socket(params_map_status_page, r)
return r
2022-07-02 20:40:14 +02:00
2022-07-02 21:29:16 +02:00
def get_status_page(self, slug: str):
r = self._call('getStatusPage', slug)
config = r["config"]
del r["config"]
r.update(config)
r = convert_from_socket(params_map_status_page, r)
2022-07-06 21:29:40 +02:00
return r
def add_status_page(self, slug: str, title: str):
return self._call('addStatusPage', (title, slug))
2022-07-05 22:12:37 +02:00
def delete_status_page(self, slug: str):
return self._call('deleteStatusPage', slug)
2022-07-05 22:12:37 +02:00
2022-07-06 21:29:40 +02:00
def save_status_page(self, slug: str, **kwargs):
status_page = self.get_status_page(slug)
status_page.update(kwargs)
2022-07-06 21:29:40 +02:00
data = _build_status_page_data(**status_page)
return self._call('saveStatusPage', data)
2022-07-02 20:40:14 +02:00
2022-07-05 22:12:37 +02:00
def post_incident(
self,
slug: str,
title: str,
content: str,
style: IncidentStyle = IncidentStyle.PRIMARY
):
incident = {
"title": title,
"content": content,
"style": style
}
r = self._call('postIncident', (slug, incident))["incident"]
r = convert_from_socket(params_map_status_page, r)
2022-07-06 21:29:40 +02:00
self.save_status_page(slug)
return r
2022-07-05 22:12:37 +02:00
def unpin_incident(self, slug: str):
r = self._call('unpinIncident', slug)
2022-07-06 21:29:40 +02:00
self.save_status_page(slug)
return r
2022-07-02 20:40:14 +02:00
2022-07-02 16:00:54 +02:00
# heartbeat
def get_heartbeats(self):
r = self._get_event_data("heartbeatList")
for i in r:
int_to_bool(i["data"], ["important", "status"])
return r
2022-07-02 16:00:54 +02:00
def get_important_heartbeats(self):
r = self._get_event_data("importantHeartbeatList")
for i in r:
int_to_bool(i["data"], ["important", "status"])
return r
2022-07-02 16:00:54 +02:00
def get_heartbeat(self):
r = self._get_event_data("heartbeat")
int_to_bool(r, ["important", "status"])
return r
2022-07-02 16:00:54 +02:00
# avg ping
def avg_ping(self):
2022-07-05 22:12:37 +02:00
return self._get_event_data("avgPing")
2022-07-02 16:00:54 +02:00
# uptime
def uptime(self):
2022-07-05 22:12:37 +02:00
return self._get_event_data("uptime")
2022-07-02 16:00:54 +02:00
# info
def info(self):
r = self._get_event_data("info")
r = convert_from_socket(params_map_info, r)
return r
2022-07-02 16:00:54 +02:00
# clear
def clear_events(self, monitor_id: int):
return self._call('clearEvents', monitor_id)
2022-07-02 16:00:54 +02:00
def clear_heartbeats(self, monitor_id: int):
return self._call('clearHeartbeats', monitor_id)
2022-07-02 16:00:54 +02:00
def clear_statistics(self):
return self._call('clearStatistics')
2022-07-02 16:00:54 +02:00
# tags
def get_tags(self):
return self._call('getTags')["tags"]
2022-07-02 16:00:54 +02:00
2022-07-05 22:12:37 +02:00
def get_tag(self, id_: int):
tags = self.get_tags()
for tag in tags:
if tag["id"] == id_:
return tag
raise UptimeKumaException("tag does not exist")
2022-07-05 22:12:37 +02:00
# not working, monitor id required?
2022-07-05 22:12:37 +02:00
# def edit_tag(self, id_: int, name: str, color: str):
# return self._call('editTag', {
2022-07-05 22:12:37 +02:00
# "id": id_,
# "name": name,
# "color": color
# })
def delete_tag(self, id_: int):
return self._call('deleteTag', id_)
2022-07-02 16:00:54 +02:00
2022-07-05 22:12:37 +02:00
def add_tag(self, name: str, color: str):
return self._call('addTag', {
2022-07-02 16:00:54 +02:00
"name": name,
2022-07-05 22:12:37 +02:00
"color": color,
2022-07-02 16:00:54 +02:00
"new": True
})["tag"]
2022-07-02 16:00:54 +02:00
# settings
def get_settings(self):
r = self._call('getSettings')["data"]
r = convert_from_socket(params_map_settings, r)
return r
def set_settings(
self,
password: str,
2022-07-02 16:00:54 +02:00
# about
check_update: bool = True,
check_beta: bool = False,
# monitor history
keep_data_period_days: int = 180,
# general
entry_page: str = "dashboard",
search_engine_index: bool = False,
primary_base_url: str = "",
steam_api_key: str = "",
# notifications
2022-08-02 11:58:49 +02:00
tls_expiry_notify_days: list = None,
# security
disable_auth: bool = False
):
if not tls_expiry_notify_days:
tls_expiry_notify_days = [7, 14, 21]
data = {
"check_update": check_update,
"check_beta": check_beta,
"keep_data_period_days": keep_data_period_days,
"entry_page": entry_page,
"search_engine_index": search_engine_index,
"primary_base_url": primary_base_url,
"steam_api_key": steam_api_key,
"tls_expiry_notify_days": tls_expiry_notify_days,
"disable_auth": disable_auth
}
data = convert_to_socket(params_map_settings, data)
return self._call('setSettings', (data, password))
2022-07-02 16:00:54 +02:00
2022-07-05 22:12:37 +02:00
def change_password(self, old_password: str, new_password: str):
return self._call('changePassword', {
2022-07-02 16:00:54 +02:00
"currentPassword": old_password,
"newPassword": new_password,
})
2022-07-05 22:12:37 +02:00
def upload_backup(self, json_data, import_handle: str):
2022-07-02 16:00:54 +02:00
if import_handle not in ["overwrite", "skip", "keep"]:
raise ValueError()
return self._call('uploadBackup', (json_data, import_handle))
2022-07-02 16:00:54 +02:00
# 2FA
def twofa_status(self):
return self._call('twoFAStatus')
2022-07-02 16:00:54 +02:00
2022-07-05 22:12:37 +02:00
def prepare_2fa(self, password: str):
return self._call('prepare2FA', password)
2022-07-02 16:00:54 +02:00
2022-07-05 22:12:37 +02:00
def save_2fa(self, password: str):
return self._call('save2FA', password)
2022-07-02 16:00:54 +02:00
2022-07-05 22:12:37 +02:00
def disable_2fa(self, password: str):
return self._call('disable2FA', password)
2022-07-02 16:00:54 +02:00
# login
2022-07-05 22:12:37 +02:00
def login(self, username: str, password: str):
return self._call('login', {
2022-07-02 16:00:54 +02:00
"username": username,
"password": password,
"token": ""
})
2022-07-05 22:12:37 +02:00
def login_by_token(self, token: str):
return self._call('loginByToken', token)
2022-07-02 16:00:54 +02:00
2022-07-05 22:12:37 +02:00
def verify_token(self, token: str, password: str):
return self._call('verifyToken', (token, password))
2022-07-02 16:00:54 +02:00
def logout(self):
return self._call('logout')
2022-07-02 16:00:54 +02:00
# setup
def need_setup(self):
return self._call('needSetup')
2022-07-02 16:00:54 +02:00
2022-07-05 22:12:37 +02:00
def setup(self, username: str, password: str):
return self._call("setup", (username, password))
2022-07-02 20:40:14 +02:00
# database
def get_database_size(self):
return self._call('getDatabaseSize')
2022-07-02 20:40:14 +02:00
def shrink_database(self):
return self._call('shrinkDatabase')