-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathsystem-metrics-collector
More file actions
95 lines (92 loc) · 2.43 KB
/
system-metrics-collector
File metadata and controls
95 lines (92 loc) · 2.43 KB
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
#!/bin/sh
#
# Simple log system metrics collector. Polls the system state every 1s and
# saves the result to /tmp/system-metrics.
#
# The simple nature of the script allows it to seamlessly run in any Linux based
# VM, Docker image, or on a MacVM host.
#
# The recommended way to start the script is to run it in the background.
#
# $ system-metrics-collector &
#
# The resulting file's format is the following:
#
# $ cat /tmp/system-metrics-collector
#
# Mon May 18 14:50:58 UTC 2020 | cpu: 6% , mem: 5.03% , system_disk 1% , docker_disk: 1% , shared_memory 1%
# Mon May 18 14:51:28 UTC 2020 | cpu: 4% , mem: 5.03% , system_disk 1.9% , docker_disk: 11% , shared_memory 41%
#
# Jobs that run for an hour collect around 120 log lines. This should be safe
# and not introduce any performance of disk usage problems.
#
DIST="`uname`"
SYSTEM_DISK_LOCATION="/"
DOCKER_DISK_LOCATION="/`[ -d /var/lib/docker ] && echo 'var/lib/docker'`"
OUTPUT="/tmp/system-metrics"
memFree(){
freeMem=-1
case "$DIST" in
"Linux")
freeMem=`free | grep Mem | awk '{ printf("%6.2f%%\n", ($3/$2 * 100.0)) }'`
;;
"Darwin")
freeMem=`memory_pressure | grep 'percentage' | tr -d '%'|awk '{print 100-$NF"%"}'`
;;
*)
;;
esac
echo $freeMem
}
sharedMemory(){
sharedMem=0
case "$DIST" in
"Linux")
sharedMem=`free -m | grep Mem | awk '{ print $5 }'`
;;
"Darwin")
sharedMem=0
;;
*)
;;
esac
echo $sharedMem
}
cpuUsage(){
usedCpu=-1
case "$DIST" in
"Linux")
usedCpu=`ps L | grep -q '%cpu' && ps -A -o %cpu 2>/dev/null | awk '{s+=$1} END {print s "%"}'`
;;
"Darwin")
usedCpu=`top -i 1 -l 1 -stats CPU | grep CPU | grep idle |tr -d '%'| awk '{print 100-$7"%"}'`
;;
*)
;;
esac
echo $usedCpu
}
diskUsage(){
disk=$1
usedDisk=-1
case "$DIST" in
"Linux")
usedDisk=`df "$disk" | sed 1d | awk '{ printf("%6.2f%%\n", ($3/$2 * 100.0)) }'`
;;
"Darwin")
usedDisk=`df / | sed 1d | awk '{print $5}'`
;;
*)
;;
esac
echo $usedDisk
}
while true; do
MEMORY="`memFree`"
SHARED_MEMORY="`sharedMemory`"
SYSTEM_DISK="`diskUsage ${SYSTEM_DISK_LOCATION}`"
DOCKER_DISK="`diskUsage ${DOCKER_DISK_LOCATION}`"
CPU_USAGE="`cpuUsage`"
echo `date +"%a %b %d %T %Z %Y"`" | cpu: ${CPU_USAGE}, mem: ${MEMORY}, system_disk: ${SYSTEM_DISK}, docker_disk: ${DOCKER_DISK}, shared_memory: ${SHARED_MEMORY} M" >> $OUTPUT
sleep 1
done