Andy Dufresne
Andy Dufresne

Reputation: 6180

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.

Upvotes: 6

Views: 13633

Answers (1)

anubhava
anubhava

Reputation: 785176

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

Upvotes: 6

Related Questions