Add -t/--timeout parameter and introduce some new pre-commit hooks

This commit is contained in:
Benjamin Renard 2024-03-13 18:36:27 +01:00
parent bf0a560566
commit a9ae368521
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC
4 changed files with 83 additions and 36 deletions

View file

@ -1,31 +1,64 @@
# Pre-commit hooks to run tests and ensure code is cleaned.
# See https://pre-commit.com for more information
---
repos:
- repo: local
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.6
hooks:
- id: pylint
- id: ruff
args: ["--fix"]
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
hooks:
- id: pyupgrade
args: ["--keep-percent-format", "--py37-plus"]
- repo: https://github.com/psf/black
rev: 23.11.0
hooks:
- id: black
args: ["--target-version", "py37", "--line-length", "100"]
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
args: ["--profile", "black", "--line-length", "100"]
- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
hooks:
- id: flake8
args: ["--max-line-length=100"]
- repo: https://github.com/codespell-project/codespell
rev: v2.2.2
hooks:
- id: codespell
args:
- --ignore-words-list=exten
- --skip="./.*,*.csv,*.json,*.ini,*.subject,*.txt,*.html,*.log,*.conf"
- --quiet-level=2
- --ignore-regex=.*codespell-ignore$
# - --write-changes # Uncomment to write changes
exclude_types: [csv, json]
- repo: https://github.com/adrienverge/yamllint
rev: v1.32.0
hooks:
- id: yamllint
ignore: .github/
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.7.1
hooks:
- id: prettier
args: ["--print-width", "100"]
- repo: local
hooks:
- id: pylint
name: pylint
entry: pylint
language: system
types: [python]
require_serial: true
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
- repo: https://github.com/PyCQA/bandit
rev: 1.7.5
hooks:
- id: flake8
args: ['--max-line-length=100']
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
hooks:
- id: pyupgrade
args: ['--keep-percent-format', '--py37-plus']
- repo: https://github.com/psf/black
rev: 22.12.0
hooks:
- id: black
args: ['--target-version', 'py37', '--line-length', '100']
- repo: https://github.com/PyCQA/isort
rev: 5.11.4
hooks:
- id: isort
args: ['--profile', 'black', '--line-length', '100']
- id: bandit
args: [--skip, "B101", --recursive]
minimum_pre_commit_version: 3.2.0

View file

@ -19,18 +19,22 @@ service nagios-nrpe-server reload
## Usage
```
usage: check_esphome_devices [-h] [-d] [-H HOST] [-r RETRY_COUNT] [-D RETRY_DELAY] [-x EXCLUDE]
usage: check_esphome_devices [-h] [-d] [-H HOST] [-r RETRY_COUNT] [-D RETRY_DELAY]
[-t TIMEOUT] [-x EXCLUDE]
optional arguments:
options:
-h, --help show this help message and exit
-d, --debug
-H HOST, --host HOST ESPHome dashboard URL (default: http://127.0.0.1:6052)
-r RETRY_COUNT, --retry RETRY_COUNT
Number of retry to retreive device status (default: 4)
Number of retry to retrieve device status (default: 4)
-D RETRY_DELAY, --delay RETRY_DELAY
Delay in second between two retry to retreive device status (default: 1s)
Delay in second between two retry to retrieve device status
(default: 1s)
-t TIMEOUT, --timeout TIMEOUT
Timeout in second on API requests (default: 10s)
-x EXCLUDE, --exclude EXCLUDE
Regex exclude pattern(
Regex exclude pattern(s)
```
## Copyright
@ -41,7 +45,6 @@ Copyright (c) 2022 Benjamin Renard <brenard@zionetrix.net>
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 3 as published by the Free Software Foundation.
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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

View file

@ -24,7 +24,7 @@ sed -i "s/^VERSION *=.*$/VERSION = '$VERSION'/" $BDIR/check_esphome_devices
if [ -z "$DEBIAN_CODENAME" ]
then
echo "Retreive debian codename using lsb_release..."
echo "Retrieve debian codename using lsb_release..."
DEBIAN_CODENAME=$( lsb_release -c -s )
else
echo "Use debian codename from environment ($DEBIAN_CODENAME)"

View file

@ -33,6 +33,7 @@ STATUS = {"OK": 0, "WARNING": 1, "CRITICAL": 2, "UNKNOWN": 3}
DEFAULT_HOST = "http://127.0.0.1:6052"
DEFAULT_RETRY_COUNT = 4
DEFAULT_RETRY_DELAY = 1
DEFAULT_TIMEOUT = 10
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--debug", action="store_true", dest="debug", default=False)
@ -52,7 +53,7 @@ parser.add_argument(
"--retry",
action="store",
dest="retry_count",
help=("Number of retry to retreive device status " f"(default: {DEFAULT_RETRY_COUNT})"),
help=("Number of retry to retrieve device status " f"(default: {DEFAULT_RETRY_COUNT})"),
type=int,
default=DEFAULT_RETRY_COUNT,
)
@ -63,13 +64,23 @@ parser.add_argument(
action="store",
dest="retry_delay",
help=(
"Delay in second between two retry to retreive device status "
"Delay in second between two retry to retrieve device status "
f"(default: {DEFAULT_RETRY_DELAY}s)"
),
type=int,
default=DEFAULT_RETRY_DELAY,
)
parser.add_argument(
"-t",
"--timeout",
action="store",
dest="timeout",
help=f"Timeout in second on API requests (default: {DEFAULT_TIMEOUT}s)",
type=int,
default=DEFAULT_TIMEOUT,
)
def exclude_pattern(value):
"""Check and compile exclusion pattern parameter"""
@ -106,17 +117,17 @@ def is_excluded(name):
return False
r = requests.get(f"{options.host}/devices")
r = requests.get(f"{options.host}/devices", timeout=options.timeout)
devices_data = r.json()
logging.debug("Devices data: %s (%s)", devices_data, type(devices_data))
if not devices_data:
print("UNKNOWN - Fail to retreive devices using ESPHome Dashboard API")
print("UNKNOWN - Fail to retrieve devices using ESPHome Dashboard API")
sys.exit(STATUS["UNKNOWN"])
COUNT = 0
while COUNT < options.retry_count:
r = requests.get(f"{options.host}/ping")
r = requests.get(f"{options.host}/ping", timeout=options.timeout)
COUNT += 1
ping_data = r.json()
logging.debug("Ping data: %s (%s)", ping_data, type(ping_data))
@ -135,7 +146,7 @@ while COUNT < options.retry_count:
time.sleep(options.retry_delay)
if not ping_data:
print("UNKNOWN - Fail to retreive devices status " "using ESPHome Dashboard API")
print("UNKNOWN - Fail to retrieve devices status using ESPHome Dashboard API")
sys.exit(STATUS["UNKNOWN"])
UPDATE_AVAILABLE = 0