Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/sharedra.../guds_mod...
File: change.py
from typing import TYPE_CHECKING
[0] Fix | Delete
import time
[1] Fix | Delete
from datetime import timedelta, datetime
[2] Fix | Delete
import sqlite3
[3] Fix | Delete
from tabulate import tabulate
[4] Fix | Delete
from cpapis import whmapi1, CpAPIError
[5] Fix | Delete
[6] Fix | Delete
if TYPE_CHECKING:
[7] Fix | Delete
from disk_cleanup import DiskCleaner
[8] Fix | Delete
[9] Fix | Delete
[10] Fix | Delete
def run_disk_change(cleaner: 'DiskCleaner') -> None:
[11] Fix | Delete
"""Displays change in disk usage on a per-user basis"""
[12] Fix | Delete
date_format = "%b%d%Y"
[13] Fix | Delete
today = time.strftime(date_format)
[14] Fix | Delete
days = int(cleaner.days)
[15] Fix | Delete
[16] Fix | Delete
try:
[17] Fix | Delete
cleaner.logger.debug(
[18] Fix | Delete
'action=run_disk_change sqlite3.connect status=connecting to '
[19] Fix | Delete
'/etc/rads/quotas.db'
[20] Fix | Delete
)
[21] Fix | Delete
conn = sqlite3.connect('/etc/rads/quotas.db')
[22] Fix | Delete
cur = conn.cursor()
[23] Fix | Delete
except sqlite3.Error as e:
[24] Fix | Delete
cleaner.logger.error(
[25] Fix | Delete
'action=run_disk_change sqlite3.connect error=%s', e
[26] Fix | Delete
)
[27] Fix | Delete
return
[28] Fix | Delete
[29] Fix | Delete
try:
[30] Fix | Delete
cleaner.logger.debug(
[31] Fix | Delete
'action=run_disk_change sqlite3.execute '
[32] Fix | Delete
'status=creating table %s',
[33] Fix | Delete
today,
[34] Fix | Delete
)
[35] Fix | Delete
cur.execute(f"drop table if exists '{today}'")
[36] Fix | Delete
cur.execute(f"create table '{today}' (diskused integer, user text)")
[37] Fix | Delete
except sqlite3.Error as e:
[38] Fix | Delete
cleaner.logger.error(
[39] Fix | Delete
'action=run_disk_change sqlite3.execute error=%s', e
[40] Fix | Delete
)
[41] Fix | Delete
return
[42] Fix | Delete
[43] Fix | Delete
try:
[44] Fix | Delete
cleaner.logger.debug(
[45] Fix | Delete
'action=run_disk_change whmapi1 listaccts '
[46] Fix | Delete
'status=connecting to whmapi1'
[47] Fix | Delete
)
[48] Fix | Delete
quota_data = whmapi1('listaccts', {"want": 'user,diskused'}, check=True)
[49] Fix | Delete
except CpAPIError as e:
[50] Fix | Delete
cleaner.logger.error(
[51] Fix | Delete
'action=run_disk_change whmapi1 listaccts error=%s', e
[52] Fix | Delete
)
[53] Fix | Delete
return
[54] Fix | Delete
[55] Fix | Delete
try:
[56] Fix | Delete
cleaner.logger.debug(
[57] Fix | Delete
'action=run_disk_change sqlite3.execute '
[58] Fix | Delete
'status=populating table %s',
[59] Fix | Delete
today,
[60] Fix | Delete
)
[61] Fix | Delete
for user in quota_data['data']['acct']:
[62] Fix | Delete
cur.execute(f"insert into '{today}' values (:diskused,:user)", user)
[63] Fix | Delete
except sqlite3.Error as e:
[64] Fix | Delete
cleaner.logger.error(
[65] Fix | Delete
'action=run_disk_change sqlite3.execute error=%s', e
[66] Fix | Delete
)
[67] Fix | Delete
return
[68] Fix | Delete
[69] Fix | Delete
conn.commit()
[70] Fix | Delete
[71] Fix | Delete
# Get a list of all the tables in /etc/rads/quotas.db
[72] Fix | Delete
try:
[73] Fix | Delete
cleaner.logger.debug(
[74] Fix | Delete
'action=run_disk_change sqlite3.execute '
[75] Fix | Delete
'status=querying /etc/rads/quotas.db'
[76] Fix | Delete
)
[77] Fix | Delete
cur.execute("SELECT name FROM sqlite_master WHERE type='table'")
[78] Fix | Delete
tables = cur.fetchall()
[79] Fix | Delete
except sqlite3.Error as exc:
[80] Fix | Delete
cleaner.logger.info(
[81] Fix | Delete
"action=run_disk_change sqlite3.execute error=%s", exc
[82] Fix | Delete
)
[83] Fix | Delete
return
[84] Fix | Delete
[85] Fix | Delete
user_days_ago = datetime.strftime(
[86] Fix | Delete
datetime.today() - timedelta(days), date_format
[87] Fix | Delete
)
[88] Fix | Delete
[89] Fix | Delete
# See if data exists in /etc/rads/quotas.db for the day the user
[90] Fix | Delete
# requested to compare against
[91] Fix | Delete
if user_days_ago not in [table[0] for table in tables]:
[92] Fix | Delete
cleaner.logger.warning(
[93] Fix | Delete
'action=run_disk_change user_days_ago '
[94] Fix | Delete
'status=unavailable date requested'
[95] Fix | Delete
)
[96] Fix | Delete
try:
[97] Fix | Delete
user_days_ago = tables[len(tables) - 2][0]
[98] Fix | Delete
except IndexError as exc:
[99] Fix | Delete
cleaner.logger.error(
[100] Fix | Delete
'action=run_disk_change user_days_ago error=%s', exc
[101] Fix | Delete
)
[102] Fix | Delete
return
[103] Fix | Delete
[104] Fix | Delete
# Query /etc/rads/quotas.db to determine 10 users with
[105] Fix | Delete
# the highest change in disk usage
[106] Fix | Delete
try:
[107] Fix | Delete
cleaner.logger.debug(
[108] Fix | Delete
'action=run_disk_change sqlite3.execute status=querying '
[109] Fix | Delete
'/etc/rads/quotas.db'
[110] Fix | Delete
)
[111] Fix | Delete
cur.execute(
[112] Fix | Delete
'SELECT {0}.user,{0}.diskused,{1}.diskused,'
[113] Fix | Delete
'({1}.diskused - {0}.diskused) AS diskchange '
[114] Fix | Delete
'FROM {0} INNER JOIN {1} ON {0}.user={1}.user '
[115] Fix | Delete
'ORDER BY diskchange '
[116] Fix | Delete
'DESC limit 10'.format(user_days_ago, today)
[117] Fix | Delete
)
[118] Fix | Delete
except sqlite3.OperationalError as e:
[119] Fix | Delete
cleaner.logger.error(
[120] Fix | Delete
'action=run_disk_change sqlite3.execute error=%s', e
[121] Fix | Delete
)
[122] Fix | Delete
return
[123] Fix | Delete
[124] Fix | Delete
table = []
[125] Fix | Delete
headers = ["User", user_days_ago, today, "Disk Change"]
[126] Fix | Delete
[127] Fix | Delete
cleaner.logger.debug(
[128] Fix | Delete
'action=run_disk_change display status=populating table'
[129] Fix | Delete
)
[130] Fix | Delete
for row in cur:
[131] Fix | Delete
table.append(row)
[132] Fix | Delete
[133] Fix | Delete
cleaner.logger.debug('action=run_disk_change display status=display table')
[134] Fix | Delete
print(tabulate(table, headers, tablefmt="fancy_grid"))
[135] Fix | Delete
[136] Fix | Delete
try:
[137] Fix | Delete
cleaner.logger.debug(
[138] Fix | Delete
'action=run_disk_change sqlite3.close status=closing '
[139] Fix | Delete
'sqlite3 connection'
[140] Fix | Delete
)
[141] Fix | Delete
conn.close()
[142] Fix | Delete
except sqlite3.Error as e:
[143] Fix | Delete
cleaner.logger.error('action=run_disk_change sqlite3.close error=%s', e)
[144] Fix | Delete
[145] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function