Ticket #28242: 0001-Improves-monitoring-of-the-Onionoo-service.patch

File 0001-Improves-monitoring-of-the-Onionoo-service.patch, 6.1 KB (added by irl, 9 months ago)
  • config/nagios-master.cfg

    From f65b19df4d76fa8ac4d8155807beaa3181913c9e Mon Sep 17 00:00:00 2001
    From: "Iain R. Learmonth" <irl@fsfe.org>
    Date: Tue, 30 Oct 2018 11:09:35 +0000
    Subject: [PATCH] Improves monitoring of the Onionoo service
    
     * Monitors the Java processes directly via NRPE
     * Monitors that the Varnish cache is serving recent data via NRPE
     * Continues to monitor at least one of the haproxy frontends
       but does not monitor individually due to certificate issues
     * The check has had (currently optional) SSL switch and hostname/port
       argument added
    
    Fixes: #28242
    ---
     config/nagios-master.cfg                   | 16 +++++-
     tor-nagios-checks/checks/tor-check-onionoo | 57 ++++++++++++++--------
     2 files changed, 50 insertions(+), 23 deletions(-)
    
    diff --git a/config/nagios-master.cfg b/config/nagios-master.cfg
    index 11cae63..12b2bcd 100644
    a b services: 
    13521352 # non-tpa services
    13531353 ####
    13541354  -
    1355     name: network service - onionoo
    1356     nrpe: "/usr/lib/nagios/plugins/tor-check-onionoo"
     1355    name: network service - onionoo backend
     1356    nrpe: "/usr/lib/nagios/plugins/tor-check-onionoo 127.0.0.1:8080"
     1357    hostgroups: onionoo-backend
     1358    depends: "process - haproxy - master"
     1359    contacts: +metrics
     1360  -
     1361    name: network service - onionoo varnish
     1362    nrpe: "/usr/lib/nagios/plugins/tor-check-onionoo 127.0.0.1:8168"
     1363    hostgroups: onionoo-backend
     1364    depends: "process - haproxy - master"
     1365    contacts: +metrics
     1366  -
     1367    name: network service - onionoo haproxy
     1368    nrpe: "/usr/lib/nagios/plugins/tor-check-onionoo -s onionoo.torproject.org"
    13571369    hostgroups: onionoo-backend
    13581370    depends: "process - haproxy - master"
    13591371    contacts: +metrics
  • tor-nagios-checks/checks/tor-check-onionoo

    diff --git a/tor-nagios-checks/checks/tor-check-onionoo b/tor-nagios-checks/checks/tor-check-onionoo
    index 353fc88..9ab1211 100755
    a b  
    1 #!/usr/bin/python
     1#!/usr/bin/python3
    22#
    3 # Copyright 2014 The Tor Project
     3# Copyright 2014,2018 The Tor Project
    44#
    55# Redistribution and use in source and binary forms, with or without
    66# modification, are permitted provided that the following conditions are
     
    2929# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    3030# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    3131# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    32 
    3332"""Nagios plugin to check that the Onionoo service is running and returns
    3433recent data."""
    3534
    3635import sys
    37 import urllib2
    3836import json
    3937import datetime
     38import urllib.error
     39from urllib.request import urlopen
    4040
    4141# Standard Nagios return codes
    4242OK, WARNING, CRITICAL, UNKNOWN = range(4)
    4343
    4444# Test URL to download and verify
    45 ONIONOO_TEST_URL = 'https://onionoo.torproject.org/summary?limit=0'
     45ONIONOO_TEST_URL = '/summary?limit=0'
    4646
    4747# ISO datetime pattern
    4848ISO_DATETIME = "%Y-%m-%d %H:%M:%S"
    CRITICAL_IF_OLDER = 6 * 60 * 60 
    5555def test_onionoo():
    5656    """Fetch minimal summary document and checks that it's recent."""
    5757
     58    # Build URL
     59    url = ("http" + ("s"
     60                     if use_https else "") + "://" + host + ONIONOO_TEST_URL)
     61
    5862    # Fetch document from Onionoo service.
    59     request = urllib2.Request(url=ONIONOO_TEST_URL)
    6063    try:
    61         response = urllib2.urlopen(request)
    62     except urllib2.URLError, error:
     64        response = urlopen(url)
     65    except urllib.error.URLError as error:
    6366        return CRITICAL, "Error fetching minimal summary document: %s" % \
    6467                         (error.reason, )
    65     except urllib2.HTTPError, error:
    66         return CRITICAL, "Error fetching minimal summary document: " \
    67                          "%s %s" % (str(error.code), error.reason, )
    68     response_str = response.read()
     68    response_str = response.read().decode('utf-8')
    6969
    7070    # Parse JSON-formatted response.
    7171    try:
    7272        response_dict = json.loads(response_str)
    73     except ValueError, error:
     73    except ValueError as error:
    7474        return CRITICAL, "Error parsing JSON format: %s %s" % \
    7575                         (error, response_str[:40], )
    7676
    def test_onionoo(): 
    7878    try:
    7979        relays_str = response_dict['relays_published']
    8080        bridges_str = response_dict['bridges_published']
    81     except KeyError, error:
     81    except KeyError as error:
    8282        return CRITICAL, "Either relays_published or bridges_published " \
    8383                         "missing in response: %s" % (response_str[:40], )
    8484
    def test_onionoo(): 
    8686    try:
    8787        relays_ts = datetime.datetime.strptime(relays_str, ISO_DATETIME)
    8888        bridges_ts = datetime.datetime.strptime(bridges_str, ISO_DATETIME)
    89     except ValueError, error:
     89    except ValueError as error:
    9090        return CRITICAL, "Unable to parse either timestamp: " \
    9191                         "relays_published=%s, bridges_published=%s" % \
    9292                         (relays_str, bridges_str, )
    def test_onionoo(): 
    113113if __name__ == "__main__":
    114114    status, message = None, None
    115115
     116    host = None
     117    use_https = False
     118
     119    if len(sys.argv) == 2:
     120        host = sys.argv[1]
     121    elif len(sys.argv) == 3 and sys.argv[1] == "-s":
     122        use_https = True
     123        host = sys.argv[2]
     124    else:
     125        # This is to not break the existing checks while
     126        # tor-nagios-checks is updated.
     127        use_https = True
     128        host = "onionoo.torproject.org"
     129
    116130    try:
    117131        status, message = test_onionoo()
    118132    except KeyboardInterrupt:
    119133        status, message = CRITICAL, "Caught Control-C..."
     134    except Exception as e:
     135        status = CRITICAL
     136        message = repr(e)
    120137    finally:
    121138        if status == OK:
    122             print "OK: %s" % message
     139            print("OK: %s" % message)
    123140        elif status == WARNING:
    124             print "WARNING: %s" % message
     141            print("WARNING: %s" % message)
    125142        elif status == CRITICAL:
    126             print "CRITICAL: %s" % message
     143            print("CRITICAL: %s" % message)
    127144        else:
    128             print "UNKNOWN: %s" % message
     145            print("UNKNOWN: %s" % message)
    129146            status = UNKNOWN
    130147
    131148        sys.exit(status)
    132 
    133