Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


[Script] Stop container if loadavg is > x
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

[Script] Stop container if loadavg is > x

vpsdeployvpsdeploy Member
edited May 2012 in General

Hi,

we have implemented this script in a cron every 15 minutes,
if some container has a high overload, we stop it.

I put here if someone is interested on it.

We have other version that checks the CTID in the SolusVM database and send a email to the customer.

Regards.

#! /bin/bash export PATH="/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin" maxload="10"; # put here the max loadavg permitted cat /dev/null > loads.txt; vzlist -o ctid,laverage > loads.txt; cat loads.txt | while read line; do vm=$(echo ${line:0:5}); load=$(echo ${line} | cut -d'/' -f3); load2=$(echo ${load} | cut -d'.' -f1); if [ $load2 -gt $maxload ] then echo "stopping $vm - $load"; vzctl stop $vm; echo "$vm - $load" | mail -s "$vm stopped for overload" [email protected] fi done

Comments

  • Why are you setting path?

  • @Daniel
    because vzlist in not in the cron shell, you need export.

  • @vpsdeploy said: because vzlist in not in the cron shell, you need export.

    Fair enough.

  • DimeCadmiumDimeCadmium Member
    edited May 2012

    @vpsdeploy said: because vzlist in not in the cron shell, you need export.

    Or... you could use the absolute path to vzlist...? It doesn't really matter, but you don't need to at all.

    Also, you don't need to cat /dev/null >loads.txt because vzlist >loads.txt will truncate it automatically if it already has data.

    Also, you don't need loads.txt anyway. Just "vzlist | while" instead of "vzlist >loads.txt; cat loads.txt | while"

  • @DimeCadmium

    • Also not run with absolute path...
    • loads.txt is empty on cat /dev/null > loads.txt;
    • I prefer to have this datas on a file to analyze in case of issue, but yes, you can do also this ;)
  • yomeroyomero Member

    @vpsdeploy said: loads.txt is empty on cat /dev/null > loads.txt;

    Again, he said this ">" operator does it already, and you don't need that line.

  • DimeCadmiumDimeCadmium Member
    edited May 2012

    @vpsdeploy said: Also not run with absolute path...

    I meant vzlist. /usr/sbin/vzlist -o ctid,laverage (or wherever vzlist is).

    loads.txt is empty on cat /dev/null > loads.txt;

    It is. It is also empty on vzlist >loads.txt, as I said. (Thanks yomero)

    (Also, temporary files traditionally go in /tmp instead of wherever the cronjob happens to be.)

Sign In or Register to comment.