Skip to content

Instantly share code, notes, and snippets.

@sh4t
Last active December 1, 2015 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sh4t/1646536f9cf80c97366b to your computer and use it in GitHub Desktop.
Save sh4t/1646536f9cf80c97366b to your computer and use it in GitHub Desktop.
elasticsearch-py zabbix play
#!/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)
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment