php管理工具 libvirt,PHP 简单管理 Libvirt 虚拟机

Libvirt-Manager

This is a simple php libvirt manager

It can start, shutdown, destory, get the virtual machines list or get the information for any virtual machine.

This project is to make it easier for developers to manage virtual machines.

You need install php_ssh2 module to use this manager.

Maybe this document has some grammatical mistakes, because the author is Chinese :P

Install Libvirt-Manager

Use composer

composer require kasuganosoras/libvirt-manager

Use git

git clone https://github.com/kasuganosoras/Libvirt-Manager

Example code for connect a server

This example code will connect to your server using SSH, make sure your server sshd service is running.

require_once __DIR__ . '/vendor/autoload.php';

use libvirt_manager\Libvirt;

$Libvirt = new Libvirt();

$Libvirt->setHost("192.168.3.181", 22, "/data/libvirt/");

$Libvirt->connect("root", "123456");

The 192.168.3.181 is your server hostname, 22 is your server port, and /data/libvirt/ is your libvirt data save path.

For authenticate, use username and password, the username and password in example code is root and 123456.

Example code for create a virtual machine

The following example code will create a virtual machine called Test and allocate 2 CPU cores, 2 GB of memory to it.

$Libvirt->createDisk("Test", "qcow2", "30G");

$Libvirt->createVMXML("Test", 2, 2048576, "/data/libvirt/images/Test/Test.qcow2", "/data/iso/CentOS-7-x86_64-Minimal-1804.iso", "cdrom", "network", "default", $Libvirt->randomMac(), "virbr0", 0, 0, 5902);

$Libvirt->define("/data/libvirt/Test.xml");

$Libvirt->setPermission("Test");

$Libvirt->start("Test");

Create a virtual disk

String createDisk ( Name, Format, Size )

create a Virtual Machine xml config file

The method of createVMXML have 13 params.

void createVMXL ( Name, vCPU, Ram, Disk, ISO, Boot Device, Network type, Network name, MAC Address, Network bridge, Bandwidth in, Bandwidth out, VNC Port )

Register the xml config file to system

String define ( XML File Path )

Set execute permission

void setPermission ( Name )

Start the virtual machine

String start ( Name )

Stop the virtual machine

String shutdown ( Name )

Force stop the virtual machine

if your virtual machine is in trouble and can't use shutdown to make if off, you can use this method to force stop it.

String destroy ( Name )

Get the virtual machine list

You can use this method to get the virtual machine list, It will return an array.

String getList ()

Get the virtual machine information

You can use this method to get any registed virtual machine information, It will return an array.

String getInfo ( Name )

Dump the virtual machine xml config file

This method can get any registered virtual machine xml config file and return.

String dumpxml ( Name )

Clone an exist virtual machine

You can use this method to clone a exist virtual machine.

This method may take a long time, you need add a line set_time_limit(120) in your code.

String cloneVM ( Name, New name, New disk path )

Set the virtual machine network

This method can control the virtual machine network.

The third param is Boolean type, if you give it a true value, the network will enable, else the network will disable.

String setNetwork ( Server, Network name, Status )

You can find out more info in libvirt/libvirt.php

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
libvirt是一种用于管理虚拟化平台的开源工具,可以通过它来管理KVM虚拟机libvirt提供了一组命令行工具和API来管理虚拟机,可以使用它来抓取KVM虚拟机的监控指标数据。 以下是使用libvirt抓取KVM虚拟机监控指标数据的步骤: 1. 安装libvirt工具及相关依赖 ```shell sudo apt-get install libvirt-bin libvirt-dev qemu-kvm ``` 2. 打开libvirt的API libvirt提供了多种语言的API,如C、Python、Java等,这里以Python为例,打开libvirt的API: ```python import libvirt conn = libvirt.open('qemu:///system') ``` 3. 获取虚拟机对象 使用`conn.listDomainsID()`函数获取所有虚拟机的ID列表,然后使用`conn.lookupByID(domain_id)`函数获取指定虚拟机的对象。 ```python domain_id_list = conn.listDomainsID() if len(domain_id_list) == 0: print('No active domains') else: for domain_id in domain_id_list: domain = conn.lookupByID(domain_id) ``` 4. 获取监控指标数据 可以使用`domain.info()`函数获取虚拟机的基本信息,如CPU使用率、内存使用情况等,也可以使用`domain.blockStats()`和`domain.interfaceStats()`函数获取虚拟机的磁盘和网络IO数据。 ```python # 获取虚拟机CPU使用率和内存使用情况 cpu_stats = domain.info()[4] mem_stats = domain.memoryStats() # 获取虚拟机磁盘IO数据 disk_stats = {} for disk in domain.listDisks(): block_info = domain.blockStats(disk) disk_stats[disk] = {'read_bytes': block_info[1], 'write_bytes': block_info[3]} # 获取虚拟机网络IO数据 net_stats = {} for interface in domain.interfaceAddresses(libvirt.VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT, 0): stats = domain.interfaceStats(interface) net_stats[interface] = {'rx_bytes': stats[0], 'tx_bytes': stats[4]} ``` 5. 关闭libvirt连接 ```python conn.close() ``` 以上就是使用libvirt抓取KVM虚拟机监控指标数据的步骤,可以根据自己的需求来调整获取的监控指标数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值