Last active
January 31, 2018 04:04
-
-
Save hemebond/da18aa90aab057973e30b1871bbfeead to your computer and use it in GitHub Desktop.
Nginx Status monitoring plugin script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Parse the Nginx Status output and return as monitoring plugin data. | |
""" | |
import nagiosplugin | |
from http.client import HTTPConnection | |
import re, argparse | |
class Nginx(nagiosplugin.Resource): | |
def __init__(self, host='localhost', port=80, url='/nginx_status', timeout=5): | |
self.host = host | |
self.port = port | |
self.url = url | |
self.timeout = timeout | |
def probe(self): | |
result = {} | |
url = "/nginx_status" | |
connection = HTTPConnection(self.host, self.port, timeout=self.timeout) | |
connection.request("GET", self.url) | |
response = connection.getresponse() | |
data = response.read().decode("UTF-8") | |
m1 = re.search(r'Active connections:\s+(?P<connections>\d+)', data) | |
m2 = re.search(r'\s*(?P<accepted>\d+)\s+(?P<handled>\d+)\s+(?P<requests>\d+)', data) | |
m3 = re.search(r'Reading:\s*(?P<reading>\d+)\s*Writing:\s*(?P<writing>\d+)\s*Waiting:\s*(?P<waiting>\d+)', data) | |
if not m1 or not m2 or not m3: | |
raise Exception('Unable to parse %s' % url) | |
yield nagiosplugin.Metric('connections', int(m1.groupdict()['connections']), min=0, context='connections') | |
_accepted = int(m2.groupdict()['accepted']) | |
_handled = int(m2.groupdict()['handled']) | |
_requests = int(m2.groupdict()['requests']) | |
yield nagiosplugin.Metric('accepted', _accepted, min=0, context='default') | |
yield nagiosplugin.Metric('handled', _handled, min=0, context='default') | |
yield nagiosplugin.Metric('requests', _requests, min=0, context='default') | |
yield nagiosplugin.Metric('reading', int(m3.groupdict()['reading']), min=0, context='default') | |
yield nagiosplugin.Metric('writing', int(m3.groupdict()['writing']), min=0, context='default') | |
yield nagiosplugin.Metric('waiting', int(m3.groupdict()['waiting']), min=0, context='default') | |
def main(): | |
argp = argparse.ArgumentParser(description=__doc__, add_help=False) | |
argp.add_argument('-h', '--host', default='localhost', help='') | |
argp.add_argument('-p', '--port', default=80, help='') | |
argp.add_argument('-u', '--url', default='/nginx_status', help='') | |
argp.add_argument('-t', '--timeout', default=5, help='') | |
argp.add_argument('-w', '--warning', metavar='WARN', default='', help='return warning if number of connections greater than WARN') | |
argp.add_argument('-c', '--critical', metavar='CRIT', default='', help='return critical if number of connections is greater than CRIT') | |
argp.add_argument('--help', action='help', help='show this help message and exit') | |
args = argp.parse_args() | |
check = nagiosplugin.Check( | |
Nginx(args.host, args.port, args.url, args.timeout), | |
nagiosplugin.ScalarContext('connections', args.warning, args.critical, fmt_metric='{value} connections') | |
) | |
check.main() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment