Skip to content

Commit 8cf46e2

Browse files
committed
* Made python code PEP8 clean
* fixed bug in example
1 parent dd02ba5 commit 8cf46e2

File tree

8 files changed

+53
-50
lines changed

8 files changed

+53
-50
lines changed

examples/add_item.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515

1616
host_name = 'example.com'
1717

18-
hosts = zapi.host.get(filter={"host": host_name},selectInterfaces=["interfaceid"])
18+
hosts = zapi.host.get(filter={"host": host_name}, selectInterfaces=["interfaceid"])
1919
if hosts:
2020
host_id = hosts[0]["hostid"]
2121
print("Found host id {0}".format(host_id))
2222

2323
try:
24-
item=zapi.item.create(
24+
item = zapi.item.create(
2525
hostid=host_id,
2626
name='Used disk space on $1 in %',
2727
key_='vfs.fs.size[/,pused]',
@@ -33,6 +33,6 @@
3333
except ZabbixAPIException as e:
3434
print(e)
3535
sys.exit()
36-
print("Added item with itemid {0} to host: {1}".format(item["itemids"][0],host_name))
36+
print("Added item with itemid {0} to host: {1}".format(item["itemids"][0], host_name))
3737
else:
3838
print("No hosts found")

examples/current_issues.py

+21-23
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
Shows a list of all current issues (AKA tripped triggers)
33
"""
44

5-
from getpass import getpass
65
from pyzabbix import ZabbixAPI
76

87
# The hostname at which the Zabbix web interface is available
@@ -15,34 +14,33 @@
1514

1615
# Get a list of all issues (AKA tripped triggers)
1716
triggers = zapi.trigger.get(only_true=1,
18-
skipDependent=1,
19-
monitored=1,
20-
active=1,
21-
output='extend',
22-
expandDescription=1,
23-
expandData='host',
24-
)
17+
skipDependent=1,
18+
monitored=1,
19+
active=1,
20+
output='extend',
21+
expandDescription=1,
22+
expandData='host',
23+
)
2524

2625
# Do another query to find out which issues are Unacknowledged
2726
unack_triggers = zapi.trigger.get(only_true=1,
28-
skipDependent=1,
29-
monitored=1,
30-
active=1,
31-
output='extend',
32-
expandDescription=1,
33-
expandData='host',
34-
withLastEventUnacknowledged=1,
35-
)
27+
skipDependent=1,
28+
monitored=1,
29+
active=1,
30+
output='extend',
31+
expandDescription=1,
32+
expandData='host',
33+
withLastEventUnacknowledged=1,
34+
)
3635
unack_trigger_ids = [t['triggerid'] for t in unack_triggers]
3736
for t in triggers:
38-
t['unacknowledged'] = True if t['triggerid'] in unack_trigger_ids\
39-
else False
37+
t['unacknowledged'] = True if t['triggerid'] in unack_trigger_ids \
38+
else False
4039

4140
# Print a list containing only "tripped" triggers
4241
for t in triggers:
4342
if int(t['value']) == 1:
44-
print("{0} - {1} {2}".format(
45-
t['host'],
46-
t['description'],
47-
'(Unack)' if t['unacknowledged'] else '')
48-
)
43+
print("{0} - {1} {2}".format(t['host'],
44+
t['description'],
45+
'(Unack)' if t['unacknowledged'] else '')
46+
)

examples/fix_host_ips.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"""
88

99
import socket
10-
from getpass import getpass
1110
from pyzabbix import ZabbixAPI, ZabbixAPIException
1211

1312
# The hostname at which the Zabbix web interface is available
@@ -22,7 +21,7 @@
2221
zapi.login('Admin', 'zabbix')
2322

2423
# Loop through all hosts interfaces, getting only "main" interfaces of type "agent"
25-
for h in zapi.hostinterface.get(output=["dns","ip","useip"],selectHosts=["host"],filter={"main":1,"type":1}):
24+
for h in zapi.hostinterface.get(output=["dns", "ip", "useip"], selectHosts=["host"], filter={"main": 1, "type": 1}):
2625
# Make sure the hosts are named according to their FQDN
2726
if h['dns'] != h['hosts'][0]['host']:
2827
print('Warning: %s has dns "%s"' % (h['hosts'][0]['host'], h['dns']))

examples/history_data.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@
2222

2323
# Query item's history (integer) data
2424
history = zapi.history.get(itemids=[item_id],
25-
time_from=time_from,
26-
time_till=time_till,
27-
output='extend',
28-
limit='5000',
29-
)
25+
time_from=time_from,
26+
time_till=time_till,
27+
output='extend',
28+
limit='5000',
29+
)
3030

3131
# If nothing was found, try getting it from history (float) data
3232
if not len(history):
3333
history = zapi.history.get(itemids=[item_id],
34-
time_from=time_from,
35-
time_till=time_till,
36-
output='extend',
37-
limit='5000',
38-
history=0,
39-
)
34+
time_from=time_from,
35+
time_till=time_till,
36+
output='extend',
37+
limit='5000',
38+
history=0,
39+
)
4040

