Here's a simple bash script I conjured up that will find a pid, and print out either the CPU or Memory % utilization from the ps command. Keep in mind that I am in the very early stages of learning bash scripting and there probably are better ways to accomplish what I'm looking for. I'm hoping that by posting this script here, people will be able to improve it and give it back to the APM community here as a resource they can use in their own installations.
Here it is:
#!/bin/bash
#
#This is where you locate the PID or the process you want to monitor
read pid < /opt/server/server.pid
#You can change between CPU and MEM% by using either %cpu or %mem here
cpu=$(ps -o %cpu --ppid $pid --no-header)
if [ -n "$pid" ]; then
printf "Message: We're good to go\n"
printf "Statistic:$cpu\n"
exit 0
else
printf "Message: Houston, we have a problem\n"
printf "Statistic: 0\n"
exit 1
fi
The obvious shortcoming of this script is that it depends on your program writing out a pid file somewhere. Most programs write something to /var/run/ so it shouldn't be a problem to find what you're looking for. The other downside is that ps only outputs % for cpu and memory. Top outputs more info and I'm currently working on a way to sort through a batch output of top and obtain the resident memory size in megabytes.
Even though it's a simple script, I add the disclaimer that you use this at your own risk! Don't hold me responsible if it causes your server to burst into flames 