diff --git a/zabbix/README b/zabbix/README deleted file mode 100644 index 1d3f4bf..0000000 --- a/zabbix/README +++ /dev/null @@ -1,13 +0,0 @@ -This is an implementation of the Zabbix API in Python. -Please note that the Zabbix API is still in a draft state, -and subject to change. - -Implementations of the Zabbix API in other languages may -be found on the wiki. - -Zabbix 1.8 and 2.0 are supported. - -See also: -* http://www.zabbix.com/wiki/doc/api -* http://www.zabbix.com/documentation/2.0/manual/appendix/api/api -* http://www.zabbix.com/forum/showthread.php?t=15218 diff --git a/zabbix/README.md b/zabbix/README.md new file mode 100644 index 0000000..4cd7dfe --- /dev/null +++ b/zabbix/README.md @@ -0,0 +1,38 @@ +# Zabbix # + +This is an implementation of the Zabbix API in Python. +Please note that the Zabbix API is still in a draft state, +and subject to change. + +Implementations of the Zabbix API in other languages may +be found on the wiki. + +Zabbix 1.8 and 2.0 are supported. + +## Documentation ## + +* http://www.zabbix.com/wiki/doc/api +* http://www.zabbix.com/documentation/2.0/manual/appendix/api/api +* http://www.zabbix.com/forum/showthread.php?t=15218 + +## License ## + +LGPL 2.1 http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html + +Zabbix API Python Library. + +Original Ruby Library is Copyright (C) 2009 Andrew Nelson nelsonab(at)red-tux(dot)net + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA diff --git a/zabbix/examples/zabbix_add_group_host.py b/zabbix/examples/zabbix_add_group_host.py new file mode 100644 index 0000000..b06ff34 --- /dev/null +++ b/zabbix/examples/zabbix_add_group_host.py @@ -0,0 +1,66 @@ +"""Example script that do an API call to create a sample test data on Zabbix +server. +When you run this script it will create for you a test group, test host. +For all newly created items in zabbix minimal data required has been used. + +You have to define connection parameters in this script in order to run it. +Script was tested on zabbix API 1.4 which is part of Zabbix 2.0.0 or higher. + +@author: richard.kellner +@created: 27.10.2012 +""" +from zabbix_api import ZabbixAPI + +"""You need to specify connection variables""" +server="http://127.0.0.1" +username="api" +password="apipass" + +def create_group(group): + """Function that will create host group on Zabbix Server.""" + result = zapi.hostgroup.create({ 'name' : group }) + try: + result['groupids'] + except NameError: + """API throws an exception if such group already exists""" + print 'There was na error while creating group' + + print 'Group "'+ group +'" has been created with id: '+ \ + result['groupids'][0] + return result['groupids'][0] + +def create_host(host): + """Function that will create host on Zabbix Server.""" + result = zapi.host.create({ "host" : (host), + "interfaces" : [{ + "type": 1, + "main": 1, + "useip" : 1, + "ip" : "127.0.0.1", + "dns" : "", + "port" : "10050", + }], + "groups" : [{ + "groupid" : groupid, + }], + }) + try: + result['hostids'] + except NameError: + """API throws an exception if such host already exists""" + print 'There was na error while creating host' + print 'Host "'+ host +'" has been created with id: '+ \ + result['hostids'][0] + return result['hostids'][0] + +def test_API_version(): + """Method to check if server has compatible version of API.""" + if zapi.api_version() <= 1.4: + raise Exception('Example script works only with API 1.4 or higher.') + +zapi = ZabbixAPI(server=server, path="", log_level=30) +zapi.login(username, password) + +test_API_version() +groupid = create_group('test_API_group') +hostid = create_host('test_API_host') diff --git a/zabbix/zabbix_item_add_example.py b/zabbix/examples/zabbix_add_item_example.py similarity index 100% rename from zabbix/zabbix_item_add_example.py rename to zabbix/examples/zabbix_add_item_example.py diff --git a/zabbix/examples/zabbix_test_connection.py b/zabbix/examples/zabbix_test_connection.py new file mode 100644 index 0000000..4131bfb --- /dev/null +++ b/zabbix/examples/zabbix_test_connection.py @@ -0,0 +1,25 @@ +"""Example script that do an API connection to Zabbix server and print API +version. + +You have to define connection parameters in this script in order to run it. + +@author: richard.kellner +@created: 28.10.2012 +""" +import sys +from zabbix_api import ZabbixAPI, ZabbixAPIException + +"""You need to specify connection variables""" +server="http://127.0.0.1" +username="api" +password="apipass" + +zapi = ZabbixAPI(server=server, path="", log_level=30) +zapi.login(username, password) + +try: + zapi.login(username, password) + print "Logged in: %s" % str(zapi.test_login()) + print "Zabbix API Version: %s" % zapi.api_version() +except ZabbixAPIException, e: + sys.stderr.write(str(e) + '\n') diff --git a/zabbix/zabbix_rpc_test.py b/zabbix/zabbix_rpc_test.py index 3b4b1a9..c1ed581 100755 --- a/zabbix/zabbix_rpc_test.py +++ b/zabbix/zabbix_rpc_test.py @@ -1,7 +1,6 @@ #!/usr/bin/python import optparse import sys -import traceback from getpass import getpass from zabbix_api import ZabbixAPI, ZabbixAPIException @@ -18,6 +17,8 @@ def get_options(): dest="username", help="Username (Will prompt if not given)") parser.add_option("-p", "--password", action="store", type="string", \ dest="password", help="Password (Will prompt if not given)") + parser.add_option("-l", "--log_level", action="store", type="int", \ + dest="log_level", help="Log Level (Default value 30)") options, args = parser.parse_args() @@ -34,6 +35,9 @@ def get_options(): if not options.username and not options.password: show_help(parser) + if not options.log_level: + options.log_level = 30 + return options, args def show_help(p): @@ -48,7 +52,7 @@ def errmsg(msg): if __name__ == "__main__": options, args = get_options() - zapi = ZabbixAPI(server=options.server,log_level=3) + zapi = ZabbixAPI(server=options.server,log_level=options.log_level) try: zapi.login(options.username, options.password)