4141
# Print out each datapoint
4242
for point in history:
4343
print("{0}: {1}".format(datetime.fromtimestamp(int(point['clock']))
44-
.strftime("%x %X"), point['value']))
44+
.strftime("%x %X"), point['value']))

examples/import_templates.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,4 @@
7676
try:
7777
zapi.confimport('xml', template, rules)
7878
except ZabbixAPIException as e:
79-
print e
79+
print(e)

examples/timeout.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
TIMEOUT = 3.5
1515

1616
# You can define the timeout while creating the ZabbixAPI object:
17-
zapi = ZabbixAPI(ZABBIX_SERVER,timeout=TIMEOUT)
17+
zapi = ZabbixAPI(ZABBIX_SERVER, timeout=TIMEOUT)
1818

1919
# Login to the Zabbix API
2020
zapi.login('api_username', 'api_password')
2121

2222
# Or you can re-define it after
23-
zapi.timeout=TIMEOUT
23+
zapi.timeout = TIMEOUT

pyzabbix/__init__.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ def __init__(self,
5959
def login(self, user='', password=''):
6060
"""Convenience method for calling user.authenticate and storing the resulting auth token
6161
for further commands.
62-
If use_authenticate is set, it uses the older (Zabbix 1.8) authentication command"""
62+
If use_authenticate is set, it uses the older (Zabbix 1.8) authentication command
63+
:param password: Password used to login into Zabbix
64+
:param user: Username used to login into Zabbix
65+
"""
6366

6467
# If we have an invalid auth token, we are not allowed to send a login
6568
# request. Clear it before trying.
@@ -69,13 +72,17 @@ def login(self, user='', password=''):
6972
else:
7073
self.auth = self.user.login(user=user, password=password)
7174

72-
def confimport(self, format='', source='', rules=''):
75+
def confimport(self, confformat='', source='', rules=''):
7376
"""Alias for configuration.import because it clashes with
74-
Python's import reserved keyword"""
77+
Python's import reserved keyword
78+
:param rules:
79+
:param source:
80+
:param confformat:
81+
"""
7582

7683
return self.do_request(
7784
method="configuration.import",
78-
params={"format": format, "source": source, "rules": rules}
85+
params={"format": confformat, "source": source, "rules": rules}
7986
)['result']
8087

8188
def api_version(self):
@@ -93,7 +100,6 @@ def do_request(self, method, params=None):
93100
if self.auth and method != 'apiinfo.version':
94101
request_json['auth'] = self.auth
95102

96-
97103
logger.debug("Sending: %s", json.dumps(request_json,
98104
indent=4,
99105
separators=(',', ': ')))
@@ -124,7 +130,7 @@ def do_request(self, method, params=None):
124130
self.id += 1
125131

126132
if 'error' in response_json: # some exception
127-
if 'data' not in response_json['error']: # some errors don't contain 'data': workaround for ZBX-9340
133+
if 'data' not in response_json['error']: # some errors don't contain 'data': workaround for ZBX-9340
128134
response_json['error']['data'] = "No data"
129135
msg = "Error {code}: {message}, {data}".format(
130136
code=response_json['error']['code'],

tests/test_api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,4 @@ def test_host_delete(self):
108108
)
109109

110110
# Check response
111-
self.assertEqual(set(result["itemids"]), set(["22982", "22986"]))
111+
self.assertEqual(set(result["itemids"]), {"22982", "22986"})

0 commit comments

Comments
 (0)