From 4579f70ca84964be3d56c44b57a746485eb86e38 Mon Sep 17 00:00:00 2001 From: Giacomo Furlan Date: Fri, 5 Apr 2024 10:10:06 +0300 Subject: [PATCH] Update Python version and dependencies - Update to python 3.9 - Update dependencies - Add Dockerfile - Update documentation Co-authored-by: Harri Hirvonsalo --- Dockerfile | 16 +++++++++++ README.md | 46 ++++++++++++++++++++++++++----- check_b2share.py | 21 +++++++++++--- nagios-plugins-eudat-b2share.spec | 16 ++++++----- requirements.txt | 15 ++-------- 5 files changed, 84 insertions(+), 30 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..33a1a09 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM rockylinux:9 +# eudat-docker.artifactory.ci.csc.fi/ + +LABEL maintainer Giacomo Furlan +LABEL description="Image to test nagios plugin on eudat b2share instances." + +RUN dnf update -y && \ + dnf install -y python3.9 && \ + dnf install -y python3.9-pip + +WORKDIR /root + +ADD check_b2share.py check_b2share.py +ADD requirements.txt requirements.txt + +RUN pip3 install -r requirements.txt diff --git a/README.md b/README.md index 77bd053..684aeb7 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,21 @@ # B2SHARE Monitoring probe for ARGO ## Setting up environment -This probe has been written for Python 3 and tested with Python 3.5.2 -You may need to install (using e.g. `pip`) the following Python modules as +This probe has been written for Python 3 and tested with Python 3.9 +You may need to install (using e.g. `pip3`) the following Python modules as they do not come with original distribution: - requests - jsonschema -- validators -- enum (in case lower than Python 3.4) ## Overview -The B2SHARE probe for ARGO does the following interaction +The B2SHARE probe for ARGO does the following interaction with B2SHARE REST API: - Search for records - Fetch record's metadata from search results - Fetch record's metadata schema - Validate record's metadata agains record's metadata schema -- If a record with file is available, check that a file +- If a record with file is available, check that a file should be able to be downloaded (HTTP HEAD request) B2SHARE ARGO probe: @@ -27,6 +25,30 @@ B2SHARE ARGO probe: ## Pre-requisites: - None + +## Package dependences + +Python modules "requests" and "jsonschema" have the following dependencies: + +```python +requests==2.31.0 +├── certifi [required: >=2017.4.17, installed: 2024.2.2] +├── charset-normalizer [required: >=2,<4, installed: 3.3.2] +├── idna [required: >=2.5,<4, installed: 3.6] +└── urllib3 [required: >=1.21.1,<3, installed: 2.2.1] + + +jsonschema==4.21.1 +├── attrs [required: >=22.2.0, installed: 23.2.0] +├── jsonschema-specifications [required: >=2023.03.6, installed: 2023.12.1] +│ └── referencing [required: >=0.31.0, installed: 0.34.0] +│ ├── attrs [required: >=22.2.0, installed: 23.2.0] +│ └── rpds-py [required: >=0.7.0, installed: 0.18.0] +├── referencing [required: >=0.28.4, installed: 0.34.0] +│ ├── attrs [required: >=22.2.0, installed: 23.2.0] +│ └── rpds-py [required: >=0.7.0, installed: 0.18.0] +└── rpds-py [required: >=0.7.1, installed: 0.18.0] +``` ## How it works? ``` @@ -50,7 +72,7 @@ optional arguments: Example -`$ python check_b2share.py -u https://b2share.eudat.eu:443 -t 15 -vv` +`$ python3 check_b2share.py -u https://b2share.eudat.eu:443 -t 15 -vv` ``` TLS certificate verification: OFF @@ -70,6 +92,16 @@ Fetching first file of the bucket. --------------------------- OK, records, metadata schemas and files are accessible. ``` +# How to run the code in a conatiner + +In the root folder of the project, build the container: +```bash +docker build -t : . +``` +Then run the code in the container +```bash +docker run -it --rm : python3 check_b2share.py -u https://b2share.eudat.eu:443 -t 15 -vv +``` ## Credits This code is based on [EUDAT-B2ACCESS/b2access-probe](https://github.com/EUDAT-B2ACCESS/b2access-probe) diff --git a/check_b2share.py b/check_b2share.py index 78d83b3..b6a6bf6 100755 --- a/check_b2share.py +++ b/check_b2share.py @@ -24,9 +24,8 @@ import jsonschema import requests -import requests.packages.urllib3 -import validators -from requests.exceptions import HTTPError +from requests.models import PreparedRequest +from requests.exceptions import HTTPError, MissingSchema class Verbosity(IntEnum): @@ -64,6 +63,20 @@ def get_dict_from_url(url, verify_tls_cert=False, verbosity=False): return r.json() +def validate_url(url): + """Validate if a string is an url. + Based on https://stackoverflow.com/a/34266413 + (python-validators package was not available as rpm package in Rocky Linux 9) + """ + prepared_request = PreparedRequest() + try: + prepared_request.prepare_url(url, None) + if not prepared_request.url: + return False + except MissingSchema: + return False + return True + if __name__ == '__main__': parser = argparse.ArgumentParser(description='B2SHARE Nagios probe') @@ -101,7 +114,7 @@ def get_dict_from_url(url, verify_tls_cert=False, verbosity=False): verbosity = Verbosity(param.verbose) # Validate parameters - if not validators.url(param.url): + if not validate_url(param.url): raise SyntaxError( 'CRITICAL: Invalid URL syntax {0}'.format( param.url)) diff --git a/nagios-plugins-eudat-b2share.spec b/nagios-plugins-eudat-b2share.spec index e1c2972..668f576 100644 --- a/nagios-plugins-eudat-b2share.spec +++ b/nagios-plugins-eudat-b2share.spec @@ -14,7 +14,7 @@ # limitations under the License. Name: nagios-plugins-eudat-b2share -Version: 0.1.1 +Version: 0.2.1 Release: 1%{?dist} Summary: Nagios B2SHARE probe License: Apache License, Version 2.0 @@ -26,11 +26,9 @@ BuildRoot: %{_tmppath}/%{name}-%{version} AutoReqProv: no -Requires: python -Requires: python-argparse -Requires: python-requests -Requires: python-jsonschema -Requires: python-validators +Requires: python3 +Requires: python3-requests +Requires: python3-jsonschema %description @@ -40,7 +38,7 @@ Nagios probe to check functionality of B2SHARE Service %setup -q %define _unpackaged_files_terminate_build 0 -%define probe_namespace eudat-b2share +%define probe_namespace eudat-b2share %install @@ -55,6 +53,10 @@ install -m 755 check_b2share.py %{buildroot}/%{_libexecdir}/argo-monitoring/prob %attr(0755,root,root) /%{_libexecdir}/argo-monitoring/probes/%{probe_namespace}/check_b2share.py %changelog +* Fri Apr 05 2024 Giacomo Furlan - 0.2.1 +- Update python to 3.9 +- Update requirements and dependencies +- Remove validator dependency * Mon Sep 02 2019 Harri Hirvonsalo - 0.1.1-1 - Improved error handling to address false positive alerts. * Wed Dec 05 2018 Harri Hirvonsalo - 0.1-1 diff --git a/requirements.txt b/requirements.txt index d79b77a..7292564 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ # # This file is part of B2SHARE Nagios monitoring plugin. # -# Copyright (C) 2018 Harri Hirvonsalo +# Copyright (C) 2024 Harri Hirvonsalo # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -15,14 +15,5 @@ # See the License for the specific language governing permissions and # limitations under the License. -attrs==19.1.0 # from jsonschema -certifi==2019.6.16 # from requests -chardet==3.0.4 # from requests -decorator==4.4.0 # from validators -idna==2.8 # from requests -jsonschema==3.0.2 -pyrsistent==0.15.4 # from jsonschema -requests==2.22.0 -six==1.12.0 # from validators, jsonschema -urllib3==1.25.3 # from requests -validators==0.14.0 +jsonschema==4.21.1 +requests==2.31.0