i want to write a script that checks the memory usage of processes and send a mail with the name of the process witch is using more then 300mb RAM.
dose anybody have a sample script or an idea how i can make it ?
PROCCESSES="snmpd sendmail"
for myVar in $PROCCESSES
do
var1=$(ps -eo fname,rss | grep $myVar |awk '{print $2}'| awk '$1 > 200000')
echo $var1
if [ -n $var1 ]; then
echo "test1 mail later on"
else
echo "test2 mail later on"
fi
done
Doing the SMTP dialog on your own with telnet is error-prone and brittle; are you sure you're not better of passing the message to a proper MTA?
Just like in the Sendmail case, you should probably put in some headers in the message. Maybe your MTA accepts it without the headers, but they really are supposed to be required.
Note also the fix to the Useless Use of grep | awk | awk
ps -eo fname,rss | awk '!/RSS/{ if ($2 > 350000) print $1}' >/tmp/tempfile
test -s /tmp/tempfile &&
(
echo HELO test.com
sleep 2
echo "mail from:<test@mail.com>"
sleep 2
echo "RCPT TO:<mailto@server.com>"
sleep 2
echo DATA
sleep 2
cat <<HERE
From: test@mail.com
To: mailto@server.com
Subject: Processes taking more than 350MB
These processes are taking more than 350MB RAM.
Please check it.
HERE
cat /tmp/tempfile
echo .
echo QUIT
sleep 2) | telnet 1.*.*.* 25
rm /tmp/tempfile
Thank you very much for your help era, i am using SMTP telnet because i am very limited on the machine ( i can not configure the sendmail file).
do you know an example how to use MTA?