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/Jenkinsfile b/Jenkinsfile index 8b848e5..0e94321 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -13,7 +13,16 @@ pipeline { stages { stage ('Build'){ parallel { - + stage ('Build Rocky 9 RPM') { + steps { + echo 'Building Rocky 9 RPM...' + withCredentials(bindings: [sshUserPrivateKey(credentialsId: 'jenkins-rpm-repo', usernameVariable: 'REPOUSER', \ + keyFileVariable: 'REPOKEY')]) { + sh "/home/jenkins/build-rpm.sh -w ${WORKSPACE} -b ${BRANCH_NAME} -d rocky9 -p ${PROJECT_DIR} -s ${REPOKEY}" + } + archiveArtifacts artifacts: '**/*.rpm', fingerprint: true + } + } stage ('Build Centos 7') { agent { docker { 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/argo-probe-eudat-b2share b/argo-probe-eudat-b2share new file mode 100644 index 0000000..8f8329f --- /dev/null +++ b/argo-probe-eudat-b2share @@ -0,0 +1,46 @@ +Name: argo-probe-eudat-b2share +Version: 0.7 +Release: 1%{?dist} +Summary: Monitoring scripts that check the functionalities of B2SHARE +License: GPLv3+ +Packager: Themis Zamani + +Source: %{name}-%{version}.tar.gz +BuildArch: noarch +BuildRoot: %{_tmppath}/%{name}-%{version} +AutoReqProv: no +Requires: python3 +Requires: python3-requests +Requires: python-jsonschema +Requires: python-enum34 + +%description +Nagios probe to check functionality of B2SHARE service + +%prep +%setup -q + +%define _unpackaged_files_terminate_build 0 + +%install +install -d %{buildroot}/%{_libexecdir}/argo/probes/eudat-b2share +install -m 755 check_b2share.py %{buildroot}/%{_libexecdir}/argo/probes/eudat-b2share/check_b2share.py + +%files +%dir /%{_libexecdir}/argo +%dir /%{_libexecdir}/argo/probes/ +%dir /%{_libexecdir}/argo/probes/eudat-b2share + +%attr(0755,root,root) /%{_libexecdir}/argo/probes/eudat-b2share/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 Mar 14 2022 Themis Zamani - 0.5 +- Update package prerequisites based on argo monitoring. +* Tue Nov 27 2018 Themis Zamani - 0.1-1 +- Initial version of the package. +* Tue Nov 27 2018 Harri Hirvonsalo - 0.1-1 +- Initial version of the package. diff --git a/argo-probe-eudat-b2share.spec b/argo-probe-eudat-b2share.spec index 9c6c62c..6512213 100644 --- a/argo-probe-eudat-b2share.spec +++ b/argo-probe-eudat-b2share.spec @@ -1,5 +1,5 @@ Name: argo-probe-eudat-b2share -Version: 0.6 +Version: 0.7 Release: 1%{?dist} Summary: Monitoring scripts that check the functionalities of B2SHARE License: GPLv3+ @@ -9,7 +9,10 @@ Source: %{name}-%{version}.tar.gz BuildArch: noarch BuildRoot: %{_tmppath}/%{name}-%{version} AutoReqProv: no -Requires: python-requests, python2-jsonschema, python-enum34 +Requires: python3 +Requires: python3-requests +Requires: python-jsonschema +Requires: python-enum34 %description Nagios probe to check functionality of B2SHARE service @@ -31,10 +34,15 @@ install -m 755 check_b2share.py %{buildroot}/%{_libexecdir}/argo/probes/eudat-b2 %attr(0755,root,root) /%{_libexecdir}/argo/probes/eudat-b2share/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 Mar 14 2022 Themis Zamani - 0.5 +- Update package prerequisites based on argo monitoring. * Mon Mar 14 2022 Themis Zamani - 0.5 - Update package prerequisites based on argo monitoring. * Tue Nov 27 2018 Themis Zamani - 0.1-1 - Initial version of the package. * Tue Nov 27 2018 Harri Hirvonsalo - 0.1-1 - Initial version of the package. - diff --git a/check_b2share.py b/check_b2share.py index 1aea6e4..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)) @@ -149,14 +162,10 @@ def get_dict_from_url(url, verify_tls_cert=False, verbosity=False): rec_with_files_url = None for hit in search_results['hits']['hits']: - if 'files' in hit: - # Check if there are files in the record - if len(hit['files']) > 0: - # NTS: Could throw KeyError if there is something - # seriously wrong or B2SHARE REST API responses have - # changed. - rec_with_files_url = hit['links']['self'] - break + # Check if there are files in the record + if len(hit.get('files', "")) > 0: + rec_with_files_url = hit['links']['self'] + break if rec_with_files_url: diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7292564 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,19 @@ +# +# This file is part of B2SHARE Nagios monitoring plugin. +# +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +jsonschema==4.21.1 +requests==2.31.0