From 5f0cb289c1d91513427ffdc8f646bc86516c2759 Mon Sep 17 00:00:00 2001 From: nico Date: Mon, 29 Jun 2020 11:03:49 +0200 Subject: black reformat * black reformatted the code --- influx.py | 57 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 25 deletions(-) (limited to 'influx.py') diff --git a/influx.py b/influx.py index e5b0065..b51d8f5 100644 --- a/influx.py +++ b/influx.py @@ -21,8 +21,8 @@ class Influx: @staticmethod def _rmspace(key: str = None, value: (str, int) = None): try: - key = key.replace(' ', '\ ') - value = value.replace(' ', '\ ') + key = key.replace(" ", "\ ") + value = value.replace(" ", "\ ") except (TypeError, AttributeError): pass @@ -36,10 +36,10 @@ class Influx: # create tag_key=tag_value pairs for all elements and append them to name for k, v in tags.items(): - output += ',{}={}'.format(*self._rmspace(k, v)) + output += ",{}={}".format(*self._rmspace(k, v)) # append key=value to name - output += ' {}={}i {}'.format(*self._rmspace(key, value), ts) + output += " {}={}i {}".format(*self._rmspace(key, value), ts) return output def write_metrics(self): @@ -47,53 +47,60 @@ class Influx: # global values cur_ts = self._timestamp() - data.append(f'ejabberd s2s_in={self._metrics.get_s2s_in()}i {cur_ts}') - data.append(f'ejabberd s2s_out={self._metrics.get_s2s_out()}i {cur_ts}') - data.append(f'ejabberd uptime={self._metrics.get_uptime()}i {cur_ts}') - data.append(f'ejabberd processes={self._metrics.get_processes()}i {cur_ts}') + data.append(f"ejabberd s2s_in={self._metrics.get_s2s_in()}i {cur_ts}") + data.append(f"ejabberd s2s_out={self._metrics.get_s2s_out()}i {cur_ts}") + data.append(f"ejabberd uptime={self._metrics.get_uptime()}i {cur_ts}") + data.append(f"ejabberd processes={self._metrics.get_processes()}i {cur_ts}") # vhost values for vhost in self._metrics.get_vhosts(): cur_ts = self._timestamp() - data.append(f'ejabberd,vhost={vhost} registered={self._metrics.get_registered(vhost)}i {cur_ts}') - data.append(f'ejabberd,vhost={vhost} muc={self._metrics.get_muc(vhost)}i {cur_ts}') + data.append(f"ejabberd,vhost={vhost} registered={self._metrics.get_registered(vhost)}i {cur_ts}") + data.append(f"ejabberd,vhost={vhost} muc={self._metrics.get_muc(vhost)}i {cur_ts}") # vhost statistics on their respective node for node in self._metrics.get_nodes(): cur_ts = self._timestamp() for k, v in self._metrics.get_online_by_status(node=node, vhost=vhost).items(): - data.append(self._parse('ejabberd_online_status', k, v, cur_ts, {'node': node, 'vhost': vhost})) + data.append(self._parse("ejabberd_online_status", k, v, cur_ts, {"node": node, "vhost": vhost})) for k, v in self._metrics.get_online_by_client(node=node, vhost=vhost).items(): - data.append(self._parse('ejabberd_online_client', k, v, cur_ts, {'node': node, 'vhost': vhost})) + data.append(self._parse("ejabberd_online_client", k, v, cur_ts, {"node": node, "vhost": vhost})) for k, v in self._metrics.get_online_by_ipversion(node=node, vhost=vhost).items(): - data.append(self._parse('ejabberd_online_ipversion', k, v, cur_ts, {'node': node, 'vhost': vhost})) + data.append(self._parse("ejabberd_online_ipversion", k, v, cur_ts, {"node": node, "vhost": vhost})) for k, v in self._metrics.get_online_by_connection(node=node, vhost=vhost).items(): - data.append(self._parse('ejabberd_online_connection', k, v, cur_ts, {'node': node, 'vhost': vhost})) + data.append(self._parse("ejabberd_online_connection", k, v, cur_ts, {"node": node, "vhost": vhost})) for cl, ipv in self._metrics.get_online_client_by_ipversion(node=node, vhost=vhost).items(): for k, v in ipv.items(): - data.append(self._parse('ejabberd_online_client_ipversion', k, v, cur_ts, - {'vhost': vhost, 'node': node, 'ipversion': k, 'client': cl})) + data.append( + self._parse( + "ejabberd_online_client_ipversion", + k, + v, + cur_ts, + {"vhost": vhost, "node": node, "ipversion": k, "client": cl}, + ) + ) # write output to database - self.client.write_points(data, time_precision='ms', batch_size=10000, protocol='line') + self.client.write_points(data, time_precision="ms", batch_size=10000, protocol="line") -if __name__ == '__main__': +if __name__ == "__main__": # load config config = Config() - if config.get('debug', default=False): + if config.get("debug", default=False): logging.getLogger().setLevel(logging.DEBUG) # credentials and parameters - url = config.get('url', default='http://localhost:5280/api') - login = config.get('login', default=None) - api = config.get('api', default='rest') + url = config.get("url", default="http://localhost:5280/api") + login = config.get("login", default=None) + api = config.get("api", default="rest") # config influxdb - influx_host = config.get('influxdb_host', default='localhost') - influx_port = config.get('influxdb_port', default=8086) - influx_dbname = config.get('influxdb_db', default='ejabberd') + influx_host = config.get("influxdb_host", default="localhost") + influx_port = config.get("influxdb_port", default=8086) + influx_dbname = config.get("influxdb_db", default="ejabberd") # init handler metrics = EjabberdMetrics(url, login, api) -- cgit v1.2.3-54-g00ecf From 044e0334d2bfb9c4b076afc562e4fb3d17c7db20 Mon Sep 17 00:00:00 2001 From: nico Date: Mon, 29 Jun 2020 18:59:16 +0200 Subject: flake8 fixup * fix trailing/ leading whitespaces * fix None comparison to use is + add noqa ignore statements for the influx escape replacements + add noqa ignore statements around the bare excepts TODO: This is only a temporary fix for most of the bare excepts, we need further specify those. --- README.md | 6 +++--- api.py | 2 +- cleanup.py | 6 +++--- influx.py | 4 ++-- prometheus.py | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) (limited to 'influx.py') diff --git a/README.md b/README.md index 7cbd2cf..ac33b00 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ listen: acl: api: - user: + user: - "api_user@magicbroccoli.de" loopback: ip: @@ -59,7 +59,7 @@ acl: api_permissions: "api access": from: - - mod_http_api + - mod_http_api who: access: allow: @@ -81,7 +81,7 @@ configuration file should be located at `/etc/ejabberd-metrics.yml`. #### systemd service To properly operate the metrics exporter tools, we created some systemd service templates, to simplify the whole process. If the `ejabberd-metrics.yml` file is not accessible for the user`nobody:nogroup`, it is required to update the -`User` and `Group` definitions inside the service file. +`User` and `Group` definitions inside the service file. If a virtualenv is used, it is required to update the `Environment=PATH` to include the `venv/bin` directory created earlier. diff --git a/api.py b/api.py index 3372976..6b83d2b 100644 --- a/api.py +++ b/api.py @@ -53,6 +53,6 @@ class EjabberdApi: return fn(self._login, data) return fn(data) - except: + except: # noqa: E722 # this needs to be more specific return {} diff --git a/cleanup.py b/cleanup.py index ee9def1..671c466 100755 --- a/cleanup.py +++ b/cleanup.py @@ -38,13 +38,13 @@ class EjabberdCleanup(EjabberdApiCalls): lastdate = None try: lastdate = datetime.datetime.strptime(last_stamp, "%Y-%m-%dT%H:%M:%SZ") - except: + except: # noqa: E722 try: lastdate = datetime.datetime.strptime(last_stamp, "%Y-%m-%dT%H:%M:%S.%fZ") - except: + except: # noqa: E722 logging.error(f"{user}@{host}: not able to parse '{last_stamp}'") return - if lastdate != None and lastdate - datetime.datetime.now() > datetime.timedelta( + if lastdate is not None and lastdate - datetime.datetime.now() > datetime.timedelta( days=self.offline_since_days ): self.delete_user(host, user, f"last seen @ {lastdate}") diff --git a/influx.py b/influx.py index b51d8f5..9437831 100644 --- a/influx.py +++ b/influx.py @@ -21,8 +21,8 @@ class Influx: @staticmethod def _rmspace(key: str = None, value: (str, int) = None): try: - key = key.replace(" ", "\ ") - value = value.replace(" ", "\ ") + key = key.replace(" ", "\ ") # noqa: W605 + value = value.replace(" ", "\ ") # noqa: W605 except (TypeError, AttributeError): pass diff --git a/prometheus.py b/prometheus.py index 6d7bbe5..a526851 100755 --- a/prometheus.py +++ b/prometheus.py @@ -23,7 +23,7 @@ class DynamicMetricsHandler(BaseHTTPRequestHandler): registry = registry.restricted_registry(params["name[]"]) try: output = generate_latest(registry) - except: + except: # noqa: E722 self.send_error(500, "error generating metric output") raise self.send_response(200) -- cgit v1.2.3-54-g00ecf