Use Shell scripts to monitor KVM virtual machines

Source: Internet
Author: User

Use Shell scripts to monitor KVM virtual machines

Recently, a friend asked me what software I usually use to manage KVM virtual machines. In fact, I usually use the virsh command to manage virtual machines through the command line. Although RedHat has a virtual machine management and monitoring platform, it can only be used on the redhat release. Other platforms are not associated with this software, including CentOS.

If you use the virsh command to manage kvm, what is the monitoring command?
My solution is to monitor the use of zabbix by kvm virtual machines, and to monitor the use of ps commands and top commands on the host.

Zabbix is not the focus of my discussion here. If you are interested, you can access relevant information online. Here I will only describe how to use top and ps commands to monitor virtual machines on the host.

However, the preceding two commands are problematic:
You can only view the process ID (PID) and command description (qemu-kvm) by using the top command. You cannot see which virtual machine is used (as shown below ), the virsh command uses the Virtual Machine name to manage the virtual machine:

   PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND  3525 root      20   0 20.8g  16g 5636 S 58.1 12.8  19499:19 qemu-kvm  3586 root      20   0 20.6g  10g 5636 S 56.1  8.2  18289:31 qemu-kvm129225 root      20   0 6634m 1.2g 5596 S  4.0  1.0   1001:20 qemu-kvm129268 root      20   0 4946m 707m 5596 S  4.0  0.5 964:44.63 qemu-kvm  3664 root      20   0 5561m 1.2g 5608 S  3.6  0.9   1318:51 qemu-kvm

Although the relationship between processes and virtual machines can be seen through the ps command alone, real-time monitoring data cannot be seen (as follows ):

ps aux|grep -v 'grep'|grep -v 'vhost-'|grep 'qemu-kvm'|awk '{print $2" "$13}'3525 CKB_JinDie_Server3586 CKB_JinDie_HA_Server3664 DNS_Server129225 Discuz_Server129268 TCExam_Server

Therefore, you must write a shell script to combine the two commands to match the Virtual Machine name with real-time data. At the same time, you must regularly refresh the data, to monitor the running status of virtual machines in real time on the host.

The shell script is as follows:

#! /Bin/bash # view the running status of the local kvm virtual machine # Author: CMZSteven # Create Date: 2016-06-08 # Modified Date: 2016-06-08while true; do # clear the screen display clear; # obtain the top information at this time point and output it to the specified file top-bn1>/tmp/kvm_top.txt; # obtain the table corresponding to the process number and Virtual Machine name, output to the specified file ps aux | grep-v 'grep' | grep-v 'vhost-'| grep 'qemu-kvm' | awk '{print $2 "" $13} '>/tmp/kvm_list.txt # obtain the first seven lines of the top command, that is, the Summary Information Line cat/tmp/kvm_top.txt | head-n 7; # Place the Virtual Machine name on the previous line of the specified information line, for I in 'cat/tmp/kvm_list.txt | awk '{print $1 }''; do grep $ I/tmp/kvm_list.txt | awk '{print $2}'; grep $ I/tmp/kvm_top.txt | grep-v 'vhost-'; done; # After Entering the relevant monitoring information, pause for 10 seconds to view the relevant data sleep 10 done;

The actual running result is as follows:

top - 17:03:55 up 24 days,  6:29,  2 users,  load average: 0.91, 0.98, 1.07Tasks: 742 total,   1 running, 741 sleeping,   0 stopped,   0 zombieCpu(s):  0.9%us,  1.5%sy,  0.0%ni, 97.5%id,  0.1%wa,  0.0%hi,  0.0%si,  0.0%stMem:  131915040k total, 81581596k used, 50333444k free,   143408k buffersSwap:  8388604k total,        0k used,  8388604k free, 48952552k cached   PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMANDCKB_JinDie_Server  3525 root      20   0 20.8g  16g 5636 S 56.4 12.8  19547:40 qemu-kvmCKB_JinDie_HA_Server  3586 root      20   0 20.6g  10g 5636 S 56.4  8.2  18330:33 qemu-kvmDNS_Server  3664 root      20   0 5561m 1.2g 5608 S  5.8  0.9   1321:51 qemu-kvmDiscuz_Server129225 root      20   0 6634m 1.2g 5596 S  3.9  1.0   1004:30 qemu-kvmTCExam_Server129268 root      20   0 4946m 707m 5596 S  5.8  0.5 967:46.78 qemu-kvm

In fact, this script still has room for improvement. If you are interested, you can try to upgrade it yourself.

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.