About getting the start time of a VM on a remote server

Source: Internet
Author: User
Due to recent work needs, we have studied how to obtain the VM boot time on the remote server locally.
The first idea is to use the virsh command of libvirt to retrieve it. However, after reading its API, no corresponding command is available.
As a result, considering that there will be system file logs in the Linux system, we plan to start from this and find that the directory storing the system time is/var/log/wtmp. you can use last-F wtmp to view the time as follows:
PTS/9: 0.0 Wed Aug 10 still logged in
PTS/4: 0.0 Wed Aug 10 :30 still logged in
PTS/8: 0.0 Wed Aug 10)
PTS/7: 0.0 Wed Aug 10)
PTS/6: 0.0 Wed Aug 10)
PTS/5: 0.0 Wed Aug 10)
PTS/0: 0.0 Wed Aug 10)
Tty1: 0 Wed Aug 10 08:59 still logged in
Reboot system boot 2.6.35.6-45. fc14 Wed Aug 10)
PTS/6: 0.0 Tue Aug 9)
PTS/5: 0.0 Tue Aug 9)
PTS/0: 0.0 Tue Aug 9)
Tty1: 0 Tue Aug 9-down)
Reboot system boot 2.6.35.6-45. fc14 Tue Aug 9)
PTS/0: 0.0 Tue Aug 9)
Tty1: 0 Tue Aug 9-down)
Reboot system boot 2.6.35.6-45. fc14 Tue Aug 9)
PTS/10: 0.0 Tue Aug 9)
PTS/8: 0.0 Tue Aug 9)
PTS/7: 0.0 Tue Aug 9)
PTS/6: 0.0 Tue Aug 9)
PTS/5: 0.0 Tue Aug 9)
PTS/4: 0.0 Tue Aug 9)
PTS/0: 0.0 Tue Aug 9)

It can display records of 10 days. But there is a problem here. The time recorded in this file does not include the virtual machine. However, the problem is fixed. PTS starts from 0, and each time the device time is recorded, it is incremented in turn. We can see that PTS is not from 0 to the maximum, but intermittent in the middle, so considering that the missing PTS number is the PTS of the virtual machine.
Continue to use the virsh command to find help. view the XML file configured by the virtual machine through the virsh dumpxml vmname, and finally find the file location that stores the Time of the Virtual Machine. Part of the XML file is as follows:
<Console type = 'PTY' tty = '/dev/pts/1'>
<Source Path = '/dev/pts/1'/>
<Target type = 'serial' Port = '0'/>
<Alias name = 'serial0'/>
</Console>
The tty property value is required, and/dev/pts/* is created and destroyed when the VM is started. Use the STAT command to obtain relevant information.

[Root @ nicai PTS] # stat 1
File: '1'
Size: 0 blocks: 0 Io block: 1024 character special file
Device: Ah/10D inode: 4 links: 1 device type: 88,1
Access: (0620/CrW -- w ----) uid: (107/qemu) gid: (5/tty)
Access: 08:58:38. 178742005 + 0800
Modify: 08:58:38. 178742005 + 0800
Change: 08:58:38. 178742005 + 0800
 
Stat-T name can be converted to seconds. At this point, another problem occurs. There is no file creation time, and only access, modify, and change time are available. The creation time is not provided in Linux, if the file is not modified, the creation time is equal to the modification time. Through Operation verification on the virtual machine, it is found that if the/dev/pts/* file is not manually modified, the modify recorded by it will not change, that is, the boot time of the virtual machine.

I feel very good at solving a problem. Here I will record the process and hope to help others. If you have any shortcomings, I hope you can point out that flowers and bricks are welcome !!!

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.