Commit 308026f2 by René Moser

improved zabbix inventory. added sys.exit(1) where needed, added some exception handling.

parent 500e6fa3
...@@ -4,8 +4,8 @@ ...@@ -4,8 +4,8 @@
[zabbix] [zabbix]
# Server location # Server location
server = http://192.168.0.1/zabbix server = http://zabbix.example.com/zabbix
# Login # Login
username = username = admin
password = password = zabbix
#!/usr/bin/python #!/usr/bin/env python
# (c) 2013, Greg Buehler # (c) 2013, Greg Buehler
# #
...@@ -20,16 +20,26 @@ ...@@ -20,16 +20,26 @@
###################################################################### ######################################################################
""" """
Zabbix external inventory script. Returns hosts and hostgroups from Zabbix. Zabbix Server external inventory script.
========================================
Returns hosts and hostgroups from Zabbix Server.
Configuration is read from `zabbix.ini`.
Tested with Zabbix Server 2.0.6.
""" """
import os, sys import os, sys
import json import json
import argparse import argparse
import ConfigParser import ConfigParser
from zabbix_api import ZabbixAPI
try:
from zabbix_api import ZabbixAPI
except:
print "Error: Zabbix API library must be installed: pip install zabbix-api."
sys.exit(1)
try: try:
import json import json
...@@ -97,8 +107,12 @@ class ZabbixInventory(object): ...@@ -97,8 +107,12 @@ class ZabbixInventory(object):
self.read_cli() self.read_cli()
if self.zabbix_server and self.zabbix_username: if self.zabbix_server and self.zabbix_username:
api = ZabbixAPI(server=self.zabbix_server) try:
api.login(user=self.zabbix_username, password=self.zabbix_password) api = ZabbixAPI(server=self.zabbix_server)
api.login(user=self.zabbix_username, password=self.zabbix_password)
except BaseException, e:
print "Error: Could not login to Zabbix server. Check your zabbix.ini."
sys.exit(1)
if self.options.host: if self.options.host:
data = self.get_host(api, self.options.host) data = self.get_host(api, self.options.host)
...@@ -107,7 +121,13 @@ class ZabbixInventory(object): ...@@ -107,7 +121,13 @@ class ZabbixInventory(object):
elif self.options.list: elif self.options.list:
data = self.get_list(api) data = self.get_list(api)
print json.dumps(data, indent=2) print json.dumps(data, indent=2)
else:
print "usage: --list ..OR.. --host <hostname>"
sys.exit(1)
else: else:
print "Configuration of server and credentials is required" print "Error: Configuration of server and credentials are required. See zabbix.ini."
sys.exit(1)
ZabbixInventory() ZabbixInventory()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment