2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Zabbix."""
|
2017-01-17 08:41:37 +00:00
|
|
|
import logging
|
|
|
|
from urllib.parse import urljoin
|
|
|
|
|
2019-11-29 01:07:52 +00:00
|
|
|
from pyzabbix import ZabbixAPI, ZabbixAPIException
|
2017-01-17 08:41:37 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOST,
|
|
|
|
CONF_PASSWORD,
|
2019-11-29 01:07:52 +00:00
|
|
|
CONF_PATH,
|
|
|
|
CONF_SSL,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_USERNAME,
|
|
|
|
)
|
2017-01-17 08:41:37 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEFAULT_SSL = False
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_PATH = "zabbix"
|
|
|
|
DOMAIN = "zabbix"
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
DOMAIN: vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Optional(CONF_PASSWORD): cv.string,
|
|
|
|
vol.Optional(CONF_PATH, default=DEFAULT_PATH): cv.string,
|
|
|
|
vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
|
|
|
|
vol.Optional(CONF_USERNAME): cv.string,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2017-01-17 08:41:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setup(hass, config):
|
|
|
|
"""Set up the Zabbix component."""
|
|
|
|
|
|
|
|
conf = config[DOMAIN]
|
|
|
|
if conf[CONF_SSL]:
|
2019-07-31 19:25:30 +00:00
|
|
|
schema = "https"
|
2017-01-17 08:41:37 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
schema = "http"
|
2017-01-17 08:41:37 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
url = urljoin("{}://{}".format(schema, conf[CONF_HOST]), conf[CONF_PATH])
|
2017-01-17 08:41:37 +00:00
|
|
|
username = conf.get(CONF_USERNAME, None)
|
|
|
|
password = conf.get(CONF_PASSWORD, None)
|
|
|
|
|
|
|
|
zapi = ZabbixAPI(url)
|
|
|
|
try:
|
|
|
|
zapi.login(username, password)
|
|
|
|
_LOGGER.info("Connected to Zabbix API Version %s", zapi.api_version())
|
2018-03-08 23:25:10 +00:00
|
|
|
except ZabbixAPIException as login_exception:
|
|
|
|
_LOGGER.error("Unable to login to the Zabbix API: %s", login_exception)
|
2017-01-17 08:41:37 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
hass.data[DOMAIN] = zapi
|
|
|
|
return True
|