Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added some new functions for work with users, hosts and maps #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .idea/atlassian-ide-plugin.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/py-zabbix.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

335 changes: 335 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

92 changes: 92 additions & 0 deletions pyzabbix/hosts-organizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# -*- encoding: utf-8 -*-
#
# Copyright © 2017 Andrey Makarov
#
# This file is user for simple manipulations with hosts
from pprint import pprint

from pyzabbix import ZabbixAPIException


class HostsOrganizer:

"""
This function gets an object of ZabbixAPI connection to the Zabbix server
Gets template id, host id, must be a string value
Used for setting template on host
"""
def add_template_on_host(z, template_id, host_id):
try:
response = z.do_request(method="template.massadd", params={
"templates": [
{
"templateid": template_id
}
],
"hosts": [
{
"hostid": host_id
},
]
})
pprint(response)
except ZabbixAPIException as e:
print(e)

"""
This function gets an object of ZabbixAPI connection to the Zabbix server
Gets hosts group's id, must be a string value
Returns list of duplicates
"""
def search_hosts_duplicates(z, group_id):
ids = z.do_request(method="host.get", params={
"output": ["hostid"],
"filter": {
"groupids": group_id
}
})

ips = []
duplicates = []
for i, host in enumerate(ids['result']):
try:
response = z.do_request(method="hostinterface.get", params={
"output": ["ip", "hostid"],
"hostids": host['hostid']
})
except ZabbixAPIException as e:
print(e)
else:
ip = response['result'][0]['ip']
if ip in ips:
duplicates.append({
'ip': ip,
'id': response['result'][0]['hostid']
})
else:
ips.append(ip)
return duplicates

"""
This function gets an object of ZabbixAPI connection to the Zabbix server
Gets template id, group id, must be a string value
Used for setting template on group
"""
def add_template_on_group(z, template_id, group_id):
icmp_ping = "10104"
try:
response = z.do_request(method="template.massadd", params={
"templates": [
{
"templateid": template_id
}
],
"groups": [
{
"groupid": group_id
}
]
})
pprint(response)
except ZabbixAPIException as e:
print(e)
151 changes: 151 additions & 0 deletions pyzabbix/maps-organizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# -*- encoding: utf-8 -*-
#
# Copyright © 2017 Andrey Makarov
#
# This file is user for simple manipulations with hosts's maps

from pprint import pprint

from pyzabbix import ZabbixAPIException


class MapsOrganizer:
"""
This function gets an object of ZabbixAPI connection to the Zabbix server
Gets user name, must be a string value
Returns list with user's maps with map's labels and ids
"""
def get_user_maps(z, user_name):
id = z.get_user_id(user_name)
try:
response = z.do_request(method="map.get", params={
"output": ["label"],
"userids": id
})
except ZabbixAPIException as e:
print(e)
return response

"""
This function gets an object of ZabbixAPI connection to the Zabbix server
Gets user name, must be a string value
Returns user's id
"""
def get_user_id(z, user_name):
try:
response = z.do_request(method="user.get", params={
"search": {"alias": user_name},
"output": "extend"
})
except ZabbixAPIException as e:
print(e)
return response['result'][0]["userid"]

"""
This function gets an object of ZabbixAPI connection to the Zabbix server
Gets maps id, must be a string value
Get's label's text, must be a string value
Used for changing label's value
"""
def change_elements_label(z, sysmapid, label):
for id in sysmapid:
# pprint(sysmapid)
new_request = []
try:
response = z.do_request(method="map.get", params={
"sysmapids": id,
"selectSelements": "extend",
"output": "extend"
})['result'][0]['selements']

# pprint(response)

for item in response:
item["label"] = label
new_request.append(item)
# pprint(new_request)

except ZabbixAPIException as e:
print(e)
else:
try:
resp = z.do_request(method="map.update", params={
"sysmapid": id,
"selements": new_request,
})
except ZabbixAPIException as e:
print(e, id)

def get_sysmapid():
ids = []
try:
response = z.do_request(method="map.get", params={
"output": "extends"
})
for id in response['result']:
i = id['sysmapid']
ids.append(i)
except ZabbixAPIException as e:
print(e)
return []
else:
return ids

"""
This function gets an object of ZabbixAPI connection to the Zabbix server
Gets map's id, must be a string value
Get's returned element such as links
Returns map's elements
"""
def get_elements(z, sysmapid, returned_element):
try:
response = z.do_request(method="map.get", params={
"sysmapids": sysmapid,
"output": "extend",
"selectSelements": "extend",
"selectLinks": "extend",
"selectUsers": "extend",
"selectUserGroups": "extend",
"selectShapes": "extend",
"selectLines": "extend"
})

# pprint(response['result'][0]['links'])

except ZabbixAPIException as e:
print(e)
return response['result'][0][returned_element]

"""
This function gets an object of ZabbixAPI connection to the Zabbix server
Gets map's id, must be a string value
Get's label type, must be a string value
Used for changing label type
"""
def change_label_type(z, sysmapid, label_type):
for id in sysmapid:
# pprint(sysmapid)
new_request = []
try:
response = z.do_request(method="map.get", params={
"sysmapids": id,
"output": "extend"
})['result'][0]['label_type']

# pprint(response)

for item in response:
item = label_type
new_request.append(item)
# pprint(new_request)

except ZabbixAPIException as e:
print(e)
else:
try:
resp = z.do_request(method="map.update", params={
"sysmapid": id,
"selements": new_request,
})
except ZabbixAPIException as e:
print(e)
82 changes: 82 additions & 0 deletions pyzabbix/users-organizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# -*- encoding: utf-8 -*-
#
# Copyright © 2017 Andrey Makarov
#
# This file is user for simple manipulations with users

from pprint import pprint

from pyzabbix import ZabbixAPIException


class UserOrganizer:
"""
This function gets an object of ZabbixAPI connection to the Zabbix server
Returns user's id and aliases
"""
def get_users(z):
try:
response = z.do_request(method="user.get", params={
"filter": "alias",
"output": ["userid", "alias"]
})
result = response['result']
except ZabbixAPIException as e:
print(e)
return result

"""
This function gets an object of ZabbixAPI connection to the Zabbix server
Gets user name, must be a string value
Prints users alias, ip, name, surname, id
"""
def get_user_info(z, user_name):
try:
response = z.do_request(method="user.get", params={
"search": {"alias": user_name},
"output": "extend"
})
except ZabbixAPIException as e:
print(e)
nick_name = response['result'][0]['alias']
attempt_ip = response['result'][0]['attempt_ip']
name = response['result'][0]['name']
surname = response['result'][0]['surname']
user_id = response['result'][0]['userid']

pprint("nick name = " + nick_name)
pprint("ip = " + attempt_ip)
pprint("name = " + name)
pprint("surname = " + surname)
pprint("user id = " + user_id)

"""
This function gets an object of ZabbixAPI connection to the Zabbix server
Gets user name, must be a string value
Returns user's maps
"""
def get_user_maps(z, user_name):
id = z.get_user_id(user_name)
try:
response = z.do_request(method="map.get", params={
"output": ["label"],
"userids": id
})
except ZabbixAPIException as e:
print(e)
return response

"""
This function gets an object of ZabbixAPI connection to the Zabbix server
Gets user name, must be a string value
Returns user's id
"""
def get_user_id(z, user_name):
try:
response = z.do_request(method="user.get", params={
"search": {"alias": user_name},
"output": "extend"
})
except ZabbixAPIException as e:
print(e)
return response['result'][0]["userid"]