diff --git a/.github/workflows/tests-pull_request.yml b/.github/workflows/tests-pull_request.yml index 5929f5661..a47f24d69 100644 --- a/.github/workflows/tests-pull_request.yml +++ b/.github/workflows/tests-pull_request.yml @@ -25,7 +25,7 @@ jobs: python -m pip install pylint==2.17.7 pylint_runner flask - name: Lint with pyLint run: | - pylint $(git ls-files '*.py') --fail-under 4 + pylint $(git ls-files '*.py') --fail-under 6 - name: Check with Unitests run: | ./tests.py diff --git a/.github/workflows/tests-push.yml b/.github/workflows/tests-push.yml index 229ac3b74..6a82c6cb7 100644 --- a/.github/workflows/tests-push.yml +++ b/.github/workflows/tests-push.yml @@ -27,7 +27,7 @@ jobs: #if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Lint with pyLint run: | - pylint $(git ls-files '*.py') --fail-under 4 + pylint $(git ls-files '*.py') --fail-under 6 - name: Check with Unitest run: | ./tests.py diff --git a/collectors/lib/docker_engine/docker_metrics.py b/collectors/lib/docker_engine/docker_metrics.py index 88b940a09..e1aab69cb 100644 --- a/collectors/lib/docker_engine/docker_metrics.py +++ b/collectors/lib/docker_engine/docker_metrics.py @@ -15,9 +15,10 @@ from __future__ import print_function import time import requests -from prometheus_client.parser import text_string_to_metric_families +from prometheus_client.parser import text_string_to_metric_families # pylint: disable=import-error from collectors.lib.docker_engine.metric import Metric + class DockerMetrics(object): def __init__(self, url): self._url = url diff --git a/collectors/lib/postgresqlutils.py b/collectors/lib/postgresqlutils.py index 9e09f4b33..5e06d510d 100644 --- a/collectors/lib/postgresqlutils.py +++ b/collectors/lib/postgresqlutils.py @@ -15,61 +15,61 @@ Collector Utilities for PostgreSQL. """ -import sys import os -import time import socket -import errno try: - import psycopg2 + import psycopg2 except ImportError: - psycopg2 = None # handled in main() + psycopg2 = None # handled in main() -CONNECT_TIMEOUT = 2 # seconds +CONNECT_TIMEOUT = 2 # seconds from collectors.lib import utils from collectors.etc import postgresqlconf # Directories under which to search socket files SEARCH_DIRS = frozenset([ - "/var/run/postgresql", # Debian default - "/var/pgsql_socket", # MacOS default - "/usr/local/var/postgres", # custom compilation - "/tmp", # custom compilation + "/var/run/postgresql", # Debian default + "/var/pgsql_socket", # MacOS default + "/usr/local/var/postgres", # custom compilation + "/tmp", # custom compilation ]) + def find_sockdir(): - """Returns a path to PostgreSQL socket file to monitor.""" - for dir in SEARCH_DIRS: - for dirpath, dirnames, dirfiles in os.walk(dir, followlinks=True): - for name in dirfiles: - # ensure selection of PostgreSQL socket only - if (utils.is_sockfile(os.path.join(dirpath, name)) and "PGSQL" in name): - return(dirpath) + """Returns a path to PostgreSQL socket file to monitor.""" + for dir in SEARCH_DIRS: + for dirpath, dirnames, dirfiles in os.walk(dir, followlinks=True): + for name in dirfiles: + # ensure selection of PostgreSQL socket only + if (utils.is_sockfile(os.path.join(dirpath, name)) and "PGSQL" in name): + return dirpath + def postgres_connect(sockdir): - """Connects to the PostgreSQL server using the specified socket file.""" - user, password = postgresqlconf.get_user_password() + """Connects to the PostgreSQL server using the specified socket file.""" + user, password = postgresqlconf.get_user_password() + + try: + return psycopg2.connect("host='%s' user='%s' password='%s' " + "connect_timeout='%s' dbname=postgres" + % (sockdir, user, password, + CONNECT_TIMEOUT)) + except (EnvironmentError, EOFError, RuntimeError, socket.error) as e: + utils.err("Couldn't connect to DB :%s" % (e)) - try: - return psycopg2.connect("host='%s' user='%s' password='%s' " - "connect_timeout='%s' dbname=postgres" - % (sockdir, user, password, - CONNECT_TIMEOUT)) - except (EnvironmentError, EOFError, RuntimeError, socket.error) as e: - utils.err("Couldn't connect to DB :%s" % (e)) def connect(): - """Returns an initialized connection to Postgresql.""" - if psycopg2 is None: - raise RuntimeError("error: Python module 'psycopg2' is missing") + """Returns an initialized connection to Postgresql.""" + if psycopg2 is None: + raise RuntimeError("error: Python module 'psycopg2' is missing") - sockdir = find_sockdir() - if not sockdir: # Nothing to monitor - raise RuntimeError("error: Can't find postgresql socket file") + sockdir = find_sockdir() + if not sockdir: # Nothing to monitor + raise RuntimeError("error: Can't find postgresql socket file") - db = postgres_connect(sockdir) - db.autocommit=True + db = postgres_connect(sockdir) + db.autocommit = True - return db \ No newline at end of file + return db diff --git a/collectors/lib/utils.py b/collectors/lib/utils.py index 25e35c2cc..e34c0759d 100644 --- a/collectors/lib/utils.py +++ b/collectors/lib/utils.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # This file is part of tcollector. -# Copyright (C) 2013 The tcollector Authors. +# Copyright (C) 2013-2024 The tcollector Authors. # # This program is free software: you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by @@ -22,8 +22,6 @@ import errno import sys -PY3 = sys.version_info[0] > 2 - # If we're running as root and this user exists, we'll drop privileges. USER = "nobody" @@ -58,14 +56,5 @@ def err(msg): print(msg, file=sys.stderr) -if PY3: - def is_numeric(value): - return isinstance(value, (int, float)) -else: - def is_numeric(value): - try: - float(str(value)) - return True - except ValueError: - pass - return False +def is_numeric(value): + return isinstance(value, (int, float)) diff --git a/collectors/test/docker_engine/test_docker_metrics.py b/collectors/test/docker_engine/test_docker_metrics.py index 46336baa4..4b216800a 100644 --- a/collectors/test/docker_engine/test_docker_metrics.py +++ b/collectors/test/docker_engine/test_docker_metrics.py @@ -33,5 +33,6 @@ def test_eval_prometheus_line(self): provided_line = provided.get_metric_lines() self.assertEqual(expected_line, provided_line) + if __name__ == '__main__': unittest.main() diff --git a/mocks.py b/mocks.py index af2048ed7..9da2ba938 100644 --- a/mocks.py +++ b/mocks.py @@ -17,9 +17,11 @@ # for debugging real_stderr = sys.stderr + class SocketDone(Exception): pass + class Socket(): def __init__(self): self.AF_INET = 0 @@ -42,13 +44,14 @@ def close(self): return None def recvfrom(self, inBytes): - if (len(self.state['udp_in']) > 0): + if len(self.state['udp_in']) > 0: line = self.state['udp_in'].pop(0) return (line, None) else: raise SocketDone('stop reading from socket') -class Sys(): + +class Sys: def __init__(self): self.stderr = self.Stderr() self.stdout = self.Stdout() @@ -59,21 +62,22 @@ def exit(self, exitCode): msg = 'exit called with code %s\n stderr: %s\n trace: %s' raise Exception(msg % (exitCode, err, trace)) - class Stderr(): + class Stderr: def __init__(self): self.lines = [] def write(self, outString): self.lines.append(outString) - class Stdout(): + class Stdout: def __init__(self): self.lines = [] def write(self, outString): self.lines.append(outString) -class Utils(): + +class Utils: def __init__(self): self.drop_privileges = lambda: None diff --git a/tests.py b/tests.py index cbbb3db4e..0af14daab 100755 --- a/tests.py +++ b/tests.py @@ -29,9 +29,6 @@ import tcollector -PY3 = sys.version_info[0] > 2 - - def return_none(x): return None @@ -113,10 +110,7 @@ def check_access_rights(top): elif S_ISREG(mode): # file, check permissions permissions = oct(os.stat(pathname)[ST_MODE]) - if PY3: - self.assertEqual("0o100755", permissions) - else: - self.assertEqual("0100755", permissions) + self.assertEqual("0o100755", permissions) else: # unknown file type pass