From 67591e125e9c1333f5ff147a415f376e34b61217 Mon Sep 17 00:00:00 2001 From: Richard Kellner Date: Sat, 27 Oct 2012 00:25:28 +0200 Subject: [PATCH 1/4] Updated rpc test to have option to set logging level --- zabbix/zabbix_rpc_test.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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) From c78dcb4adf4a9aab6bb5edcd678d23464b0ed5b7 Mon Sep 17 00:00:00 2001 From: Richard Kellner Date: Sun, 28 Oct 2012 18:07:51 +0100 Subject: [PATCH 2/4] Added test connection script set logging to level 30 by default --- zabbix/examples/zabbix_add_group_host.py | 66 +++++++++++++++++++ .../zabbix_add_item_example.py} | 0 zabbix/examples/zabbix_test_connection.py | 25 +++++++ 3 files changed, 91 insertions(+) create mode 100644 zabbix/examples/zabbix_add_group_host.py rename zabbix/{zabbix_item_add_example.py => examples/zabbix_add_item_example.py} (100%) create mode 100644 zabbix/examples/zabbix_test_connection.py 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') From 45f3a1dab32bbd1adbca04838e6813cb1d537a9a Mon Sep 17 00:00:00 2001 From: Richard Kellner Date: Sun, 28 Oct 2012 18:35:02 +0100 Subject: [PATCH 3/4] Added licence information and minor styling updates --- zabbix/README | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/zabbix/README b/zabbix/README index 1d3f4bf..4cd7dfe 100644 --- a/zabbix/README +++ b/zabbix/README @@ -1,3 +1,5 @@ +# 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. @@ -7,7 +9,30 @@ be found on the wiki. Zabbix 1.8 and 2.0 are supported. -See also: +## 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 From 4d1b4048b52d9d7c731d18f372450179311ce32d Mon Sep 17 00:00:00 2001 From: Richard Kellner Date: Sun, 28 Oct 2012 18:37:54 +0100 Subject: [PATCH 4/4] Renamed README so it have markdown on github --- zabbix/{README => README.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename zabbix/{README => README.md} (100%) diff --git a/zabbix/README b/zabbix/README.md similarity index 100% rename from zabbix/README rename to zabbix/README.md