Monitor disk size of VM

1. Giới thiệu

- Có rất nhiều công cụ dùng để thu thập các metrics của HOST và VM như Collectd, Stats, Telegraf, Promethus, Diamond,...
- Mình sử dụng công cụ Diamond để thu thập các metrics của HOST và các VM chạy trên HOST đó. Diamond được viết bằng Python, vì vậy ta có thể dễ dàng sửa đổi code, cũng như code thêm 1 số plugin #. Tham khảo source code diamond:
https://github.com/python-diamond/Diamond

- Bài này mình sẽ giới thiệu về việc sử dụng:

  • Diammond để thu thập disk size và disk partitions size.
  • InfluxDB để lưu trữ dữ liệu các metrics.
  • Grafana để generate dữ liệu thành các biểu đồ tiện theo dõi.

2. Diamond

- Diamond có plugin LibvirtKVMCollector để thu thập các metrics của VM như CPU, Memory,... Tuy vậy, nếu bạn muốn thu thập disk size và disk partitions size của VM thì Diamond chưa hỗ trợ.
Vì vậy, ta cần code thêm plugin để có thu thập disk size và disk partitions size.
- Thư viện libivrt không hỗ trợ lấy disk size và disk partitions size của VM, nên ta phải dùng thư viện bên thứ 3 là libguestFS (http://libguestfs.org/ ).
Kết hợp 2 thư viện libivirtlibguestfs, ta có thể thu thập metrics disk size và disk partitions size của tất cả các VM đang chạy trên HOST tương đối dễ dàng.
- Ví dụ code lấy disk size và disk partitions size:

#!/usr/bin/python3

import libvirt
import guestfs

# Get disks and disk partitions size of of domain
def get_disks_and_parts_size (conn, dom_name):
    # Connect to domain "dom_name"
    dom1 = conn.lookupByName(dom_name)
    if dom1 == None:
        print('Failed to find the domain '+dom_name)
        exit(1)

    # All new Python code should pass python_return_dict=True
    # to the constructor.  It indicates that your program wants
    # to receive Python dicts for methods in the API that return
    # hashtables.
    g = guestfs.GuestFS(python_return_dict=True)

    # Attach the disk image read-only to libguestfs.
    g.add_libvirt_dom(dom=dom1, readonly=True)

    # Run the libguestfs back-end.
    g.launch()

    # Get disks total size of VM
    disks = g.list_devices()    # ['/dev/sda', '/dev/sdb']
    disks_total_size = {}   # {'/dev/sda': 41126400, '/dev/sdb': 21474836480}
                            # unit is byte

    for disk in disks:
        size_of_disk_total = g.blockdev_getsize64(disk)
        disks_total_size[disk] = size_of_disk_total

    # Get disk partitions size of VM
    partitions_size = []    # [ ['/dev/sda1', '2058100', '988608', '1053108', '49%', '/sysroot'] ; 
                            #   ['/dev/sda15', '106858', '3668', '103190', '4%', '/sysroot'] ]
                            # unit is KiB
    partitions = g.list_partitions()

    for partition in partitions:
        try:
            g.mount_ro(partition, "/")
        except RuntimeError:
            continue
        df_list = g.df()
        df_list_line = df_list.splitlines()
        partition_size = df_list_line[5].split()
        partitions_size.append(partition_size)

3. InfluxDB và Grafana

- Sau khi Diamond thu thập được disk size và disk partitions size, ta sẽ gửi các metrics đó đến InflubDB. Cuối cùng ta dùng Grafana để generate các dữ liệu đó và được biểu đồ, hình dưới minh họa các biểu đồ về disk size và disk partitions size:
screenshot

Show Comments