Memory check script

Hi all,

OS: Solaris 10

I'm trying to put together a script to check memory utilisation.

The command I'm running is:

ps -ef -o pmem -o pid -o rss -o vsz -o args -o user | grep 2 | grep -v "VSZ COMMAND" | sort | tail -10 | sort -r

And the output looks something like this:

==============================
2.4 1103 761936 1357176 PSAPPSRV -C dom=FSPRD5_56906 -g 99 -i 8 -u syd0738 -U /psoft/fs/fsprd/psft/pt/8 fsprd
2.0 4744 636944 758368 JSH -c 41 -i 5 -s 96 -S 1320 fsprd
1.7 27412 540088 1119624 PSAPPSRV -C dom=FSPRD3_38073 -g 99 -i 6 -u syd0738 -U /psoft/fs/fsprd/psft/pt/8 fsprd
1.7 4617 539912 965504 PSSUBHND -C dom=FSPRDIB_65215 -g 98 -i 302 -u syd0738 -U /psoft/fs/fsprd/psft/p fsprd
1.6 29539 498288 536760 PSAPPSRV -C dom=FSPRD5_56906 -g 99 -i 1 -u syd0738 -U /psoft/fs/fsprd/psft/pt/8 fsprd
1.6 29282 498096 536752 PSAPPSRV -C dom=FSPRD5_56906 -g 99 -i 7 -u syd0738 -U /psoft/fs/fsprd/psft/pt/8 fsprd
1.6 25877 525032 1103712 PSAPPSRV -C dom=FSPRD5_56906 -g 99 -i 9 -u syd0738 -U /psoft/fs/fsprd/psft/pt/8 fsprd
1.6 6227 497384 528568 PSAPPSRV -C dom=FSPRD1_38536 -g 99 -i 2 -u syd0738 -U /psoft/fs/fsprd/psft/pt/8 fsprd
1.6 4614 514544 932736 PSSUBHND -C dom=FSPRDIB_65215 -g 98 -i 301 -u syd0738 -U /psoft/fs/fsprd/psft/p fsprd
1.5 5798 469792 512208 PSQRYSRV -C dom=FSPRD3_38073 -g 99 -i 72 -u syd0738 -U /psoft/fs/fsprd/psft/pt/ fsprd

What I know to know is what command I can use so that if the first column is greater than 3.0 i.e. -gt 3.0 (which represents % memory usage), then satisfy a condition.

Looking forward to hearing some suggestions. Something written for korn shell would be perfect.

Thanks in advance.

Assuming you are running Kshell you could pipe it into a while like this:

ps -ef -o pmem -o pid -o rss -o vsz -o args -o user | grep 2 | grep -v "VSZ COMMAND" | sort | tail -10 | sort -r | while read used rest
do
     if (( $used > 3.0 ))
     then
         echo "$rest"
     fi
done

Bash throws an error when I attempt something like this, so you'll probably have to do something like the following if you must use bash:

ps -ef -o pmem -o pid -o rss -o vsz -o args -o user | grep 2 | grep -v "VSZ COMMAND" | sort | tail -10 | sort -r | while read used rest
do
     if (( ${used/./} > 30 ))       # replace . with nil and compare to threshold * 10
     then
         echo "$rest"
     fi
done
1 Like

Thanks agama. I'm using Korn so gave the first solution a try but it did not work:

#!/bin/ksh -x
ps -ef -o pmem -o pid -o rss -o vsz -o args -o user | grep 2 | grep -v "VSZ COMMAND" | sort | tail -10 | sort -r | while read used rest
do
     if (( $used > 3.0 ))
     then
         echo "$rest"
     fi
done

The result of the above script is:

