[1] RunLevel is set with linking to /etc/systemd/system/default.target. For example, the default setting without GUI is like follows.
# show current setting
[root@dlp ~]#systemctl get-default
multi-user.target
[root@dlp ~]#ll /etc/systemd/system/default.target
lrwxrwxrwx. 1 root root 37 Jul 9 06:04 /etc/systemd/system/default.target -> /lib/systemd/system/multi-user.target
[2] For example, if you'd like to change the RunLevel to Graphical-login, set like follows.
[root@dlp ~]#systemctl set-default graphical.target
rm '/etc/systemd/system/default.target'
ln -s '/usr/lib/systemd/system/graphical.target' '/etc/systemd/system/default.target'
# make sute the setting
[root@dlp ~]#systemctl get-default
graphical.target
[root@dlp ~]#ll /etc/systemd/system/default.target
lrwxrwxrwx 1 root root 36 Jul 9 21:55 /etc/systemd/system/default.target -> /lib/systemd/system/graphical.target
2014年7月16日 星期三
[CentOS 7.0] Configure Services
[1] Configure Services of the System.
# list services which are booted (list all includes inactive one with "--all" option)
[root@dlp ~]#systemctl -t service
UNIT LOAD ACTIVE SUB DESCRIPTION
auditd.service loaded active running Security Auditing Service
avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack
crond.service loaded active running Command Scheduler
dbus.service loaded active running D-Bus System Message Bus
getty@tty1.service loaded active running Getty on tty1
...
...
...
systemd-udevd.service loaded active running udev Kernel Device Manager
systemd-update-utmp.service loaded active exited Update UTMP about System Reboot/Shutdown
systemd-user-sessions.service loaded active exited Permit User Sessions
systemd-vconsole-setup.service loaded active exited Setup Virtual Console
tuned.service loaded active running Dynamic System Tuning Daemon
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.
37 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
# list boot config of services
[root@dlp ~]#systemctl list-unit-files -t service
UNIT FILE STATE
arp-ethers.service disabled
auditd.service enabled
autovt@.service disabled
avahi-daemon.service enabled
blk-availability.service disabled
...
...
...
systemd-user-sessions.service static
systemd-vconsole-setup.service static
teamd@.service static
tuned.service enabled
wpa_supplicant.service disabled
124 unit files listed.
[2] Stop and turn OFF auto-start setting for a service if you don'd need it. (it's postfix as an example below)
[root@dlp ~]#systemctl stop postfix
[root@dlp ~]#systemctl disable postfix
rm '/etc/systemd/system/multi-user.target.wants/postfix.service'
[3] There are some SysV services yet. Those are controled by chkconfig like below.
[root@dlp ~]#chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
iprdump 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iprinit 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iprupdate 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
# for exmaple, turn OFF auto-start for netconsole
[root@dlp ~]#chkconfig netconsole off
# list services which are booted (list all includes inactive one with "--all" option)
[root@dlp ~]#systemctl -t service
UNIT LOAD ACTIVE SUB DESCRIPTION
auditd.service loaded active running Security Auditing Service
avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack
crond.service loaded active running Command Scheduler
dbus.service loaded active running D-Bus System Message Bus
getty@tty1.service loaded active running Getty on tty1
...
...
...
systemd-udevd.service loaded active running udev Kernel Device Manager
systemd-update-utmp.service loaded active exited Update UTMP about System Reboot/Shutdown
systemd-user-sessions.service loaded active exited Permit User Sessions
systemd-vconsole-setup.service loaded active exited Setup Virtual Console
tuned.service loaded active running Dynamic System Tuning Daemon
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.
37 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
# list boot config of services
[root@dlp ~]#systemctl list-unit-files -t service
UNIT FILE STATE
arp-ethers.service disabled
auditd.service enabled
autovt@.service disabled
avahi-daemon.service enabled
blk-availability.service disabled
...
...
...
systemd-user-sessions.service static
systemd-vconsole-setup.service static
teamd@.service static
tuned.service enabled
wpa_supplicant.service disabled
124 unit files listed.
[2] Stop and turn OFF auto-start setting for a service if you don'd need it. (it's postfix as an example below)
[root@dlp ~]#systemctl stop postfix
[root@dlp ~]#systemctl disable postfix
rm '/etc/systemd/system/multi-user.target.wants/postfix.service'
[3] There are some SysV services yet. Those are controled by chkconfig like below.
[root@dlp ~]#chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
iprdump 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iprinit 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iprupdate 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
# for exmaple, turn OFF auto-start for netconsole
[root@dlp ~]#chkconfig netconsole off
[CentOS 7.0] Configure Networking
[1]
Set static IP address to the server. ( Replace the section 'ifcfg-***' to your own interface name )
# install the package below which includes ifconfig, netstat and so on first
[root@dlp ~]#yum -y install net-tools
[root@dlp ~]#vi /etc/sysconfig/network-scripts/ifcfg-eno16777736
HWADDR="00:0C:29:CD:9C:2D"
TYPE="Ethernet"
# change
BOOTPROTO="none"
DEFROUTE="yes"
PEERDNS="yes"
PEERROUTES="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"
IPV6_FAILURE_FATAL="no"
NAME="eno16777736"
UUID="3eb052b5-f8f2-4609-b70b-e44a050f6524"
ONBOOT="yes"
# add like follows
# this server's IP address
IPADDR="10.0.0.30"
# subnet mask
NETMASK="255.255.255.0"
# default gateway
GATEWAY="10.0.0.1"
# DNS server's IP address
DNS1="10.0.0.1"
[root@dlp ~]#systemctl stop NetworkManager
[root@dlp ~]#systemctl disable NetworkManager
rm '/etc/systemd/system/multi-user.target.wants/NetworkManager.service'
rm '/etc/systemd/system/dbus-org.freedesktop.NetworkManager.service'
rm '/etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service'
[root@dlp ~]#systemctl restart network
[root@dlp ~]#chkconfig network on
[root@dlp ~]#ifconfig
eno16777736: flags=4163 mtu 1500
inet 10.0.0.30 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::20c:29ff:fecd:9c2d prefixlen 64 scopeid 0x20
ether 00:0c:29:cd:9c:2d txqueuelen 1000 (Ethernet)
RX packets 5978 bytes 7634267 (7.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3256 bytes 287222 (280.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 0 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[2] Disable IPv6 if you do not need it.
[root@dlp ~]#vi /etc/default/grub
# line 6: add
GRUB_CMDLINE_LINUX="ipv6.disable=1 rd.lvm.lv=fedora/swap rd.md=0.....
# reload
[root@dlp ~]#grub2-mkconfig -o /boot/grub2/grub.cfg
[root@dlp ~]#reboot
[3] if you'd like to use the network interface name as ethX, configure like follows.
[root@dlp ~]#vi /etc/default/grub
# line 6: add
GRUB_CMDLINE_LINUX="net.ifnames=0 rd.lvm.lv=fedora/swap rd.md=0.....
# reload
[root@dlp ~]#grub2-mkconfig -o /boot/grub2/grub.cfg
[root@dlp ~]#reboot
# install the package below which includes ifconfig, netstat and so on first
[root@dlp ~]#yum -y install net-tools
[root@dlp ~]#vi /etc/sysconfig/network-scripts/ifcfg-eno16777736
HWADDR="00:0C:29:CD:9C:2D"
TYPE="Ethernet"
# change
BOOTPROTO="none"
DEFROUTE="yes"
PEERDNS="yes"
PEERROUTES="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"
IPV6_FAILURE_FATAL="no"
NAME="eno16777736"
UUID="3eb052b5-f8f2-4609-b70b-e44a050f6524"
ONBOOT="yes"
# add like follows
# this server's IP address
IPADDR="10.0.0.30"
# subnet mask
NETMASK="255.255.255.0"
# default gateway
GATEWAY="10.0.0.1"
# DNS server's IP address
DNS1="10.0.0.1"
[root@dlp ~]#systemctl stop NetworkManager
[root@dlp ~]#systemctl disable NetworkManager
rm '/etc/systemd/system/multi-user.target.wants/NetworkManager.service'
rm '/etc/systemd/system/dbus-org.freedesktop.NetworkManager.service'
rm '/etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service'
[root@dlp ~]#systemctl restart network
[root@dlp ~]#chkconfig network on
[root@dlp ~]#ifconfig
eno16777736: flags=4163
inet 10.0.0.30 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::20c:29ff:fecd:9c2d prefixlen 64 scopeid 0x20
ether 00:0c:29:cd:9c:2d txqueuelen 1000 (Ethernet)
RX packets 5978 bytes 7634267 (7.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3256 bytes 287222 (280.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 0 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[2] Disable IPv6 if you do not need it.
[root@dlp ~]#vi /etc/default/grub
# line 6: add
GRUB_CMDLINE_LINUX="ipv6.disable=1 rd.lvm.lv=fedora/swap rd.md=0.....
# reload
[root@dlp ~]#grub2-mkconfig -o /boot/grub2/grub.cfg
[root@dlp ~]#reboot
[3] if you'd like to use the network interface name as ethX, configure like follows.
[root@dlp ~]#vi /etc/default/grub
# line 6: add
GRUB_CMDLINE_LINUX="net.ifnames=0 rd.lvm.lv=fedora/swap rd.md=0.....
# reload
[root@dlp ~]#grub2-mkconfig -o /boot/grub2/grub.cfg
[root@dlp ~]#reboot
[CentOS 7.0] set Time Zone
# show the list of timezones
[root@dlp ~]#timedatectl list-timezones
Asia/Aden
Asia/Almaty
Asia/Amman
Asia/Anadyr
Asia/Aqtau
Asia/Aqtobe
Asia/Ashgabat
...
...
...
Pacific/Rarotonga
Pacific/Saipan
Pacific/Tahiti
Pacific/Tarawa
Pacific/Tongatapu
Pacific/Wake
Pacific/Wallis
# set timezone
[root@dlp ~]#timedatectl set-timezone Asia/Tokyo
# show status
[root@dlp ~]#timedatectl
Local time: Wed 2014-07-09 18:31:16 JST
Universal time: Wed 2014-07-09 09:31:16 UTC
RTC time: Wed 2014-07-09 09:31:15
Timezone: Asia/Tokyo (JST, +0900)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
[root@dlp ~]#timedatectl list-timezones
Asia/Aden
Asia/Almaty
Asia/Amman
Asia/Anadyr
Asia/Aqtau
Asia/Aqtobe
Asia/Ashgabat
...
...
...
Pacific/Rarotonga
Pacific/Saipan
Pacific/Tahiti
Pacific/Tarawa
Pacific/Tongatapu
Pacific/Wake
Pacific/Wallis
# set timezone
[root@dlp ~]#timedatectl set-timezone Asia/Tokyo
# show status
[root@dlp ~]#timedatectl
Local time: Wed 2014-07-09 18:31:16 JST
Universal time: Wed 2014-07-09 09:31:16 UTC
RTC time: Wed 2014-07-09 09:31:15
Timezone: Asia/Tokyo (JST, +0900)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
2014年7月14日 星期一
[CentOS 7.0] Change the default “enxx” network device to old “eth0” on CentOS 7
The easiest way to restore the old way Kernel/modules/udev rename your ethernet interfaces is supplying these kernel parameters to CentOS 7:
net.ifnames=0
biosdevname=0
To do so follow this steps:
Edit /etc/default/grub
At the end of GRUB_CMDLINE_LINUX line append "net.ifnames=0 biosdevname=0"
Save the file
Type "grub2-mkconfig -o /boot/grub2/grub.cfg"
Type "reboot"
If you didn't supply these parameters during the installation, you will probably need to adjust and/or rename interface files at /etc/sysconfig/network-scripts/ifcfg-*.
Up to Fedora 18, just biosdevname=0 was enough.
As an example, in a certain machine, in a exhaustive research, I got:
-No parameters: NIC identified as "enp5s2".
-Parameter biosdevname=0: NIC identified as "enp5s2".
-Parameter net.ifnames=0: NIC identified as "em1".
-Parameter net.ifnames=0 AND biosdevname=0: NIC identified as "eth0".
net.ifnames=0
biosdevname=0
To do so follow this steps:
Edit /etc/default/grub
At the end of GRUB_CMDLINE_LINUX line append "net.ifnames=0 biosdevname=0"
Save the file
Type "grub2-mkconfig -o /boot/grub2/grub.cfg"
Type "reboot"
If you didn't supply these parameters during the installation, you will probably need to adjust and/or rename interface files at /etc/sysconfig/network-scripts/ifcfg-*.
Up to Fedora 18, just biosdevname=0 was enough.
As an example, in a certain machine, in a exhaustive research, I got:
-No parameters: NIC identified as "enp5s2".
-Parameter biosdevname=0: NIC identified as "enp5s2".
-Parameter net.ifnames=0: NIC identified as "em1".
-Parameter net.ifnames=0 AND biosdevname=0: NIC identified as "eth0".
[CentOS 7.0] Change network interface name (eno* to eth0)
Step1)
# vim /etc/udev/rules.d/70-persistent-net.rules
-----------------------------------------------------------------------
# PCI device 0x14e4:0x1680 (tg3)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:7b:d8:06", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eno16777736", NAME="eth0"
-----------------------------------------------------------------------
or
-----------------------------------------------------------------------
# PCI device 0x14e4:0x1680 (tg3)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:7b:d8:06", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eno*", NAME="eth0"
-----------------------------------------------------------------------
Step2)
# mv /etc/sysconfig/network-scripts/ifcfg-eno16777736 /etc/sysconfig/network-scripts/ifcfg-eth0
Step3)
# vim /etc/sysconfig/network-scripts/ifcfg-eth0
-----------------------------------------------------------------------
HWADDR="00:0C:29:7B:D8:06"
TYPE="Ethernet"
BOOTPROTO="dhcp"
DEFROUTE="yes"
PEERDNS="yes"
PEERROUTES="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"
IPV6_FAILURE_FATAL="no"
NAME="eth0"
UUID="2aa76b48-487b-425e-a021-ace79c0773cd"
ONBOOT="yes"
-----------------------------------------------------------------------
Step4)
# reboot
Reference: http://dreamtails.pixnet.net/blog/post/30192150-%5Bcentos-7.0%5D-change-network-interface-name-%28eno*-to-eth0%29
# vim /etc/udev/rules.d/70-persistent-net.rules
-----------------------------------------------------------------------
# PCI device 0x14e4:0x1680 (tg3)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:7b:d8:06", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eno16777736", NAME="eth0"
-----------------------------------------------------------------------
or
-----------------------------------------------------------------------
# PCI device 0x14e4:0x1680 (tg3)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:7b:d8:06", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eno*", NAME="eth0"
-----------------------------------------------------------------------
Step2)
# mv /etc/sysconfig/network-scripts/ifcfg-eno16777736 /etc/sysconfig/network-scripts/ifcfg-eth0
Step3)
# vim /etc/sysconfig/network-scripts/ifcfg-eth0
-----------------------------------------------------------------------
HWADDR="00:0C:29:7B:D8:06"
TYPE="Ethernet"
BOOTPROTO="dhcp"
DEFROUTE="yes"
PEERDNS="yes"
PEERROUTES="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"
IPV6_FAILURE_FATAL="no"
NAME="eth0"
UUID="2aa76b48-487b-425e-a021-ace79c0773cd"
ONBOOT="yes"
-----------------------------------------------------------------------
Step4)
# reboot
Reference: http://dreamtails.pixnet.net/blog/post/30192150-%5Bcentos-7.0%5D-change-network-interface-name-%28eno*-to-eth0%29
2013年7月18日 星期四
[轉] KVM and OpenVSwitch on Centos 6.3 Minimal
原文網址:http://nullworks.wordpress.com/2012/09/19/kvm-and-openvswitch-on-centos-6-3-minimal/
These are my notes for installing KVM on Centos 6.3 minimal.
These are my notes for installing KVM on Centos 6.3 minimal.
2013年7月3日 星期三
將 Raid Card module 放入 initrd.img 中
原始機器 IBM X3550M3 --> Raid card module --> mptsas
目的機器 IBM X3550M4 --> Raid card module --> megaraid_sas ( LSI M5110 )
目的機器 IBM X3550M4 --> Raid card module --> megaraid_sas ( LSI M5110 )
2013年6月25日 星期二
[轉] Speed up scp file transfer
Speed up scp file transfer
20/05/2008
Syed Atif Ali Whats the fastest encryption to transfer files with SCP ?
Description :
Many times it is required to transfer files across machines on the same network , all machinesbeing on the same network and behind proxy its useless to transfer them with an encryptionwhich is more CPU ntensive than other and also slower than others.There can be other methods to transfer files of course , ftp , sftp , rsync, but this document is justabout scp command.
Objective :
To find out what is the faster cypher to define in scp command to transfer files on an internallocal network.
Environment :
Two Linux machines, with same openssh versions.Connected on a 100 Mbps LAN . Both being idle nothing is running on them at the moment .Before each test files will be cached on the file system buffer , so the tests are not affected withcache.
Tests :
Copy 1000 small, medium , large files across the network using different cyphers in scp command and measure their real time.
These are the file sizes :
Small : 10K
Medium: 100K
Large:1000K
Overall arcfour performed better than any other cypher used. It is safe to say that arcfour
can beused to transfer files using scp on an internal network.
You can define which cypher to use by -c option of scp.
scp -c arcfour filename.txt host:/path/filename
修改 /etc/ssh/ssh_config
Ciphers arcfour128,arcfour256,arcfour,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc
2012年7月5日 星期四
Openstack中虛擬機的監控
轉自 http://livemoon.dyndns.org/life/2011/12/openstack-1.html
本文涉及的程序代码均可以从我的github上下载:https://github.com/livemoon/openstack/tree/master/libvirt_code 并且持续更新代码
虛擬化使用kvm, 使用libvirt作為C API
基本思想:host負責運行程序,採集數據,額外一台服務器作為server收集每台host的數據進行分析
程序介紹:
首先我們需要打開一個和hypervisor的連接,需要一個virConnectPtr的指針
virConnectOpenReadOnly(char *) 返回的就是這麼一個指針。初始化程序例如:
關閉連接的函數
有了id號我們就可以獲取每個實例的詳細信息,假設我有一個id為7的虛擬機實例:
virDomainPtr dom = NULL;
dom = virDomainLookupByID(conn, 7);
dom這個變量就是以後我們要一直用到的,釋放函數:
virDomainFree(dom);
cpu監控程序:
磁盤監控:
網絡部份這裡我們要用到libvirt中的Network Filters openstack实例的libvirt.xml在nova.conf中定义的实例目录下,里面有
...
...
filterref中包括了其他的filters,默认在/etc/libvirt/nwfilter目录下
你也可以使用virsh管理工具查看具体filter内容
# virsh nwfilter-dumpxml nova-instance-instance-00000007-02163e23f37d
Filtering chains
Filtering chains就是你在目录下看到的许多filters的文件。譬如有arp, 有dhcp, mac等
在程序中使用libvirt
網卡的interface部分
中這一部分中的vnet0,同理前面說得disk裡面的“vda”也是從這裡獲取,
所以你需要運行一下這個程序
本文涉及的程序代码均可以从我的github上下载:https://github.com/livemoon/openstack/tree/master/libvirt_code 并且持续更新代码
虛擬化使用kvm, 使用libvirt作為C API
基本思想:host負責運行程序,採集數據,額外一台服務器作為server收集每台host的數據進行分析
程序介紹:
首先我們需要打開一個和hypervisor的連接,需要一個virConnectPtr的指針
virConnectOpenReadOnly(char *) 返回的就是這麼一個指針。初始化程序例如:
void conn_init(char *ip, virConnectPtr *conn) {
*conn = NULL; /* the hypervisor connection */
char *p;
p = (char *)malloc(35*sizeof(char));
*conn = virConnectOpenReadOnly(p);
free(p);
if (*conn == NULL) {
fprintf(stderr, "Failed to connect to hypervisor\n");
}
}
第二個參數是一個指向virConnectPtr變量的指針,這裡的p指向的是類似“qemu+ssh://10.0.0.1/system”的字符串,10.0.0.1是你的host ip關閉連接的函數
void conn_close(virConnectPtr *conn) {
if (*conn != NULL)
virConnectClose(*conn);
}
現在我們有了一個指向host的hypervisor的連接,我們可以用他來獲得host上跑的虛擬機的情況
void list_id_domain(virConnectPtr conn) {
int ids[10];
int maxids=10;
int num, i;
num = virConnectListDomains(conn, ids, maxids);
for(i = 0;i < num;i++) {
printf("%d\n",ids[i]);
}
}
這個函數使用上面得到的conn這個指針,列出host上跑的實例的id號有了id號我們就可以獲取每個實例的詳細信息,假設我有一個id為7的虛擬機實例:
virDomainPtr dom = NULL;
dom = virDomainLookupByID(conn, 7);
dom這個變量就是以後我們要一直用到的,釋放函數:
virDomainFree(dom);
cpu監控程序:
void list_info_domain(virDomainPtr domain) {
virDomainInfo info;
int interval = 2;
struct timeval startTime;
struct timeval endTime;
int realTime;
int cpuTime;
double cpuUsage;
virDomainGetInfo(domain, &info);
unsigned long long startCpuTime = info.cpuTime;
if (gettimeofday(&startTime, NULL) == -1) {
printf("Failed to get start time\n");
}
sleep(interval);
virDomainGetInfo(domain, &info);
unsigned long long endCpuTime = info.cpuTime;
if (gettimeofday(&endTime, NULL) == -1) {
printf("Failed to get end time\n");
}
cpuTime = (endCpuTime - startCpuTime)/1000;
realTime = 1000000 * (endTime.tv_sec - startTime.tv_sec) + (endTime.tv_usec - startTime.tv_usec);
cpuUsage = cpuTime / (double)(realTime);
printf("\t\tstate is %d\n", info.state);
printf("\t\tvCPU is %d\n", info.nrVirtCpu);
printf("\t\tMAXmemory is %ld\n", info.maxMem/1024);
printf("\t\tmemory is %ld\n", info.memory/1024);
printf("\t\tcpuUsage is %.2f%\n", cpuUsage*100);
}
解釋一下程序,首先virDomainGetInfo函數,傳入剛才我們得到的domain,另外一個參數是要返回的virDomainInfo
的結構體變量,其中包含了cpu個數,分配的時間,和分配的mem信息。我們分別取了間隔為2妙的info信息,使用裡面的info.cpuTime運行
時間,把後一次減去前一次,然後再除以實際的gettimeofday函數得到host的cpu運行時間,得到一個近似的百分比,反應的是此虛擬機實例的
cpu使用情況在整個host的cpu使用情況中的百分比。磁盤監控:
void list_disk_domain(virDomainPtr domain) {
virDomainBlockStatsStruct stats;
size_t size;
const char *disk = "vda";
size = sizeof(stats);
int interval = 2;
virDomainBlockStats(domain, disk, &stats, size);
long long start_rd_bytes = stats.rd_bytes;
long long start_wr_bytes = stats.wr_bytes;
sleep(interval);
virDomainBlockStats(domain, disk, &stats, size);
long long end_rd_bytes = stats.rd_bytes;
long long end_wr_bytes = stats.wr_bytes;
long rd_bytes = end_rd_bytes - start_rd_bytes;
long wr_bytes = end_wr_bytes - start_wr_bytes;
int rd_usage = rd_bytes/interval;
int wr_usage= wr_bytes/interval;
// printf("%s:\n", virDomainGetName(domain));
printf("\t\tread: %dbytes/s\n", rd_usage);
printf("\t\twrite: %dbytes/s\n", wr_usage);
printf("\t\trd_req: %lld\n", stats.rd_req);
printf("\t\trd_bytes: %lld\n", stats.rd_bytes);
printf("\t\twr_req: %lld\n", stats.wr_req);
printf("\t\twr_bytes: %lld\n", stats.wr_bytes);
}
磁盤使用情況的方法和cpu類似,這裡用到的是virDomainBlockStats(domain, disk, &stats, size)這個函數,disk指向的字符串這裡為“vda”,實際使用甚麼你要根據xml裡面的信息網絡部份這裡我們要用到libvirt中的Network Filters openstack实例的libvirt.xml在nova.conf中定义的实例目录下,里面有
...
...
filterref中包括了其他的filters,默认在/etc/libvirt/nwfilter目录下
你也可以使用virsh管理工具查看具体filter内容
# virsh nwfilter-dumpxml nova-instance-instance-00000007-02163e23f37d
Filtering chains
Filtering chains就是你在目录下看到的许多filters的文件。譬如有arp, 有dhcp, mac等
在程序中使用libvirt
int list_network_domain(virDomainPtr domain) {
const char *path;
virDomainInterfaceStatsStruct stats;
size_t size;
size = sizeof(stats);
path = "vnet1";
int interval = 2;
if( virDomainInterfaceStats(domain, path, &stats, size) )
return FALSE;
long long start_rx_bytes = stats.rx_bytes;
long long start_tx_bytes = stats.tx_bytes;
sleep(interval);
if( virDomainInterfaceStats(domain, path, &stats, size) )
return FALSE;
long long end_rx_bytes = stats.rx_bytes;
long long end_tx_bytes = stats.tx_bytes;
int rx_usage = (end_rx_bytes - start_rx_bytes)/interval;
int tx_usage = (end_tx_bytes - start_tx_bytes)/interval;
printf("\t\trx usage: %d bytes/s", rx_usage);
printf("\trx bytes: %lld bytes", stats.rx_bytes);
printf("\t\trx packets: %lld", stats.rx_packets);
printf("\trx errs: %lld\n", stats.rx_errs);
printf("\t\ttx usage: %d bytes/s", tx_usage);
printf("\ttx bytes: %lld bytes", stats.tx_bytes);
printf("\t\ttx packets: %lld", stats.tx_packets);
printf("\ttx errs: %lld\n", stats.tx_errs);
}
这个函數很重要,其中將返回stats指針所指向的內容便是domain中各個網口的信息。這裡有個問題,就是path的值,他是由domain中
網卡的名字,不是eth0也不是em0等,而是要通過獲取domain的xml中所以你需要運行一下這個程序
char *xmldesc;
xmldesc = virDomainGetXMLDesc(dom, 0);
if ((fp = fopen(virDomainGetName(dom), "w")) == NULL) {
printf("Cannot open file test\n");
}
fprintf(fp,xmldesc);
fclose(fp);
free(xmldesc);
返回的是字符串指針指向了xml的內容,記住這個程序運行好需要free指針。
訂閱:
文章 (Atom)