-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Len Rugen
committed
Jan 6, 2023
1 parent
0aa88b2
commit b16329c
Showing
5 changed files
with
399 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
# zabbix-api-examples | ||
Examples using the Zabbix API in Python | ||
|
||
These were originally written over many years, Python style will evolve with time, hopefully for the better. These are sanitized copies from a working environment. | ||
|
||
Many reports generate html, most write to stdout. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/python3 | ||
# | ||
# Generate summary of filesystem names in DEVOP systems | ||
# | ||
|
||
from pyzabbix import ZabbixAPI | ||
from operator import itemgetter | ||
import sys | ||
from os import environ | ||
|
||
#import logging | ||
#logging.basicConfig(filename='pyzabbix_debug.log',level=logging.DEBUG) | ||
|
||
# The hostname at which the Zabbix web interface is available | ||
ZABBIX_SERVER = 'https://'+environ['Zhost']+'/zabbix' | ||
|
||
zapi = ZabbixAPI(ZABBIX_SERVER) | ||
|
||
sel_groups = ["DEVOP/Linux*", "DEVOP/Windows*"] | ||
|
||
# Login to the Zabbix API | ||
zapi.login(environ['Zuser'], environ['Zpass']) | ||
|
||
group_hosts = zapi.hostgroup.get(output='extend',search={'name':sel_groups},searchByAny="true",searchWildcardsEnabled="true",selectHosts=['name']) | ||
exclude = ['/', '/boot', 'C:', '/tmp'] | ||
|
||
for group in sorted(group_hosts, key=itemgetter('name')): | ||
for host in sorted(group['hosts'], key=itemgetter('name')): | ||
host_items = zapi.host.get(output='extend',hostids=host['hostid'],selectItems=['key_']) | ||
for item in host_items[0]['items']: | ||
if item['key_'].find('vfs.fs.size') > -1 and item['key_'].find('total') > -1: | ||
fsname_temp = item['key_'].replace('vfs.fs.size[','') | ||
fsname = fsname_temp.replace(',total]','') | ||
if fsname not in exclude: | ||
print(group['name']+','+host['name']+','+fsname) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#!/usr/bin/python3 | ||
""" | ||
Get selected host group starting with CSG, UM DOIT and User monitor. | ||
""" | ||
|
||
from pyzabbix import ZabbixAPI | ||
|
||
import json | ||
from os import environ | ||
|
||
# The hostname at which the Zabbix web interface is available | ||
ZABBIX_SERVER = 'https://'+environ['Zhost']+'/zabbix' | ||
|
||
zapi = ZabbixAPI(ZABBIX_SERVER) | ||
|
||
# Login to the Zabbix API | ||
zapi.login(environ['Zuser'], environ['Zpass']) | ||
|
||
print("<html>") | ||
print("<title>Zabbix Hosts by Proxy</title>") | ||
print("<body>") | ||
print("<h1>Zabbix Hosts by Proxy</h1>") | ||
|
||
proxies = zapi.proxy.get(output='extend',sortfield='host') | ||
|
||
#print json.dumps(proxies, indent=4, sort_keys=True) | ||
|
||
for proxy in proxies: | ||
proxy_id = proxy['proxyid'] | ||
proxy_name = proxy['host'] | ||
count = 0 | ||
items = 0 | ||
|
||
print("<h3>"+proxy_name+"</h3>") | ||
hosts = zapi.host.get(output=['host'], proxyids=proxy_id, sortfield='host',selectItems='count') | ||
print("<ol>") | ||
for host in hosts: | ||
print("<li>"+host['host'], 'items', host['items'], '</li>') | ||
count += 1 | ||
items += int(host['items']) | ||
|
||
print("</ol>") | ||
print("<p>Monitored hosts:", count, 'with', items, 'items') | ||
|
||
print("<h3>zabbix5-server.doit.missouri.edu</h3>") | ||
|
||
count = 0 | ||
items = 0 | ||
hosts = zapi.host.get(output=['host', 'proxy_hostid'], sortfield='host', selectItems='count') | ||
print("<ol>") | ||
for host in hosts: | ||
# print host['host'] | ||
if host['proxy_hostid'] == '0': | ||
# print json.dumps(host, indent=4, sort_keys=True) | ||
print("<li>"+host['host'], "items", host['items'], "</li>") | ||
count += 1 | ||
items += int(host['items']) | ||
|
||
print("</ol>") | ||
print("<p>Monitored hosts:", count, 'with', items, 'items') | ||
|
||
print("</body>") | ||
print("</html") | ||
|
||
# print json.dumps(members, indent=4, sort_keys=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,232 @@ | ||
#!/usr/bin/python3 | ||
|
||
from pyzabbix import ZabbixAPI | ||
import json | ||
import pprint | ||
from datetime import * | ||
from operator import itemgetter | ||
from os import environ | ||
|
||
# import logging | ||
# logging.basicConfig(filename='pyzabbix_debug.log',level=logging.DEBUG) | ||
|
||
# The hostname at which the Zabbix web interface is available | ||
ZABBIX_SERVER = "https://" + environ["Zhost"] + "/zabbix" | ||
|
||
zapi = ZabbixAPI(ZABBIX_SERVER) | ||
|
||
# Login to the Zabbix API | ||
zapi.login(environ["Zuser"], environ["Zpass"]) | ||
|
||
item_type = [ | ||
"Agent", | ||
"notUsed", | ||
"Trapper", | ||
"Simple", | ||
"notUsed", | ||
"Internal", | ||
"notUsed", | ||
"Agent active", | ||
"Aggregate", | ||
"Web", | ||
"External", | ||
"DataBase", | ||
"IMPI", | ||
"SSH", | ||
"TELNET", | ||
"calculated", | ||
"JMX", | ||
"SNMP Trap", | ||
"Dependant", | ||
"HTTP", | ||
"SNMP", | ||
] | ||
|
||
|
||
trigger_sev = ["N/C", "Info", "Warning", "Average", "High", "Disaster"] | ||
|
||
templates = zapi.template.get(output="extend", selectHosts="count", sortfield=["name"]) | ||
|
||
print("<html>") | ||
print("<title>Zabbix templates</title>") | ||
print("<body>") | ||
print("<h1>Zabbix templates</h1>") | ||
print("<h2>Contents</h2>") | ||
print("<ol>") | ||
|
||
# Create Contents | ||
|
||
for template in templates: | ||
if int(template["hosts"]) > 0: | ||
print( | ||
'<li><a href="#' + template["templateid"] + '">' + template["name"] + "</a>" | ||
) | ||
|
||
print("</ol>") | ||
|
||
print("<h1>Template Details</h1>") | ||
|
||
print("<hr>") | ||
|
||
# Create body | ||
|
||
for template in templates: | ||
if int(template["hosts"]) > 0: | ||
detail = zapi.template.get( | ||
output="extend", | ||
templateids=template["templateid"], | ||
selectGroups="extend", | ||
selectTemplates="extend", | ||
selectParentTemplates="extend", | ||
selectHttpTests="extend", | ||
selectItems="extend", | ||
selectDiscoveries="extend", | ||
selectTriggers="extend", | ||
selectHosts="extend", | ||
) | ||
|
||
print("<hr>") | ||
print( | ||
'<a name="' + template["templateid"] + '"</a>', | ||
"<h2>" + template["name"] + "</h2>", | ||
) | ||
|
||
if len(detail[0]["description"]) > 0: | ||
print("<b>Notes:</b>", detail[0]["description"]) | ||
|
||
if len(detail[0]["groups"]) > 0: | ||
print("<h3>Groups</h3>") | ||
for group in detail[0]["groups"]: | ||
print(group["name"], "</br>") | ||
|
||
if len(detail[0]["parentTemplates"]) > 0: | ||
print("<h3>Linked templates</h3>") | ||
for template in sorted(detail[0]["parentTemplates"], key=itemgetter("name")): | ||
print(template["name"], "<br>") | ||
|
||
if len(detail[0]["templates"]) > 0: | ||
print("<h3>Linked to templates</h3>") | ||
for template in sorted(detail[0]["templates"], key=itemgetter("name")): | ||
print(template["name"], "<br>") | ||
|
||
if len(detail[0]["hosts"]) > 0: | ||
print("<h3>Linked to hosts</h3>") | ||
linked_hosts = len(detail[0]["hosts"]) | ||
n = 0 | ||
for host in sorted(detail[0]["hosts"], key=itemgetter("name")): | ||
print(host["name"], "<br>") | ||
n += 1 | ||
if n > 9: | ||
print("..... and", linked_hosts - n, "more") | ||
break | ||
|
||
if len(detail[0]["httpTests"]) > 0: | ||
print("<h3>Http Tests</h3>") | ||
for test in detail[0]["httpTests"]: | ||
print(test["name"], "<br>") | ||
|
||
if len(detail[0]["discoveries"]) > 0: | ||
print("<h3>Discovery</h3>") | ||
print( | ||
"<table border=2><tr><th>Name</th><th>Key</th><th>Type<th>Interval</th></tr>" | ||
) | ||
for discovery in sorted(detail[0]["discoveries"], key=itemgetter("name")): | ||
print( | ||
"<tr><td>" | ||
+ discovery["name"] | ||
+ "</td><td>" | ||
+ discovery["key_"] | ||
+ "</td><td>" | ||
+ item_type[int(discovery["type"])] | ||
+ "</td><td align=right>" | ||
+ discovery["delay"] | ||
+ "</td></tr>" | ||
) | ||
lld_rule = zapi.discoveryrule.get( | ||
output="extend", | ||
templateids=detail[0]["templateid"], | ||
selectItems="extend", | ||
selectTriggers="extend", | ||
) | ||
|
||
print( | ||
"<tr><td align=right>Items</td><td colspan=4>", | ||
"<table border=1 width=100%><tr><th>Name</th><th>Key</th><th>Interval</th><th>Type</th></tr>", | ||
) | ||
for item in sorted(lld_rule[0]["items"], key=itemgetter("name")): | ||
print( | ||
"<tr><td>" | ||
+ item["name"] | ||
+ "</td><td>" | ||
+ item["key_"] | ||
+ "</td><td align=right>" | ||
+ item["delay"] | ||
+ "</td><td>" | ||
+ item_type[int(item["type"])] | ||
+ "</td></tr>" | ||
) | ||
print("</table></td></tr>") | ||
|
||
if len(lld_rule[0]["triggers"]) > 0: | ||
print( | ||
"<tr><td align=right>Triggers</td><td colspan=4>", | ||
"<table border=1 width=100%><tr><th>Name</th><th>Severity</th></tr>", | ||
) | ||
for trigger in sorted( | ||
lld_rule[0]["triggers"], key=itemgetter("description") | ||
): | ||
print( | ||
"<tr><td>" | ||
+ trigger["description"] | ||
+ "</td><td>" | ||
+ trigger_sev[int(trigger["priority"])] | ||
+ "</td></tr>" | ||
) | ||
print("</table></td></tr>") | ||
|
||
print("</table>") | ||
|
||
# print("<tr><td colspan=4><pre>", json.dumps(trigger, indent=4, sort_keys=True), "</td></pre>") | ||
|
||
if len(detail[0]["items"]) > 0: | ||
print("<h3>Items</h3>") | ||
print("<table border=2>") | ||
print("<tr><th>Name<r/th><th>Key</th><th>Interval</th><th>Type</th></tr>") | ||
|
||
for item in sorted(detail[0]["items"], key=itemgetter("name")): | ||
print( | ||
"<tr><td>" | ||
+ item["name"] | ||
+ "</td><td>" | ||
+ item["key_"] | ||
+ "</td><td align=right>" | ||
+ item["delay"] | ||
+ "</td><td>" | ||
+ item_type[int(item["type"])] | ||
+ "</td></tr>" | ||
) | ||
|
||
print("</table>") | ||
|
||
if len(detail[0]["triggers"]) > 0: | ||
print("<h3>Triggers</h3>") | ||
print("<table border=2>") | ||
print("<tr><th>Name</th><th>Severity</th></tr>") | ||
|
||
for trigger in sorted(detail[0]["triggers"], key=itemgetter("description")): | ||
print( | ||
"<tr><td>" | ||
+ trigger["description"] | ||
+ "</td><td>" | ||
+ trigger_sev[int(trigger["priority"])] | ||
+ "</td></tr>" | ||
) | ||
|
||
print("</table>") | ||
|
||
print("<br><hr>") | ||
|
||
# print("<pre>") | ||
# if len(detail[0]['discoveries']) > 0: | ||
# print(json.dumps(detail, indent=4, sort_keys=True) | ||
# print("</pre>") |
Oops, something went wrong.