-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcleanlogs.py
68 lines (52 loc) · 1.69 KB
/
cleanlogs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/python3
# created_at: 03.11.2023
# Truncates Log Files to size 0 in /var/log, /tmp and /var/lib/docker/containers
# returns metric message
import logging
import sys
import os
from operator import add
from pathlib import Path
logging.basicConfig(stream=sys.stdout, level=logging.ERROR)
dockerContainerPath="/var/lib/docker/containers"
sysLogPath="/var/log"
tmpLogPath="/tmp"
def cleanLogsInPath(p, selector="*.log"):
amount=0
cleanedAmount=0
failedAmount=0
for path in Path(p).rglob(selector):
fpath=path.resolve()
if not os.path.isfile(path):
continue
amount+=1
try:
with open(fpath, 'r+') as f:
f.truncate(0)
logging.info("[+] truncated logfile '{}'".format(fpath))
cleanedAmount+=1
except Exception as e:
logging.exception(e)
print("[-] ERROR".format(e))
failedAmount+=1
return [amount, cleanedAmount, failedAmount]
def calcSums (ar=[]):
previous=[]
for i in range(0, len(ar)):
if i == 0:
previous=ar[i]
else:
previous=list( map(add, previous, ar[i]) )
return previous
amounts = [
cleanLogsInPath (dockerContainerPath),
cleanLogsInPath (sysLogPath, selector="*"),
cleanLogsInPath (tmpLogPath)
]
sums = calcSums(amounts)
totalFiles=sums[0]
totalCleaned=sums[1]
totalFailed=sums[2]
totalOperations = len(amounts)
percentageSuccess = round((totalCleaned/totalFiles), 2)*100
print("[+] Truncated Log Files. [operations: {}, total: {}, success: {}, failed: {}, percent:{:.2f}]".format(totalOperations, totalFiles, totalCleaned, totalFailed, percentageSuccess))