Hey, I'm not a huge poster, but I've been working on a short script to solve a problem I've been having.
Long story short, I have 5 CyberPower UPSs that I want to monitor over USB.
I know NUT exists, but I hear it's a hassle, and I like the trending/reporting that's available in the CyberPower Software.
Unfortunately, I can only monitor 1 UPS per instance of the software.
And on top of that, when/if the server reboots, all USB Passthrough is reset, as the USB UUID changes.
However I noticed that in "xe pusb-list" there is a static "Path" designation which I realized I could use to my advantage
So I built this script to run at startup (probably going to add a 1-2 minute delay on the front end, as sometimes the USB drivers take a minute to load, I'm learning). It grabs the UUID of a USB and saves it to a txt file based on the "Path". It then grabs the UUID of my designated VMs (UPS1-UPS5, just single core 1GB Ram ubuntu to run the software). Main reason I did this instead of hardcoding the UUID is in case I ever need to delete/restore the VM from backup and the UUID Changes. and Finally it starts the VM.
I haven't finished installing all of the UPSs, so I only have 3 of the 5 VMs programmed currently, but it's in a good enough state That I wanted to share it in case anyone else has been looking to solve a similar problem.
#set working directory
cd /home/temp
#remove old text files
rm -f *.txt
#create list of all pUSBs
xe pusb-list > pusb.txt
#split list per device and remove old list
csplit -sz pusb.txt /uuid/ '{*}'
rm -f pusb.txt
#for all split files, if Vendor ID is CyberPower, create a txt file named by Serial Number, containing the USB uuid, and enable for passthrough
for i in xx*
do
var1=$(awk '/vendor-id/ {print $4}' "$i")
if [ "$var1" -eq 0764 ]
then path=$(awk '/path/ {print $4}' "$i")
awk '/uuid/ {print $5}' "$i" > "$path".txt
xe pusb-param-set uuid=$(cat "$path".txt) passthrough-enabled=true
xe usb-group-list PUSB-uuids=$(cat "$path".txt) > "$i"-2
awk '/uuid/ {print $5}' "$i"-2 > "$path"group.txt
fi
done
#remove all csplit files
rm -f xx*
#create list of all VMs
xe vm-list > vm.txt
#split list per vm and remove old list
csplit -sz vm.txt /uuid/ '{*}'
rm -f vm.txt
#for all split files, get UUID of needed VMs
for f in xx*
do
vmname=$(awk '/name/ {print $4}' "$f")
if [ "$vmname" == 'UPS1' ] || [ "$vmname" == 'UPS2' ] || [ "$vmname" == 'UPS3' ] || [ "$vmname" == 'UPS4' ] || [ "$vmname" == 'UPS5' ]
then awk '/uuid/ {print $5}' "$f" > "$vmname".txt
fi
done
#remove all csplit files
rm -f xx*
#if expected UPS is present, attach to expected VM
if test -f 2-2.1group.txt
then xe vusb-create usb-group-uuid=$(cat 2-2.1group.txt) vm-uuid=$(cat UPS1.txt)
xe vm-start uuid=$(cat UPS1.txt)
fi
if test -f 2-2.2group.txt
then xe vusb-create usb-group-uuid=$(cat 2-2.2group.txt) vm-uuid=$(cat UPS2.txt)
xe vm-start uuid=$(cat UPS2.txt)
fi
if test -f 2-2.4group.txt
then xe vusb-create usb-group-uuid=$(cat 2-2.4group.txt) vm-uuid=$(cat UPS5.txt)
xe vm-start uuid=$(cat UPS5.txt)
fi
#remove leftover txt files
rm -f *.txt
I am by no means an expert in scripting or linux... This has all been a learning endeavor for me... so if anyone has any suggestions on how to improve this script, I do welcome feedback.