Munin KVM VMs real memory usage plugin

I've got some machines that use KVM as virtualization platform. I monitor then using munin, but I was not able to find munin plugin that shows proper usage of memory by virtual machines. I've found this one http://munin-monitoring.org/browser/munin-contrib/plugins/virtualization..., but it shows memory declared for VM in config file (maximum memory value for VM to be honest). So this one is not good if you want to see realtime memory usage. I'm not a munin plugin programer, so I've just used this one, and changed it to show proper values (real memory usage). Below is plugin code (content of /etc/munin/plugins/kvm_mem_real file):

#!/bin/bash
# -*- coding: utf-8 -*-
# vim: set fileencoding=utf-8
#
# Munin plugin to show real amount of memory used by vm
#
# Copyright Lukasz Jozwinski based on plugin by Maxence Dunnewind, Rodolphe QuiƩdeville, Adrien Pujol
#
# License : GPLv3

VMS=`/usr/bin/virsh list | /bin/grep ^\ [0-9] | /usr/bin/tr -s " " | /bin/cut -d " " -f3`

case $1 in
config)
echo "graph_title KVM Virtual Machine Memory usage
graph_vlabel Bytes
graph_category KVM
graph_info This graph shows the current amount of memory used by virtual machines
graph_args --base 1024 --lower-limit 0"

min=0
for i in $VMS
do
echo "${i}_mem.label $i"
echo "${i}_mem.type GAUGE"
if [ $min = 0 ]
then
echo "${i}_mem.min 0"
echo "${i}_mem.draw AREA"
else echo "${i}_mem.draw STACK"
fi
min=1
echo "${i}_mem.info memory used by virtual machine $i"
done
exit 0;;
esac

for i in $VMS
do
mem=`echo "\`/usr/bin/virsh dommemstat $i | grep rss | tr -s " " | cut -d " " -f2\`*1024" | bc`
echo "${i}_mem.value $mem"
done

Depending on linux distro you should change paths for binaries used in this plugin (they are hardcoded here).
Plugin have to be run by user which is able to run virsh. It could be root (I do not recommend this), some dedicated user for the plugin or user that is able to run virsh, but is not root. So you need to properly configure this to let plugin work. In CentOS, you put configuration file of the plugin to /etc/munin/plugin-conf.d/kvm_mem_real, putting there:

[kvm_mem_real]
user name_or_user_running_plugin

If you change grep rss to actual plugin gonna show value which is memory declared (allocated) for each VM, which is not real memory usage and it's not maximum declared in the config file. It's value from qemu VM config file. It's not a bad idea to use both (rss and actual) plugins. You can also substract rss from actual to see how much memory is left for each machine. There are a lot of possibilities.

This plugin works like a charm, below is example of the output.

Comments

Hi Friend,

I have tried this code as a Plugin and it is working fine on the command prompt, it shows list of Virtual Machines with utilized memory. But the graph is showing only allocated memory to the Virtual Machine. Same value for MIN, AVG, MAX, CUR.

Can you provide us the complete plugin, this seems some part of the plugin.

Regards,
Chetan

Plugin is complete, and it works fine. It's supposed to show all virtual machines on one graph, like KVM plugin for CPU usage. All values are the same on the graph for the first run of the plugin. Then when you got more probes during the time plugin runs, value would change, unless all your VMs are already utilizing maximum amount of memory.

Hi Friend,

Is this a complete plugin ? the code is not sending the value anywhere, can you please provide me the complete plugin which will show me such graph and also allow me to separate it for every virtual Machine.

add "-r -c qemu:///system" after virsh command.

#!/bin/bash
# -*- coding: utf-8 -*-
# vim: set fileencoding=utf-8
#
# Munin plugin to show real amount of memory used by vm
#
# Copyright Lukasz Jozwinski based on plugin by Maxence Dunnewind, Rodolphe QuiƩdeville, Adrien Pujol
#
# License : GPLv3

#VMS=`/usr/bin/virsh list | /bin/grep ^\ [0-9] | /usr/bin/tr -s " " | /bin/cut -d " " -f3`
VMS=`virsh -r -c qemu:///system list --all | grep ^\ [0-9] | tr -s " " | cut -d " " -f3`

case $1 in
config)
echo "graph_title KVM Virtual Machine Memory usage
graph_vlabel Bytes
graph_category KVM
graph_info This graph shows the current amount of memory used by virtual machines
graph_args --base 1024 --lower-limit 0"

min=0
for i in $VMS
do
echo "${i}_mem.label $i"
echo "${i}_mem.type GAUGE"
if [ $min = 0 ]
then
echo "${i}_mem.min 0"
echo "${i}_mem.draw AREA"
else echo "${i}_mem.draw STACK"
fi
min=1
echo "${i}_mem.info memory used by virtual machine $i"
done
exit 0;;
esac

for i in $VMS
do
mem=`echo "\`virsh -r -c qemu:///system dommemstat $i | grep rss | tr -s " " | cut -d " " -f2\`*1024" | bc`
echo "${i}_mem.value $mem"
done

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

Main menu

Article | by Dr. Radut