Manuel_accu

Well-Known Member
Jun 19, 2005
191
0
166
Hello,

I have found this script from below mention URL: Linux Web Admin

I want some more suggestion and modification on this script to make it batter.

Some useful script for yoru server maintenance:

Script which will send you email alert when disk space usage is more than 70%:
----------------------------------------------------------------------------------------------


#!/bin/bash
usage=`df -h | awk '{print $5}' | sed -e N -e 's/\n/ /' | awk '{print $2}' | tr -d % -s "\n"`
devnm=`df -h | awk '{print $1}' | sed -e N -e 's/\n/ /' | awk '{print $2}' | tr -s "\n"`
str="============================="

if [ $usage -ge 70 ]; then
info="Disk usage for $devnm is more than 70% , Current Disk usage is $usage % "
echo -e "$str\n$info\n$str" |mail -s "Alert: Disk Usage for `hostname` on `date`" [email protected]

else
info="Disk Usage is $usage% for $devnm"
# echo -e "$str\n$info\n$str" | mail -s "Alert: Disk Usage for `hostname` on `date`" [email protected]
fi
------------------------------------------------------------------------------------------------
Script which will send you email alert when swapIO us more than 100 MB:
---------------------------------------------------------------------------------------------------


#!/bin/bash
io=`free -m | awk '{print $3}' | sed '1,3 d'`
info1="SwapIO is : $io MB"
str="============================="

if [ $io -ge 100 ]; then
info="SwapIO is more than 100 MB"
echo -e "$str\n$info\n$info1\n$str" | mail -s "Alert: SwapIO for `hostname` on `date`" [email protected]
else
# echo -e "$str\n$info1\n$str" | mail -s "Alert: SwapIO for `hostname` on `date`" [email protected]
fi
------------------------------------------------------------------------------------------------
Script which will send you email alert wehn load average is more than 5:
-----------------------------------------------------------------------------------------------


#!/bin/bash
avg=`uptime | awk '{print $8" " $9 " "$10 $11 $12 }' | tr -s , " "`
cur=`uptime | awk '{print $10}' | tr -d , | cut -d. -f1`
str="============================="
info="Curent $avg"

if [ $cur -ge 5 ]; then
info1="Server load is high presently"
echo -e "$str\n$info\n$info1\n$str" | mail -s "Alert: Load Average for `hostname` on `date`" [email protected]
else
# echo -e "$str\n$info\n$str"
fi
-----------------------------------------------------------------------------------------------
 

Anishts

Active Member
Oct 6, 2005
37
0
156
Hi

Thanks buddy... Good scripts...
 

WebScHoLaR

Well-Known Member
Dec 14, 2005
508
3
168
Planet Earth
disk space usage is quite useful but it will be more useful if it will show the disk usage of of each drive seperately e.g

/home is 70%
/usr is 89%
/var is 88%
/tmp is 88%

As these are three most important drives.
 

Manuel_accu

Well-Known Member
Jun 19, 2005
191
0
166
I have edited this file and added the top cpu consuming processed when load average is high and top memory consuming process when swapio is high:

Script which will send you email alert when load average is more than 5:
(Includes top cpu consume process in mail)



#!/bin/bash
avg=`uptime | awk '{print $8" " $9 " "$10 $11 $12 }' | tr -s , " "`
cur=`uptime | awk '{print $10}' | tr -d , | cut -d. -f1`
str="============================="
info="Curent $avg"

if [ $cur -ge 5 ]; then
info1="Server load is high presently"
touch /tmp/tmp.00
echo -e "$str\n$info\n$info1\n$str\n" >> /tmp/tmp.00
ps aux | head -1 >> /tmp/tmp.00
ps aux | sort -rn +2 | head -10 >> /tmp/tmp.00;
mail -s "Alert: Load Average for `hostname` on `date` " [email protected] < /tmp/tmp.00;
rm -f /tmp/tmp.00
else
echo -e "$str\n$info\n$str"
fi
Script which will send you email alert when swapIO us more than 100 MB
(Includes top memory usage process details in mail)


#!/bin/bash
io=`free -m | awk '{print $3}' | sed '1,3 d'`
info1="SwapIO is : $io MB"
str="============================="

