Measuring Memory in Linux and Parsing Results in JSON

Measuring Memory Linux

Today is the day to present for you two useful commands to know better about your memory usage in Linux.

The free command

I like to see this command as a light version of memory metrics. What the man page of the free command shows to us is a pretty straightforward description of its features and some columns and flags are interesting to have a look.

The columns total, used and free are normally what catches your eyes first. As their names say, we have displayed the total amount of memory available for your computer, how much memory is used (equals to total – free – buffers – cache) and the quantity of available memory, respectively.

Within flags options, I think particularly that -h and -s are interesting.

The -h flag shows all the data in a human format, it means, with well-known measurements of bytes (B, Ki, Mi, Gi, etc.).

The -s flag is important when you want to set a loop, in seconds, of how many times the command supposed to run.

Free command example.
free command example.

The vmstat command

The vmstat command is a more complete tool because it gives you a summary not only about memory usage but, as well, about processes, block IO, CPU activity, etc.

One interesting feature of the vmstat command is available by the flag -s. This flag shows us a summary of all the data collected, in a line by line format.

vmstat command example.
vmstat command example.

The summary above is great but what if you would like to export this data, from time to time, in a more standard format?

That is what the next section will show! Hold on!

Exporting Linux Memory Stats in JSON

To be able to export the statistics from the vmstat -s command above, we will need to use two other tools.

The awk command will be dealing with parsing and formatting the data in a proper JSON structure and, the jq command, that displays valid JSON data in our terminal, extremely handy when using the CLI commands in AWS.

HINT: I have used version 1.5 of jq. If for whatever reason you get an older version, please follow those instructions of how to install the jq command.

The command

vmstat -s | awk 'BEGIN {printf "["} {printf "{\"quantity\":\"%s\",\"description\":\"%s\"}", $1, substr($0, index($0,$2))} NR!=26 {printf ","} END {print "]"}' | jq
vmstat -s command displaying in JSON.
vmstat -s command displaying in JSON.

Keep in mind that I am always considering the vmstat -s command returning exactly 26 lines, this is what the NR variable available from awk give to us.

The possibilities from this approach are limitless!

For instance, you could export this data to some third party software, fulfill some logging tool already available in your Linux environment, etc.

Just play with it and have some fun!

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.