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.

This should do the job:

top -b | awk -v logfile=/tmp/log.txt '
{
	if($1 == "PID")
	{
		command="date +%T";
		command | getline ts
		close(command);
	}
	if($12 == "redis" || $12 == "logstash" || $12 == "elasticsearch" || $12 == "kibana")
	{
		printf "%s,%s,%s,%s,%s\n",ts,$1,$9,$10,$12 > logfile
	}
}'

You were very close with your original script, all you needed was:

top -b | awk -v redis="$REDIS_PID" -v logstash="$LOGSTASH_PID" '
    $12 ~ redis || $12 ~ logstash {print $1","$9","$10","$12}'

To add time just pass another variable in with the time:

top -b | awk -v redis="$REDIS_PID" -v logstash="$LOGSTASH_PID" -v logtime="$(date +%T)" '
    $12 ~ redis || $12 ~ logstash {print logtime","$1","$9","$10","$12}'