#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2014             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# ails.  You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.
import getopt, os, sys, socket, re, pprint, requests, json

# Fixes inspired on
# https://stackoverflow.com/questions/34836777/print-complete-key-path-for-all-the-values-of-a-python-nested-dictionary

def usage():
    sys.stderr.write("""Check_MK nginx plus Agent
USAGE: agent_nginx_plus_status [OPTIONS] PROTO HOST PORT PAGE
       agent_nginx_plus_status -h
ARGUMENTS:
   HOST                         Hostname or IP address of the nginx instance to monitor
OPTIONS:
  -h, --help                    Show this help message and exit
  -t, --timeout SEC             Set the network timeout to <SEC> seconds.
                                Default is 10 seconds. Note: the timeout is not
                                applied to the whole check, instead it is used for
                                each API query.
  --debug                       Debug mode: let Python exceptions come through
""")

short_options = 'h:t:d'
long_options  = [
    'help', 'timeout=', 'debug'
]
host_proto        = None
host_address      = None
host_port         = None
host_page         = None
opt_debug         = False
opt_timeout       = 10

def dict_path(my_dict, path=None):
    if path is None:
        path = []
    for key, value in my_dict.iteritems():
        newpath = path + [key]
        if isinstance(value, dict):
            for item in dict_path(value, newpath):
                yield item
        if isinstance(value, list):
            for item in value:
                dict_path(path, value)
        else:
            v = check_unicode(value)
            if isinstance(v, dict):
                dict_path(path, v)
            else:
                yield newpath, value

def check_unicode(value):
    try:
        d = json.loads(str(value))
        return d
    except:
        return value

def check_nginx_plus_get_dict(address):
    r = requests.get(address)
    return status_data = r.json()

try:
    opts, args = getopt.getopt(sys.argv[1:], short_options, long_options)
except getopt.GetoptError, err:
    sys.stderr.write("%s\n" % err)
    sys.exit(1)

for o,a in opts:
    if o in [ '--debug' ]:
        opt_debug = True
    elif o in [ '-t', '--timeout' ]:
        opt_timeout = int(a)
    elif o in [ '-h', '--help' ]:
        usage()
        sys.exit(0)

if len(args) == 4:
    host_proto = args[0]
    host_address = args[1]
    host_port = args[2]
    host_page = args[3]
elif not args:
    sys.stderr.write("ERROR: No host given.\n")
    sys.exit(1)
else:
    sys.stderr.write("ERROR: Please specify exactly one host.\n")
    sys.exit(1)

socket.setdefaulttimeout(opt_timeout)

class RequestError(Exception):
    pass

address = "%s://%s:%s/%s" % (host_proto, host_address, host_port, host_page)
dataDict = check_nginx_plus_get_dict(address)

print '<<<nginx_plus_status>>>'
for path, value in dict_path(dataDict):
    print '_'.join(path), " ", value
