Skip to content

How to Get Information From A Virtual Machine in Proxmox Using a Bash Script?

get information from virtual machine proxmox

I have many proxmox servers where each proxmox server has many virtual machines and I have to record information from each virtual machine manually. Before I used this bash script, I had to fill it in manually and it was very tiring and confusing because the virtual machine often changes either it changes its specifications such as memory and hard disk, or the state of the virtual machine changes such as it is dead or has been deleted. Therefore, I made a bash script to automatically record information from each virtual machine from the proxmox server.

 

Problem

How to get information from a virtual machine in proxmox using a bash script?

 

Solution

Basically, the bash script that I will make is a group of proxmox commands that I usually do to find information about virtual machines on the proxmox server such as qm config, qm list, and so on. The items I will get from the virtual machine using this bash script are as shown below:

The items that I want to get from virtual machines

Here is a bash script to get information from the virtual machine that is in the virtual machine:

#!/bin/bash

### Get Data from Virtual machines

qm list | sed '1d'  > list.txt
qm list | awk '{print $1}' | sed '1d' > pid.txt
qm list  | awk '{print $1,$3}' > qm.txt

ip_address () {

mac=$(qm config "$1" | awk '/net0/ { print tolower($2) }' | sed -r 's/virtio=(.*),.*/\1/g')
ip=$(arp-scan --interface=vmbr0 192.168.56.0/24 | grep "$mac" | awk '{print $1}')

echo $ip
}



cat pid.txt | while read line
do
  qm config $line | grep name | cut -d ' ' -f2- >> $line.txt
  qm config $line | grep ostype | cut -d ' ' -f2- >> $line.txt
  qm config $line | grep memory | cut -d ' ' -f2- >> $line.txt
  qm config $line | grep cores | cut -d ' ' -f2- >> $line.txt
  qm config $line | grep sockets | cut -d ' ' -f2- >> $line.txt
  ip --oneline addr show | awk '$3 == "inet" && $2 != "lo" { print $2 ": " $4 }' | awk '{print $2}' | sed '$ s/.$//' | sed '$ s/.$//' | sed '$ s/.$//' | head -n 1 >> $line.txt
  ip_address $line >> $line.txt

  ip=`sed -n '7p' $line.txt | cut -b 1-2`

  if [ -z "$ip" ]
  then
  sed -i "7i VM is stopped or QEMU is not configured" $line.txt
  fi


  qm config $line | grep description | cut -d ' ' -f2- >> $line.txt
  note=`sed -n '8p' $line.txt`
#echo $note

  if [ -z "$note" ]
  then
  sed -i "8i Please write the description" $line.txt
  sed -i '/^$/d' $line.txt
  fi


 second=`qm status $line -verbose | grep uptime | awk '{print $2}'`
  printf '%dd %dh:%dm:%ds\n' $((second/86400)) $((second%86400/3600)) $((second%3600/60)) $((second%60)) >> $line.txt


  sed -i 's/l26/Linux/g' $line.txt
done


#Get vmid
number=`cat pid.txt | wc -l`
i=0

while [ $i -lt $number ]
do
  i=`expr $i + 1`
  value=`cat pid.txt | sed -n "$i"'p'`
  echo $value >> ` cat pid.txt | sed -n "$i"'p'`.txt
done

#Get status

qm list | sed '1d' > status.txt

cat pid.txt | while read line
do
  cat qm.txt | grep $line | awk '{print $2}' >> $line.txt
done



# get HDD
cat list.txt | awk '{print $5}' > hdd.txt

number=`cat hdd.txt | wc -l`

i=0
while [ $i -lt $number ]; do
    i=`expr $i + 1`
    value=`cat hdd.txt | sed -n "$i"'p'`
    echo $value >> ` cat pid.txt | sed -n "$i"'p'`.txt
done



cat pid.txt | while read line
do

  sed -i "s/$/,/g" $line.txt

  sed -i 's/%0A//g' $line.txt
  sed -i 's/%3A//g' $line.txt

awk 'BEGIN { ORS = " " } { print }' $line.txt | sed 's/"[[:space:]]\+"/","/g'  > $line.csv
done

Look at line 12 in the bash script above. you have to change the IP 192.168.56.0/24 with the IP used by your proxmox server. The script will generate two file formats namely a txt file and a CSV file for each virtual machine. In these 2 file formats, sometimes there are some empty results because there is no data on the virtual machine. For example, the ip_vm column will result in VM is stopped or QEMU is not configured if the script does not get the IP of the VM. To see the IP of a VM on proxmox, please look at this article. Likewise, if the script does not get the VM description, the note field will be empty. This is because in the VM the Notes column does not contain information as shown below:

The Notes on proxmox

This is an example of 2 files that are generated after we run this bash script:

Example of 2 files

 

Note

If you have several proxmox servers then you can use the cron application to run this bash script periodically to retrieve data from the virtual machines on all your proxmox servers. And you can also insert the CSV file that you get after running this script to be inserted into the MySQL/MariaDB database.

 

Reference

pve.proxmox.com

 

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *