Created
December 12, 2018 22:24
-
-
Save dirtycajunrice/c70249023bbbae641a3936b927f786e5 to your computer and use it in GitHub Desktop.
Cachet status push for streams and ms response time
This file contains hidden or 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
import time | |
from json import dumps | |
from requests import Session | |
PLEX_URL = 'http://plex.asdf.asdf:32400' | |
PLEX_LATENCY_METRIC_ID = 2 | |
CACHET_URL = 'https://status.asdf.asdf' | |
CACHET_API_KEY = 'asdfasdfasdf' | |
TAUTULLI_URL = 'https://tautulli.asdf.asdf' | |
TAUTULLI_API_KEY = 'asdfasdfasdfasdf' | |
TAUTULLI_STREAMS_METRIC_ID = 1 | |
class CachetMetrics(object): | |
def __init__(self): | |
self.t_session = Session() | |
self.t_session.params = {'apikey': TAUTULLI_API_KEY, 'cmd': 'get_activity'} | |
self.tautulli_stream_count = 0 | |
self.p_session = Session() | |
self.c_session = Session() | |
self.c_session.headers = {'Content-Type': 'application/json', 'X-Cachet-Token': CACHET_API_KEY} | |
def get_tautulli_stream_count(self): | |
endpoint = '/api/v2' | |
# Get stream count from tautulli | |
count = self.t_session.get(TAUTULLI_URL + endpoint).json()['response']['data']['stream_count'] | |
self.post(TAUTULLI_STREAMS_METRIC_ID, count) | |
def get_plex_response_time(self): | |
endpoint = '/identity' | |
# Get plex response time | |
get = self.p_session.get(PLEX_URL + endpoint) | |
response_time = get.elapsed.total_seconds() | |
response_ms = (response_time * 1000) | |
if get.status_code != 200: | |
response_ms = 0 | |
self.post(PLEX_LATENCY_METRIC_ID, response_ms) | |
def post(self, metric, value): | |
v = value | |
m = metric | |
timestamp = time.time() | |
rounded_time = int(timestamp // 60 * 60) | |
endpoint = '/api/v1/metrics/{metric}/points' | |
# Post stream info to cachet | |
data = { | |
"value": v, | |
"timestamp": rounded_time | |
} | |
full_url = CACHET_URL + endpoint.format(metric=m) | |
post = self.c_session.post(full_url, data=dumps(data)).content | |
print('Data sent: {}'.format(post)) | |
if __name__ == "__main__": | |
CACHET = CachetMetrics() | |
CACHET.get_tautulli_stream_count() | |
CACHET.get_plex_response_time() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment