From 5aad8b4d01e325a8eaf5a70fd0094ab24dbc6bc0 Mon Sep 17 00:00:00 2001 From: Themis Zamani Date: Fri, 19 Oct 2018 16:41:30 +0300 Subject: [PATCH 01/13] B2STAGE initial probe --- LICENSE | 201 ++++++++++++++++++++++++++++++ Makefile | 19 +++ README.md | 53 +++++++- check_b2stage_http-api.py | 123 ++++++++++++++++++ nagios-plugins-eudat-b2stage.spec | 38 ++++++ 5 files changed, 433 insertions(+), 1 deletion(-) create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 check_b2stage_http-api.py create mode 100644 nagios-plugins-eudat-b2stage.spec diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..cd0e1e0 --- /dev/null +++ b/Makefile @@ -0,0 +1,19 @@ +PKGNAME=nagios-plugins-eudat-b2stage +SPECFILE=${PKGNAME}.spec +FILES=Makefile ${SPECFILE} check_b2stage_http-api.py + +PKGVERSION=$(shell grep -s '^Version:' $(SPECFILE) | sed -e 's/Version:\s*//') + + +dist: + rm -rf dist + mkdir -p dist/${PKGNAME}-${PKGVERSION} + cp -pr ${FILES} dist/${PKGNAME}-${PKGVERSION}/. + cd dist ; tar cfz ../${PKGNAME}-${PKGVERSION}.tar.gz ${PKGNAME}-${PKGVERSION} + rm -rf dist + +sources: dist + +clean: + rm -rf ${PKGNAME}-${PKGVERSION}.tar.gz + rm -rf dist diff --git a/README.md b/README.md index 9588923..892f95e 100644 --- a/README.md +++ b/README.md @@ -1 +1,52 @@ -# nagios-plugin-b2stage + +# B2STAGE plugin for Nagios + +Nagios plugin to check B2STAGE functionality + +## Usage + +NAME +``` + check_b2stage_http-api.py - B2STAGE Http API Nagios plugin +``` + +DESCRIPTION + RESTful HTTP-API for the B2STAGE service inside the EUDAT project. + The plugin checks the health of B2STAGE HTTP-API service. + +SYNOPSIS + check_b2stage_http-api.py [--version] [--help] [--verbose ] + [--timeout ] --hostname [--port ] + + Options: + --help,-h : Display this help. + --verbose,-v : Same as debug option (0-9). + --timeout,-t : Time threshold to wait before timeout (in second). + + --hostname,-H : The B2STAGER server host + The B2STAGE server host. It can be a DNS name or an IP address. + + --port + The B2STAGE server port (default to 80). + + +EXAMPLES + Using script: + ./check_b2stage_http-api.py -H www.b2stage.fr -p 443 + diff --git a/check_b2stage_http-api.py b/check_b2stage_http-api.py new file mode 100644 index 0000000..d32299f --- /dev/null +++ b/check_b2stage_http-api.py @@ -0,0 +1,123 @@ +#!/usr/bin/python + +import sys, requests +import argparse + +# ############################################################################## +# B2STAGE Client # +# ############################################################################## + + +def ValidateValues(arguments): + """ Validate values - input values """ + + 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 not arguments.hostname.startswith("http"): + print("\nNo schema supplied with hostname, did you mean https://%s?\n" % arguments.hostname) + print_help() + exit() + +def print_help(): + """ Print help values.""" + + print "usage: check_b2stage_http-api.py -H -p" + print "--- ---- ---- ---- ---- ---- ----\n" + print "main arguments:" + print "-H hostname" + print "\n" + print "optional arguments:" + print " -h, --help show this help message and exit" + print "-p port" + print "-t timeout" + print "-v verbose" + + + +def debugValues(arguments): + """ Print debug values. + Args: + arguments: the input arguments + """ + if arguments.debug: + print "[debugValues] - hostname:"+ arguments.hostname + if arguments.port!='': + print "[debugValues] - port:" + arguments.port + if arguments.timeout!='': + print "[debugValues] - timeout:" + str(arguments.timeout) + if arguments.t!='': + print "[debugValues] - timeout:" + str(arguments.t) + +def checkHealth(URL,timeout): + """ Check service status. + Args: + URL : service hostname + timeout : how long should we wati + """ + try: + out = requests.get(url=URL, timeout=timeout) + + content = out.json() + + if out.status_code != 200: + description = "WARNING - Unexpected status code %s" % out.status_code + exit_code = 1 + + if content['Response']['data'] != "Server is alive!": + description = "WARNING - Unexpected response: %s" % content['Response']['data'] + exit_code = 1 + + description = "OK - Service reachable" + exit_code = 0 + + except requests.exceptions.ConnectionError: + description = "CRITICAL - Service unreachable" + exit_code = 2 + + description = "UNKNOWN - Status unknown" + exit_code = 3 + return description, exit_code + +def printResult(description, exit_code): + """ Print the predefined values + Args: + description: the nagios description + exit_code: the code that should be returned to nagios + """ + + print 'description' + sys.exit(exit_code) + +def main(): + + parser = argparse.ArgumentParser(description='B2STAGE probe ' + 'Supports healthcheck.') + parser.add_argument("--hostname", "-H", help='The Hostname of B2STAGE service') + parser.add_argument("--port", "-p", type=int) + parser.add_argument("--timeout", "-t", metavar="seconds", help="Timeout in seconds. Must be greater than zero", type=int, default=30) + parser.add_argument("--verbose","-v", dest='debug', help='Set verbosity level', action='count', default=0) + arguments = parser.parse_args() + ValidateValues(arguments) + NAGIOS_RESULT = 0 + + URL=arguments.hostname + if arguments.port is not None: + URL += ":%s" % arguments.port + + URL += "/api/status" + timeout = arguments.timeout + description, exit_code = checkHealth(URL,timeout) + printResult(description, exit_code) + + +if __name__ == "__main__": + main() + diff --git a/nagios-plugins-eudat-b2stage.spec b/nagios-plugins-eudat-b2stage.spec new file mode 100644 index 0000000..0372fd2 --- /dev/null +++ b/nagios-plugins-eudat-b2stage.spec @@ -0,0 +1,38 @@ +Name: nagios-plugins-eudat-b2stage +Version: 0.5 +Release: 1%{?dist} +Summary: Nagios probe for B2STAGE +License: GPLv3+ +Packager: Themis Zamani + +Source: %{name}-%{version}.tar.gz +BuildArch: noarch +BuildRoot: %{_tmppath}/%{name}-%{version} +AutoReqProv: no + +%description +Nagios probe to check functionality of B2STAGE service + +%prep +%setup -q + +%define _unpackaged_files_terminate_build 0 + +%install + +install -d %{buildroot}/%{_libexecdir}/argo-monitoring/probes/eudat-b2stage +install -d %{buildroot}/%{_sysconfdir}/nagios/plugins/eudat-b2stage +install -m 755 check_cas.pl %{buildroot}/%{_libexecdir}/argo-monitoring/probes/eudat-b2stage/check_b2stage_http-api.py + +%files +%dir /%{_libexecdir}/argo-monitoring +%dir /%{_libexecdir}/argo-monitoring/probes/ +%dir /%{_libexecdir}/argo-monitoring/probes/eudat-b2stage + +%attr(0755,root,root) /%{_libexecdir}/argo-monitoring/probes//eudat-b2stage/check_b2stage_http-api.py + +%changelog +* Fri Oct 19 2018 Themis Zamani - 0.1-1 +- Initial version of the package. +* Thu Oct 18 2018 Mattia D'Antonio - 0.1-1 +- Initial version of the package. From 3022cad92f1ca9f1fd1e15c3c4429d15b2a31979 Mon Sep 17 00:00:00 2001 From: Themis Zamani Date: Fri, 19 Oct 2018 16:46:18 +0300 Subject: [PATCH 02/13] B2STAGE initial probe --- nagios-plugins-eudat-b2stage.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nagios-plugins-eudat-b2stage.spec b/nagios-plugins-eudat-b2stage.spec index 0372fd2..5fb493a 100644 --- a/nagios-plugins-eudat-b2stage.spec +++ b/nagios-plugins-eudat-b2stage.spec @@ -22,7 +22,7 @@ Nagios probe to check functionality of B2STAGE service install -d %{buildroot}/%{_libexecdir}/argo-monitoring/probes/eudat-b2stage install -d %{buildroot}/%{_sysconfdir}/nagios/plugins/eudat-b2stage -install -m 755 check_cas.pl %{buildroot}/%{_libexecdir}/argo-monitoring/probes/eudat-b2stage/check_b2stage_http-api.py +install -m 755 check_b2stage_http-api.py %{buildroot}/%{_libexecdir}/argo-monitoring/probes/eudat-b2stage/check_b2stage_http-api.py %files %dir /%{_libexecdir}/argo-monitoring From 91048285d7b9d00cb4526cb54e9d9951a27a6e01 Mon Sep 17 00:00:00 2001 From: Themis Zamani Date: Fri, 19 Oct 2018 16:49:46 +0300 Subject: [PATCH 03/13] B2STAGE initial probe --- check_b2stage_http-api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/check_b2stage_http-api.py b/check_b2stage_http-api.py index d32299f..fd3b717 100644 --- a/check_b2stage_http-api.py +++ b/check_b2stage_http-api.py @@ -87,11 +87,11 @@ def checkHealth(URL,timeout): return description, exit_code def printResult(description, exit_code): - """ Print the predefined values + """ Print the predefined values Args: description: the nagios description exit_code: the code that should be returned to nagios - """ + """ print 'description' sys.exit(exit_code) From 7abf32fb7970373038b5b9b39d5f3aa2d598e991 Mon Sep 17 00:00:00 2001 From: Themis Zamani Date: Fri, 19 Oct 2018 16:54:21 +0300 Subject: [PATCH 04/13] B2STAGE initial probe --- README.md | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 892f95e..c541cab 100644 --- a/README.md +++ b/README.md @@ -5,18 +5,23 @@ Nagios plugin to check B2STAGE functionality ## Usage -NAME +### NAME + ``` check_b2stage_http-api.py - B2STAGE Http API Nagios plugin ``` -DESCRIPTION +### DESCRIPTION + RESTful HTTP-API for the B2STAGE service inside the EUDAT project. The plugin checks the health of B2STAGE HTTP-API service. -SYNOPSIS +### SYNOPSIS + +``` check_b2stage_http-api.py [--version] [--help] [--verbose ] [--timeout ] --hostname [--port ] +``` Options: --help,-h : Display this help. @@ -26,10 +31,10 @@ SYNOPSIS --hostname,-H : The B2STAGER server host Date: Thu, 25 Oct 2018 09:58:31 +0200 Subject: [PATCH 05/13] BUG fix: syntax errors in print Fixes in code formatting --- check_b2stage_http-api.py | 61 ++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/check_b2stage_http-api.py b/check_b2stage_http-api.py index fd3b717..6cc24a3 100644 --- a/check_b2stage_http-api.py +++ b/check_b2stage_http-api.py @@ -1,6 +1,7 @@ #!/usr/bin/python -import sys, requests +import sys +import requests import argparse # ############################################################################## @@ -26,20 +27,20 @@ def ValidateValues(arguments): print_help() exit() + def print_help(): """ Print help values.""" - print "usage: check_b2stage_http-api.py -H -p" - print "--- ---- ---- ---- ---- ---- ----\n" - print "main arguments:" - print "-H hostname" - print "\n" - print "optional arguments:" - print " -h, --help show this help message and exit" - print "-p port" - print "-t timeout" - print "-v verbose" - + print("usage: check_b2stage_http-api.py -H -p") + print("--- ---- ---- ---- ---- ---- ----\n") + print("main arguments:") + print("-H hostname") + print("\n") + print("optional arguments:") + print(" -h, --help show this help message and exit") + print("-p port") + print("-t timeout") + print("-v verbose") def debugValues(arguments): @@ -48,19 +49,20 @@ def debugValues(arguments): arguments: the input arguments """ if arguments.debug: - print "[debugValues] - hostname:"+ arguments.hostname - if arguments.port!='': - print "[debugValues] - port:" + arguments.port - if arguments.timeout!='': - print "[debugValues] - timeout:" + str(arguments.timeout) - if arguments.t!='': - print "[debugValues] - timeout:" + str(arguments.t) - -def checkHealth(URL,timeout): + print("[debugValues] - hostname: %s" % arguments.hostname) + if arguments.port != '': + print("[debugValues] - port: %s" % arguments.port) + if arguments.timeout != '': + print("[debugValues] - timeout: %s" % arguments.timeout) + if arguments.t != '': + print("[debugValues] - timeout: %s" % arguments.t) + + +def checkHealth(URL, timeout): """ Check service status. Args: URL : service hostname - timeout : how long should we wati + timeout : how long should we wati """ try: out = requests.get(url=URL, timeout=timeout) @@ -86,16 +88,18 @@ def checkHealth(URL,timeout): exit_code = 3 return description, exit_code + def printResult(description, exit_code): - """ Print the predefined values + """ Print the predefined values Args: description: the nagios description exit_code: the code that should be returned to nagios """ - print 'description' + print(description) sys.exit(exit_code) + def main(): parser = argparse.ArgumentParser(description='B2STAGE probe ' @@ -103,21 +107,20 @@ def main(): parser.add_argument("--hostname", "-H", help='The Hostname of B2STAGE service') parser.add_argument("--port", "-p", type=int) parser.add_argument("--timeout", "-t", metavar="seconds", help="Timeout in seconds. Must be greater than zero", type=int, default=30) - parser.add_argument("--verbose","-v", dest='debug', help='Set verbosity level', action='count', default=0) + parser.add_argument("--verbose", "-v", dest='debug', help='Set verbosity level', action='count', default=0) arguments = parser.parse_args() ValidateValues(arguments) NAGIOS_RESULT = 0 - URL=arguments.hostname + URL = arguments.hostname if arguments.port is not None: - URL += ":%s" % arguments.port + URL += ":%s" % arguments.port URL += "/api/status" timeout = arguments.timeout - description, exit_code = checkHealth(URL,timeout) + description, exit_code = checkHealth(URL, timeout) printResult(description, exit_code) if __name__ == "__main__": main() - From b9ff6e27d10da3d028bbb674be0ddc56a390a286 Mon Sep 17 00:00:00 2001 From: Mattia Date: Thu, 25 Oct 2018 10:01:24 +0200 Subject: [PATCH 06/13] BUG fix returning check results --- check_b2stage_http-api.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/check_b2stage_http-api.py b/check_b2stage_http-api.py index 6cc24a3..9c269aa 100644 --- a/check_b2stage_http-api.py +++ b/check_b2stage_http-api.py @@ -67,22 +67,27 @@ def checkHealth(URL, timeout): try: out = requests.get(url=URL, timeout=timeout) - content = out.json() - if out.status_code != 200: description = "WARNING - Unexpected status code %s" % out.status_code exit_code = 1 + return description, exit_code + + content = out.json() + resp = content['Response']['data'] - if content['Response']['data'] != "Server is alive!": - description = "WARNING - Unexpected response: %s" % content['Response']['data'] + if resp != "Server is alive!": + description = "WARNING - Unexpected response: %s" % resp exit_code = 1 + return description, exit_code description = "OK - Service reachable" exit_code = 0 + return description, exit_code except requests.exceptions.ConnectionError: description = "CRITICAL - Service unreachable" exit_code = 2 + return description, exit_code description = "UNKNOWN - Status unknown" exit_code = 3 From 78ba7dfe3b077b686de082ea7f7488191f914412 Mon Sep 17 00:00:00 2001 From: Mattia Date: Thu, 25 Oct 2018 10:06:08 +0200 Subject: [PATCH 07/13] Checking SSL certificate validity --- check_b2stage_http-api.py | 42 ++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/check_b2stage_http-api.py b/check_b2stage_http-api.py index 9c269aa..3f3b6ee 100644 --- a/check_b2stage_http-api.py +++ b/check_b2stage_http-api.py @@ -64,33 +64,39 @@ def checkHealth(URL, timeout): URL : service hostname timeout : how long should we wati """ + out = None try: out = requests.get(url=URL, timeout=timeout) - if out.status_code != 200: - description = "WARNING - Unexpected status code %s" % out.status_code - exit_code = 1 - return description, exit_code - - content = out.json() - resp = content['Response']['data'] - - if resp != "Server is alive!": - description = "WARNING - Unexpected response: %s" % resp - exit_code = 1 - return description, exit_code - - description = "OK - Service reachable" - exit_code = 0 + except requests.exceptions.SSLError: + description = "WARNING - Invalid SSL certificate" + exit_code = 1 return description, exit_code - except requests.exceptions.ConnectionError: description = "CRITICAL - Service unreachable" exit_code = 2 return description, exit_code - description = "UNKNOWN - Status unknown" - exit_code = 3 + if out is None: + description = "UNKNOWN - Status unknown" + exit_code = 3 + return description, exit_code + + if out.status_code != 200: + description = "WARNING - Unexpected status code %s" % out.status_code + exit_code = 1 + return description, exit_code + + content = out.json() + resp = content['Response']['data'] + + if resp != "Server is alive!": + description = "WARNING - Unexpected response: %s" % resp + exit_code = 1 + return description, exit_code + + description = "OK - Service reachable" + exit_code = 0 return description, exit_code From 8be2c4b633ace33984eb6543b68866c0a06ebe93 Mon Sep 17 00:00:00 2001 From: Mattia Date: Tue, 6 Nov 2018 10:12:17 +0100 Subject: [PATCH 08/13] Added checks on login and logout --- check_b2stage_http-api.py | 122 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 116 insertions(+), 6 deletions(-) diff --git a/check_b2stage_http-api.py b/check_b2stage_http-api.py index 3f3b6ee..5c57b6f 100644 --- a/check_b2stage_http-api.py +++ b/check_b2stage_http-api.py @@ -41,6 +41,8 @@ def print_help(): print("-p port") print("-t timeout") print("-v verbose") + print("-u user") + print("-P password") def debugValues(arguments): @@ -56,17 +58,22 @@ def debugValues(arguments): print("[debugValues] - timeout: %s" % arguments.timeout) if arguments.t != '': print("[debugValues] - timeout: %s" % arguments.t) + if arguments.user != '': + print("[debugValues] - user: %s" % arguments.user) + if arguments.password != '': + print("[debugValues] - password: ******") def checkHealth(URL, timeout): """ Check service status. Args: URL : service hostname - timeout : how long should we wati + timeout : how long should we wait for a response from the server """ out = None + u = URL + "/api/status" try: - out = requests.get(url=URL, timeout=timeout) + out = requests.get(url=u, timeout=timeout) except requests.exceptions.SSLError: description = "WARNING - Invalid SSL certificate" @@ -100,6 +107,88 @@ def checkHealth(URL, timeout): return description, exit_code +def checkAuthentication(URL, timeout, user, password): + """ Check service authentication. + Args: + URL : service hostname + timeout : how long should we wait for a response from the server + user : username to authenticate the session + password : password to authenticate the session + """ + + token = None + out = None + u = URL + "/auth/b2safeproxy" + try: + payload = {'username': user, 'password': password} + out = requests.post(url=u, timeout=timeout, data=payload) + except BaseException as e: + description = "UNKNOWN - Unknown error: %s" % str(e) + exit_code = 3 + return description, exit_code, token + + if out.status_code == 401: + description = "CRITICAL - Invalid credentials" + exit_code = 2 + return description, exit_code, token + + if out.status_code != 200: + description = "WARNING - Unexpected status code %s" % out.status_code + exit_code = 1 + return description, exit_code, token + + content = out.json() + resp = content['Response']['data'] + + if 'token' not in resp: + description = "CRITICAL - Unable to get a valid authentication token" + exit_code = 2 + return description, exit_code, token + + token = resp['token'] + + description = "OK - Service reachable" + exit_code = 0 + return description, exit_code, token + + +def checkLogout(URL, timeout, token): + """ Check service authentication. + Args: + URL : service hostname + timeout : how long should we wait for a response from the server + token : authentication token received from login + """ + + u = URL + "/auth/logout" + headers = {'Authorization': 'Bearer %s' % token} + try: + out = requests.get(url=u, timeout=timeout, headers=headers) + except BaseException as e: + description = "UNKNOWN - Unknown error: %s" % str(e) + exit_code = 3 + return description, exit_code, token + + if out.status_code == 401: + description = "CRITICAL - Invalid token" + exit_code = 2 + return description, exit_code, token + + if out.status_code == 401: + description = "CRITICAL - Invalid credentials" + exit_code = 2 + return description, exit_code, token + + if out.status_code != 204: + description = "WARNING - Unexpected status code %s" % out.status_code + exit_code = 1 + return description, exit_code, token + + description = "OK - Service reachable" + exit_code = 0 + return description, exit_code + + def printResult(description, exit_code): """ Print the predefined values Args: @@ -118,18 +207,39 @@ def main(): parser.add_argument("--hostname", "-H", help='The Hostname of B2STAGE service') parser.add_argument("--port", "-p", type=int) parser.add_argument("--timeout", "-t", metavar="seconds", help="Timeout in seconds. Must be greater than zero", type=int, default=30) + parser.add_argument("--user", "-u", metavar="user", help="User name to allow checks on authenticated endpoints") + parser.add_argument("--password", "-P", metavar="password", help="Passoword to allow checks on authenticated endpoints") parser.add_argument("--verbose", "-v", dest='debug', help='Set verbosity level', action='count', default=0) arguments = parser.parse_args() ValidateValues(arguments) - NAGIOS_RESULT = 0 URL = arguments.hostname if arguments.port is not None: URL += ":%s" % arguments.port - URL += "/api/status" - timeout = arguments.timeout - description, exit_code = checkHealth(URL, timeout) + description, exit_code = checkHealth(URL, arguments.timeout) + + # Healt check failed, unable to continue + if exit_code > 0: + printResult(description, exit_code) + + # Authenticated tests not allowed, unable to continue + if arguments.user is None or arguments.password is None: + printResult(description, exit_code) + + description, exit_code, token = checkAuthentication( + URL, arguments.timeout, arguments.user, arguments.password) + + # Authentication failed, unable to continue + if exit_code > 0: + printResult(description, exit_code) + + # No valid authentication token received, unable to continue + if token is None: + printResult(description, exit_code) + + description, exit_code = checkLogout(URL, arguments.timeout, token) + printResult(description, exit_code) From 83de385122f0f5fb9f3ac9f21d15504251fdbd70 Mon Sep 17 00:00:00 2001 From: Themis Zamani Date: Mon, 17 Dec 2018 10:29:54 +0200 Subject: [PATCH 09/13] Update nagios-plugins-eudat-b2stage.spec --- nagios-plugins-eudat-b2stage.spec | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/nagios-plugins-eudat-b2stage.spec b/nagios-plugins-eudat-b2stage.spec index 5fb493a..ee2d5c5 100644 --- a/nagios-plugins-eudat-b2stage.spec +++ b/nagios-plugins-eudat-b2stage.spec @@ -1,5 +1,5 @@ Name: nagios-plugins-eudat-b2stage -Version: 0.5 +Version: 0.6 Release: 1%{?dist} Summary: Nagios probe for B2STAGE License: GPLv3+ @@ -32,6 +32,8 @@ install -m 755 check_b2stage_http-api.py %{buildroot}/%{_libexecdir}/argo-monito %attr(0755,root,root) /%{_libexecdir}/argo-monitoring/probes//eudat-b2stage/check_b2stage_http-api.py %changelog +* Mon Dec 17 2018 Themis Zamani - 0.1-1 +- Updated package version. * Fri Oct 19 2018 Themis Zamani - 0.1-1 - Initial version of the package. * Thu Oct 18 2018 Mattia D'Antonio - 0.1-1 From b99ad7150f953b51984d16e216dd4a0fb8b3ce8d Mon Sep 17 00:00:00 2001 From: Themis Zamani Date: Mon, 17 Dec 2018 10:30:07 +0200 Subject: [PATCH 10/13] Update nagios-plugins-eudat-b2stage.spec --- nagios-plugins-eudat-b2stage.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nagios-plugins-eudat-b2stage.spec b/nagios-plugins-eudat-b2stage.spec index ee2d5c5..07f39c8 100644 --- a/nagios-plugins-eudat-b2stage.spec +++ b/nagios-plugins-eudat-b2stage.spec @@ -32,7 +32,7 @@ install -m 755 check_b2stage_http-api.py %{buildroot}/%{_libexecdir}/argo-monito %attr(0755,root,root) /%{_libexecdir}/argo-monitoring/probes//eudat-b2stage/check_b2stage_http-api.py %changelog -* Mon Dec 17 2018 Themis Zamani - 0.1-1 +* Mon Dec 17 2018 Themis Zamani - 0.6-1 - Updated package version. * Fri Oct 19 2018 Themis Zamani - 0.1-1 - Initial version of the package. From 12f290e1f1a6c18894389b0e08556425adc298f5 Mon Sep 17 00:00:00 2001 From: Themis Zamani Date: Mon, 17 Dec 2018 13:12:39 +0200 Subject: [PATCH 11/13] Update check_b2stage_http-api.py new error code for checkLogout (Endpoint not found) updated return values to checklogout --- check_b2stage_http-api.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/check_b2stage_http-api.py b/check_b2stage_http-api.py index 5c57b6f..855eeb8 100644 --- a/check_b2stage_http-api.py +++ b/check_b2stage_http-api.py @@ -179,6 +179,11 @@ def checkLogout(URL, timeout, token): exit_code = 2 return description, exit_code, token + if out.status_code == 404: + description = "CRITICAL - Endpoint not found" + exit_code = 2 + return description, exit_code, token + if out.status_code != 204: description = "WARNING - Unexpected status code %s" % out.status_code exit_code = 1 @@ -186,7 +191,7 @@ def checkLogout(URL, timeout, token): description = "OK - Service reachable" exit_code = 0 - return description, exit_code + return description, exit_code, token def printResult(description, exit_code): @@ -238,7 +243,7 @@ def main(): if token is None: printResult(description, exit_code) - description, exit_code = checkLogout(URL, arguments.timeout, token) + description, exit_code, token = checkLogout(URL, arguments.timeout, token) printResult(description, exit_code) From e65b340fb7c524f83acd9dd5e288fcdbb212e954 Mon Sep 17 00:00:00 2001 From: Themis Zamani Date: Mon, 17 Dec 2018 13:13:13 +0200 Subject: [PATCH 12/13] Update nagios-plugins-eudat-b2stage.spec --- nagios-plugins-eudat-b2stage.spec | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/nagios-plugins-eudat-b2stage.spec b/nagios-plugins-eudat-b2stage.spec index 07f39c8..91e3c51 100644 --- a/nagios-plugins-eudat-b2stage.spec +++ b/nagios-plugins-eudat-b2stage.spec @@ -1,5 +1,5 @@ Name: nagios-plugins-eudat-b2stage -Version: 0.6 +Version: 0.7 Release: 1%{?dist} Summary: Nagios probe for B2STAGE License: GPLv3+ @@ -32,6 +32,8 @@ install -m 755 check_b2stage_http-api.py %{buildroot}/%{_libexecdir}/argo-monito %attr(0755,root,root) /%{_libexecdir}/argo-monitoring/probes//eudat-b2stage/check_b2stage_http-api.py %changelog +* Mon Dec 17 2018 Themis Zamani - 0.7-1 +- Updated package version. * Mon Dec 17 2018 Themis Zamani - 0.6-1 - Updated package version. * Fri Oct 19 2018 Themis Zamani - 0.1-1 From bed585d663f90be238923f0d7babc7365d500ef3 Mon Sep 17 00:00:00 2001 From: Themis Zamani Date: Fri, 3 Apr 2020 13:56:14 +0300 Subject: [PATCH 13/13] Version 0.8: to support a new message --- check_b2stage_http-api.py | 2 +- nagios-plugins-eudat-b2stage.spec | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/check_b2stage_http-api.py b/check_b2stage_http-api.py index 855eeb8..2ab58eb 100644 --- a/check_b2stage_http-api.py +++ b/check_b2stage_http-api.py @@ -97,7 +97,7 @@ def checkHealth(URL, timeout): content = out.json() resp = content['Response']['data'] - if resp != "Server is alive!": + if (resp != "Server is alive!") or (resp != "Server is alive"): description = "WARNING - Unexpected response: %s" % resp exit_code = 1 return description, exit_code diff --git a/nagios-plugins-eudat-b2stage.spec b/nagios-plugins-eudat-b2stage.spec index 91e3c51..f3ea42e 100644 --- a/nagios-plugins-eudat-b2stage.spec +++ b/nagios-plugins-eudat-b2stage.spec @@ -1,5 +1,5 @@ Name: nagios-plugins-eudat-b2stage -Version: 0.7 +Version: 0.8 Release: 1%{?dist} Summary: Nagios probe for B2STAGE License: GPLv3+ @@ -32,6 +32,8 @@ install -m 755 check_b2stage_http-api.py %{buildroot}/%{_libexecdir}/argo-monito %attr(0755,root,root) /%{_libexecdir}/argo-monitoring/probes//eudat-b2stage/check_b2stage_http-api.py %changelog +* Fri April 3 2020 Themis Zamani - 0.8-1 +- Small updates to messages * Mon Dec 17 2018 Themis Zamani - 0.7-1 - Updated package version. * Mon Dec 17 2018 Themis Zamani - 0.6-1