Last active
January 7, 2022 20:16
-
-
Save JonnyWong16/f561f06a6266db66dad9 to your computer and use it in GitHub Desktop.
Send a Tautulli notification when disk usage exceeds a threshold.
This file contains 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
#!/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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I get the following errors when attempting to use the "test code" function in Notification Agents > Scripts. Any ideas?