• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 359
  • Last Modified:

Monitoring with top command

Hi Experts,

I am monitoring a linux system with top command for a particular process. If that process takes more than 50% of CPU , it should email me. How can be done this with scripting.
0
07592161981m
Asked:
07592161981m
  • 2
1 Solution
 
tel2Commented:
Q1. How long should a process suck more than 50% CPU for you to be emailed, 07592161981m?
Q2. More than 50% of one CPU, or the total CPU of the system (or is it just a single CPU system)?
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
Use ps command in batch mode after which you filter for your process name. Then grep the column that has the CPU usage % and with an if/then construction send mail (or not).
0
 
07592161981mAuthor Commented:
Hi Tel2,

The CPU should be sucked 50% atleast 60 seconds. It is a single CPU system.
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
Here's a sample script that will monitor the process firefox for a max. 50% cpu usage. It uses top -b to get current cpu usage, if above limit, a counter is increased. When max. is reached you can have it send a mail. Script takes a sample every 10s - if you need more resolution, change sleep to 5s and the if count to 12 instead of 6.
#!/bin/bash
process=firefox
count=0
limit=50
while [ 1=1 ]
do
        usage=$(top -b -n 1 | grep ${process} | awk '{ print $10 }')
        if [ $(echo "${usage} > ${limit}"|bc) -eq 1 ]
        then
                # usage above target setting
                let count=${count}+1
                echo "Usage: of ${process} ${usage} > ${limit} - increasing counter - count is now ${count}"
                if [ ${count} -gt 6 ]
                then
                        echo "Usage of ${process} > ${limit} for too long - sending mail"
                        # put your mail command here
                        count=0
                fi;
        else
                # usage below target setting
                echo "Usage: of ${process} ${usage}"
                count=0
        fi;
        sleep 10
done

Open in new window

0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now