-
Notifications
You must be signed in to change notification settings - Fork 3
/
checkDiskSpace.sh
executable file
·89 lines (66 loc) · 1.85 KB
/
checkDiskSpace.sh
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
#!/bin/bash
LOG_DIR=~/build/bin
LONG_DATE=$(date "+%Y-%m-%d_%H-%M-%S")
DISK_SPACE_LOG_FILE=${LOG_DIR}/diskspace-${LONG_DATE}.log
MEM_SPACE_LOG_FILE=${LOG_DIR}/memspace-${LONG_DATE}.log
HEADER_ON_EVERY_LINES=30
#
#-----------------------------------------
#
# Functions
WriteLog()
(
TIMESTAMP=$( date +%Y-%m-%d_%H-%M-%S)
printf "%s:%s\n" "${TIMESTAMP}" "$1" >> $2
)
ControlC()
# run if user hits control-c
{
echo "\n*** KILL signal recived! Exiting ***\n"
WriteLog "End." "${DISK_SPACE_LOG_FILE}"
WriteLog "End." "${MEM_SPACE_LOG_FILE}"
exit $?
}
WriteHeaders()
{
# Print disk space header
header1=$(df -h . | egrep "^(Filesystem)" | tr '\n' ' ')
header2=$(df -hP . | egrep "^(Filesystem)" | tr '\n' ' ')
header="${header1} ${header2}"
WriteLog "${header}" "${DISK_SPACE_LOG_FILE}"
# Print memory space header
header=$(free | egrep "^(\s)" | tr '\n' ' ')
WriteLog "${header}" "${MEM_SPACE_LOG_FILE}"
}
#
#-----------------------------------------
#
# Main start
WriteLog "Disk space logger started." "${DISK_SPACE_LOG_FILE}"
WriteLog "Memory space logger started." "${MEM_SPACE_LOG_FILE}"
# trap keyboard interrupt (control-c)
trap ControlC SIGINT
trap ControlC SIGTERM
trap ControlC SIGKILL
WriteHeaders
#lineCount=0
while true
do
#if [[ ${lineCount} -eq 0 ]]
#then
# WriteHeaders
#fi
#lineCount=$(( ${lineCount} + 1))
#if [[ ${lineCount} -eq ${HEADER_ON_EVERY_LINES} ]]
#then
# #lineCount=0
#fi
myDs1=$(df -lh . | egrep "^(/dev/)" | tr '\n' ' ')
# On our testfarm /var/lib located on other partition/disk
myDs2=$(df -Ph /var/lib | egrep "^(/dev/)" | tr '\n' ' ')
myDs="${myDs1} ${myDs2}"
WriteLog "${myDs}" "${DISK_SPACE_LOG_FILE}"
myMem=$( free | egrep "^(Mem|Swap)" | tr '\n' ' ')
WriteLog "${myMem}" "${MEM_SPACE_LOG_FILE}"
sleep 10
done