Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace probe endpoint #13

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ jobs:
with:
fetch-depth: 0

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt

# Runs the Super-Linter action
- name: Run Super-Linter on new changes
uses: github/super-linter@v6
Expand Down
65 changes: 33 additions & 32 deletions nagios_plugins_egi_notebooks/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,62 @@
import logging
import sys

from six.moves.urllib.parse import urljoin

import requests
from requests.exceptions import ConnectionError, HTTPError
from six.moves.urllib.parse import urljoin


def status2code(status):
code_map = {
'OK': 0,
'WARNING': 1,
'CRITICAL': 2,
"OK": 0,
"WARNING": 1,
"CRITICAL": 2,
}
return code_map.get(status.upper(), 2)


def get_notebook_status(status_url, timeout=10):
logging.debug('Querying %s for status', status_url)
logging.debug("Querying %s for status", status_url)
try:
r = requests.get(status_url, timeout=timeout,
headers={'accept': 'application/json'})
r = requests.get(status_url, timeout=timeout)
r.raise_for_status()
status = r.json()
logging.debug('Full status message: %s' % status)
logging.info("%s: %s", status['code'], status['msg'])
return status2code(status['code'])
logging.debug("Full message: %s" % r.text)
logging.info("%s: %s", r.reason, r.status_code)
return status2code(r.reason)
except (ConnectionError, HTTPError) as e:
logging.info("CRITICAL: Unable to get status, %s", e.message)
return status2code('CRITICAL')
logging.info("CRITICAL: Unable to get status, %s", e)
return status2code("CRITICAL")


def main():
parser = argparse.ArgumentParser(
description='Nagios plugin for EGI Notebooks',
description="Nagios plugin for EGI Notebooks",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
fromfile_prefix_chars='@',
fromfile_prefix_chars="@",
conflict_handler="resolve",
)

parser.add_argument('-t', '--timeout', default=10, type=int,
help='Timeout in seconds of the probe')
parser.add_argument(
"-t", "--timeout", default=10, type=int, help="Timeout in seconds of the probe"
)

parser.add_argument('--url', help='URL of the the EGI notebooks endpoint')
parser.add_argument("--url", help="URL of the the EGI notebooks endpoint")

parser.add_argument('--status-path', default='services/status/',
help=('Path in the endpoint for the monitoring '
'service'))
parser.add_argument(
"--status-path",
default="hub/metrics",
help=("Path in the endpoint for the monitoring " "service"),
)

parser.add_argument('-v', '--verbose', default=False, action='store_true',
help='Be verbose')
parser.add_argument(
"-v", "--verbose", default=False, action="store_true", help="Be verbose"
)

parser.add_argument('-H', '--host', help='Host to be checked')
parser.add_argument("-H", "--host", help="Host to be checked")

parser.add_argument('-p', '--port', default=443, type=int,
help='Port to be checked')
parser.add_argument(
"-p", "--port", default=443, type=int, help="Port to be checked"
)

opts = parser.parse_args()
log_level = logging.INFO
Expand All @@ -65,13 +68,11 @@ def main():
# URL takes precedence over -H and -p
if not opts.url:
if not opts.host:
logging.error('Mising url or host to check')
sys.exit(status2code('CRITICAL'))
status_url = 'https://%s:%d/' % (opts.host, opts.port)
logging.error("Mising url or host to check")
sys.exit(status2code("CRITICAL"))
status_url = "https://%s:%d/" % (opts.host, opts.port)
else:
status_url = opts.url

if not status_url.endswith('/'):
status_url = status_url + '/'
full_status_url = urljoin(status_url, opts.status_path)
sys.exit(get_notebook_status(full_status_url, opts.timeout))