Find the answer to your Linux question:
Results 1 to 6 of 6
Hello, I am attempting to write a very basic script that monitors a server's load and automatically emails an administrator upon the load reaching a certain threshold... So far I ...
  1. #1
    Just Joined!
    Join Date
    Mar 2007
    Posts
    20

    Bash script to monitor server load, mail from command line

    Hello,

    I am attempting to write a very basic script that monitors a server's load and automatically emails an administrator upon the load reaching a certain threshold...

    So far I have:

    #load=`uptime | awk -F ' ' {'print $10'} | cut -d ',' -f1 | awk -F '.' {'print $1'}`
    if [ $load -gt 4 ];
    then
    mail -s "WARNING: Server Load Threshold Reached" $EMAILADDRESS
    fi
    exit 0

    I'm running into a few problems, however...

    1.) `uptime | awk -F ' ' {'print $10'} | cut -d ',' -f1 | awk -F '.' {'print $1'}` -- the output from this command results in a decimal figure, so when that value is parsed and placed in an if statement, the value is not seen as a number.

    load-monitor.sh: line 9: [: 4.96: integer expression expected

    How can I allow a number like "4.56" to be seen as an actual number within the if statement and be compared to 4, for instance?

    2.) I am running a cPanel server on CentOS 5.4 -- when I run mail -s "SUBJECT" $EMAILADDRESS the command just hangs, and stracing the process shows it stuck on a read syscall.

    3.) If I wanted to write this script in PHP, I have one primary confusion -- how can I mimic the functionality of obtaining the output from uptime via awk, etc. so that I can determine what the server load is at a given time? Which PHP function(s) would assist in that regard?

  2. #2
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,099
    Is writing the script yourself mandatory?

    If not, there are enough monitoring solution for these kind of problems.
    One that is quite simple to setup is Munin - Trac

    You can do email alerting
    HowToContact - Munin - Trac

    and you have these nice rrd graphs, that show load/etc over time and therefore helping in getting a better overview of the system.
    Munin :: ping.uio.no :: camilla.ping.uio.no


    (for example: "Why is there a IO spike every night at 23:00" Ah, backup starts)
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    Mar 2007
    Posts
    20
    Hello,

    Well, to me it's necessary simply for the educational aspect.

    But thanks for the information.

  4. #4
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    Munin is a great tool, unfortunately it only produces graphs and you cannot store the actual data. Moreover, you cannot tweak the 5 min sampling interval.

    For agntsgotnosecret problem, you can compare the load value as follows :
    Code:
    res=$(bc <<EOF
    $load > 4
    EOF
    )
    The comparison result will be 0 or 1, self-explicitly.
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  5. #5
    Just Joined!
    Join Date
    Mar 2007
    Posts
    20
    <code>res=$(bc <<EOF
    $load > 4
    EOF
    )
    </code>

    Pardon my ignorance, but where exactly would you insert code into my script?

  6. #6
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    Code:
    load=`uptime | awk -F ' ' {'print $10'} | cut -d ',' -f1 | awk -F '.' {'print $1'}`
    res=$(bc <<EOF
    $load > 4
    EOF
    )
    if [ "$res" = "1" ];
    then
    mail -s "WARNING: Server Load Threshold Reached" $EMAILADDRESS <<EOF
    EOF
    fi
    exit 0
    Here Documents
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...