Skip to content

Commit c9c5e1c

Browse files
committed
Add script to log system metrics
1 parent f1b3f90 commit c9c5e1c

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

tools/ci/circle/system_metrics

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env bash
2+
#
3+
# @license Apache-2.0
4+
#
5+
# Copyright (c) 2018 The Stdlib Authors.
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
19+
# Build script to log metrics in order to monitor resource usage over time.
20+
21+
# VARIABLES #
22+
23+
# Define an output file to store metrics:
24+
log_file="${CIRCLE_ARTIFACTS}/system_metrics.txt"
25+
26+
# Define an interval for logging metrics:
27+
interval='1s'
28+
29+
30+
# FUNCTIONS #
31+
32+
# Defines an error handler.
33+
on_error() {
34+
echo 'ERROR: An error was encountered during execution.' >&2
35+
exit 1
36+
}
37+
38+
# Prints system memory usage.
39+
#
40+
# Format:
41+
#
42+
# memory_used_in_MB,total_memory_in_MB,percent_used
43+
#
44+
memory_usage() {
45+
local mem=$(free -m | awk 'NR==2 {printf "%sMB,%sMB,%.2f%%)", $3, $2, $3*100/$2}')
46+
echo "${mem}"
47+
}
48+
49+
# Prints system disk usage.
50+
#
51+
# Format:
52+
#
53+
# disk_used_in_GB,total_disk_space_in_GB,percent_used
54+
#
55+
disk_usage() {
56+
local disk=$(df -h | awk '$NF=="/" {printf "%dGB,%dGB,%s", $3, $2, $5}')
57+
echo "${disk}"
58+
}
59+
60+
# Prints the system load average.
61+
load_avg() {
62+
local load=$(top -bn1 | grep load | awk '{printf "%.2f", $(NF-2)}')
63+
echo "${load}"
64+
}
65+
66+
# Polls the system for system metrics.
67+
#
68+
# $1 - poll interval
69+
# $2 - output log file
70+
poll() {
71+
local disk
72+
local mem
73+
local cpu
74+
75+
# Set headers:
76+
echo 'mem,total,pct,du,total,pct,load' >> "$2"
77+
78+
# Poll for metrics:
79+
while true; do
80+
mem=$(memory_usage);
81+
disk=$(disk_usage);
82+
cpu=$(load_avg);
83+
echo "${mem},${disk},${cpu}" >> "$2";
84+
sleep "$1";
85+
done
86+
}
87+
88+
# Main execution sequence.
89+
main() {
90+
poll "${interval}" "${log_file}"
91+
}
92+
93+
# Set an error handler:
94+
trap 'on_error' ERR
95+
96+
# Run main:
97+
main

0 commit comments

Comments
 (0)