-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathsystem-metrics-collector
37 lines (34 loc) · 1.55 KB
/
system-metrics-collector
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
#!/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 seemlesly 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.
#
SYSTEM_DISK_LOCATION="/"
DOCKER_DISK_LOCATION="/$([ -d /var/lib/docker ] && echo 'var/lib/docker')"
OUTPUT="/tmp/system-metrics"
while true; do
MEMORY=$(free | grep Mem | awk '{ printf("%6.2f%%\n", ($3/$2 * 100.0)) }')
SHARED_MEMORY=$(free -m | grep Mem | awk '{ print $5 }')
SYSTEM_DISK=$(df "$SYSTEM_DISK_LOCATION" | sed 1d | awk '{ printf("%6.2f%%\n", ($3/$2 * 100.0)) }')
DOCKER_DISK=$(df "$DOCKER_DISK_LOCATION" | sed 1d | awk '{ printf("%6.2f%%\n", ($3/$2 * 100.0)) }')
CPU_USAGE=$(ps L | grep -q '%cpu' && ps -A -o %cpu 2>/dev/null | awk '{s+=$1} END {print s "%"}')
echo "$(date) | cpu:$CPU_USAGE, mem:$MEMORY, system_disk:$SYSTEM_DISK, docker_disk:$DOCKER_DISK, shared_memory: $SHARED_MEMORY M" >> $OUTPUT
sleep 1
done