From 67591e125e9c1333f5ff147a415f376e34b61217 Mon Sep 17 00:00:00 2001 From: Richard Kellner Date: Sat, 27 Oct 2012 00:25:28 +0200 Subject: [PATCH 1/6] 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/6] 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/6] 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/6] 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 From ddc5d38978e90d6153139a168ae6b9134c70de38 Mon Sep 17 00:00:00 2001 From: Richard Kellner Date: Sun, 28 Oct 2012 19:36:07 +0100 Subject: [PATCH 5/6] Made zabbixAPI instalable --- MANIFEST.in | 2 + zabbix/README.md => README.md | 0 setup.py | 43 +++++++++++++++++++ zabbix/setup.py | 28 ------------ zabbixAPI/__init__.py | 0 zabbixAPI/examples/__init__.py | 0 .../examples/zabbix_add_group_host.py | 0 .../examples/zabbix_add_item_example.py | 0 .../examples/zabbix_test_connection.py | 0 {zabbix => zabbixAPI}/zabbix_api.py | 0 {zabbix => zabbixAPI}/zabbix_rpc_test.py | 0 11 files changed, 45 insertions(+), 28 deletions(-) create mode 100644 MANIFEST.in rename zabbix/README.md => README.md (100%) create mode 100755 setup.py delete mode 100755 zabbix/setup.py create mode 100644 zabbixAPI/__init__.py create mode 100644 zabbixAPI/examples/__init__.py rename {zabbix => zabbixAPI}/examples/zabbix_add_group_host.py (100%) rename {zabbix => zabbixAPI}/examples/zabbix_add_item_example.py (100%) rename {zabbix => zabbixAPI}/examples/zabbix_test_connection.py (100%) rename {zabbix => zabbixAPI}/zabbix_api.py (100%) rename {zabbix => zabbixAPI}/zabbix_rpc_test.py (100%) diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..f229d57 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,2 @@ +include README.md +recursive-include zabbixAPI/examples *.py diff --git a/zabbix/README.md b/README.md similarity index 100% rename from zabbix/README.md rename to README.md diff --git a/setup.py b/setup.py new file mode 100755 index 0000000..e7a3301 --- /dev/null +++ b/setup.py @@ -0,0 +1,43 @@ +#!/usr/bin/python2 +# -*- coding: utf-8 -*- + +""" +ZabbixAPI + +This software is licensed as described in the README.md file, which you should +have received as part of this distribution. +""" +import os +from setuptools import setup, find_packages, findall + + +def read(fname): + return open(os.path.join(os.path.dirname(__file__), fname)).read() + +setup( + name='zabbixAPI', + url='https://github.com/ricco386/zabbixAPI', + version='0.1', + license='GNU LGPL 2.1', + author='Aleksandr Balezin', + author_email='gescheit@list.ru', + description='Zabbix API', + long_description=read('README.md'), + keywords="zabbix api monitoring", + py_modules=['zabbixAPI'], + packages=['zabbixAPI'], + platforms='any', + classifiers = [ + "Programming Language :: Python", + "Programming Language :: Python :: 2", + "Programming Language :: Python :: 3", + "License :: OSI Approved :: GNU Library or Lesser General Public \ +License (LGPL)", + "Operating System :: OS Independent", + "Development Status :: 4 - Beta", + "Natural Language :: English", + "Topic :: System :: Monitoring", + "Topic :: System :: Networking :: Monitoring", + "Topic :: System :: Systems Administration", + ] +) diff --git a/zabbix/setup.py b/zabbix/setup.py deleted file mode 100755 index 783fd35..0000000 --- a/zabbix/setup.py +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/python2 -# -*- coding: utf-8 -*- - -""" -Zabbix API -""" -import os -from setuptools import setup, find_packages, findall - - -def read(fname): - return open(os.path.join(os.path.dirname(__file__), fname)).read() - - -setup( - name='zabbix-api', - url='https://github.com/gescheit/scripts', - version='0.1', - license='GNU LGPL 2.1', - author='Aleksandr Balezin', - author_email='gescheit@list.ru', - description='Zabbix API', - long_description=read('README'), - py_modules=['zabbix_api'], - include_package_data=True, - zip_safe=False, - platforms='any', -) diff --git a/zabbixAPI/__init__.py b/zabbixAPI/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/zabbixAPI/examples/__init__.py b/zabbixAPI/examples/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/zabbix/examples/zabbix_add_group_host.py b/zabbixAPI/examples/zabbix_add_group_host.py similarity index 100% rename from zabbix/examples/zabbix_add_group_host.py rename to zabbixAPI/examples/zabbix_add_group_host.py diff --git a/zabbix/examples/zabbix_add_item_example.py b/zabbixAPI/examples/zabbix_add_item_example.py similarity index 100% rename from zabbix/examples/zabbix_add_item_example.py rename to zabbixAPI/examples/zabbix_add_item_example.py diff --git a/zabbix/examples/zabbix_test_connection.py b/zabbixAPI/examples/zabbix_test_connection.py similarity index 100% rename from zabbix/examples/zabbix_test_connection.py rename to zabbixAPI/examples/zabbix_test_connection.py diff --git a/zabbix/zabbix_api.py b/zabbixAPI/zabbix_api.py similarity index 100% rename from zabbix/zabbix_api.py rename to zabbixAPI/zabbix_api.py diff --git a/zabbix/zabbix_rpc_test.py b/zabbixAPI/zabbix_rpc_test.py similarity index 100% rename from zabbix/zabbix_rpc_test.py rename to zabbixAPI/zabbix_rpc_test.py From 02b36f869bf4ef03306955756a4d96ba533cbeb6 Mon Sep 17 00:00:00 2001 From: Richard Kellner Date: Sun, 28 Oct 2012 20:08:00 +0100 Subject: [PATCH 6/6] Updated README Markup --- README.md => README.rst | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) rename README.md => README.rst (90%) diff --git a/README.md b/README.rst similarity index 90% rename from README.md rename to README.rst index 4cd7dfe..3d5dc29 100644 --- a/README.md +++ b/README.rst @@ -1,4 +1,5 @@ -# Zabbix # +Zabbix +###### This is an implementation of the Zabbix API in Python. Please note that the Zabbix API is still in a draft state, @@ -9,15 +10,17 @@ be found on the wiki. Zabbix 1.8 and 2.0 are supported. -## Documentation ## +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 ## +License +------- -LGPL 2.1 http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html +`LGPL 2.1 ` Zabbix API Python Library.