Virtualization brought all the benefits of over allocation (using more resources than physically available) to us. And sharing resources like CPU makes a lot of sense if you for example have a daily distributed workload like daily traffic and nightly jobs to generate reports and don’t need all resources at a time. This works great most of the time and is acceptable to slow down your system because of lack of resources sometimes but you definitely don’t want to crash you system because of lack of resources. This is why you usually don’t want over allocate memory. The following script allows you to check this for KVM hypervisor hosts.
The script is written in Python 2.x and should run as root or user who has access to libvirt Python API which is usually installed if you have a KVM hypervisor because virsh command needs it to run. Unfortunately it is not run able with Python 3.
You can pass warning and critical parameters to raise warnings in case you want to get warned earlier or really want to allow over allocation. You can specify a overhead in MiB for domains (VMs) and reserved space MiB for the operating system so that you have enough RAM left for you host to work properly. It fulfills Nagios API so that you can include it as a active monitoring check.
You can of course also just use it to get an overview of the usage of your host.
python check_kvm_memory -v
Memory OK 10241.7460938 MiB 8.06% free|10241.7460938 domain1.local: 8242.0 MiB domain2.local: 8242.0 MiB domain3.local: 2096.0 MiB domain4.local: 8242.0 MiB domain5.local: 4146.0 MiB domain6.local: 2098.0 MiB domain7.local: 2098.0 MiB domain8.local: 5170.0 MiB domain9.local: 8242.0 MiB domain10.local: 8242.0 MiB domain11.local: 3122.0 MiB domain12.local: 8242.0 MiB domain13.local: 6194.0 MiB domain14.local: 8242.0 MiB domain15.local: 8242.0 MiB domain16.local: 8242.0 MiB domain17.local: 8242.0 MiB domain18.local: 9522.0 MiB usable: 127107.746094 MiB used: 116866.0 MiB free: 10241.7460938 MiB reserved incl.: 2048.0 MiB overhead incl.: 50.0 MiB
Sourcecode and other checks are available at github.com