I'm wondering if there are any public scripts to automatically restart OpenVZ containers on certain load amount / high cpu. Trying to limit my containers on my personal machine, thanks.
Asked
Active
Viewed 1,040 times
1
-
this is hammer approach, if you wan't to script it really you can see my answer – Hrvoje Špoljar Nov 10 '14 at 16:17
1 Answers
1
As seen on : http://openvz.org/Loadavg ; load average of some container can be acquired with
vzctl exec $CTID cat /proc/loadavg
you can script this easily with something like
#!/bin/bash
# loop over all OpenVZ containers
for container in $(vzlist -o ctid | egrep -o '[0-9]+')
do
if (( $(bc <<< "$(vzctl exec $container cat /proc/loadavg | cut -d' ' -f1) <= 0.2") == 1 ))
then
echo "Load is less than 0.2"
else
echo "Load is above 0.2, stopping container $container"
vzctl stop $container
fi
done
It uses the fact that load averages of respective intervals 1 5 and 15 can be read from /proc/loadavg

Hrvoje Špoljar
- 5,245
- 26
- 42
-
Thank you. I'm unfamiliar with bash scripting. How would I make it kill the process? – user44754 Nov 10 '14 at 16:50
-
if this script is ran inside OpenVZ container as I understood from your question, you adjust load which is too high and restart with 'reboot' command; just add it below echo command in the script. – Hrvoje Špoljar Nov 10 '14 at 16:54
-
I want to run it on the host node to check for containers load. Sorry if I wasn't clear. – user44754 Nov 10 '14 at 17:00
-
I've updated answer with more specific approach to run on system which hosts OpenVZ containers – Hrvoje Špoljar Nov 10 '14 at 17:12
-
Awesome, thank you. I'll give this a try. Would the best way to keep it running is with a cronjob? – user44754 Nov 10 '14 at 17:14
-
sure you can add it to run as cronjob every few min; also please don't forget to upvote/accept answer if you found it helpful :)) – Hrvoje Špoljar Nov 10 '14 at 17:15
-
Will do, thank you. Would it possible to do with process percentages and stop them? – user44754 Nov 10 '14 at 18:02
-
what kind of percentage? please expand your question or add new comment with details and I'll do my best to help – Hrvoje Špoljar Nov 10 '14 at 18:05
-
Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/18541/discussion-between-user44754-and-hrvoje-spoljar). – user44754 Nov 10 '14 at 18:08
-
-