diff --git a/argo-probe-eudat-b2find.spec b/argo-probe-eudat-b2find.spec index aed9036..2ddce14 100644 --- a/argo-probe-eudat-b2find.spec +++ b/argo-probe-eudat-b2find.spec @@ -1,7 +1,7 @@ Name: argo-probe-eudat-b2find -Version: 2.6 +Version: 2.7 Release: 3%{?dist} -Summary: B2FIND metrics to check the functionality of the service. +Summary: B2FIND metrics to check the functionality of the service. License: GPLv3+ Packager: Heinrich Widmann Group: Application @@ -11,9 +11,10 @@ BuildArch: noarch BuildRoot: %{_tmppath}/%{name}-%{version} -#BuildRequires: +#BuildRequires: Requires: python Requires: python-argparse +Requires: python-requests #Requires: python-json #Requires: python-urllib #Requires: python-urllib2 @@ -24,7 +25,7 @@ This plugin provides the nessecary script to check search functionality of the B %prep %setup -q -%define _unpackaged_files_terminate_build 0 +%define _unpackaged_files_terminate_build 0 install -d %{buildroot}/%{_libexecdir}/argo/probes/eudat-b2find install -m 755 checkB2FIND.py %{buildroot}/%{_libexecdir}/argo/probes/eudat-b2find/checkB2FIND.py @@ -39,6 +40,8 @@ install -m 755 checkB2FIND.py %{buildroot}/%{_libexecdir}/argo/probes/eudat-b2fi %pre %changelog +* Thu Sep 22 2022 Katarina Zailac - 2.7-1 +- ARGO-3992 Fix how checkB2FIND.py probe handles timeout * Fri Jun 22 2022 Themis Zamani - 2.6-1 * Fri Mar 04 2022 Themis Zamani - 2.5-3 * Fri Mar 26 2021 Themis Zamani - 2.4-1 diff --git a/checkB2FIND.py b/checkB2FIND.py index af8abcc..1e03d94 100644 --- a/checkB2FIND.py +++ b/checkB2FIND.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -"""checkB2FIND.py performs checks according different probes and +"""checkB2FIND.py performs checks according different probes and returns the appropriate messages and codes. Copyright (c) 2016 Heinrich Widmann (DKRZ) @@ -14,80 +14,63 @@ THE SOFTWARE. """ -import logging -import traceback -import os -import sys -import io -import time import argparse -# import timeout_decorator import socket -import json +import sys +import time + import requests -def check_url(url): - # Checks and validates a url via urllib module - # - # Parameters: - # ----------- - # (url) url - Url to check - # - # Return Values: - # -------------- - # 1. (boolean) result + +def check_url(url, timeout): + """ + Checks and validates URL using requests module + param url: URL being tested + param timeout: timeout in seconds + """ rta = 0 resplen = '--' try: start = time.time() - response = requests.get(url, timeout=1) - if(response.status_code < 501): - msg = '[OK]' - retcode = 0 - rta = time.time()-start - - except socket.timeout as e: + response = requests.get(url, timeout=timeout) + response.raise_for_status() + rta = time.time() - start + + except requests.exceptions.Timeout as e: msg = " [Socket Timeout] %s" % e - retcode = 2 #catched + retcode = 2 # caught + + except requests.exceptions.HTTPError as e: + msg = " [HTTPError] %s" % e + retcode = 2 + except IOError as e: msg = " [IOError] %s" % e retcode = 1 + except ValueError as e: msg = " [ValueError] %s" % e - retcode = 1 #catched + retcode = 1 # caught + except Exception as e: msg = " [Unknown Error] %s" % e - retcode = 3 #catched + retcode = 3 # caught + else: msg = '[OK]' retcode = 0 - return (retcode, msg, resplen, rta) + return retcode, msg, resplen, rta -def check_ckan_action(actionreq, data, rows): - # Checks and validates a request or action submitted to CKAN - # - # Parameters: - # ----------- - # (url) url - Url to check - # - # Return Values: - # -------------- - # 1. (boolean) result +def check_ckan_action(actionreq, data, timeout, rows): resplen = 0 rta = 0 try: start = time.time() - # #HEW-T print('actionreq %s' % actionreq) - # #HEW-T print('data %s' % data) - data_string = json.dumps(data).encode('utf8') - response = requests.get(actionreq, params=data) # # _string) - # #HEW-T print('response %s' % response) + response = requests.get(actionreq, timeout=timeout, params=data) result = response.json()['result'] - # #records= json.loads(response.read())##['result'] - # #print('records %s' % records[:10]) rta = time.time()-start except socket.timeout as e: @@ -98,10 +81,10 @@ def check_ckan_action(actionreq, data, rows): retcode = 1 except ValueError as e: msg = " [ValueError] %s " % e - retcode = 1 # catched + retcode = 1 # caught except Exception as e: msg = " [Error] %s " % e - retcode = 3 # catched + retcode = 3 # caught else: msg = '[OK]' retcode = 0 @@ -109,10 +92,12 @@ def check_ckan_action(actionreq, data, rows): if actionreq.endswith('organization_show'): resplen = result['package_count'] + else: resplen = len(result) - # print('resplen %s' % resplen) - return (retcode, msg, resplen, rta) + + return retcode, msg, resplen, rta + def main(): B2FIND_version = '2.4' @@ -127,30 +112,27 @@ def main(): sys.exit(checkProbes(args)) -# # @timeout_decorator.timeout(args.timeout) -def checkProbes(args): +def checkProbes(args): # # Settings for CKAN client and API - # # print 'args %s' % args - - b2find_url = 'http://'+args.hostname + b2find_url = 'http://' + args.hostname if args.port: - b2find_url += ':'+args.port - # print (' Check the service endpoint %s' % b2find_url) - ckanapi3 = b2find_url+'/api/3' + b2find_url += ':' + args.port + ckanapi3act = b2find_url+'/api/3/action/' ckan_limit = 100 - start = time.time() + suppProbes = [ + 'URLcheck', 'ListDatasets', 'ListCommunities', 'ShowGroupENES' + ] - # print ('| %-15s | %-7s | %-20s | %-7s | %-6s |' % ('Probe','RetCode','Message','ResLength','RTA')) - # print ('-----------------------------------------------') - suppProbes = ['URLcheck', 'ListDatasets', 'ListCommunities', 'ShowGroupENES'] if args.action == 'all': probes = suppProbes + else: if args.action in suppProbes: probes = [args.action] + else: print('Action %s is not supported' % args.action) sys.exit(-1) @@ -159,79 +141,114 @@ def checkProbes(args): for probe in probes: data_dict = {} if probe == 'URLcheck': - answer = check_url(b2find_url) - # print ('| %s | %s | %s' % (probe,answer[0],answer[1])) + answer = check_url(b2find_url, args.timeout) else: if probe == 'ListDatasets': action = 'package_list' + elif probe == 'ListCommunities': action = 'organization_list' + elif probe == 'ShowGroupENES': action = 'organization_show' data_dict = {'id': 'enes'} - actionreq = ckanapi3act+action - answer = check_ckan_action(actionreq, data_dict, ckan_limit) + actionreq = ckanapi3act + action - # print ('| %-15s | %-7s | %-20s | %-7s | %-7.2f | ' % (probe,answer[0], answer[1], answer[2], answer[3])) + answer = check_ckan_action( + actionreq, data_dict, args.timeout, ckan_limit + ) - print(' %-15s is %-7s - %-20s - %-7s - %-7.2f ' % (probe, answer[1], answer[0], answer[2], answer[3])) + print( + ' %-15s is %-7s - %-20s - %-7s - %-7.2f ' % ( + probe, answer[1], answer[0], answer[2], answer[3] + ) + ) print(' on service %s' % b2find_url) - if answer[0] > totretcode : totretcode = answer[0] + if answer[0] > totretcode: + totretcode = answer[0] return totretcode + def ValidateValues(arguments): - """ Validate values - input values """ + """ Validate values - input values """ - if arguments.timeout <= 0: - print("\nInvalid timeout value: %s\n" % arguments.timeout) - print_help() - exit() + if arguments.timeout <= 0: + print("\nInvalid timeout value: %s\n" % arguments.timeout) + print_help() + exit() - if arguments.hostname is None: - print("\nNo hostname provided\n") - print_help() - exit() + if arguments.hostname is None: + print("\nNo hostname provided\n") + print_help() + exit() def print_help(): - """ Print help values.""" - - print("usage: checkB2FIND.py -H -p") - print("--- ---- ---- ---- ---- ---- ----\n") - print("main arguments:") - print("-H hostname, URL of the B2FIND service, to which probes are submitted (default is b2find.eudat.eu)") - print("\n") - print("optional arguments:") - print(" -h, --help show this help message and exit") - print("-p port, The B2FIND server port.") - print("-t timeout, Time threshold to wait before timeout (in second).") - print("-v verbose") - print("-e version, Prints the B2FIND and CKAN version and exits.") - print("-a action,Action which has to be excecuted and checked. Supported actions are URLcheck, ListDatasets, ListCommunities, ShowGroupENES or all ") + """ Print help values.""" + + print("usage: checkB2FIND.py -H -p") + print("--- ---- ---- ---- ---- ---- ----\n") + print("main arguments:") + print( + "-H hostname, URL of the B2FIND service, to which probes are submitted " + "(default is b2find.eudat.eu)" + ) + print("\n") + print("optional arguments:") + print(" -h, --help show this help message and exit") + print("-p port, The B2FIND server port.") + print("-t timeout, Time threshold to wait before timeout (in second).") + print("-v verbose") + print("-e version, Prints the B2FIND and CKAN version and exits.") + print( + "-a action,Action which has to be excecuted and checked. " + "Supported actions are URLcheck, ListDatasets, ListCommunities, " + "ShowGroupENES or all " + ) + def get_args(): p = argparse.ArgumentParser( formatter_class=argparse.RawDescriptionHelpFormatter, - description = "Description: Performs checks according different probes and returns the appropriate messages and codes." + description="Description: Performs checks according different probes " + "and returns the appropriate messages and codes." ) - - p.add_argument('--version', '-v', help="prints the B2FIND and CKAN version and exits", action='store_true') - p.add_argument('--timeout', '-t', help="time out : After given number of seconds excecution terminates.", default=1000, metavar='INT') - p.add_argument('--action', '-a', help="Action which has to be excecuted and checked. Supported actions are URLcheck, ListDatasets, ListCommunities, ShowGroupENES or all (default)", default='all', metavar='STRING') - p.add_argument('--hostname', '-H', help='Hostname of the B2FIND service, to which probes are submitted (default is b2find.eudat.eu)', default='b2find.eudat.eu', metavar='URL') - p.add_argument('--port', '-p', help='(Optional) Port of the B2FIND service, to which probes are submitted (default is None)', default=None, metavar='URL') - -## p.add_argument('pattern', help='CKAN search pattern, i.e. by logical conjunctions joined field:value terms.', default='*:*', metavar='PATTERN', nargs='*') - + + p.add_argument( + '--version', '-v', help="prints the B2FIND and CKAN version and exits", + action='store_true' + ) + p.add_argument( + '--timeout', '-t', default=1000, type=float, + help="time out : After given number of seconds excecution terminates." + ) + p.add_argument( + '--action', '-a', default='all', metavar='STRING', + help="Action which has to be excecuted and checked. " + "Supported actions are URLcheck, ListDatasets, ListCommunities, " + "ShowGroupENES or all (default)" + ) + p.add_argument( + '--hostname', '-H', default='b2find.eudat.eu', metavar='URL', + help='Hostname of the B2FIND service, to which probes are submitted ' + '(default is b2find.eudat.eu)' + ) + p.add_argument( + '--port', '-p', default=None, metavar='URL', + help='(Optional) Port of the B2FIND service, to which probes are ' + 'submitted (default is None)' + ) + args = p.parse_args() ValidateValues(args) - + return args - + + if __name__ == "__main__": main()