The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Useful Script for Sys Admin

Discussion in 'General Discussion' started by Manuel_accu, Aug 30, 2006.

  1. Manuel_accu

    Manuel_accu Well-Known Member

    Joined:
    Jun 19, 2005
    Messages:
    191
    Likes Received:
    0
    Trophy Points:
    16
    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%:
    ----------------------------------------------------------------------------------------------


    ------------------------------------------------------------------------------------------------
    Script which will send you email alert when swapIO us more than 100 MB:
    ---------------------------------------------------------------------------------------------------


    ------------------------------------------------------------------------------------------------
    Script which will send you email alert wehn load average is more than 5:
    -----------------------------------------------------------------------------------------------


    -----------------------------------------------------------------------------------------------
     
  2. websupport

    websupport Well-Known Member

    Joined:
    Jun 24, 2006
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    6
    Thanks

    Thanks for the scripts :)

    Very Helpful scripts. Thanks mate

    :D
     
  3. Anishts

    Anishts Active Member

    Joined:
    Oct 6, 2005
    Messages:
    37
    Likes Received:
    0
    Trophy Points:
    6
    Hi

    Thanks buddy... Good scripts...
     
  4. WebScHoLaR

    WebScHoLaR Well-Known Member

    Joined:
    Dec 14, 2005
    Messages:
    511
    Likes Received:
    2
    Trophy Points:
    18
    Location:
    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.
     
  5. Manuel_accu

    Manuel_accu Well-Known Member

    Joined:
    Jun 19, 2005
    Messages:
    191
    Likes Received:
    0
    Trophy Points:
    16
    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)



    Script which will send you email alert when swapIO us more than 100 MB
    (Includes top memory usage process details in mail)


    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,
     
  6. WebScHoLaR

    WebScHoLaR Well-Known Member

    Joined:
    Dec 14, 2005
    Messages:
    511
    Likes Received:
    2
    Trophy Points:
    18
    Location:
    Planet Earth
    I really appreciate it. Do share with us once you complete that script.
     
  7. webicom

    webicom Well-Known Member

    Joined:
    Mar 30, 2004
    Messages:
    54
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    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
     
  8. Manuel_accu

    Manuel_accu Well-Known Member

    Joined:
    Jun 19, 2005
    Messages:
    191
    Likes Received:
    0
    Trophy Points:
    16
    The scripts posted by me are for very basic level and I have asked for suggestion

    I think below tool will match all your requirement and this is very handy and wonderful tool

    http://rfxnetworks.com/sim.php

    thx
     
  9. webicom

    webicom Well-Known Member

    Joined:
    Mar 30, 2004
    Messages:
    54
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    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
     
  10. Manuel_accu

    Manuel_accu Well-Known Member

    Joined:
    Jun 19, 2005
    Messages:
    191
    Likes Received:
    0
    Trophy Points:
    16
    Ya this one is also a good product...!!
     
  11. mctDarren

    mctDarren Well-Known Member

    Joined:
    Jan 6, 2004
    Messages:
    664
    Likes Received:
    2
    Trophy Points:
    18
    Location:
    New Jersey
    cPanel Access Level:
    Root Administrator
    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:
    Wonder why it's not working?
     
  12. Manuel_accu

    Manuel_accu Well-Known Member

    Joined:
    Jun 19, 2005
    Messages:
    191
    Likes Received:
    0
    Trophy Points:
    16
    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
     
  13. chilihost

    chilihost Well-Known Member

    Joined:
    Mar 1, 2005
    Messages:
    72
    Likes Received:
    0
    Trophy Points:
    6
    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}'`" you@yourhost.com
    
     
  14. mctDarren

    mctDarren Well-Known Member

    Joined:
    Jan 6, 2004
    Messages:
    664
    Likes Received:
    2
    Trophy Points:
    18
    Location:
    New Jersey
    cPanel Access Level:
    Root Administrator
    Centos 3.8

    Hmm, how can I test in shell for a floating point or an int? Time to google.. :)
     
  15. Manuel_accu

    Manuel_accu Well-Known Member

    Joined:
    Jun 19, 2005
    Messages:
    191
    Likes Received:
    0
    Trophy Points:
    16
    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
     
  16. Manuel_accu

    Manuel_accu Well-Known Member

    Joined:
    Jun 19, 2005
    Messages:
    191
    Likes Received:
    0
    Trophy Points:
    16
    I think below code will do the trick for you. Please check it

     
  17. mctDarren

    mctDarren Well-Known Member

    Joined:
    Jan 6, 2004
    Messages:
    664
    Likes Received:
    2
    Trophy Points:
    18
    Location:
    New Jersey
    cPanel Access Level:
    Root Administrator
    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. :)
     
  18. Manuel_accu

    Manuel_accu Well-Known Member

    Joined:
    Jun 19, 2005
    Messages:
    191
    Likes Received:
    0
    Trophy Points:
    16
    return 0 means you can consider it as a string and script also should work fine.
     
  19. mctDarren

    mctDarren Well-Known Member

    Joined:
    Jan 6, 2004
    Messages:
    664
    Likes Received:
    2
    Trophy Points:
    18
    Location:
    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! ;)
     
  20. mctDarren

    mctDarren Well-Known Member

    Joined:
    Jan 6, 2004
    Messages:
    664
    Likes Received:
    2
    Trophy Points:
    18
    Location:
    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:
    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. :)
     
Loading...

Share This Page