====== libvirt admin scripts ======
===== VNC Ports Used =====
This little one liner grabs the lines that define the vnc ports used by all defined on a node (hypervisor) and returns them as a tab delimited string.
==== Code ====
grep vnc /etc/libvirt/qemu/*.xml |\
perl -lane 'BEGIN{$,="\t"} m/^.*\/([^\/]+)\.xml:.*port=.(\d{4})/; print $1,$2;'
==== Why I did it ====
We use vnc on our libvirt domains, basically because we're familiar with vnc. When a domain is misbehaving, we use ssh port forwarding to redirect from our workstation to the domain virtual screen. However, it takes a few minutes each time to figure out which VNC port the domain in question uses.
Instead, I wanted to record this in our copy of [[https://unixservertech.com/camp/|CAMP]], and since it has a tab delimited text file importer, wanted the domain name and vnc port separated by a tab.
The same could be accomplished by just saving it as a spreadsheet.
==== How it works ====
libvirt (virsh) stores it's domain (virtual) definitions in /etc/libvirt/qemu/*.xml. The //grep// command only returns those lines (along with the file name) in the form
/etc/libvirt/qemu/andrei.dailydata.noc.xml:
We then pass those lines to a Perl one liner which grabs the filename (without the .xml) and the port, then prints them to STDOUT, tab separated. Using the -n flag to perl turns the one liner into a filter, which will run the code on all lines received via STDIN. For the above line, the following is sent to STDOUT
andrei.dailydata.noc 5917
==== Caveats ====
* If there is only one domain defined, grep does not return the filename, so the system breaks.
* This is totally dependent on how libvirt stores the data; path, key and whether the key is on the same line. Any changes to the libvirt storage will break the script.
* We use [[https://unixservertech.com/havirt/|havirt]] for our clusters. havirt stores the domain definitions in /conf, so the grep command will need to be changed.