[cod] script cpu load

Discussion in 'Call of Duty series newsletter' started by pet, Apr 5, 2010.

  1. pet

    pet Guest

    Hi

    Has anyone of You script which monitor the cpu usage of the users
    processes, and if find one which reaches 100% during 5 sec it will kill
    it? Any help would be great.
    _______________________________________________
    cod mailing list
    cod (AT) org
    http://icculus.org/mailman/listinfo/cod
     
  2. */5 * * * * /usr/bin/process_monitor.sh & >/dev/null 2>&1

    cat /usr/bin/process_monitor.sh
    #!/bin/bash

    # Disabled during maintenance 4 a.m. to 5 a.m.

    hour=`date +%H`
    if [ "$hour" = "04" ];
    then
    exit 0
    fi

    # This script assumes your top configuration is default.

    # Who to e-mail when a process is killed.

    contact="email (AT) com"

    # Define the top command based load threshold percentage.

    load="85"

    # Define location of appending log file.

    archive="/var/log/killed.log"

    # Cycle 1 -- Checking to see if a process is using too much CPU.

    cycle1=(`top -c -b -n 1 | grep -v grep | grep -v root | grep -v steam |
    grep -v bin | grep -v daemon | grep -v lp | grep -v sync | grep -v
    shutdown | grep -v halt | grep -v mail | grep -v news | grep -v uucp |
    grep -v operator | grep -v games | grep -v gopher | grep -v ftp | grep
    -v nobody | grep -v dbus | grep -v vcsa | grep -v rpm | grep -v
    haldaemon | grep -v netdump | grep -v nscd | grep -v sshd | grep -v rpc
    | grep -v rpcuser | grep -v nfsnobody | grep -v mailnull | grep -v smmsp
    | grep -v pcap | grep -v xfs | grep -v Mem: | grep -v Swap: | grep -v
    Cpu23 | grep -v Cpu22 | grep -v Cpu21 | grep -v Cpu20 | grep -v Cpu19 |
    grep -v Cpu18 | grep -v Cpu17 | grep -v Cpu16 | grep -v Cpu15 | grep -v
    Cpu14 | grep -v Cpu13 | grep -v Cpu12 | grep -v Cpu11 |grep -v Cpu10 |
    grep -v Cpu9 | grep -v Cpu8 | grep -v Cpu7 | grep -v Cpu6 | grep -v Cpu5
    | grep -v Cpu4 | grep -v Cpu3 | grep -v Cpu2 | grep -v Cpu1 | grep -v
    Cpu0 | grep -v Tasks | grep -v top | awk '$9 >= '$load' { print $1 }' |
    tail -n 1`)

    # If nothing is found, exit.

    if [ -z "$cycle1" ]; then

    exit 0

    fi

    # Pausing for a bit. The process may be just having a temporary spike
    due to a restart or map change.

    sleep 60

    # Cycle 2 -- Checking again to see if the process is still using too
    much CPU.

    cycle2=(`top -c -b -n 1 | grep -v grep | grep -v root | grep -v steam |
    grep -v bin | grep -v daemon | grep -v lp | grep -v sync | grep -v
    shutdown | grep -v halt | grep -v mail | grep -v news | grep -v uucp |
    grep -v operator | grep -v games | grep -v gopher | grep -v ftp | grep
    -v nobody | grep -v dbus | grep -v vcsa | grep -v rpm | grep -v
    haldaemon | grep -v netdump | grep -v nscd | grep -v sshd | grep -v rpc
    | grep -v rpcuser | grep -v nfsnobody | grep -v mailnull | grep -v smmsp
    | grep -v pcap | grep -v xfs | grep -v Mem: | grep -v Swap: | grep -v
    Cpu23 | grep -v Cpu22 | grep -v Cpu21 | grep -v Cpu20 | grep -v Cpu19 |
    grep -v Cpu18 | grep -v Cpu17 | grep -v Cpu16 | grep -v Cpu15 | grep -v
    Cpu14 | grep -v Cpu13 | grep -v Cpu12 | grep -v Cpu11 |grep -v Cpu10 |
    grep -v Cpu9 | grep -v Cpu8 | grep -v Cpu7 | grep -v Cpu6 | grep -v Cpu5
    | grep -v Cpu4 | grep -v Cpu3 | grep -v Cpu2 | grep -v Cpu1 | grep -v
    Cpu0 | grep -v Tasks | grep -v top | awk '$9 >= '$load' { print $1 }' |
    tail -n 1`)


    # Comparing to see if the same process is using too much CPU, if so kill
    the process, if not, exit the script.

    if [ "$cycle1" = "$cycle2" ];

    then

    # Logs killed process

    top -c -b -n 1 | grep "$cycle2" | grep -v grep | head -n 1 >> $archive

    # E-mail killed process

    top -c -b -n 1 | grep "$cycle2" | grep -v grep | head -n 1 | mail -s
    "Excessive CPU Process killed at $HOSTNAME" $contact

    # Kill the process

    kill -9 "$cycle2"

    fi

    exit 0

    #EOF

    :)

    On 4/5/2010 5:20 PM, pet wrote:
    > Hi
    >
    > Has anyone of You script which monitor the cpu usage of the users
    > processes, and if find one which reaches 100% during 5 sec it will kill
    > it? Any help would be great.
    > _______________________________________________
    > cod mailing list
    > cod (AT) org
    > http://icculus.org/mailman/listinfo/cod
    >

    _______________________________________________
    cod mailing list
    cod (AT) org
    http://icculus.org/mailman/listinfo/cod
     
  3. If you are looking at monitoring specific processes, and not just
    looking for /any/ process which exceeds some resource limitations,
    then I highly recommend Monit (http://mmonit.com/monit/) which is very
    easy to configure and incredibly flexible. Example config for Apache:

    check process apache with pidfile /usr/local/apache/logs/httpd.pid
    start program = "/etc/init.d/httpd start" with timeout 60 seconds
    stop program = "/etc/init.d/httpd stop"
    if cpu > 60% for 2 cycles then alert
    if cpu > 80% for 5 cycles then restart
    if totalmem > 200.0 MB for 5 cycles then restart
    if children > 250 then restart
    if loadavg(5min) greater than 10 for 8 cycles then stop
    if failed host www.tildeslash.com port 80 protocol http
    and request "/monit/doc/next.php"
    then restart
    if failed port 443 type tcpssl protocol http
    with timeout 15 seconds
    then restart
    if 3 restarts within 5 cycles then timeout
    depends on apache_bin
    group server

    Cheers,
    Einar

    On Mon, Apr 5, 2010 at 4:20 PM, pet <games (AT) pl> wrote:
    > Hi
    >
    > Has anyone of  You script which monitor the cpu usage of the users
    > processes, and if find one which reaches 100% during 5 sec it will kill it?
    > Any help would be great.
    > _______________________________________________
    > cod mailing list
    > cod (AT) org
    > http://icculus.org/mailman/listinfo/cod
    >

    _______________________________________________
    cod mailing list
    cod (AT) org
    http://icculus.org/mailman/listinfo/cod
     
  4. pet

    pet Guest

    great help, many thanks


    > #!/bin/bash
    >
    > # Disabled during maintenance 4 a.m. to 5 a.m.
    >
    > hour=`date +%H`
    > if [ "$hour" = "04" ];
    > then
    > exit 0
    > fi
    >
    > # This script assumes your top configuration is default.
    >
    > # Who to e-mail when a process is killed.
    >
    > contact="email (AT) com"
    >
    > # Define the top command based load threshold percentage.
    >
    > load="85"
    >
    > # Define location of appending log file.
    >
    > archive="/var/log/killed.log"
    >
    > # Cycle 1 -- Checking to see if a process is using too much CPU.
    >
    > cycle1=(`top -c -b -n 1 | grep -v grep | grep -v root | grep -v steam
    > | grep -v bin | grep -v daemon | grep -v lp | grep -v sync | grep -v
    > shutdown | grep -v halt | grep -v mail | grep -v news | grep -v uucp |
    > grep -v operator | grep -v games | grep -v gopher | grep -v ftp | grep
    > -v nobody | grep -v dbus | grep -v vcsa | grep -v rpm | grep -v
    > haldaemon | grep -v netdump | grep -v nscd | grep -v sshd | grep -v
    > rpc | grep -v rpcuser | grep -v nfsnobody | grep -v mailnull | grep -v
    > smmsp | grep -v pcap | grep -v xfs | grep -v Mem: | grep -v Swap: |
    > grep -v Cpu23 | grep -v Cpu22 | grep -v Cpu21 | grep -v Cpu20 | grep
    > -v Cpu19 | grep -v Cpu18 | grep -v Cpu17 | grep -v Cpu16 | grep -v
    > Cpu15 | grep -v Cpu14 | grep -v Cpu13 | grep -v Cpu12 | grep -v Cpu11
    > |grep -v Cpu10 | grep -v Cpu9 | grep -v Cpu8 | grep -v Cpu7 | grep -v
    > Cpu6 | grep -v Cpu5 | grep -v Cpu4 | grep -v Cpu3 | grep -v Cpu2 |
    > grep -v Cpu1 | grep -v Cpu0 | grep -v Tasks | grep -v top | awk '$9 >=
    > '$load' { print $1 }' | tail -n 1`)
    >
    > # If nothing is found, exit.
    >
    > if [ -z "$cycle1" ]; then
    >
    > exit 0
    >
    > fi
    >
    > # Pausing for a bit. The process may be just having a temporary spike
    > due to a restart or map change.
    >
    > sleep 60
    >
    > # Cycle 2 -- Checking again to see if the process is still using too
    > much CPU.
    >
    > cycle2=(`top -c -b -n 1 | grep -v grep | grep -v root | grep -v steam
    > | grep -v bin | grep -v daemon | grep -v lp | grep -v sync | grep -v
    > shutdown | grep -v halt | grep -v mail | grep -v news | grep -v uucp |
    > grep -v operator | grep -v games | grep -v gopher | grep -v ftp | grep
    > -v nobody | grep -v dbus | grep -v vcsa | grep -v rpm | grep -v
    > haldaemon | grep -v netdump | grep -v nscd | grep -v sshd | grep -v
    > rpc | grep -v rpcuser | grep -v nfsnobody | grep -v mailnull | grep -v
    > smmsp | grep -v pcap | grep -v xfs | grep -v Mem: | grep -v Swap: |
    > grep -v Cpu23 | grep -v Cpu22 | grep -v Cpu21 | grep -v Cpu20 | grep
    > -v Cpu19 | grep -v Cpu18 | grep -v Cpu17 | grep -v Cpu16 | grep -v
    > Cpu15 | grep -v Cpu14 | grep -v Cpu13 | grep -v Cpu12 | grep -v Cpu11
    > |grep -v Cpu10 | grep -v Cpu9 | grep -v Cpu8 | grep -v Cpu7 | grep -v
    > Cpu6 | grep -v Cpu5 | grep -v Cpu4 | grep -v Cpu3 | grep -v Cpu2 |
    > grep -v Cpu1 | grep -v Cpu0 | grep -v Tasks | grep -v top | awk '$9 >=
    > '$load' { print $1 }' | tail -n 1`)
    >
    >
    > # Comparing to see if the same process is using too much CPU, if so
    > kill the process, if not, exit the script.
    >
    > if [ "$cycle1" = "$cycle2" ];
    >
    > then
    >
    > # Logs killed process
    >
    > top -c -b -n 1 | grep "$cycle2" | grep -v grep | head -n 1 >> $archive
    >
    > # E-mail killed process
    >
    > top -c -b -n 1 | grep "$cycle2" | grep -v grep | head -n 1 | mail -s
    > "Excessive CPU Process killed at $HOSTNAME" $contact
    >
    > # Kill the process
    >
    > kill -9 "$cycle2"
    >
    > fi
    >
    > exit 0
    >
    > #EOF


    _______________________________________________
    cod mailing list
    cod (AT) org
    http://icculus.org/mailman/listinfo/cod
     

Share This Page

Loading...