Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
collect memory information(free command and /proc/meminfo), and output in CSV format.
#!/usr/bin/bash
#---------------------
# Copyright 2017 N.Fujita
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#---------------------
#NAME: collect memory information(free command and /proc/meminfo), and output in CSV format.
#SYNOPSIS: ./collect_mem.sh
#DESCRIPTION:
# Collect memory information from free command and /proc/meminfo.
# And, collected information is outputted to a file in CSV format.
# Exit with "Ctrl+c".
# *You can modify the collect interval with "INTERAL" variable.
#---------------------
LANG=C
PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:
export LANG PATH
umask 022
LOGFILE_BASE="./meminfo"
LOGFILE="${LOGFILE_BASE}.`date '+%Y%m%d%H%M%S'`.csv"
INTERVAL=3
# Write header
echo -n '"DATE","TIME"' > ${LOGFILE}
echo -n ',"free-mem-total(kb)","free-mem-used(kb)","free-mem-free(kb)","free-mem-share(kb)","free-mem-buff/cache(kb)","available(kb)"' >> ${LOGFILE}
echo -n ',"free-swap-total(kb)","free-swap-used(kb)","free-swap-free(kb)"' >> ${LOGFILE}
awk '
{
printf ",\"%s(%s)\"",$1,$3
}
END{
printf "\n"
}' /proc/meminfo >> ${LOGFILE}
# Measure memory data(meminfo and free command)
while true
do
# Write free command result
free | awk '
BEGIN{
printf strftime("%Y/%m/%d, %H:%M:%S")
}
/^(Mem|Swap)/{
for(i=2; i<= NF; i++){
printf ",%d",$i
}
}
' >> ${LOGFILE}
# Write meminfo data
awk '
{
printf ",%d",$2
}
END{
printf "\n"
}' /proc/meminfo >> ${LOGFILE}
# Interval
sleep ${INTERVAL}
done
exit 0
@Noppy

This comment has been minimized.

Copy link
Owner Author

Noppy commented Oct 31, 2017

Add ".csv" extension at the output file name.

@mriostamez

This comment has been minimized.

Copy link

mriostamez commented Jun 15, 2018

Hi @Noppy! on line 23 "*You can modify the collect interval with "INTERAL" variable." You mean INTERVAL, correct?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.