Skip to content

Instantly share code, notes, and snippets.

@JonnyWong16
Last active January 7, 2022 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonnyWong16/f561f06a6266db66dad9 to your computer and use it in GitHub Desktop.
Save JonnyWong16/f561f06a6266db66dad9 to your computer and use it in GitHub Desktop.
Send a Tautulli notification when disk usage exceeds a threshold.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Description: Send a Tautulli notification when disk usage exceeds a threshold.
Author: /u/SwiftPanda16
Requires: psutil, requests
Tautulli script trigger:
* Notify on recently added
Tautulli script arguments:
* Recently Added:
--disk "D:/" --threshold 95.0 --notifier_id 2 --subject "Tautulli" --body "Disk {disk} usage exceeded threshold {threshold}%%."
Usage:
python notify_disk_usage.py --disk "D:/" --threshold 95.0 --notifier_id 2 --subject "Tautulli" --body "Disk {disk} usage exceeded threshold {threshold}%%."
'''
import argparse
import os
import psutil
import requests
TAUTULLI_URL = ''
TAUTULLI_APIKEY = ''
# Environmental Variables
TAUTULLI_URL = os.getenv('TAUTULLI_URL', TAUTULLI_URL)
TAUTULLI_APIKEY = os.getenv('TAUTULLI_APIKEY', TAUTULLI_APIKEY)
## CODE BELOW ##
def disk_exceed_threshold(disk, threshold):
disk_usage = psutil.disk_usage(disk)
return disk_usage.percent >= THRESHOLD
def notify_tautulli(notifier_id, subject, body):
params = {
'apikey': TAUTULLI_APIKEY,
'cmd': 'notify',
'notifier_id': notifier_id,
'subject': subject,
'body': body
}
requests.post(TAUTULLI_URL.rstrip('/') + 'api/v2', params=params)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--disk', required=True)
parser.add_argument('--threshold', type=float, required=True)
parser.add_argument('--notifier_id', type=int, required=True)
parser.add_argument('--subject')
parser.add_argument('--body')
opts = parser.parse_args()
if disk_exceed_threshold(opts.disk, opts.threshold):
subject = opts.subject.format(disk=opts.disk, threshold=opts.threshold)
body = opts.body.format(disk=opts.disk, threshold=opts.threshold)
notify_tautulli(opts.notifier_id, subject, body)
@timespaced
Copy link

I get the following errors when attempting to use the "test code" function in Notification Agents > Scripts. Any ideas?

PlexPy Notifiers :: Script error:
    Traceback (most recent call last):
        File "/opt/plexpy/scripts/notify_disk_usage.py", line 36, in
            r = requests.post(url)
        File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 112, in post
            return request('post', url, data=data, json=json, **kwargs)
        File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 58, in request
            return session.request(method=method, url=url, **kwargs)
        File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 494, in request
            prep = self.prepare_request(req)
        File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 437, in prepare_request
            hooks=merge_hooks(request.hooks, self.hooks),
        File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 305, in prepare
            self.prepare_url(url, params)
        File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 373, in prepare_url
            raise InvalidURL(*e.args)
    requests.exceptions.InvalidURL: Failed to parse: localhost:8182api 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment