Collectd for VM monitoring

Posted by Amol Dighe on November 22, 2017

Here’s an intresting use case - There is a need check the usage of customer VM’s without installing an agent on their VM.

Solution - Use collectd with libvirt plugin enabled on your KVM host.

I have tried this solution on my Ubuntu laptop which is running 2 KVM-VMs

root@hp-envy:~# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     ubuntu14                       running
 2     ubuntu14-new                   running
  • Install collectd on KVM server
apt-get install collectd
  • Collectd has several plugins available and enabled in
/etc/collectd/collectd.conf
  • Configure collectd to load libvirt, better know as - virt plugin by creating this config file
root@hp-envy:~# cat /etc/collectd/collectd.conf.d/libvirt.conf 
<LoadPlugin virt>
 Globals false
</LoadPlugin>

 <Plugin "virt">
  Connection "qemu:///system"
  RefreshInterval 60
  Domain "dom0"
  BlockDevice "name:device"
  InterfaceDevice "name:interface"
  IgnoreSelected true
  HostnameFormat "name"
</Plugin>

We will be adding a new config file to /etc/collectd/collectd.conf.d/ which will be included as per this directive in /etc/collectd/collectd.conf

<Include "/etc/collectd/collectd.conf.d">
        Filter "*.conf"
</Include>
  • Restart collectd to enable virt plugin
service collectd restart
  • Now to view the metrics collected by collectd-virt plugin, we will be setting up collectd-web, using the below process:
cd /usr/local/

git clone https://github.com/httpdss/collectd-web.git

cd collectd-web/

chmod +x cgi-bin/graphdefs.cgi

The python script at /usr/local/collectd-web/runserver.py is configured to run on localhost IP 127.0.0.1:8888

root@hp-envy:/usr/local/collectd-web# ./runserver.py 
Collectd-web server running at http://127.0.0.1:8888/

To configure it to run on all interface acttached to KVM host, replace 127.0.0.1 to 0.0.0.0, and start the python server

root@hp-envy:/usr/local/collectd-web# ./runserver.py &
[1] 3592
root@hp-envy:/usr/local/collectd-web# Collectd-web server running at http://0.0.0.0:8888/

I have my local wifi router DHCP assigned the IP - 192.168.0.51

root@hp-envy:~# ip a | grep wlo1 
3: wlo1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    inet 192.168.0.51/24 brd 192.168.0.255 scope global dynamic wlo1

Now I can hit the IP http://192.168.0.51:8888/ in my local browser to bring up the collectd web UI

  • Reference Links

https://www.tecmint.com/install-collectd-and-collectd-web-to-monitor-server-resources-in-linux/

https://syedali.net/monitoring/

https://collectd.org/documentation/manpages/collectd.conf.5.shtml#plugin_virt

https://github.com/httpdss/collectd-web