add vnstat

This commit is contained in:
BennyThink 2021-08-20 23:48:56 +08:00
parent 46856ba374
commit f2aabf6a1d
No known key found for this signature in database
GPG key ID: 6CD0DBDA5235D481
2 changed files with 19 additions and 5 deletions

View file

@ -6,12 +6,14 @@ RUN pip3 install --user -r /tmp/requirements.txt && rm /tmp/requirements.txt
FROM python:alpine FROM python:alpine
RUN apk update && apk add --no-cache ffmpeg WORKDIR /ytdlbot
ENV TZ=Asia/Shanghai
RUN apk update && apk add --no-cache ffmpeg vnstat
COPY --from=builder /root/.local /usr/local COPY --from=builder /root/.local /usr/local
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY . /ytdlbot COPY . /ytdlbot
RUN echo "/usr/sbin/vnstatd -d;/usr/local/bin/python ytdl.py"> /ytdlbot/start.sh
WORKDIR /ytdlbot CMD ["sh", "start.sh"]
ENV TZ=Asia/Shanghai
CMD ["python", "ytdl.py"]

View file

@ -13,6 +13,7 @@ import math
import os import os
import re import re
import sqlite3 import sqlite3
import subprocess
import tempfile import tempfile
import time import time
from io import BytesIO from io import BytesIO
@ -39,6 +40,7 @@ class Redis:
quota_banner = "=" * 20 + "Quota" + "=" * 20 quota_banner = "=" * 20 + "Quota" + "=" * 20
metrics_banner = "=" * 20 + "Metrics" + "=" * 20 metrics_banner = "=" * 20 + "Metrics" + "=" * 20
usage_banner = "=" * 20 + "Usage" + "=" * 20 usage_banner = "=" * 20 + "Usage" + "=" * 20
vnstat_banner = "=" * 20 + "vnstat" + "=" * 20
self.final_text = f""" self.final_text = f"""
{db_banner} {db_banner}
%s %s
@ -53,6 +55,10 @@ class Redis:
{usage_banner} {usage_banner}
%s
{vnstat_banner}
%s %s
""" """
@ -108,7 +114,13 @@ class Redis:
fd.sort(key=lambda x: int(x[1])) fd.sort(key=lambda x: int(x[1]))
quota_text = self.generate_table(["UserID", "bytes", "human readable", "refresh time"], fd) quota_text = self.generate_table(["UserID", "bytes", "human readable", "refresh time"], fd)
return self.final_text % (db_text, quota_text, metrics_text, usage_text) # vnstat
if os.uname().sysname == "Darwin":
cmd = "/usr/local/bin/vnstat -i en0".split()
else:
cmd = "/usr/bin/vnstat -i eth0".split()
vnstat_text = subprocess.check_output(cmd).decode('u8')
return self.final_text % (db_text, quota_text, metrics_text, usage_text, vnstat_text)
def reset_today(self): def reset_today(self):
pairs = self.r.hgetall("metrics") pairs = self.r.hgetall("metrics")