{{tag>Brouillon Zabbix Python Dev Supervision}}
= Zabbix script python monitor unmonitor autoadd host
Ce script permet :
* l'ajout automatique d'un nouveau hôte à superviser
* La désactivation automatique de la supervision d'un hôte
Il prend comme paramètre le nom de l'hôte ou l'adresse IP.
Il servait au "cloud", dans un contexte de création automatique de VM et destruction automatique après traitement.
''zbxunmon.py''
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# License: GNU GPL
""" Switch Zabbix status to 'Monitored' or 'Not monitored'
zbxunmon.ini
[SERVER]
URL=https://acme.fr/zabbix
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('.py', '')
ficconf=ficconf + '.ini' # Work even if this script's name isn't ended by ".py"
config = configparser.ConfigParser()
config.read(ficconf)
zbxconf = config['SERVER']
zabbix_url = config['SERVER']['URL']
zabbix_user = zbxconf.get('USER')
zabbix_pwd = zbxconf.get('PWD')
try:
environ['http_proxy']=environ['https_proxy']=zbxconf.get('HTTP_PROXY')
except TypeError:
pass
# Zabbix 'status' code
MONITORED='0'
NOT_MONITORED='1'
parser = argparse.ArgumentParser()
parser.add_argument('-e', '--enable', action='store_true', help='Enable')
parser.add_argument('-d', '--disable', action='store_true', help='Disable')
parser.add_argument('-i', '--ip', help='IP Address')
parser.add_argument('-n', '--name', help='Hostname')
args = parser.parse_args()
def die(exitcode, *objs):
""" print on STDERR
"""
print(*objs, file=stderr)
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.".format(argv[0]))
ip = args.ip
hostname=args.name
def zbx_ip2hostid(s, ip):
hostinterface=s.hostinterface.get(filter={'ip':ip}, output=['hostid'])
if len(hostinterface) == 1:
hostinterface=hostinterface[0]
return(hostinterface['hostid'])
else:
die(7, 'IP not found')
def zbx_host2hostid(s, host):
host=s.host.get(filter={'host':host}, output=['hostid'])
if len(host) == 1:
host=host[0]
return(host['hostid'])
else:
die(6, 'Host not found')
def zbx_getstatus(hostid):
host=s.host.get(hostids=hostid, output=['status'])
if len(host) == 1:
host=host[0]
return(host['status'])
else:
die(8, "Can't get status")
s = zabbix_client.ZabbixServerProxy(zabbix_url)
s.user.login(user=zabbix_user, password=zabbix_pwd)
if ip:
hostid=zbx_ip2hostid(s, ip)
elif hostname:
hostid=zbx_host2hostid(s, hostname)
else:
die(2, 'Fatal error, ip or hostname need to be provide !')
# Change Monitor status
if args.disable :
s.host.update({'hostid': hostid, 'status': NOT_MONITORED})
if zbx_getstatus(hostid) != NOT_MONITORED:
die(4, 'Fail to change status to NOT_MONITORED')
elif args.enable :
s.host.update({'hostid': hostid, 'status': MONITORED})
if zbx_getstatus(hostid) != MONITORED:
die(4, 'Fail to change status to MONITORED')
else:
die(5, 'Fatal error, autodestruction')
s.user.logout()
''zbxunmon.ini''
[SERVER]
# If HTTP is used instead of HTTPS password will be sent in clear !
URL=https://acme.fr/zabbix
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''
zabbix-client>=0.1.1