Created
December 21, 2015 14:58
-
-
Save q1x/a4be282d3833c8e6def8 to your computer and use it in GitHub Desktop.
zabbix pythonrc
This file contains 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 python | |
# -*- coding: utf-8 -*- | |
# vim: set ft=python : | |
try: | |
import readline | |
import ConfigParser | |
import os | |
import os.path | |
import sys | |
import distutils.util | |
from urlparse import urlparse | |
import csv, codecs, cStringIO | |
except ImportError: | |
print("Not all modules are available.") | |
else: | |
import rlcompleter | |
if 'libedit' in readline.__doc__: | |
readline.parse_and_bind("bind ^I rl_complete") | |
else: | |
readline.parse_and_bind("tab: complete") | |
from pyzabbix import ZabbixAPI | |
# define config helper function | |
def ConfigSectionMap(section): | |
dict1 = {} | |
options = Config.options(section) | |
for option in options: | |
try: | |
dict1[option] = Config.get(section, option) | |
if dict1[option] == -1: | |
DebugPrint("skip: %s" % option) | |
except: | |
print("exception on %s!" % option) | |
dict1[option] = None | |
return dict1 | |
# set default vars | |
defconf = os.getenv("HOME") + "/.zbx.conf" | |
username = "" | |
password = "" | |
api = "" | |
noverify = "" | |
# load config module | |
Config = ConfigParser.ConfigParser() | |
Config | |
if os.path.isfile(defconf) and os.access(defconf, os.R_OK): | |
Config.read(defconf) | |
# try to load available settings from config file | |
try: | |
username=ConfigSectionMap("Zabbix API")['username'] | |
password=ConfigSectionMap("Zabbix API")['password'] | |
api=ConfigSectionMap("Zabbix API")['api'] | |
noverify=bool(distutils.util.strtobool(ConfigSectionMap("Zabbix API")["no_verify"])) | |
except: | |
sys.exit("Could not read Zabbix Configuration.") | |
#setup Zabbix API connection | |
z = ZabbixAPI(api) | |
if noverify is True: | |
z.session.verify = False | |
# Login to the Zabbix API | |
try: | |
z.login(username, password) | |
except: | |
sys.exit("Could not connect to Zabbix API.") | |
zhost=urlparse(api).hostname | |
print "\nWelcome to the Zabbix Python Shell.\n" | |
print " * API: %s" % api | |
print " * Version: %s" % z.api_version() | |
print " * User: %s\n" % username | |
print " Use 'z' to perform API calls." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment