Solved

Scripting for monitoring the memory usage of some processes

Posted on 2002-07-11
9
297 Views
Last Modified: 2013-12-27
Hi All
I need a script which monitors the memory usage of the machine for some specific processes.

Something like

./CheckUtilisation.sh <process handle> <threshold>

and it outputs let's say "Yes" if threshold exceeeds.

Regards
Khem
0
Comment
Question by:k_suchdeva
9 Comments
 
LVL 4

Expert Comment

by:Otetelisanu
ID: 7145799
Install

application RMCmem         The MemTool Package

and you have all for memory.

Example:
memps ang gui programms.
0
 
LVL 2

Author Comment

by:k_suchdeva
ID: 7145801
I only want a simple script.

Can not install third party packages.
0
 
LVL 4

Expert Comment

by:Otetelisanu
ID: 7148515
what is your Solaris ???

2.6, or 2.7 or 2.8 or 2.9
0
 
LVL 2

Author Comment

by:k_suchdeva
ID: 7148554
uname -a
SunOS XXXXXXX 5.7 Generic_106541-16 sun4u sparc SUNW,Ultra-80


0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 51

Expert Comment

by:ahoffmann
ID: 7148716
not for a specified process, but probably worth looking at it:
   man sar
0
 

Expert Comment

by:emrehayretci
ID: 7153417
Improve the following script. It should help you.


frequency=3

while (true); do
STATUS=`vmstat $frequency 3| awk -e '{print $5}'`
LASTSTATUS=`echo $STATUS| awk -e '{print $5}'`
if [ $LASTSTATUS -lt <put your trshhold>]; then
    echo "TreshHold Exceeds"
    <write want to want to do in here(i.e. send mail to
    someone)>
fi

done
0
 
LVL 2

Accepted Solution

by:
blowfish earned 100 total points
ID: 7218950
Hi,  

The answer proposed by emrehayretci, uses the vmstat command to display the machine's virual memory and CPU statistics.  If you want to monitor resource usage of a process, then you need to use the ps command.  So, here is a solution to your question.  You could read the man page for ps, for more information regarding the command line options.  

I'm not sure what you mean by <process handle>, so I'm going to assume that you meant process-id, or, PID.  Memory usage can be reported as a process's total virtual size (VSZ), or it's current resident memory size (RSS).  I will assume that you want to monitor it's total virtual size.  

To get a detailed (-f for full) list of all (-e for everything) processes on the machine, use the command:

  ps -ef

This listing however does not give you the memory usage that you are looking for, so, you'll have to use the -o command line option.  The -o command line option tells ps what colums to display in it's output.  You will need to use the following command to list all processes, the user, the process-id, the virtual size (kb), and the command-line arguments:

  ps -e -o user,pid,vsz,args

Now that you can list the information, we need to wrap it in a script that can extract the details of the processes that you are interested in.  

For something like this, I usually use awk.  Awk is a natural when it comes to selecting data that is presented in columns.  Let's say that you want to monitor all of root's processes, and report on any that are bigger than 1024kb:

while :
do
  ps -e -o user,pid,vsz,args | awk '$1 ~ /root/ && $3 > 1024 {print}'
  echo "  sleeping ..."
  sleep 30
done

Hope this helps,

--frankf
0
 
LVL 20

Expert Comment

by:tfewster
ID: 8003363
No comment has been added lately, so it's time to clean up this Topic Area.
I will leave a recommendation for this question in the Cleanup topic area as follows:

- Answered by blowfish

Please leave any comments here within the next 7 days

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

tfewster
Cleanup Volunteer
0
 

Expert Comment

by:SpideyMod
ID: 8065839
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now