Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script for logging cpu and memory usage of a linux process

I am looking for a way to log and graphically display cpu and RAM usage of linux processes over time. Since I couldn't find a simple tool to so (I tried zabbix and munin but installation failed) I started writing a shell script to do so

The script file parses the output of top command through awk and logs into a csv file. It

  1. Figures out the pid of the processes through ps command
  2. Uses top and awk to log cpu and memory usage.

Here is how the script looks like

#!/bin/sh
#A script to log the cpu and memory usage of linux processes namely - redis, logstash, elasticsearch and kibana

REDIS_PID=$(ps -ef | grep redis | grep -v grep | awk '{print $2}')

LOGSTASH_PID=$(ps -ef | grep logstash | grep -v grep | awk '{print $2}')

ELASTICSEARCH_PID=$(ps -ef | grep elasticsearch | grep -v grep | awk '{print $2}')

KIBANA_PID=$(ps -ef | grep kibana | grep -v grep | awk '{print $2}')

LOG_FILE=/var/log/user/usage.log
echo $LOG_FILE
top -b | awk -v redis="$REDIS_PID" -v logstash="$LOGSTASH_PID" '/redis|logstash/ {print $1","$9","$10","$12}'

How do I

  1. Print the resource usage for multiple processes. Specifying multiple variables in the awk pattern is not working. It prints the usage for the first pid (redis in the above script)
  2. Print current timestamp when printing the resource details (through date +"%T")
  3. Print the process name along with the resource usage. Redis, Logstash, ElasticSearch or Kibana in the above case
  4. Redirect the above commands output to a log file. I tried > $LOG_FILE but it didn't work.

Thoughts/Inputs?

Thanks in advance.

like image 500
Andy Dufresne Avatar asked May 07 '13 09:05

Andy Dufresne


People also ask

How do I check memory and CPU status in Linux?

Linux comes with different set of commands to check memory usage. The free command displays the total amount of free and used physical and swap memory in the system, as well as the buffers used by the kernel. The vmstat command reports information about processes, memory, paging, block IO, traps, and cpu activity.


1 Answers

To figure out PIDs you can simplify your script greatly using pgrep:

REDIS_PID=$(pgrep -f redis)

LOGSTASH_PID=$(pgrep -f logstash)

ELASTICSEARCH_PID=$(pgrep -f elasticsearch)

KIBANA_PID=$(pgrep -f kibana)

EDIT: Sorry had to leave for some work and couldn't provide the full answer.

In order to capture top's output use following script:

while :; do 
  top -n 1 -b | awk -v redis="$REDIS_PID" -v logstash="$LOGSTASH_PID"
         '$1 == redis || $1 == logstash {print $1","$9","$10","$12}' >> $LOG_FILE
  sleep 3
done
like image 189
anubhava Avatar answered Sep 18 '22 04:09

anubhava