forked from Drewster727/sabnzbd-influxdb-export
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sabnzbd_influxdb_export.py
219 lines (187 loc) · 7.47 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/python
import argparse # for arg parsing...
import logging
import os
import time
from datetime import datetime # for obtaining and formattying time
from multiprocessing import Process
from influxdb import InfluxDBClient # via apt-get install python-influxdb
import pytimeparse
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
# suppress unverified cert warnings
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
url_format = '{0}://{1}:{2}/sabnzbd/api?apikey={3}&output=json'
FORMAT = '%(asctime)s %(levelname)-8s: %(message)s'
logging.basicConfig(format=FORMAT, level=logging.INFO)
log = logging.getLogger()
def main():
args = parse_args()
if args.verbose or os.environ.get('DEBUG'):
log.setLevel(logging.DEBUG)
elif args.quiet:
log.setLevel(logging.WARNING)
url = get_url(args.sabnzbdwebprotocol,
args.sabnzbdhost,
args.sabnzbdport,
args.sabnzbdapikey)
log.debug("Using sabnzbd url: %s", url)
influxdb_client = InfluxDBClient(args.influxdbhost,
args.influxdbport,
args.influxdbuser,
args.influxdbpassword,
args.influxdbdatabase)
log.debug("Using influxdb: %s:%s",
args.influxdbhost, args.influxdbport)
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=os.environ.get('INTERVAL', 5),
help='Interval of export in seconds')
parser.add_argument(
'--sabnzbdwebprotocol', type=str, required=False,
default=os.environ.get('SABNZBD_PROTOCOL', 'http'),
help='SABnzbd web protocol (http)')
parser.add_argument(
'--sabnzbdhost', type=str, required=False,
default=os.environ.get('SABNZBD_HOST', 'localhost'),
help='SABnzbd host (e.g. test.com))')
parser.add_argument(
'--sabnzbdport', type=int, required=False,
default=os.environ.get('SABNZBD_PORT', 8080),
help='SABnzbd port')
parser.add_argument(
'--sabnzbdapikey', type=str, required=False,
default=os.environ.get('SABNZBD_API_KEY', ''),
help='SABnzbd API key')
parser.add_argument(
'--influxdbhost', type=str, required=False,
default=os.environ.get('INFLUXDB_HOST', 'localhost'),
help='InfluxDB host')
parser.add_argument(
'--influxdbport', type=int, required=False,
default=os.environ.get('INFLUXDB_PORT', 8086),
help='InfluxDB port')
parser.add_argument(
'--influxdbuser', type=str, required=False,
default=os.environ.get('INFLUXDB_USER', ''),
help='InfluxDB user')
parser.add_argument(
'--influxdbpassword', type=str, required=False,
default=os.environ.get('INFLUXDB_PASSWORD', ''),
help='InfluxDB password')
parser.add_argument(
'--influxdbdatabase', type=str, required=False,
default=os.environ.get('INFLUXDB_DATABASE', 'sabnzbd'),
help='InfluxDB database')
loggroup = parser.add_mutually_exclusive_group()
loggroup.add_argument(
'-v', '--verbose', action='store_true',
help='Enable verbose (debug) logging.')
loggroup.add_argument(
'-q', '--quiet', action='store_true',
help='Only show log messages at warning level and higher.')
return parser.parse_args()
def qstatus(url, influxdb_client):
log.debug("Getting queue status")
try:
data = requests.get(
'{0}{1}'.format(url, '&mode=queue'), verify=False).json()
except Exception:
log.exception("Error getting queue status from sabnzbd.")
return
if not data:
log.debug("No data returned.")
return
log.debug("Data from sabnzbd: %s", data)
queue = data['queue']
try:
speedlimit_abs = float(queue["speedlimit_abs"])
except ValueError:
speedlimit_abs = 0.0
seconds_left = pytimeparse.parse(queue.get("timeleft"))
json_body = {
"measurement": "qstatus",
"time": datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ'),
"fields": {
"speed": float(queue["kbpersec"]),
"total_mb_left": float(queue["mbleft"]),
"speedlimit": float(queue["speedlimit"]),
"speedlimit_abs": speedlimit_abs,
"total_jobs": float(queue["noofslots"]),
"status": queue.get("status"),
"diskspace1": float(queue.get("diskspace1")),
"diskspace2": float(queue.get("diskspace2")),
"diskspacetotal1": float(queue.get("diskspacetotal1")),
"diskspacetotal2": float(queue.get("diskspacetotal2")),
"diskspace1_norm": queue.get("diskspace1_norm"),
"diskspace2_norm": queue.get("diskspace2_norm"),
"loadavg_1m": float(queue.get("loadavg").split('|')[0]),
"loadavg_5m": float(queue.get("loadavg").split('|')[1]),
"loadavg_15m": float(queue.get("loadavg").split('|')[2]),
"have_warnings": queue.get("have_warnings"),
}
}
if not queue.get('paused'):
json_body["fields"].update({
"timeleft": queue.get("timeleft"),
"seconds_left": seconds_left,
"eta": queue.get("eta"),
"eta_timestamp": 1000 * (seconds_left + time.time())
})
try:
influxdb_client.write_points([json_body])
except Exception:
log.exception("Error posting queue status to InfluxDB")
def server_stats(url, influxdb_client):
log.debug("Getting server status.")
try:
data = requests.get(
'{0}{1}'.format(url, '&mode=server_stats'), verify=False).json()
except Exception:
log.exception("Error getting server status from sabnzbd.")
if not data:
log.debug("No data received for server status.")
return
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
}
}]
try:
influxdb_client.write_points(json_body)
except Exception:
log.exception("Error posting server status to InfluxDB.")
def create_database(influxdb_client, database):
log.debug("Creating influxdb database %s", database)
try:
influxdb_client.query('CREATE DATABASE {0}'.format(database))
except Exception:
log.exception("Error creating database in InfluxDB")
def init_exporting(interval, url, influxdb_client):
log.info("Starting monitoring with interval of %ss", interval)
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()