tech:zabbix_script_python_monitor_unmonitor_autoadd_host
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédente | |||
| tech:zabbix_script_python_monitor_unmonitor_autoadd_host [2025/11/11 17:09] – Jean-Baptiste | tech:zabbix_script_python_monitor_unmonitor_autoadd_host [2026/05/29 11:49] (Version actuelle) – Jean-Baptiste | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | < | ||
| + | {{tag> | ||
| + | |||
| + | # Zabbix script python monitor unmonitor autoadd host | ||
| + | |||
| + | Ce script permet : | ||
| + | * l' | ||
| + | * La désactivation automatique de la supervision d'un hôte | ||
| + | |||
| + | Il prend comme paramètre le nom de l' | ||
| + | |||
| + | Il servait au " | ||
| + | |||
| + | `zbxunmon.py` | ||
| + | ~~~python | ||
| + | #! / | ||
| + | # -*- coding: utf-8 -*- | ||
| + | # License: GNU GPL | ||
| + | |||
| + | """ | ||
| + | zbxunmon.ini | ||
| + | [SERVER] | ||
| + | URL=https:// | ||
| + | USER=api | ||
| + | PWD=P@ssw0rd | ||
| + | """ | ||
| + | |||
| + | import socket | ||
| + | import argparse | ||
| + | import configparser | ||
| + | from sys import argv, exit, stderr | ||
| + | from os import environ, path | ||
| + | |||
| + | import zabbix_client | ||
| + | |||
| + | ficconf=argv[0] | ||
| + | ficconf=ficconf.replace(' | ||
| + | ficconf=ficconf + ' | ||
| + | |||
| + | config = configparser.ConfigParser() | ||
| + | config.read(ficconf) | ||
| + | zbxconf | ||
| + | zabbix_url | ||
| + | zabbix_user = zbxconf.get(' | ||
| + | zabbix_pwd | ||
| + | try: | ||
| + | environ[' | ||
| + | except TypeError: | ||
| + | pass | ||
| + | |||
| + | |||
| + | # Zabbix ' | ||
| + | MONITORED=' | ||
| + | NOT_MONITORED=' | ||
| + | |||
| + | parser = argparse.ArgumentParser() | ||
| + | parser.add_argument(' | ||
| + | parser.add_argument(' | ||
| + | parser.add_argument(' | ||
| + | parser.add_argument(' | ||
| + | args = parser.parse_args() | ||
| + | |||
| + | def die(exitcode, | ||
| + | """ | ||
| + | """ | ||
| + | print(*objs, | ||
| + | try: | ||
| + | s | ||
| + | except NameError: | ||
| + | pass | ||
| + | else: # If no exception occured, do : | ||
| + | s.user.logout() | ||
| + | exit(exitcode) | ||
| + | |||
| + | |||
| + | |||
| + | if (not args.ip and not args.name) and (not args.enable and not args.disable): | ||
| + | die(1, "{0}: missing arguments\nTry '{0} -h' for more information." | ||
| + | |||
| + | ip = args.ip | ||
| + | hostname=args.name | ||
| + | |||
| + | def zbx_ip2hostid(s, | ||
| + | hostinterface=s.hostinterface.get(filter={' | ||
| + | if len(hostinterface) == 1: | ||
| + | hostinterface=hostinterface[0] | ||
| + | return(hostinterface[' | ||
| + | else: | ||
| + | die(7, 'IP not found' | ||
| + | |||
| + | def zbx_host2hostid(s, | ||
| + | host=s.host.get(filter={' | ||
| + | if len(host) == 1: | ||
| + | host=host[0] | ||
| + | return(host[' | ||
| + | else: | ||
| + | die(6, 'Host not found' | ||
| + | |||
| + | def zbx_getstatus(hostid): | ||
| + | host=s.host.get(hostids=hostid, | ||
| + | if len(host) == 1: | ||
| + | host=host[0] | ||
| + | return(host[' | ||
| + | else: | ||
| + | die(8, " | ||
| + | |||
| + | |||
| + | s = zabbix_client.ZabbixServerProxy(zabbix_url) | ||
| + | s.user.login(user=zabbix_user, | ||
| + | |||
| + | if ip: | ||
| + | hostid=zbx_ip2hostid(s, | ||
| + | elif hostname: | ||
| + | hostid=zbx_host2hostid(s, | ||
| + | else: | ||
| + | die(2, 'Fatal error, ip or hostname need to be provide !') | ||
| + | |||
| + | |||
| + | # Change Monitor status | ||
| + | if args.disable : | ||
| + | s.host.update({' | ||
| + | if zbx_getstatus(hostid) != NOT_MONITORED: | ||
| + | die(4, 'Fail to change status to NOT_MONITORED' | ||
| + | elif args.enable : | ||
| + | s.host.update({' | ||
| + | if zbx_getstatus(hostid) != MONITORED: | ||
| + | die(4, 'Fail to change status to MONITORED' | ||
| + | else: | ||
| + | die(5, 'Fatal error, autodestruction' | ||
| + | |||
| + | |||
| + | s.user.logout() | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | `zbxunmon.ini` | ||
| + | ~~~ini | ||
| + | [SERVER] | ||
| + | # If HTTP is used instead of HTTPS password will be sent in clear ! | ||
| + | URL=https:// | ||
| + | USER=api | ||
| + | PWD=P@ssw0rd | ||
| + | |||
| + | # For GNU/Linux : Empty value for no proxy. Comment this line for default value (env http_proxy or https_proxy) | ||
| + | HTTP_PROXY= | ||
| + | ~~~ | ||
| + | |||
| + | `requirements.txt` | ||
| + | ~~~python | ||
| + | zabbix-client> | ||
| + | ~~~ | ||
| + | |||
