Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../proc/self/root/opt/maint/bin
File: clear_domlogs.py
#!/opt/imh-python/bin/python3
[0] Fix | Delete
"""Removes domlogs for terminated accounts"""
[1] Fix | Delete
# Vanessa V 12/21/12 (aka "the end of the world that didn't happen" day)
[2] Fix | Delete
[3] Fix | Delete
import os
[4] Fix | Delete
import sys
[5] Fix | Delete
import shutil
[6] Fix | Delete
import yaml
[7] Fix | Delete
[8] Fix | Delete
LOG_DIR = '/var/log/apache2/domlogs/'
[9] Fix | Delete
[10] Fix | Delete
# Create a list of deleted domains
[11] Fix | Delete
with open('/var/cpanel/deleteddomains.yaml', encoding='utf-8') as f:
[12] Fix | Delete
del_map = yaml.load(f, Loader=yaml.SafeLoader)
[13] Fix | Delete
[14] Fix | Delete
if del_map is None:
[15] Fix | Delete
del_map = {}
[16] Fix | Delete
[17] Fix | Delete
userdoms = set()
[18] Fix | Delete
with open('/etc/userdomains', encoding='utf-8') as f:
[19] Fix | Delete
for line in f:
[20] Fix | Delete
userdoms.add(line.split(':')[0])
[21] Fix | Delete
[22] Fix | Delete
EXT_LIST = [
[23] Fix | Delete
'-smtpbytes_log',
[24] Fix | Delete
'-smtpbytes_log',
[25] Fix | Delete
'-bytes_log',
[26] Fix | Delete
'-bytes_log.offset',
[27] Fix | Delete
'.offsetftpbytes ',
[28] Fix | Delete
'.bkup',
[29] Fix | Delete
'-bkup2',
[30] Fix | Delete
'-ftp_log ',
[31] Fix | Delete
'-popbytes_log',
[32] Fix | Delete
'-imapbytes_log',
[33] Fix | Delete
'-ssl_log',
[34] Fix | Delete
'-ssl_data_log',
[35] Fix | Delete
]
[36] Fix | Delete
[37] Fix | Delete
# For each domain, see if a domlogs entry exists
[38] Fix | Delete
[39] Fix | Delete
[40] Fix | Delete
def try_remove(path):
[41] Fix | Delete
try:
[42] Fix | Delete
os.unlink(path)
[43] Fix | Delete
except OSError:
[44] Fix | Delete
return
[45] Fix | Delete
print('Deleted', path)
[46] Fix | Delete
[47] Fix | Delete
[48] Fix | Delete
def main():
[49] Fix | Delete
for domain in del_map.keys():
[50] Fix | Delete
if domain is None or domain in userdoms: # it was added back
[51] Fix | Delete
continue
[52] Fix | Delete
# remove leftover domlogs for domains which no longer exist
[53] Fix | Delete
dom_path = os.path.join(LOG_DIR, domain)
[54] Fix | Delete
try_remove(dom_path)
[55] Fix | Delete
for ext in EXT_LIST:
[56] Fix | Delete
ext_path = f'{dom_path}{ext}'
[57] Fix | Delete
try_remove(ext_path)
[58] Fix | Delete
# Find the user
[59] Fix | Delete
user = del_map[domain]['user']
[60] Fix | Delete
if user is None:
[61] Fix | Delete
continue
[62] Fix | Delete
# Remove the user's folder if it exists and the user does not
[63] Fix | Delete
user_logdir = os.path.join(LOG_DIR, user)
[64] Fix | Delete
users_file = os.path.join('/var/cpanel/users', user)
[65] Fix | Delete
if not os.path.exists(users_file) and os.path.isdir(user_logdir):
[66] Fix | Delete
try:
[67] Fix | Delete
shutil.rmtree(user_logdir)
[68] Fix | Delete
print('Deleted logs in', user_logdir)
[69] Fix | Delete
except (OSError, shutil.Error) as exc:
[70] Fix | Delete
print('Could not delete', user_logdir)
[71] Fix | Delete
print(exc, file=sys.stderr)
[72] Fix | Delete
[73] Fix | Delete
[74] Fix | Delete
if __name__ == '__main__':
[75] Fix | Delete
main()
[76] Fix | Delete
[77] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function