blacktwin / JBOPS

Just a Bunch Of Plex Scripts
1.67k stars 307 forks source link

kill_stream.py notification not passing session_id #174

Closed SlothCroissant closed 5 years ago

SlothCroissant commented 5 years ago

When testing kill_stream.py using the following command, the notification passed to Tautulli (it's a Pushover notification agent) has the body "Killed session ID 'None'. Reason: Killing all this users' streams".

python /config/scripts/killstream.py --jbop allStreams --userId 144976 --notify 7 --killMessage 'Killing all this users' streams'

The streams themselves are killed, but the session_id is "None" for every single one when the notification goes out.

Pulled the latest script today, running Tautulli VERSION V2.1.32 (though I'm running manually from Python in my test above).

Thoughts?

For more data, here's a quick debug output when run from Tautulli as well (acting in this case as a "if you have multiple streams, kill them all" scenario) The net result is exactly the same - session_id is set to "None" in the notification:

2019-07-06 09:45:15 - INFO :: Thread-2 : Tautulli NotificationHandler :: Preparing notification for notifier_id 6.
2019-07-06 09:45:15 - DEBUG :: Thread-2 : Tautulli Notifiers :: Trying to run notify script, action: concurrent, arguments: [u'--jbop', u'allStreams', u'--userId', '144976', u'--notify', u'7', u'--killMessage', u'Stream killed due to multiple concurrent streams', u'--debug']
2019-07-06 09:45:15 - DEBUG :: Thread-2 : Tautulli Notifiers :: Full script is: ['python', u'/config/scripts/killstream.py', '--jbop', 'allStreams', '--userId', '144976', '--notify', '7', '--killMessage', 'Stream killed due to multiple concurrent streams', '--debug']
2019-07-06 09:45:15 - DEBUG :: Thread-2 : Tautulli Notifiers :: Executing script in a new thread.
2019-07-06 09:45:16 - INFO :: CP Server Thread-8 : Tautulli Config :: Writing configuration to file
2019-07-06 09:45:16 - INFO :: CP Server Thread-8 : Tautulli Pmsconnect :: Terminating session 827 (session_id 4FA4C271-07EE-4FBD-9C51-EA9572AB01D6).
2019-07-06 09:45:16 - INFO :: CP Server Thread-8 : Tautulli NotificationHandler :: Preparing notification for notifier_id 7.
2019-07-06 09:45:16 - INFO :: CP Server Thread-8 : Tautulli Notifiers :: Sending Pushover notification...
2019-07-06 09:45:16 - INFO :: CP Server Thread-8 : Tautulli Notifiers :: Pushover notification sent.
2019-07-06 09:45:17 - INFO :: CP Server Thread-8 : Tautulli Config :: Writing configuration to file
2019-07-06 09:45:17 - INFO :: CP Server Thread-8 : Tautulli Pmsconnect :: Terminating session 828 (session_id 7E04E9A0-29C3-4CDF-B239-224539161E41).
2019-07-06 09:45:17 - INFO :: CP Server Thread-8 : Tautulli NotificationHandler :: Preparing notification for notifier_id 7.
2019-07-06 09:45:17 - INFO :: CP Server Thread-8 : Tautulli Notifiers :: Sending Pushover notification...
2019-07-06 09:45:17 - INFO :: CP Server Thread-8 : Tautulli Notifiers :: Pushover notification sent.
2019-07-06 09:45:17 - DEBUG :: Thread-554 : Tautulli Notifiers :: Subprocess returned with status code 0.
2019-07-06 09:45:17 - DEBUG :: Thread-554 : Tautulli Notifiers :: Script returned:
  Tautulli URL - http://localhost:8181
  Tautulli Public URL - https://tautulli.url.com/
  Verify SSL - False
  Tautulli API key - xxxxxxxxxxxxxxxxxxxxxxxxxxxx884d
  Successfully called Tautulli API cmd 'get_activity'
  Successfully called Tautulli API cmd 'terminate_session'
  Successfully called Tautulli API cmd 'notify'
  Successfully called Tautulli API cmd 'terminate_session'
  Successfully called Tautulli API cmd 'notify'
blacktwin commented 5 years ago

Gotcha. Relevant area it the code.

# notify(opts, kill_message, 'All Streams', a_stream, tautulli_server)
def notify(all_opts, message, kill_type=None, stream=None, tautulli=None):
    """Decides which notifier type to use"""
    if all_opts.notify and all_opts.richMessage:
        rich_notify(all_opts.notify, all_opts.richMessage, all_opts.richColor, kill_type,
                    all_opts.serverName, all_opts.plexUrl, all_opts.posterUrl, message, stream, tautulli)
    elif all_opts.notify:
        basic_notify(all_opts.notify, all_opts.sessionId, all_opts.username, message, stream, tautulli)

I've commented out what's getting passed to the notify function. Notify function could have an additional if to check allStreams and adjust the basic_notify or rich_notify accordingly.

Can you add the --sessionId {session_id} argument to your agent and test again?

SlothCroissant commented 5 years ago

Worked like a charm! Thanks!

blacktwin commented 5 years ago

So adding that argument allowed for each session_id to be displayed? What worked?

SlothCroissant commented 5 years ago

Sorry, it half worked -

Adding the session ID variable to the arguments as you noted did in fact mean that a session ID showed in the notification for each killed stream. However, it passed the same session ID in the notification for both streams it killed, instead of the two unique session IDs that were killed.

Hope that makes sense!

blacktwin commented 5 years ago

It does make sense as that's what should happen. Your first observation is also what should happen. It just wasn't what you maybe expected.

The base message can be found in the script and you can change it how ever you like.

BODY_TEXT = "Killed session ID '{id}'. Reason: {message}"

If you want to customize the message that is sent out that will be up to you. If you feel like your edit to the script would be popular for others, please share it via PR. As I see it, there isn't anything to fix.