Solved

Bash Script to audit Xen Servers

Posted on 2014-10-30
4
375 Views
Last Modified: 2014-11-03
Hi,

I have been working on a script found at the following location;
http://www.crucial.com.au/blog/2013/02/08/getting-to-know-your-xenserver/

I am having issues with the syntax and I can't seem to come up with a solution as to why I am getting the errors. I have exported the code into shellcheck.net where I am getting stuck is on line 13
 eval array=($(xe vm-list params=uuid is-control-domain=false|grep “uuid”|cut -c 17-))

on this line I am getting a error of "Parsing stopped here because of parsing errors." And "(' is invalid here. Did you forget to escape it?"
 There are other errors within the script but I think I might have got them like "hv_name appears unused. Verify it or export it". Although, bash scripting isn't something I have much to do with on a daily basis. I am just stuck as to why this is happening. Any pointers in the right direction would be really appreciated.


All credits go to Scott Francis

#!/bin/bash
#echo "Bash version ${BASH_VERSION}..."
#
#Written by Scott Francis
#Crucial Cloud Hosting - 08/02/2013

#Find a HV name-label
hv_name=$(xe host-list params=name-label|grep name-label|cut -c 23-|head -n1)

echo “UUID|VM Name|Operating System|vCPU Count|Memory(MB)|Networks|VLANS||Disk1 Size(GB)|Disk1 SR|Disk2 Size(GB)|Disk2 SR|Disk3 Size(GB)|Disk3 SR|Disk4 Size(GB)|Disk4 SR”

#Find all VMs which are not a Control Domain
eval array=($(xe vm-list params=uuid is-control-domain=false|grep “uuid”|cut -c 17-))

#Loop through each VM and
for i in “${array[@]}”
do
vm_name=$(xe vm-list params=name-label uuid=$i|grep “name-label”|cut -c 23-)
os_version=$(xe vm-list params=os-version uuid=$i|grep “os-version”|cut -c 29-|cut -d| -f1)
VCPUs_number=$(xe vm-list params=VCPUs-number uuid=$i|grep “VCPUs-number”|cut -c 25-)
memory=$(xe vm-list params=memory-static-max uuid=$i|grep “memory-static-max”|cut -c 30-)
networks=$(xe vm-list params=networks uuid=$i|grep “networks”|cut -c 21-)

#Clear concatenated VLAN Variable for the next loop
concat_vlans=

#Loop through the VM interfaces and find the VLAN associated with its Network
eval array2=($(xe vif-list vm-uuid=$i|grep “network-uuid”|cut -c 25-))
for a in “${array2[@]}”
do
vlan_number=$(xe pif-list network-uuid=$a host-name-label=$hv_name params=VLAN|cut -c 17-)
concat_vlans=${concat_vlans}”~”$vlan_number
#echo “|$vlan_number”
done

#Clear concatenated Disk Variable for the next loop
concat_disks=

#Loop though the VM disks and find SR name and Size allocation
eval array3=($(xe vbd-list vm-uuid=$i type=Disk params=vdi-uuid|grep “vdi-uuid”|cut -c 21-))
for b in “${array3[@]}”
do
disk_size=$(xe vdi-list uuid=$b params=physical-utilisation|grep “physical-utilisation”|cut -c 33-)
disk_size_calc=$(($disk_size/1024/1024/1024))
sr_name=$(xe vdi-list  uuid=$b params=sr-name-label|grep “sr-name-label”|cut -c 26-)
disk_size_sr=”$disk_size_calc|$sr_name”
concat_disks=${concat_disks}”|”$disk_size_sr
done

#Convert memory to MB
mem_calc=$(($memory/1024/1024))

#Print a pipe delimited row for the information
echo “$i|$vm_name|$os_version|$VCPUs_number|$mem_calc|$networks|$concat_vlans|$concat_disks”
done

Open in new window

0
Comment
Question by:MattyS82
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40414672
I would first try changing your “ characters to "
0
 
LVL 1

Author Comment

by:MattyS82
ID: 40414675
Thanks, I will start with that.
0
 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 500 total points
ID: 40414676
Try replacing those strange ” and “ quotes with normal " quotes, for example:

sr_name=$(xe vdi-list  uuid=$b params=sr-name-label|grep “sr-name-label”|cut -c 26-)

Open in new window


would become:

sr_name=$(xe vdi-list  uuid=$b params=sr-name-label|grep "sr-name-label"|cut -c 26-)

Open in new window


Also: can you show sample output of that xe command?
0
 
LVL 1

Author Closing Comment

by:MattyS82
ID: 40418944
Thanks for your assistance. Sorry I didn't get back to you sooner....dreaded man flu struck.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question