glances_api 0.2.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,89 @@
1
+ """Client to interact with the Glances API."""
2
+ import asyncio
3
+ import logging
4
+
5
+ import aiohttp
6
+ import async_timeout
7
+
8
+ from . import exceptions
9
+
10
+ _LOGGER = logging.getLogger(__name__)
11
+ _RESOURCE = "{schema}://{host}:{port}/api/{version}"
12
+
13
+
14
+ class Glances(object):
15
+ """A class for handling the data retrieval."""
16
+
17
+ def __init__(
18
+ self,
19
+ loop,
20
+ session,
21
+ host="localhost",
22
+ port=61208,
23
+ version=2,
24
+ ssl=False,
25
+ username=None,
26
+ password=None,
27
+ ):
28
+ """Initialize the connection."""
29
+ schema = "https" if ssl else "http"
30
+ self._loop = loop
31
+ self._session = session
32
+ self.url = _RESOURCE.format(
33
+ schema=schema, host=host, port=port, version=version
34
+ )
35
+ self.data = None
36
+ self.values = None
37
+ self.plugins = None
38
+ self.username = username
39
+ self.password = password
40
+
41
+ async def get_data(self):
42
+ """Retrieve the data."""
43
+ url = "{}/{}".format(self.url, "all")
44
+
45
+ try:
46
+ with async_timeout.timeout(5, loop=self._loop):
47
+ if self.password is None:
48
+ response = await self._session.get(url)
49
+ else:
50
+ auth = aiohttp.BasicAuth(self.username, self.password)
51
+ response = await self._session.get(url, auth=auth)
52
+
53
+ _LOGGER.debug("Response from Glances API: %s", response.status)
54
+ print(response.status)
55
+ print(response.text)
56
+ self.data = await response.json()
57
+ _LOGGER.debug(self.data)
58
+ except (asyncio.TimeoutError, aiohttp.ClientError):
59
+ _LOGGER.error("Can not load data from Glances API")
60
+ raise exceptions.GlancesApiConnectionError()
61
+
62
+ async def get_metrics(self, element):
63
+ """Get all the metrics for a monitored element."""
64
+ await self.get_data()
65
+ await self.get_plugins()
66
+
67
+ if element in self.plugins:
68
+ self.values = self.data[element]
69
+ else:
70
+ raise exceptions.GlancesApiError("Element data not available")
71
+
72
+ async def get_plugins(self):
73
+ """Retrieve the available plugins."""
74
+ url = "{}/{}".format(self.url, "pluginslist")
75
+
76
+ try:
77
+ with async_timeout.timeout(5, loop=self._loop):
78
+ if self.password is None:
79
+ response = await self._session.get(url)
80
+ else:
81
+ auth = aiohttp.BasicAuth(self.username, self.password)
82
+ response = await self._session.get(url, auth=auth)
83
+
84
+ _LOGGER.debug("Response from Glances API: %s", response.status)
85
+ self.plugins = await response.json()
86
+ _LOGGER.debug(self.plugins)
87
+ except (asyncio.TimeoutError, aiohttp.ClientError):
88
+ _LOGGER.error("Can not load plugins from Glances API")
89
+ raise exceptions.GlancesApiConnectionError()
@@ -0,0 +1,19 @@
1
+ """Exceptions for the client."""
2
+
3
+
4
+ class GlancesApiError(Exception):
5
+ """General GlancesApiError exception occurred."""
6
+
7
+ pass
8
+
9
+
10
+ class GlancesApiConnectionError(GlancesApiError):
11
+ """When a connection error is encountered."""
12
+
13
+ pass
14
+
15
+
16
+ class GlancesApiNoDataAvailable(GlancesApiError):
17
+ """When no data is available."""
18
+
19
+ pass
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017-2021 Fabian Affolter <fabian@affolter-engineering.ch>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,67 @@
1
+ Metadata-Version: 2.1
2
+ Name: glances-api
3
+ Version: 0.2.1
4
+ Summary: Python API for interacting with Glances.
5
+ Home-page: https://github.com/home-assistant-ecosystem/python-glances-api
6
+ Author: Fabian Affolter
7
+ Author-email: fabian@affolter-engineering.ch
8
+ License: MIT
9
+ Download-URL: https://github.com/home-assistant-ecosystem/python-glances-api/releases
10
+ Platform: UNKNOWN
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Environment :: Console
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Operating System :: MacOS :: MacOS X
16
+ Classifier: Operating System :: Microsoft :: Windows
17
+ Classifier: Operating System :: POSIX
18
+ Classifier: Programming Language :: Python :: 3.5
19
+ Classifier: Programming Language :: Python :: 3.6
20
+ Classifier: Topic :: Utilities
21
+ Requires-Dist: aiohttp (<4,>=3.7.4)
22
+ Requires-Dist: async-timeout (<4)
23
+
24
+ python-glances-api
25
+ ==================
26
+
27
+ A Python client for interacting with `Glances <https://nicolargo.github.io/glances/>`_.
28
+
29
+ This module is not official, developed, supported or endorsed by Glances.
30
+
31
+ Installation
32
+ ------------
33
+
34
+ The module is available from the `Python Package Index <https://pypi.python.org/pypi>`_.
35
+
36
+ .. code:: bash
37
+
38
+ $ pip3 install glances_api
39
+
40
+ Or on a Fedora-based system or on a CentOS/RHEL machine with has EPEL enabled.
41
+
42
+ .. code:: bash
43
+
44
+ $ sudo dnf -y install python3-glances-api
45
+
46
+ Usage
47
+ -----
48
+
49
+ The file ``example.py`` contains an example about how to use this module.
50
+
51
+ Development
52
+ -----------
53
+
54
+ For development is recommended to use a ``venv``.
55
+
56
+ .. code:: bash
57
+
58
+ $ python3 -m venv .
59
+ $ source bin/activate
60
+ $ python3 setup.py develop
61
+
62
+ License
63
+ -------
64
+
65
+ ``python-glances-api`` is licensed under MIT, for more details check LICENSE.
66
+
67
+
@@ -0,0 +1,8 @@
1
+ glances_api/__init__.py,sha256=txpgnHwBpxSIp9AuFhlR1gDNbJfhl-awUWYBQT-KZ8E,2937
2
+ glances_api/exceptions.py,sha256=4W4Zr-dUtt-g9By-QB_UoqYvq0cmZijFM73jGxoR874,344
3
+ glances_api-0.2.1.dist-info/LICENSE,sha256=2xZES9gdkl1p4_hiSH_OnCSBHdxq0fSjKPE9OmAzHyI,1110
4
+ glances_api-0.2.1.dist-info/METADATA,sha256=f9BG9bst-OD_lxoR3C3y6d68kgIvE_FhWuD2fnsuN64,1751
5
+ glances_api-0.2.1.dist-info/WHEEL,sha256=g4nMs7d-Xl9-xC9XovUrsDHGXt-FT0E17Yqo92DEfvY,92
6
+ glances_api-0.2.1.dist-info/top_level.txt,sha256=2Ue0146zzIXjLpGcsPV0S0tis4D6K96AENPFY7mF8V4,12
7
+ glances_api-0.2.1.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
8
+ glances_api-0.2.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.34.2)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ glances_api
@@ -0,0 +1 @@
1
+