CPU and Memory Usage Monitor
A shell script to monitor CPU and memory usage for a given process ID.
(1) Generates CPU and memory usage reports and stores them in a directory; and
(2) Notifies user by email whenever CPU or memory usage exceeds certain thresholds that you set.
Implementing CPU Monitor -
In order to catch a usage spike early, we are only interested in measuring CPU usage over the period {time interval}. For example, if the time interval is 5 seconds, then you will always report the CPU usage for the last 5 seconds (and not from the time the monitored process started).
One way to check if the CPU usage percentages you are computing are reasonable is to compare them to those reported by the command top CPU usage report You will generate usage reports containing CPU usage information.
Implementing Memory Usage Monitor -
The physical memory used by the process is found in the file /proc/{pid}/status. You should extract the value of “VmRSS”. As opposed to CPU usage, we are now interested in measuring the used memory from the time the process started.
CPU and Memory Usage Report Expand the CPU Usage report to contain a field for memory as well.
Email notification Whenever the memory usage exceeds the provided maximum memory, an email is sent to $USER containing the last usage report. At this point, your script should be generating reports if a user exceeds either the memory or CPU threshold.