syd0738:fsprd:/tmp> ./dt.sh
+ grep 2
+ ps -ef -o pmem -o pid -o rss -o vsz -o args -o user
+ grep -v VSZ COMMAND
+ read used rest
+ tail -10
+ sort
+ sort -r
+ (( 2.4 > 3.0 ))
+ read used rest
+ (( 2.0 > 3.0 ))
+ read used rest
+ (( 1.7 > 3.0 ))
+ read used rest
+ (( 1.7 > 3.0 ))
+ read used rest
+ (( 1.6 > 3.0 ))
+ read used rest
+ (( 1.6 > 3.0 ))
+ read used rest
+ (( 1.6 > 3.0 ))
+ read used rest
+ (( 1.6 > 3.0 ))
+ read used rest
+ (( 1.5 > 3.0 ))
+ read used rest
+ (( 1.5 > 3.0 ))
+ read used rest

Any clues?

Looks to me like none of your values are greater than 3.0, so nothing is being printed. Test by setting 3.0 to 1.5 or somesuch and see if there is a difference.

Alright. It looks like it doesn't take into account the percentage point.

For example, I tried with ">1.0", and the only row that echoed was a result that was "2.0".

Rows of for example "1.6, 1.7" did not echo.

This is not a big deal as I'm happy to be notified with any results of 3 and above.

Let me play around with the command and will let you know how I go.

Cheers.

Update from your own code.

ps -ef -o pmem -o pid -o rss -o vsz -o args -o user |awk '!/VSZ COMMAND/&&/2/&&$1>3.0' |sort -r |head -10
1 Like

Thanks agama and rdcwayx.

Both your solutions worked a treat.

Cheers guys.

Hi guys, sorry to resurrect this thread but I have an extended request to the previous solution.

Currently the code looks like this:

ps -ef -o pmem -o pid -o rss -o vsz -o args -o user | grep 2 | grep -v "VSZ COMMAND" | sort | tail -10 | sort -r | while read used rest
do
     if (( $used >= 3.0 ))
     then
         echo "Top 10 Memory processes" >> $LOG
         echo " " >> $LOG
         echo "%MEM   PID  RSS  VSZ COMMAND                                                                              USER" >> $LOG 
         echo ps -ef -o pmem -o pid -o rss -o vsz -o args -o user | grep "VSZ COMMAND" | grep -v grep; ps -ef -o pmem -o pid -o rss -o vsz -o args -o user | grep -v "VSZ COMMAND" | sort | tail -10 | sort -r >> $LOG
         echo " " >> $LOG
         echo "Please investigate above Unix process(es) that have greater than 3.0% memory utilisation." >> $LOG
         mailx -s "Memory WARNING! - $HOST "  < /star/fs/logs/mon_memory_$HOST.log
     exit 1    
     else
         echo "Server Memory OK" >> $LOG
     exit 0
     fi
done

And the output looks something like this:

MEM PID RSS VSZ COMMAND USER
39.6 18776 12947256 13005008 PSQRYSRV -C dom=FSPRD4_50390 -g 99 -i 72 -u syd0739 -U /psoft/fs/fsprd/psft/pt/ fsprd
2.2 28370 715432 1320800 PSAPPSRV -C dom=FSPRD2_60674 -g 99 -i 5 -u syd0739 -U /psoft/fs/fsprd/psft/pt/8 fsprd

What I would like to implement now in the script is when it satisfies "$used >=3.0" as per the code, perform a "kill" against the pid when it is a "PSQRYSRV" which is >=3.0.

Thanks in advance.

cat infile

MEM PID RSS VSZ COMMAND USER
39.6 18776 12947256 13005008 PSQRYSRV -C dom=FSPRD4_50390 -g 99 -i 72 -u syd0739 -U /psoft/fs/fsprd/psft/pt/ fsprd
2.2 28370 715432 1320800 PSAPPSRV -C dom=FSPRD2_60674 -g 99 -i 5 -u syd0739 -U /psoft/fs/fsprd/psft/pt/8 fsprd

awk '$1>3.0&&$5=="PSQRYSRV"{print $2}' infile |xargs kill 

I will give it a shot and see how I go.

Thanks.