From ca91adbd53888d8d686e4a4d8640c72a8dbab261 Mon Sep 17 00:00:00 2001 From: Jonathan Wong Date: Tue, 17 Nov 2015 21:54:54 -0800 Subject: [PATCH] Add check for Plex external port down --- plexpy/__init__.py | 2 ++ plexpy/activity_pinger.py | 76 +++++++++++++++++++++++++++------------ plexpy/plextv.py | 19 ++++++++++ 3 files changed, 75 insertions(+), 22 deletions(-) diff --git a/plexpy/__init__.py b/plexpy/__init__.py index 39c9a470..9e3e25c6 100644 --- a/plexpy/__init__.py +++ b/plexpy/__init__.py @@ -287,6 +287,8 @@ def initialize_scheduler(): hours=12, minutes=0, seconds=0) schedule_job(activity_pinger.check_recently_added, 'Check for recently added items', hours=0, minutes=0, seconds=seconds) + schedule_job(activity_pinger.check_server_response, 'Check for server response', + hours=0, minutes=0, seconds=seconds) # If we're not using websockets then fall back to polling if not CONFIG.MONITORING_USE_WEBSOCKET or POLLING_FAILOVER: diff --git a/plexpy/activity_pinger.py b/plexpy/activity_pinger.py index d01b49c1..9f4b6a0e 100644 --- a/plexpy/activity_pinger.py +++ b/plexpy/activity_pinger.py @@ -13,21 +13,21 @@ # You should have received a copy of the GNU General Public License # along with PlexPy. If not, see . -from plexpy import logger, pmsconnect, notification_handler, database, helpers, activity_processor +from plexpy import logger, pmsconnect, plextv, notification_handler, database, helpers, activity_processor import threading import plexpy import time +import urllib2 monitor_lock = threading.Lock() -ping_count = 0 +ext_ping_count = 0 +int_ping_count = 0 def check_active_sessions(ws_request=False): with monitor_lock: - global ping_count - pms_connect = pmsconnect.PmsConnect() session_list = pms_connect.get_current_activity() monitor_db = database.MonitorDatabase() @@ -35,7 +35,6 @@ def check_active_sessions(ws_request=False): # logger.debug(u"PlexPy Monitor :: Checking for active streams.") if session_list: - ping_count = 0 media_container = session_list['sessions'] # Check our temp table for what we must do with the new streams @@ -166,19 +165,6 @@ def check_active_sessions(ws_request=False): monitor_process.write_session(session) else: logger.debug(u"PlexPy Monitor :: Unable to read session list.") - response = pms_connect.get_server_response() - - if not response: - ping_count += 1 - logger.warn(u"PlexPy Monitor :: Unable to get a response from the server, ping attempt %s." % str(ping_count)) - - if ping_count == 3: - # Fire off notifications - threading.Thread(target=notification_handler.notify_timeline, - kwargs=dict(notify_action='down')).start() - else: - ping_count = 0 - def check_recently_added(): @@ -201,7 +187,7 @@ def check_recently_added(): metadata = [metadata_list['metadata']] else: logger.error(u"PlexPy Monitor :: Unable to retrieve metadata for rating_key %s" \ - % str(item['rating_key'])) + % str(item['rating_key'])) else: metadata_list = pms_connect.get_metadata_children_details(item['rating_key']) @@ -209,7 +195,7 @@ def check_recently_added(): metadata = metadata_list['metadata'] else: logger.error(u"PlexPy Monitor :: Unable to retrieve children metadata for rating_key %s" \ - % str(item['rating_key'])) + % str(item['rating_key'])) if metadata: if not plexpy.CONFIG.NOTIFY_RECENTLY_ADDED_GRANDPARENT: @@ -230,8 +216,54 @@ def check_recently_added(): item = metadata_list['metadata'] else: logger.error(u"PlexPy Monitor :: Unable to retrieve grandparent metadata for grandparent_rating_key %s" \ - % str(item['rating_key'])) + % str(item['rating_key'])) # Fire off notifications threading.Thread(target=notification_handler.notify_timeline, - kwargs=dict(timeline_data=item, notify_action='created')).start() \ No newline at end of file + kwargs=dict(timeline_data=item, notify_action='created')).start() + +def check_server_response(): + + with monitor_lock: + plex_tv = plextv.PlexTV() + external_response = plex_tv.get_server_response() + pms_connect = pmsconnect.PmsConnect() + internal_response = pms_connect.get_server_response() + + global ext_ping_count + global int_ping_count + + if not external_response: + ext_ping_count += 1 + logger.warn(u"PlexPy Monitor :: Plex remote access port mapping failed, ping attempt %s." \ + % str(ext_ping_count)) + else: + host = external_response[0]['host'] + port = external_response[0]['port'] + + try: + http_response = urllib2.urlopen('http://' + host + ':' + port) + except urllib2.HTTPError, e: + ext_ping_count = 0 + except urllib2.URLError, e: + ext_ping_count += 1 + logger.warn(u"PlexPy Monitor :: Unable to get an external response from the server, ping attempt %s." \ + % str(ext_ping_count)) + else: + ext_ping_count = 0 + + if not internal_response: + int_ping_count += 1 + logger.warn(u"PlexPy Monitor :: Unable to get an internal response from the server, ping attempt %s." \ + % str(int_ping_count)) + else: + int_ping_count = 0 + + if ext_ping_count == 3: + # Fire off notifications + threading.Thread(target=notification_handler.notify_timeline, + kwargs=dict(notify_action='extdown')).start() + if int_ping_count == 3: + # Fire off notifications + threading.Thread(target=notification_handler.notify_timeline, + kwargs=dict(notify_action='intdown')).start() \ No newline at end of file diff --git a/plexpy/plextv.py b/plexpy/plextv.py index 4610121a..76c55d69 100644 --- a/plexpy/plextv.py +++ b/plexpy/plextv.py @@ -463,3 +463,22 @@ class PlexTV(object): break return server_times + + def get_server_response(self): + response = self.get_plextv_server_list(output_format='xml') + + server_url = [] + + try: + xml_head = response.getElementsByTagName('Server') + except: + return False + + for a in xml_head: + if helpers.get_xml_attr(a, 'machineIdentifier') == plexpy.CONFIG.PMS_IDENTIFIER: + server_url.append({"host": helpers.get_xml_attr(a, 'host'), + "port": helpers.get_xml_attr(a, 'port') + }) + break + + return server_url \ No newline at end of file