Skip to content

Commit

Permalink
improved zabbix inventory. added sys.exit(1) where needed, added some…
Browse files Browse the repository at this point in the history
… exception handling.
  • Loading branch information
resmo committed Aug 22, 2013
1 parent 500e6fa commit 308026f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
6 changes: 3 additions & 3 deletions plugins/inventory/zabbix.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
[zabbix]

# Server location
server = http://192.168.0.1/zabbix
server = http://zabbix.example.com/zabbix

# Login
username =
password =
username = admin
password = zabbix
34 changes: 27 additions & 7 deletions plugins/inventory/zabbix.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python

# (c) 2013, Greg Buehler
#
Expand All @@ -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 json
import argparse
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:
import json
Expand Down Expand Up @@ -97,8 +107,12 @@ def __init__(self):
self.read_cli()

if self.zabbix_server and self.zabbix_username:
api = ZabbixAPI(server=self.zabbix_server)
api.login(user=self.zabbix_username, password=self.zabbix_password)
try:
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:
data = self.get_host(api, self.options.host)
Expand All @@ -107,7 +121,13 @@ def __init__(self):
elif self.options.list:
data = self.get_list(api)
print json.dumps(data, indent=2)

else:
print "usage: --list ..OR.. --host <hostname>"
sys.exit(1)

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()

0 comments on commit 308026f

Please sign in to comment.