Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
pyzabbix example
from pyzabbix import ZabbixAPI
import sys
import argparse
url = 'http://localhost/'
user = 'Admin'
password = 'zabbix'
CLI=argparse.ArgumentParser()
CLI.add_argument(
"--hosts", # name on the CLI - drop the `--` for positional/required parameters
nargs="*", # 0 or more values expected => creates a list
type=str,
default=[], # default if nothing is provided
)
args = CLI.parse_args()
status = 0
try:
zapi = ZabbixAPI(url)
zapi.login(user, password)
groups_id = 1
for h in args.hosts:
#print("host: %r" % h.replace(".yml", ""))
x = {
"host": h,
"groups": groups_id,
"interfaces": [
{"type":1,"main":1,"useip":1,"ip":"127.0.0.1","dns":"","port":"10050"}
]
}
rc = zapi.host.create()
print("host: %r; rc: %d" % h.replace(".yml", ""), rc)
status += rc
print(rc)
sys.exit(status)
except Exception as e:
print("exception")
sys.exit(999)
from pyzabbix import ZabbixAPI
import sys
url = 'http://localhost/'
user = 'Admin'
password = 'zabbix'
try:
zapi = ZabbixAPI(url)
zapi.login(user, password)
hosts = zapi.host.get()
for h in hosts:
print("HOSTNAME: '%s'" % (h['host']))
sys.exit(0)
except Exception as e:
print("exception")
sys.exit(999)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.