Skip to content

Commit

Permalink
Update Python version and dependencies
Browse files Browse the repository at this point in the history
- Update to python 3.9
- Update dependencies
- Add Dockerfile
- Update documentation
  • Loading branch information
gek20 committed Apr 5, 2024
1 parent d7fe89d commit 34b8fed
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 30 deletions.
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM rockylinux:9
# eudat-docker.artifactory.ci.csc.fi/

LABEL maintainer Giacomo Furlan <[email protected]>
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
46 changes: 39 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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?

```
Expand All @@ -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
Expand All @@ -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 <name_of_the_image>:<tag_of_the_image> .
```
Then run the code in the container
```bash
docker run -it --rm <name_of_the_image>:<tag_of_the_image> 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)
21 changes: 17 additions & 4 deletions check_b2share.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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))
Expand Down
16 changes: 9 additions & 7 deletions nagios-plugins-eudat-b2share.spec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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 <[email protected]> - 0.2.1
- Update python to 3.9
- Update requirements and dependencies
- Remove validator dependency
* Mon Sep 02 2019 Harri Hirvonsalo <[email protected]> - 0.1.1-1
- Improved error handling to address false positive alerts.
* Wed Dec 05 2018 Harri Hirvonsalo <[email protected]> - 0.1-1
Expand Down
15 changes: 3 additions & 12 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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

0 comments on commit 34b8fed

Please sign in to comment.