forked from semaphoreci/toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem-metrics-collector
36 lines (33 loc) · 1.33 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
#!/bin/sh
#
# Simple log system metrics collector. Polls the system state every 30s 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 | mem: 5.03%, disk: 47.75%
# Mon May 18 14:51:28 UTC 2020 | mem: 5.03%, disk: 47.75%
#
# 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)) }')
echo "$(date) | mem:$MEMORY, system_disk:$SYSTEM_DISK, docker_disk:$DOCKER_DISK, shared_memory: $SHARED_MEMORY M" >> $OUTPUT
sleep 1
done