Skip to content

Instantly share code, notes, and snippets.

@sh4t
Last active December 1, 2015 06:09
Show Gist options
  • Save sh4t/1646536f9cf80c97366b to your computer and use it in GitHub Desktop.
Save sh4t/1646536f9cf80c97366b to your computer and use it in GitHub Desktop.

Revisions

  1. sh4t revised this gist Dec 1, 2015. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions es_zabbix.py
    Original file line number Diff line number Diff line change
    @@ -13,10 +13,6 @@ def es_fail(e):
    print "Error: %s" % e
    sys.exit(2)

    def zbx_fail():
    print "Zabbix Not Supported"
    sys.exit(2)

    returnval = None

    if len(sys.argv) < 3:
  2. sh4t created this gist Dec 1, 2015.
    79 changes: 79 additions & 0 deletions es_zabbix.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    #!/usr/bin/env python

    import sys, os, pwd
    import kaptan
    import urllib3
    from elasticsearch import *
    urllib3.disable_warnings()

    you = pwd.getpwuid(os.getuid()).pw_name

    def es_fail(e):
    print "Something went wrong"
    print "Error: %s" % e
    sys.exit(2)

    def zbx_fail():
    print "Zabbix Not Supported"
    sys.exit(2)

    returnval = None

    if len(sys.argv) < 3:
    es_fail("too few args")

    if '.' not in sys.argv[1]:
    es_fail("dont see a period anywhere, silly..")

    api = sys.argv[1].split(".")

    try:
    conn = Elasticsearch(['https://log.sjc1.defense.net:9443'],
    use_ssl=True,
    verify_certs=False)
    except Exception, e:
    es_fail("connection issue")

    config = kaptan.Kaptan()

    if "cluster" in sys.argv[1]:
    config.import_config(getattr(conn.cluster, api[1])())
    returnval = config.get(sys.argv[2])

    elif "nodes" in sys.argv[1]:
    nodestats = getattr(conn.nodes, api[1])()
    config.import_config(nodestats)

    # Map node_id to node_name
    node_names = {}
    for node_id in nodestats['nodes']:
    node_name = nodestats['nodes'][node_id]['name']
    node_names[node_name] = node_id

    # Split the key in parts and replace the node_name
    # with node_id if possible
    key_string = sys.argv[2]
    key_parts = key_string.split(".")
    for key in key_parts:
    if key in node_names:
    key_string = key_string.replace(key, node_names[key])

    # Get the value
    returnval = config.get(key_string)

    else:
    es_fail("i dont support that yet, %s" % you)

    # Return a value or fail state
    if returnval is None:
    es_fail("return was None, oops")
    else:
    # Map status green/yellow/red to integers
    if returnval == 'green':
    returnval = 0
    elif returnval == 'yellow':
    returnval = 1
    elif returnval == 'red':
    returnval = 2

    print returnval