diff options
author | Devaev Maxim <[email protected]> | 2020-08-30 23:30:45 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2020-08-30 23:30:45 +0300 |
commit | 146a44844aaec2889ef4ec0b320aca3f1c7fb3c0 (patch) | |
tree | e6001ea8e6beb271287a5ca1e8ac580260588b35 /kvmd | |
parent | 164f2620cf7d292bc6e540ee2cf99fb73ace1c9e (diff) |
refactoring
Diffstat (limited to 'kvmd')
-rw-r--r-- | kvmd/apps/kvmd/api/export.py | 72 | ||||
-rw-r--r-- | kvmd/apps/kvmd/api/info.py | 32 | ||||
-rw-r--r-- | kvmd/apps/kvmd/http.py | 17 | ||||
-rw-r--r-- | kvmd/apps/kvmd/server.py | 2 |
4 files changed, 74 insertions, 49 deletions
diff --git a/kvmd/apps/kvmd/api/export.py b/kvmd/apps/kvmd/api/export.py new file mode 100644 index 00000000..c37feb57 --- /dev/null +++ b/kvmd/apps/kvmd/api/export.py @@ -0,0 +1,72 @@ +# ========================================================================== # +# # +# KVMD - The main Pi-KVM daemon. # +# # +# Copyright (C) 2018 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +# ========================================================================== # + + +import asyncio +import operator + +from typing import Any +from typing import List + +from aiohttp.web import Request +from aiohttp.web import Response + +from ....plugins.atx import BaseAtx + +from ..info import InfoManager + +from ..http import exposed_http + + +# ===== +class ExportApi: + def __init__(self, info_manager: InfoManager, atx: BaseAtx) -> None: + self.__info_manager = info_manager + self.__atx = atx + + # ===== + + @exposed_http("GET", "/export/prometheus/metrics") + async def __prometheus_metrics_handler(self, _: Request) -> Response: + (atx_state, hw_state) = await asyncio.gather(*[ + self.__atx.get_state(), + self.__info_manager.get_submanager("hw").get_state(), + ]) + rows: List[str] = [] + self.__append_prometheus_rows(rows, atx_state["enabled"], "pikvm_atx_enabled") + self.__append_prometheus_rows(rows, atx_state["leds"]["power"], "pikvm_atx_power") + if hw_state is not None: + self.__append_prometheus_rows(rows, hw_state["health"], "pikvm_hw") + return Response(text="\n".join(rows)) + + def __append_prometheus_rows(self, rows: List[str], value: Any, path: str) -> None: + if isinstance(value, bool): + value = int(value) + if isinstance(value, (int, float)): + rows.extend([ + f"# TYPE {path} gauge", + f"{path} {value}", + "", + ]) + elif isinstance(value, dict): + for (sub_key, sub_value) in sorted(value.items(), key=operator.itemgetter(0)): + sub_path = (f"{path}_{sub_key}" if sub_key != "parsed_flags" else path) + self.__append_prometheus_rows(rows, sub_value, sub_path) diff --git a/kvmd/apps/kvmd/api/info.py b/kvmd/apps/kvmd/api/info.py index 478d26d8..b33a4b6c 100644 --- a/kvmd/apps/kvmd/api/info.py +++ b/kvmd/apps/kvmd/api/info.py @@ -22,8 +22,6 @@ import asyncio -from typing import Any -from typing import Dict from typing import List from aiohttp.web import Request @@ -36,22 +34,6 @@ from ..info import InfoManager from ..http import exposed_http from ..http import make_json_response -from ..http import make_text_response - - -# ==== -def _build_metrics(metrics: List[str], name: str, value: Any) -> None: - if isinstance(value, bool): - value = 1 if value else 0 - if isinstance(value, (int, float)): - metrics.append(f"# TYPE {name} gauge") - metrics.append(f"{name} {value}") - elif isinstance(value, dict): - for key, val in value.items(): - if key == "parsed_flags": - _build_metrics(metrics, name, val) - else: - _build_metrics(metrics, f"{name}_{key}", val) # ===== @@ -77,17 +59,3 @@ class InfoApi: subval=(lambda field: check_string_in_list(field, "info field", subs)), name="info fields list", ))) or subs) - - @exposed_http("GET", "/export/prometheus/metrics", False) - async def __metrics_handler(self, _: Request) -> Response: - data = await asyncio.gather(self.__info_manager.get_submanager("hw").get_state()) - if data is None: - return make_text_response("error", 500) - else: - data_exists: Dict[Any, Any] = data - health = data_exists[0]["health"] - - metrics: List[str] = [] - _build_metrics(metrics, "pikvm", health) - - return make_text_response("\n".join(metrics)) diff --git a/kvmd/apps/kvmd/http.py b/kvmd/apps/kvmd/http.py index 0e870225..729c8498 100644 --- a/kvmd/apps/kvmd/http.py +++ b/kvmd/apps/kvmd/http.py @@ -147,23 +147,6 @@ def make_json_exception(err: Exception, status: Optional[int]=None) -> aiohttp.w }, status=status) -def make_text_response( - result: str, - status: int=200, - set_cookies: Optional[Dict[str, str]]=None, -) -> aiohttp.web.Response: - - response = aiohttp.web.Response( - text=result, - status=status, - content_type="text/plain", - ) - if set_cookies: - for (key, value) in set_cookies.items(): - response.set_cookie(key, value) - return response - - # ===== async def get_multipart_field(reader: aiohttp.MultipartReader, name: str) -> aiohttp.BodyPartReader: field = await reader.next() diff --git a/kvmd/apps/kvmd/server.py b/kvmd/apps/kvmd/server.py index 52981568..40fd263a 100644 --- a/kvmd/apps/kvmd/server.py +++ b/kvmd/apps/kvmd/server.py @@ -90,6 +90,7 @@ from .api.hid import HidApi from .api.atx import AtxApi from .api.msd import MsdApi from .api.streamer import StreamerApi +from .api.export import ExportApi # ===== @@ -190,6 +191,7 @@ class KvmdServer(HttpServer): # pylint: disable=too-many-arguments,too-many-ins AtxApi(atx), MsdApi(msd, sync_chunk_size), StreamerApi(streamer), + ExportApi(info_manager, atx), ] self.__ws_handlers: Dict[str, Callable] = {} |