Last active
November 6, 2020 17:31
-
-
Save divadsn/9c5e1bfa95c8b78ceefff12f9e5c17c4 to your computer and use it in GitHub Desktop.
Shitty python script to delete old VM logs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import argparse | |
rootdir = "/var/log/pve/tasks" | |
logfiles = { | |
"active": [], | |
"index": [] | |
} | |
def find_logs_for_vm(vm_id): | |
logs = [] | |
for subdir, dirs, files in os.walk(rootdir, topdown=True): | |
for file in files: | |
task = file.split(":") | |
if len(task) < 7: | |
continue | |
if task[6] == str(vm_id): | |
logs.append(os.path.join(subdir, file)) | |
return logs | |
def remove_logs(lines, vm_id): | |
logs = [] | |
for line in lines: | |
task = line.split(":") | |
if len(task) < 7: | |
continue | |
if task[6] != str(vm_id): | |
logs.append(line) | |
return logs | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Cleans task history from log files for given VM ID.") | |
parser.add_argument("vm_id", type=int, help="ID of the VM") | |
args = parser.parse_args() | |
for logfile in find_logs_for_vm(args.vm_id): | |
os.remove(logfile) | |
for logfile in logfiles: | |
with open(os.path.join(rootdir, logfile), "r") as f: | |
logfiles[logfile] = f.readlines() | |
for logfile in logfiles: | |
with open(os.path.join(rootdir, logfile), "w") as f: | |
f.writelines(remove_logs(logfiles[logfile], args.vm_id)) | |
os.system("systemctl restart pve-cluster") | |
print("Deleted logs for VM " + str(args.vm_id) + " and restarted Proxmox.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment