-
Notifications
You must be signed in to change notification settings - Fork 7
/
sabnzbd_influxdb_export.py
111 lines (93 loc) · 4.45 KB
/
sabnzbd_influxdb_export.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/python
import time
import argparse # for arg parsing...
import json # for parsing json
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from multiprocessing import Process
from datetime import datetime # for obtaining the curren time and formatting it
from influxdb import InfluxDBClient # via apt-get install python-influxdb
requests.packages.urllib3.disable_warnings(InsecureRequestWarning) # suppress unverified cert warnings
url_format = '{0}://{1}:{2}/sabnzbd/api?apikey={3}&output=json'
def main():
args = parse_args()
url = get_url(args.sabnzbdwebprotocol, args.sabnzbdhost, args.sabnzbdport, args.sabnzbdapikey)
influxdb_client = InfluxDBClient(args.influxdbhost, args.influxdbport, args.influxdbuser, args.influxdbpassword, args.influxdbdatabase)
create_database(influxdb_client, args.influxdbdatabase)
init_exporting(args.interval, url, influxdb_client)
def parse_args():
parser = argparse.ArgumentParser(description='Export SABnzbd data to influxdb')
parser.add_argument('--interval', type=int, required=False, default=5, help='Interval of export in seconds')
parser.add_argument('--sabnzbdwebprotocol', type=str, required=False, default="http", help='SABnzbd web protocol (http)')
parser.add_argument('--sabnzbdhost', type=str, required=False, default="localhost", help='SABnzbd host (test.com))')
parser.add_argument('--sabnzbdport', type=int, required=False, default=8080, help='SABnzbd port')
parser.add_argument('--sabnzbdapikey', type=str, required=True, default="", help='SABnzbd API key')
parser.add_argument('--influxdbhost', type=str, required=False, default="localhost", help='InfluxDB host')
parser.add_argument('--influxdbport', type=int, required=False, default=8086, help='InfluxDB port')
parser.add_argument('--influxdbuser', type=str, required=False, default="", help='InfluxDB user')
parser.add_argument('--influxdbpassword', type=str, required=False, default="", help='InfluxDB password')
parser.add_argument('--influxdbdatabase', type=str, required=False, default="sabnzbd", help='InfluxDB database')
return parser.parse_args()
def qstatus(url,influxdb_client):
try:
data = requests.get('{0}{1}'.format(url, '&mode=queue'), verify=False).json()
if data:
queue = data['queue']
speed = float(queue['kbpersec'])
total_mb_left = float(queue['mbleft']) # mbleft?
total_jobs = float(queue['noofslots'])
status = queue['status']
json_body = [
{
"measurement": "qstatus",
"time": datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ'),
"fields" : {
"speed": speed,
"total_mb_left": total_mb_left,
"total_jobs": total_jobs,
"status": status
}
}]
influxdb_client.write_points(json_body)
except Exception as e:
print str(e)
pass
def server_stats(url,influxdb_client):
try:
data = requests.get('{0}{1}'.format(url, '&mode=server_stats'), verify=False).json()
if data:
total = long(data['total'])
total_month = long(data['month'])
total_week = long(data['week'])
total_day = long(data['day'])
json_body = [
{
"measurement": "server_stats",
"time": datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ'),
"fields" : {
"total": total,
"total_month": total_month,
"total_week": total_week,
"total_day": total_day
}
}]
influxdb_client.write_points(json_body)
except Exception as e:
print str(e)
pass
def create_database(influxdb_client, database):
try:
influxdb_client.query('CREATE DATABASE {0}'.format(database))
except Exception:
pass
def init_exporting(interval, url, influxdb_client):
while True:
queuestatus = Process(target=qstatus, args=(url,influxdb_client,))
queuestatus.start()
serverstats = Process(target=server_stats, args=(url,influxdb_client,))
serverstats.start()
time.sleep(interval)
def get_url(protocol,host,port,apikey):
return url_format.format(protocol,host,port,apikey)
if __name__ == '__main__':
main()