-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathSystem_maint_module
392 lines (365 loc) · 12.2 KB
/
System_maint_module
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/bin/bash
#Module containing functions called from cylon package
#Functions: SystemMaintFunc SystemMaintAutoFunc configFileScanFunc
#FUNCTION HEADER
# NAME : SystemMaintFunc
# DESCRIPTION:carries out maintenance checks
# OUTPUTS : various output files
# PROCESS : systemd , SSD trim , broken syslinks ,journalcontrol errors
#lostfiles check with lostfiles package, systemdanalysis boot,
#NOTES : needs lostfiles installed
function SystemMaintFunc
{
clear
local optionsSMArr=("Failed Systemd Services and status" "Check Journalctl log for Errors"\
"Check Journalctl for fstrim SSD trim" "Analyze system boot-up performance"\
"Check for Broken Symlinks" "Find files where no group or User corresponds to file's numeric ID"\
"$(checkPacFunc lostfiles NOMES)" "Diskspace usage" "Find 200 of the biggest files" "Find inodes usage" "Old configuration files scan"\
"Print sensors information" "Clean journal files"\
"Delete core dumps /var/lib/systemd/coredump/" "Delete files"\
"$(checkPacFunc bleachbit NOMES)"\
"$(checkPacFunc rmlint NOMES)" "List All Open Files" "DMI table decoder"\
"Return")
while true; do # loop until exit
local choiceSMVar=""
#change dir for log files
msgFunc blue "System Maintenance options"
select choiceSMVar in "${optionsSMArr[@]}"
do
case "$choiceSMVar" in
"${optionsSMArr[0]}")
# -systemd --failed:
makeDirFunc "-SYSINFO"
msgFunc green "Systemd status"
msgFunc green "Done!"
systemctl status > Systemdstatus
msgFunc green "All Failed Systemd Services"
systemctl --failed --all
systemctl --failed --all > Systemderrlog
msgFunc green "Done!"
msgFunc green "All Failed Active Systemd Services"
systemctl --failed
systemctl --failed >> Systemderrlog
;;
"${optionsSMArr[1]}")
# -Logfiles:
msgFunc green "Check log Journalctl for Errors"
makeDirFunc "-SYSINFO"
journalctl -p 3 -xb > Journalctlerrlog
;;
"${optionsSMArr[2]}")
#check if ssd trim functioning ok in log
#am I on a sdd drive? , 0 for SDD 1 for HDD from command
local sdxVar=""
sdxVar="$(df /boot/ --output=source | tail -1 | cut -c 6-8)"
sdxVar=$(grep 0 /sys/block/"$sdxVar"/queue/rotational)
if [ "$sdxVar" = "0" ]
then
msgFunc green "Check Journalctl for fstrim SSD trim"
makeDirFunc "-SYSINFO"
echo "SSD trim" > JournalctlerrSDDlog
journalctl -u fstrim > JournalctlerrSDDlog
else
msgFunc red "HDD detected no SSD trim check done"
fi
;;
"${optionsSMArr[3]}")
# systemd-analyze - Analyze system boot-up performance
msgFunc green "Analyze system boot-up performance"
makeDirFunc "-SYSINFO"
{
echo "Analyze boot-up performance"
systemd-analyze time
echo "CRITICAL-CHAIN"
systemd-analyze critical-chain
echo "BLAME"
systemd-analyze blame
} >> systemdanalyzelog
;;
"${optionsSMArr[4]}")
#Checking for broken symlinks:
msgFunc green "Checking for Broken Symlinks"
makeDirFunc "-SYSINFO"
#root or home?
local choiceSYMVar=""
msgFunc norm "Do you want to scan / or $HOME? [R/h]"
read -r choiceSYMVar
if [ "$choiceSYMVar" = "h" ]
then
find "$HOME" -type l -! -exec test -e {} \; -print > symlinkerr
else
find / -path /proc -prune -o -type l -! -exec test -e {} \; -print 2>/dev/null > symlinkerr
fi
msgFunc anykey
;;
"${optionsSMArr[5]}")
msgFunc green "Find files where no group or User corresponds to file's numeric user/group ID."
makeDirFunc "-SYSINFO"
find / -nogroup > filenogrouplog 2> /dev/null
find / -nouser > filenouserlog 2> /dev/null
;;
"${optionsSMArr[6]}")
#check if lostfiles package installed
if ! checkPacFunc lostfiles
then
msgFunc anykey
return
fi
msgFunc green "Lostfiles :-Search for files which are not part of installed Arch Linux packages"
makeDirFunc "-SYSINFO"
msgFunc norm "Lostfiles strict scan running, outputing to file"
sudo bash -c "lostfiles -sz > lostfilesStrictlist.txt"
msgFunc green "Done!"
msgFunc norm "Lostfiles relaxed scan running, outputing to file"
sudo bash -c "lostfiles -z > lostfilesRelaxedlist.txt"
;;
"${optionsSMArr[7]}")
msgFunc green "Diskspace usage"
# This is useful for finding out what is taking so much space on your drives!
makeDirFunc "-SYSINFO"
msgFunc norm "Do you want to scan / or $HOME? [R/h]"
local choiceDUVar=""
read -r choiceDUVar
if [ "$choiceDUVar" = "h" ]
then
du -H "$HOME" 2> /dev/null | sort -n -r >> diskspace
else
du -H / 2> /dev/null | sort -n -r >> diskspace
fi
;;
"${optionsSMArr[8]}") #find files
msgFunc green "Find 200 of the biggest files"
makeDirFunc "-SYSINFO"
find / -type f -exec du -Sh {} + 2>/dev/null | sort -rh | head -n 200 > filesortsize
;;
"${optionsSMArr[9]}") #inodes
msgFunc green "Find inodes usage"
makeDirFunc "-SYSINFO"
df -ih | grep sd.
msgFunc norm "Output list to fileinodescan"
du / -Sh --inodes 2>/dev/null | sort -nr | head -2000 >fileinodescan
;;
"${optionsSMArr[10]}") #old config files
msgFunc green "Old configuration files scan"
makeDirFunc "-SYSINFO"
configFileScanFunc
;;
"${optionsSMArr[11]}")
msgFunc green "Print sensor information"
sensors
;;
"${optionsSMArr[12]}")
msgFunc green "Clean journalctl files(systemd logging system)"
msgFunc norm "Remove archived journal files until the disk space they use falls "
msgFunc norm "below 100M: press y or any other key to quit [y/n] ?"
if msgFunc yesno
then #yes
sudo journalctl --vacuum-size=100M
else
return
fi
;;
"${optionsSMArr[13]}")
msgFunc green "Delete core dumps /var/lib/systemd/coredump/"
sudo rm -rvf /var/lib/systemd/coredump/*
;;
"${optionsSMArr[14]}") # delete folders /files
SystemCleanFunc "FOLDERS"
;;
"${optionsSMArr[15]}") #system clean with bleachbit
SystemCleanFunc
;;
"${optionsSMArr[16]}") #rmlint
RmLintFunc
;;
"${optionsSMArr[17]}") # List All Open Files
find /proc/*/fd -xtype f -printf "%l\n" 2>/dev/null |grep -v " (deleted)$"| sort -u
;;
"${optionsSMArr[18]}") # DMI table decoder
makeDirFunc "-SYSINFO"
sudo dmidecode | tee demidecodeoutput
;;
*)#quit
msgFunc green "Done!"
clear
return
;;
esac
break
done
msgFunc green "Done!"
msgFunc anykey
clear
done
}
#FUNCTION HEADER
# NAME : SystemMaintAutoFunc
# DESCRIPTION:carries out most of the maintenance checks done by
#SystemMaintFunc BUT in one auto pass as opposed to menu selection
#run by cylon -m
# OUTPUTS : various output files
# PROCESS : systemd , SSD trim , broken syslinks ,journalcontrol errors
#lostfiles check with lostfiles package, systemdanalysis boot,
#NOTES : needs lostfiles installed
function SystemMaintAutoFunc
{
clear
#change dir for log files
makeDirFunc "-SYSINFOAUTO"
msgFunc norm "Files report will be written to path above -"
msgFunc green "System Maintenance Scan"
# -systemd --failed:
msgFunc green "Systemd status"
msgFunc green "Done!"
systemctl status > Systemdstatus
msgFunc green "All Failed Systemd Services"
systemctl --failed --all
systemctl --failed --all > Systemderrlog
msgFunc green "Done!"
msgFunc green "All Failed Active Systemd Services"
systemctl --failed
systemctl --failed >> Systemderrlog
# -Logfiles:
msgFunc green "Check log Journalctl for Errors"
journalctl -p 3 -xb > Journalctlerrlog
#check if ssd trim functioning ok in log
#am I on a sdd drive? , 0 for SDD 1 for HDD from command
local sdxVar=""
sdxVar="$(df /boot/ --output=source | tail -1 | cut -c 6-8)"
sdxVar=$(grep 0 /sys/block/"$sdxVar"/queue/rotational)
if [ "$sdxVar" = "0" ]
then
msgFunc green "Check Journalctl for fstrim SSD trim"
echo "SSD trim" > JournalctlerrSDDlog
journalctl -u fstrim > JournalctlerrSDDlog
else
msgFunc red "HDD detected no SSD trim check done"
fi
# systemd-analyze - Analyze system boot-up performance
msgFunc green "Analyze system boot-up performance"
{
echo "Analyze boot-up performance"
systemd-analyze time
echo "CRITICAL-CHAIN"
systemd-analyze critical-chain
echo "BLAME"
systemd-analyze blame
} >> systemdanalyzelog
#Checking for broken symlinks:
msgFunc green "Checking for Broken Symlinks"
#root or home?
msgFunc norm "Do you want to scan / or $HOME? [R/h]"
local choiceSYMVar=""
read -r choiceSYMVar
if [ "$choiceSYMVar" = "h" ]
then
find "$HOME" -type l -! -exec test -e {} \; -print > symlinkerr
else
find / -path /proc -prune -o -type l -! -exec test -e {} \; -print 2>/dev/null > symlinkerr
fi
#files
msgFunc green "Find 200 of the biggest files"
find / -type f -exec du -Sh {} + 2>/dev/null | sort -rh | head -n 200 > filesortsize
#inodes
msgFunc green "Find inodes usage"
du / -Sh --inodes 2>/dev/null | sort -nr | head -2000 >fileinodescan
#Find files where no group or User corresponds to file's numeric user/group ID.
msgFunc green "Find files where no group or User corresponds to file's numeric user/group ID."
find / -nogroup > filenogrouplog 2> /dev/null
find / -nouser > filenouserlog 2> /dev/null
#check if lostfiles package installed
if ! checkPacFunc lostfiles
then
msgFunc anykey
return
fi
msgFunc green "Lostfiles :-Search for files which are not part of installed Arch Linux packages"
msgFunc norm "Lostfiles strict scan running, outputing to file"
sudo bash -c "lostfiles strict > lostfilesStrictlist.txt"
msgFunc green "Done!"
msgFunc norm "Lostfiles relaxed scan running, outputing to file"
sudo bash -c "lostfiles relaxed > lostfilesRelaxedlist.txt"
msgFunc green "diskspace usage"
msgFunc norm "Do you want to scan / or $HOME? [R/h]"
local choiceDUVar=""
read -r choiceDUVar
if [ "$choiceDUVar" = "h" ]
then
du -H "$HOME" 2> /dev/null | sort -n -r >> diskspace
else
du -H / 2> /dev/null | sort -n -r >> diskspace
fi
msgFunc anykey
msgFunc green "Old configuration files scan"
configFileScanFunc
msgFunc green "All Done!"
msgFunc anykey
clear
}
#FUNCTION HEADER
# NAME : configFileScanFunc
# DESCRIPTION: Old configuration files scan see below text
function configFileScanFunc
{
cat <<-EOF
Old configuration files may conflict with newer software versions,
or corrupt over time. Remove unneeded configurations periodically,
This option will scan three folders and compare names of dirs with
names of installed packages and output result to files.
~/.config/ -- where apps stores their configuration
~/.cache/ -- cache of some programs may grow in size
~/.local/share/ -- old files may be lying there
The user can then inspect these files and remove anything unwanted.
EOF
msgFunc anykey
local configArr=()
local cacheArr=()
local homeArr=()
#populate arrays
mapfile -t configArr < <(find "$HOME/.config" -maxdepth 1 -type d | awk -F"/" '{print $5}')
mapfile -t cacheArr < <(find "$HOME/.cache" -maxdepth 1 -type d | awk -F"/" '{print $5}')
mapfile -t homeArr < <(find "$HOME/.local/share" -maxdepth 1 -type d | awk -F"/" '{print $6}')
#sort arrays
IFS=$'\n' configArr=($(sort <<<"${configArr[*]}"))
unset IFS
IFS=$'\n' cacheArr=($(sort <<<"${cacheArr[*]}"))
unset IFS
IFS=$'\n' homeArr=($(sort <<<"${homeArr[*]}"))
unset IFS
# get length of an array
tLen="${#configArr[@]}"
tLen1="${#cacheArr[@]}"
tLen2="${#homeArr[@]}"
# use for loop read all installed packages
msgFunc green "$HOME/.config/ -- where apps stores their configuration"
for (( i=0; i<"${tLen}"; i++ ));
do
if checkPacFunc "${configArr[i]}"
then
unset "configArr[i]"
fi
done
msgFunc anykey
printf "%s\n" "${configArr[@]}" > HOMECONFIGfile
# use for loop read all installed packages
msgFunc green " $HOME/.cache/ -- cache of some programs may grow in size"
for (( i=0; i<"${tLen1}"; i++ ));
do
if checkPacFunc "${cacheArr[i]}"
then
unset "cacheArr[i]"
fi
done
msgFunc anykey
printf "%s\n" "${cacheArr[@]}" > HOMECACHEfile
# use for loop read all installed packages
msgFunc green "$HOME/.local/share/ -- old files may be lying there"
for (( i=0; i<"${tLen2}"; i++ ));
do
if checkPacFunc "${homeArr[i]}"
then
unset "homeArr[i]"
fi
done
printf "%s\n" "${homeArr[@]}" > HOMELOCALSHAREfile
}