Last active
September 18, 2019 03:51
-
-
Save namnv609/d5aa6705eafbc4381258c0cc1800cb4a to your computer and use it in GitHub Desktop.
Log memory to CSV. View chart from log output at: https://codepen.io/namnv609/full/xxKybdW
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
function get_time_format() { | |
time_str_formatted=$(TZ="Asia/Tokyo" date +"$1") | |
echo "$time_str_formatted" | |
} | |
function init_log_file() { | |
if [[ ! -f "$1" ]]; then | |
$(touch "$1") | |
echo "Time,Used,Free,Shared,Buffer,Cache" > "$1" | |
fi | |
} | |
function get_memory_detail { | |
free_memory=$(free -m | awk -v OFS=',' 'NR==2 {print $3,$4,$5,$6,$7}') | |
echo "$free_memory" | |
} | |
LOG_FILE_TIME_FORMAT="%Y%m%d_%H" | |
LOG_CONTENT_TIME_FORMAT="%Y/%m/%d %H:%M" | |
LOG_FILE_SUFFIX=$(get_time_format "$LOG_FILE_TIME_FORMAT") | |
LOG_FILE_PATH="mem-logs/memory-log-${LOG_FILE_SUFFIX}.csv" | |
# Init log file nếu chưa có | |
init_log_file "$LOG_FILE_PATH" | |
# Lấy thời gian đã được format cho log content | |
log_time=$(get_time_format "$LOG_CONTENT_TIME_FORMAT") | |
# Lấy thông tin memory sau khi đã được format đẹp đẽ =)) | |
memory_detail=$(get_memory_detail) | |
# Ghi log thôi. Sao phải xoắn | |
echo "$log_time,$memory_detail" >> "$LOG_FILE_PATH" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment