Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rebuilt Splunk using custom library #40123

Merged
merged 9 commits into from Sep 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveragerc
Expand Up @@ -806,6 +806,7 @@ omit =
homeassistant/components/spc/*
homeassistant/components/speedtestdotnet/*
homeassistant/components/spider/*
homeassistant/components/splunk/*
homeassistant/components/spotcrime/sensor.py
homeassistant/components/spotify/__init__.py
homeassistant/components/spotify/media_player.py
Expand Down
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Expand Up @@ -403,6 +403,7 @@ homeassistant/components/songpal/* @rytilahti @shenxn
homeassistant/components/spaceapi/* @fabaff
homeassistant/components/speedtestdotnet/* @rohankapoorcom @engrbm87
homeassistant/components/spider/* @peternijssen
homeassistant/components/splunk/* @Bre77
homeassistant/components/spotify/* @frenck
homeassistant/components/sql/* @dgomes
homeassistant/components/squeezebox/* @rajlaud
Expand Down
91 changes: 51 additions & 40 deletions homeassistant/components/splunk/__init__.py
@@ -1,9 +1,10 @@
"""Support to send data to an Splunk instance."""
"""Support to send data to a Splunk instance."""
import json
import logging
import time

from aiohttp.hdrs import AUTHORIZATION
import requests
from aiohttp import ClientConnectionError, ClientResponseError
from hass_splunk import SplunkPayloadError, hass_splunk
import voluptuous as vol

from homeassistant.const import (
Expand All @@ -16,14 +17,15 @@
EVENT_STATE_CHANGED,
)
from homeassistant.helpers import state as state_helper
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entityfilter import FILTER_SCHEMA
from homeassistant.helpers.json import JSONEncoder

_LOGGER = logging.getLogger(__name__)

CONF_FILTER = "filter"
DOMAIN = "splunk"
CONF_FILTER = "filter"

DEFAULT_HOST = "localhost"
DEFAULT_PORT = 8088
Expand All @@ -48,23 +50,7 @@
)


def post_request(event_collector, body, headers, verify_ssl):
"""Post request to Splunk."""
try:
payload = {"host": event_collector, "event": body}
requests.post(
event_collector,
data=json.dumps(payload, cls=JSONEncoder),
headers=headers,
timeout=10,
verify=verify_ssl,
)

except requests.exceptions.RequestException as error:
_LOGGER.exception("Error saving event to Splunk: %s", error)


def setup(hass, config):
async def async_setup(hass, config):
"""Set up the Splunk component."""
conf = config[DOMAIN]
host = conf.get(CONF_HOST)
Expand All @@ -75,18 +61,33 @@ def setup(hass, config):
name = conf.get(CONF_NAME)
entity_filter = conf[CONF_FILTER]

if use_ssl:
uri_scheme = "https://"
else:
uri_scheme = "http://"

event_collector = f"{uri_scheme}{host}:{port}/services/collector/event"
headers = {AUTHORIZATION: f"Splunk {token}"}

def splunk_event_listener(event):
event_collector = hass_splunk(
session=async_get_clientsession(hass),
host=host,
port=port,
token=token,
use_ssl=use_ssl,
verify_ssl=verify_ssl,
)

if not await event_collector.check(connectivity=False, token=True, busy=False):
return False

payload = {
"time": time.time(),
"host": name,
"event": {
"domain": DOMAIN,
"meta": "Splunk integration has started",
},
}

await event_collector.queue(json.dumps(payload, cls=JSONEncoder), send=False)

async def splunk_event_listener(event):
"""Listen for new messages on the bus and sends them to Splunk."""
state = event.data.get("new_state")

state = event.data.get("new_state")
if state is None or not entity_filter(state.entity_id):
return

Expand All @@ -95,19 +96,29 @@ def splunk_event_listener(event):
except ValueError:
_state = state.state

json_body = [
{
payload = {
"time": event.time_fired.timestamp(),
"host": name,
"event": {
"domain": state.domain,
"entity_id": state.object_id,
"attributes": dict(state.attributes),
"time": str(event.time_fired),
"value": _state,
"host": name,
}
]

post_request(event_collector, json_body, headers, verify_ssl)
},
}

hass.bus.listen(EVENT_STATE_CHANGED, splunk_event_listener)
try:
await event_collector.queue(json.dumps(payload, cls=JSONEncoder), send=True)
except SplunkPayloadError as err:
if err.status == 401:
_LOGGER.error(err)
else:
_LOGGER.warning(err)
except ClientConnectionError as err:
_LOGGER.warning(err)
except ClientResponseError as err:
_LOGGER.error(err.message)

hass.bus.async_listen(EVENT_STATE_CHANGED, splunk_event_listener)

return True
9 changes: 7 additions & 2 deletions homeassistant/components/splunk/manifest.json
Expand Up @@ -2,5 +2,10 @@
"domain": "splunk",
"name": "Splunk",
"documentation": "https://www.home-assistant.io/integrations/splunk",
"codeowners": []
}
"requirements": [
"hass_splunk==0.1.0"
],
"codeowners": [
"@Bre77"
]
}
3 changes: 3 additions & 0 deletions requirements_all.txt
Expand Up @@ -722,6 +722,9 @@ hangups==0.4.10
# homeassistant.components.cloud
hass-nabucasa==0.37.0

# homeassistant.components.splunk
hass_splunk==0.1.0

# homeassistant.components.jewish_calendar
hdate==0.9.5

Expand Down
1 change: 0 additions & 1 deletion tests/components/splunk/__init__.py

This file was deleted.

182 changes: 0 additions & 182 deletions tests/components/splunk/test_init.py

This file was deleted.