3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-25 04:02:37 +01:00
pbot/applets/compiler_vm/host/bin/vm-host-watchdog
Pragmatic Software 33e13fd993 Start refactoring virtual machine (1/3)
This is expected to take three commits to complete. This first initial
commit does the following:

- Begin initial rough-draft of doc/VirtualMachine.md
- Begin initial refactoring of scripts

The next commit will polish up the initial rough-draft and refactoring.

The final commit will quality-check everything and fix anything overlooked.
2022-01-23 07:49:23 -08:00

45 lines
1.0 KiB
Perl
Executable File

#!/usr/bin/env perl
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
use warnings;
use strict;
use Proc::ProcessTable;
use IO::Socket;
my $SLEEP = 15;
my $MAX_PCTCPU = 25;
my $QEMU = 'qemu-system-x86';
my $DOMAIN = 'pbot-vm';
my $last_pctcpu = 0;
sub reset_vm {
print "Resetting vm\n";
system("virsh snapshot-revert $DOMAIN 1");
print "Reset vm\n";
}
while (1) {
my $t = Proc::ProcessTable->new(enable_ttys => 0);
foreach my $p (@{$t->table}) {
if ($p->fname eq $QEMU and $p->cmndline =~ m/guest=\Q$DOMAIN\E/) {
# $p->pctcpu never updates? so we use top instead.
my $pctcpu = `top -b -n 1 -p $p->{pid} | tail -n 1 | awk '{print \$9}'`;
$pctcpu =~ s/^\s+|\s+$//g;
print scalar localtime, " :: Got $DOMAIN qemu pid: $p; using $pctcpu cpu\n" if $pctcpu > 0;
if ($pctcpu >= $last_pctcpu and $last_pctcpu >= $MAX_PCTCPU) {
reset_vm;
$last_pctcpu = 0;
} else {
$last_pctcpu = $pctcpu;
}
}
}
sleep $SLEEP;
}