Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ before_install:
- curl -L https://github.com/docker/compose/releases/download/1.4.1/docker-compose-`uname -s`-`uname -m` > docker-compose
- chmod +x docker-compose
- sudo mv docker-compose /usr/local/bin
- cd tests/data/Zabbix${ZABBIX_VERSION} && docker-compose up -d
- cd ../../../
- pushd tests/data/Zabbix${ZABBIX_VERSION} && docker-compose up -d
- popd

install:
- pip install coveralls
Expand Down
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,45 @@ You can install Zabbix modules for Python with pip:
pip install py-zabbix
```

## [Documentaion for py-zabbix](https://py-zabbix.readthedocs.org/en/latest/)
## Official documentaion for [py-zabbix](https://py-zabbix.readthedocs.org/en/latest/)

## Examples

### ZabbixAPI

```python
from zabbix.api import ZabbixAPI

# Create ZabbixAPI class instance
zapi = ZabbixAPI(url='https://localhost/zabbix/', user='admin', password='zabbix')

# Get all monitored hosts
result1 = zapi.host.get(monitored_hosts=1, output='extend')

# Get all disabled hosts
result2 = zapi.do_request('host.get',
{
'filter': {'status': 1},
'output': 'extend'
})

# Filter results
hostnames1 = [host['host'] for host in result1]
hostnames2 = [host['host'] for host in result2['result']]
```

### ZabbixSender

```python
from pyzabbix import ZabbixMetric, ZabbixSender

# Send metrics to zabbix trapper
packet = [
ZabbixMetric('hostname1', 'test[cpu_usage]', 2),
ZabbixMetric('hostname1', 'test[system_status]', "OK"),
ZabbixMetric('hostname1', 'test[disk_io]', '0.1'),
ZabbixMetric('hostname1', 'test[cpu_usage]', 20, 1411598020),
]

result = ZabbixSender(use_config=True).send(packet)
```
13 changes: 0 additions & 13 deletions examples/test_api.py

This file was deleted.

20 changes: 0 additions & 20 deletions examples/test_sender.py

This file was deleted.

4 changes: 1 addition & 3 deletions pyzabbix/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import pyzabbix.version

from .api import ZabbixAPI, ZabbixAPIException
from .sender import ZabbixMetric, ZabbixSender

__version__ = pyzabbix.version.__version__
__version__ = '1.1.1'
73 changes: 0 additions & 73 deletions pyzabbix/test.py

This file was deleted.

1 change: 0 additions & 1 deletion pyzabbix/version.py

This file was deleted.

13 changes: 3 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
#!/usr/bin/env python
from setuptools import setup

import os

base_dir = os.path.dirname(__file__)

about = {}
with open(os.path.join(base_dir, 'pyzabbix', 'version.py')) as f:
exec(f.read(), about)
from pyzabbix import __version__

setup(name='py-zabbix',
version=about['__version__'],
description='Python modules to work with zabbix.',
version=__version__,
description='Python module to work with zabbix.',
url='https://github.com/blacked/py-zabbix',
author='Alexey Dubkov',
author_email='alexey.dubkov@gmail.com',
Expand Down
6 changes: 3 additions & 3 deletions tests/test_Functional_API.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
class FunctionalAPI(TestCase):
def test_LoginToServer(self):
try:
zapi = ZabbixAPI(url='http://127.0.0.1',
user='Admin',
password='zabbix')
ZabbixAPI(url='http://127.0.0.1',
user='Admin',
password='zabbix')
except ZabbixAPIException:
self.fail('Can\'t login to Zabbix')

Expand Down
6 changes: 3 additions & 3 deletions tests/test_Functional_API_Old.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
class FunctionalAPI(TestCase):
def test_LoginToServer(self):
try:
zapi = ZabbixAPI(url='http://127.0.0.1',
user='Admin',
password='zabbix')
ZabbixAPI(url='http://127.0.0.1',
user='Admin',
password='zabbix')
except ZabbixAPIException:
self.fail('Can\'t login to Zabbix')
85 changes: 85 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import json

from unittest import TestCase
from pyzabbix import ZabbixAPI
from mock import patch
from sys import version_info

# For Python 2 and 3 compatibility
if version_info[0] == 2:
urlopen = 'urllib2.urlopen'
res_type = str
elif version_info[0] >= 3:
res_type = bytes
urlopen = 'urllib.request.urlopen'


class MockResponse(object):

def __init__(self, ret, code=200, msg='OK'):
self.ret = ret
self.code = code
self.msg = msg
self.headers = {'content-type': 'text/plain; charset=utf-8'}

def read(self):
return res_type(self.ret.encode('utf-8'))

def getcode(self):
return self.code


class TestZabbixAPI(TestCase):

def setUp(self):
"Mock urllib2.urlopen"
self.patcher = patch(urlopen)
self.urlopen_mock = self.patcher.start()

def test_api_version(self):
ret = {'result': '2.2.5'}
self.urlopen_mock.return_value = MockResponse(json.dumps(ret))
res = ZabbixAPI().api_version()
self.assertEqual(res, '2.2.5')

def test_login(self):
req = {'user': 'admin', 'password': 'zabbix'}
ret = {
'jsonrpc': '2.0',
'result': '0424bd59b807674191e7d77572075f33',
'id': 1
}
self.urlopen_mock.return_value = MockResponse(json.dumps(ret))
res = ZabbixAPI().user.login(**req)
self.assertEqual(res, '0424bd59b807674191e7d77572075f33')

def test_do_request(self):
req = 'apiinfo.version'
ret = {
'jsonrpc': '2.0',
'result': '2.2.5',
'id': 1
}
self.urlopen_mock.return_value = MockResponse(json.dumps(ret))
res = ZabbixAPI().do_request(req)
self.assertEqual(res, ret)

def test_get_id_item(self):
ret = {
'jsonrpc': '2.0',
'result':
[{
'itemid': '23298',
'hostid': '10084',
'name': 'Test Item',
'key_': 'system.cpu.switches',
'description': '',
}],
'id': 1,
}
self.urlopen_mock.return_value = MockResponse(json.dumps(ret))
res = ZabbixAPI().get_id('item', item='Test Item')
self.assertEqual(res, 23298)

def tearDown(self):
self.patcher.stop()
Loading