if [ $io -ge 100 ]; then
info="SwapIO is more than 100 MB"
echo -e "$str\n$info\n$info1\n$str\n\n" >> /tmp/tmp.00
ps aux | head -1 >>/tmp/tmp.00
ps aux | sort -rn +3 | head >> /tmp/tmp.00
mail -s "Alert: SwapIO for `hostname` on `date`" [email protected] -c [email protected] < /tmp/tmp.00
rm -f /tmp/tmp.00
#else
# echo -e "$str\n$info1\n$str" | mail -s "Alert: SwapIO for `hostname` on `date`" [email protected] -c [email protected]
fi
I am developing scrpt to include all partition of hard disk in disk usage, when I have developed this script at that time I have only / one partition and based on it, I have created.

thanks,
 

webicom

Well-Known Member
PartnerNOC
Mar 30, 2004
59
2
158
Slovenia
Hello all,

Good scripts but could anyone meke script and post it here, script should restart apache if load is more than 10 (10 should be able to change). Also good script could be simple script that would kill PID if PID would be responsible for high load if that PID is owned by ordinary user not root or system user. I do not have enough knowledge to make them my self but I think that those scripts would benefit to all cpanel server admins.
At the moment my knowledge to kill PID is only by PID nomber but I would like to know how to kill PIDs by surten user or eaven by killing command that PID or PIDs are linked.

Lat say that user myname is runing more commands with PID 1898, 1830, 1893... and the command of that PID is php in ssh it should look something like this:

PID USER .. .................................%CPU.............COMAND
1898 myname 72,4 php
1830 myname 20,1 php
1893 myname 8,2 php

It would be nice to have script that monitors if any user like myname is making high load fore more than let say one minute or soo script would kill all PIDs by that user. I know that such script would be dificult to make coze it is not necessarie tha only myname is to blame for high load but in most cases is if user was hacked and in hes name hackers run some script that makes server high load. I hope that you understand what Im trying to say and that out ther is someone who could help us.


Regards, Erik
 

Manuel_accu

Well-Known Member
Jun 19, 2005
191
0
166
The scripts posted by me are for very basic level and I have asked for suggestion

Good scripts but could anyone meke script and post it here, script should restart apache if load is more than 10 (10 should be able to change). Also good script could be simple script that would kill PID if PID would be responsible for high load if that PID is owned by ordinary user not root or system user. I do not have enough knowledge to make them my self but I think that those scripts would benefit to all cpanel server admins.
At the moment my knowledge to kill PID is only by PID nomber but I would like to know how to kill PIDs by surten user or eaven by killing command that PID or PIDs are linked.
I think below tool will match all your requirement and this is very handy and wonderful tool

http://rfxnetworks.com/sim.php

thx
 

webicom

Well-Known Member
PartnerNOC
Mar 30, 2004
59
2
158
Slovenia
Thanx for your answer. Today I have installed chirpy csf: v2.21 tool and I must say that it looks very good. It looks like this script will do the trick for now.

Regards, Erik
 

mctDarren

Well-Known Member
Jan 6, 2004
665
9
168
New Jersey
cPanel Access Level
Root Administrator
Manuel_accu said:
Script which will send you email alert wehn load average is more than 5
Edited to run in a cron job every few minutes:
Code:
#!/bin/bash
avg=`uptime | awk '{print $8" " $9 " "$10 $11 $12 }' | tr -s , " "`
cur=`uptime | awk '{print $10}' | tr -d , | cut -d. -f1`
str="============================="
info="Current $avg"

if [ $cur -ge 5 ]; then
  info1="Server load is high!"
  touch /tmp/tmp.00
  echo -e "$str\n$info\n$info1\n$str\n" >> /tmp/tmp.00
  ps aux | head -1 >> /tmp/tmp.00
  ps aux | sort -rn +2 | head -10 >> /tmp/tmp.00;
  mail -s "Alert: Load Average for `hostname` on `date` " root < /tmp/tmp.00;
  rm -f /tmp/tmp.00
