Created
June 27, 2020 14:12
-
-
Save tuxmartin/4edadd641af4beb83c1e223f3c52dd50 to your computer and use it in GitHub Desktop.
pyzabbix example
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
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) |
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
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