initial
This commit is contained in:
commit
7918ee8d8e
1 changed files with 108 additions and 0 deletions
108
rclone_tele.py
Normal file
108
rclone_tele.py
Normal file
|
|
@ -0,0 +1,108 @@
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import requests
|
||||||
|
import datetime
|
||||||
|
import math
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
logfiledir = '/home/hubobel/'
|
||||||
|
logfilepath = logfiledir + 'rclone.log'
|
||||||
|
#logfilepath = 'rclone.txt'
|
||||||
|
host = 'vhost'
|
||||||
|
source = 'ultracc:/downloads/qbittorrent/'
|
||||||
|
target = '/mnt/extern/usenet/torrent/'
|
||||||
|
result_msg = []
|
||||||
|
|
||||||
|
start = datetime.datetime.now()
|
||||||
|
|
||||||
|
telegram_chat_id = "322673713"
|
||||||
|
telegram_token = "6828697809:AAHsdnom53SQGaYdgQBUrxrD2cMkwurupKI"
|
||||||
|
|
||||||
|
def parse_log(logfile, source, target):
|
||||||
|
|
||||||
|
with open(logfile) as f:
|
||||||
|
data = [json.loads(line.rstrip()) for line in f if line[0] == "{"]
|
||||||
|
stats = []
|
||||||
|
operations = []
|
||||||
|
|
||||||
|
for obj in data:
|
||||||
|
|
||||||
|
if 'accounting/stats' in obj['source']:
|
||||||
|
stats.append(obj)
|
||||||
|
elif 'operations/operations' in obj['source']:
|
||||||
|
operations.append(obj)
|
||||||
|
for obj in stats:
|
||||||
|
obj['stats']['speed'] = float(obj['stats']['speed'])
|
||||||
|
json_body = [
|
||||||
|
{
|
||||||
|
"measurement": "stats",
|
||||||
|
"tags": {
|
||||||
|
"host": host,
|
||||||
|
"level": obj['level'],
|
||||||
|
"log_entry_source": obj['source'],
|
||||||
|
"source": source,
|
||||||
|
"target": target
|
||||||
|
},
|
||||||
|
"time": obj['time'],
|
||||||
|
"fields": obj['stats']
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
for obj in operations:
|
||||||
|
json_body = [
|
||||||
|
{
|
||||||
|
"measurement": "operations",
|
||||||
|
"tags": {
|
||||||
|
"host": host,
|
||||||
|
"level": obj['level'],
|
||||||
|
"log_entry_source": obj['source'],
|
||||||
|
"objType": obj['objectType'],
|
||||||
|
"msg": obj['msg'],
|
||||||
|
"source": source,
|
||||||
|
"target": target
|
||||||
|
},
|
||||||
|
"time": obj['time'],
|
||||||
|
"fields": {
|
||||||
|
"obj": obj['object']
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
return stats[0]['stats']['totalChecks'], stats[0]['stats']['totalTransfers'], stats[0]['stats']['totalBytes']
|
||||||
|
|
||||||
|
def convert_size(size_bytes):
|
||||||
|
if size_bytes == 0:
|
||||||
|
return "0B"
|
||||||
|
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
|
||||||
|
i = int(math.floor(math.log(size_bytes, 1024)))
|
||||||
|
p = math.pow(1024, i)
|
||||||
|
s = round(size_bytes / p, 2)
|
||||||
|
return "%s %s" % (s, size_name[i])
|
||||||
|
|
||||||
|
def notify_telegram(text):
|
||||||
|
params = {"parse_mode": "HTML", "chat_id": telegram_chat_id, "text": text}
|
||||||
|
url = f"https://api.telegram.org/bot{telegram_token}/sendMessage"
|
||||||
|
requests.post(url, params=params)
|
||||||
|
|
||||||
|
basecmd = ('rclone ' + str('copy') + ' --log-file=' + str(logfilepath) +
|
||||||
|
' --use-json-log ' + '--log-level=INFO --stats=90m --fast-list').split()
|
||||||
|
|
||||||
|
basecmd.append(str(source))
|
||||||
|
basecmd.append(str(target))
|
||||||
|
|
||||||
|
response = subprocess.run(basecmd)
|
||||||
|
|
||||||
|
checks, transfers, bytes = parse_log(logfilepath, source, target)
|
||||||
|
statistic = "\n ⤷ Checks: " + str(checks) + ", Transfers: " + str(transfers) + ", Bytes: " + \
|
||||||
|
str(convert_size(bytes))
|
||||||
|
icon = "✅ " if transfers > 0 else "🆗 "
|
||||||
|
result_msg.append(icon + source + " -> " + target + statistic)
|
||||||
|
|
||||||
|
end = datetime.datetime.now()
|
||||||
|
duration = end - start
|
||||||
|
|
||||||
|
os.remove(logfilepath)
|
||||||
|
|
||||||
|
notify_telegram(("<b>Backup Statistics:</b>\n\n%s\n\n<i>Start Time</i>: %s\n<i>End Time</i>: %s\n<i>Duration</i>: %s minutes" %
|
||||||
|
("\n\n".join(result_msg), start.strftime("%Y-%m-%d %H:%M:%S"), end.strftime("%Y-%m-%d %H:%M:%S"),
|
||||||
|
divmod(duration.total_seconds(), 60)[0])))
|
||||||
Loading…
Add table
Add a link
Reference in a new issue