fi
When it actually triggers (during backups) I get an error in the email sent:
/scripts/loadcheck: line 7: [: average:: integer expression expected
Wonder why it's not working?
 

Manuel_accu

Well-Known Member
Jun 19, 2005
191
0
166
I am using the same code and it is working fine for me. I am using RedHat ES 4 but it will also work with the 3.

I had faced the same error but I already corrected that and then posted it in forums.

May I know which OS you have?

else you just need to check the conditional paraeter of if then fi

or try # sh -x load.sh

-
just pss the command

# uptime | awk '{print $10}' | tr -d , | cut -d. -f1

if you are receiving floating value then you have to modify your script to get integer value.

thx
 

chilihost

Well-Known Member
Mar 1, 2005
72
0
156
Here's another great little tip, to get notified any time the server is accessed by root via ssh, edit the file called .bash_profile in the root directory and add this line at the end:

Code:
echo 'ALERT - Root Shell Access on:' `date` `who` | mail -s "Alert: Root Access from `who | awk '{print $6}'`" [email protected]
 

mctDarren

Well-Known Member
Jan 6, 2004
665
9
168
New Jersey
cPanel Access Level
Root Administrator
Manuel_accu said:
May I know which OS you have?
Centos 3.8

Manuel_accu said:
just pss the command

# uptime | awk '{print $10}' | tr -d , | cut -d. -f1

if you are receiving floating value then you have to modify your script to get integer value.
Hmm, how can I test in shell for a floating point or an int? Time to google.. :)
 

Manuel_accu

Well-Known Member
Jun 19, 2005
191
0
166
WebScHoLaR said:
disk space usage is quite useful but it will be more useful if it will show the disk usage of of each drive seperately e.g

/home is 70%
/usr is 89%
/var is 88%
/tmp is 88%

As these are three most important drives.
I think below code will do the trick for you. Please check it

loop=`df -h | sed "1 d" | wc -l`

while [ 0 -lt "$loop" ]
do

usage=`df -h |sed "1 d" |sed -n "$loop s/%//p" | awk '{print $5}'`

if [ $usage -ge 1 ]; then

prnt=`df -h |sed "1 d" |sed -n " $loop s/\///p" | awk '{print $6 " -> " $5}'`
mail -s "Alert: Disk usage for `hostname` on `date` " [email protected] $print

fi
loop=`expr $loop - 1`
done
 

mctDarren

Well-Known Member
Jan 6, 2004
665
9
168
New Jersey
cPanel Access Level
Root Administrator
Manuel_accu said:
What is the resoult of below command:

#uptime | awk '{print $10}' | tr -d , | cut -d. -f1

if it is int then your script should work just fine! else you have to dig it
The result is a "0", but how do I know if it's returning an actual integer or if it's treated as a string? Or does shell not work that way? So used to C, Javascript and PHP I think of everything in terms of OOP languages. :)
 

mctDarren

Well-Known Member
Jan 6, 2004
665
9
168
New Jersey
cPanel Access Level
Root Administrator
Think I solved the problem by wrapping it like a numeric expression:
Code:
if [ $(($cur)) -ge 5 ]; then
One of the guys here is also asking if $cur shouldn't be wrapped in quotes, just in case the var is empty for some reason? Like this:
Code:
if [ $(("$cur")) -ge 5 ]; then
As you can see, we don't write bash scripts often! ;)
 

mctDarren

Well-Known Member
Jan 6, 2004
665
9
168
New Jersey
cPanel Access Level
Root Administrator
Well, thought I had solved the problem and the script seemed to be working fine - except the last couple of days I get this in my email:
/scripts/loadcheck: line 7: average: + 0: syntax error in expression (error token is ": + 0")
Here's my modified script:
Code:
#!/bin/bash
avg=`uptime | awk '{print $8" " $9 " "$10 $11 $12 }' | tr -s , " "`
cur=`uptime | awk '{print $10}' | tr -d , | cut -d. -f1`
str="============================="
info="Current $avg"

  if [ $(($cur + 0)) -ge 2 ]; then
    info1="Server load is high!"
    touch /tmp/tmp.00
    echo -e "$str\n$info\n$info1\n$str\n" >> /tmp/tmp.00
    ps aux | head -1 >> /tmp/tmp.00
    ps aux | sort -rn +2 | head -10 >> /tmp/tmp.00;
    mail -s "Alert: Load Average for `hostname` on `date` " root < /tmp/tmp.00;
    rm -f /tmp/tmp.00
  fi

exit 0
I believe that error message is telling me that the value of $cur is not always a number, it seems to be a string for some reason with the word "average:" in there. This only seems to occur during backups when load goes pretty high. I think I'll modify the script again tonight so that it emails me the values of both $avg and $cur. Hopefully I can get a handle on what's happening. :)