VMware {code} Community
Noman0118
Contributor
Contributor

VMINFO.PL: how to list the power state of the VM????

Hello all,

I have downloaded and compiled the VMware Infrastructure (VI) Perl Toolkit Packages (Latest Version: 1.5.0 | 1/17/2008 | Build: 67153). I am trying to get the

power state of the VM by executing the below script, but i cant get the output to do so. It simply prints out the help menu instead. When i use the powerstatus as a switch, do i pass anything as a argument? Please, help.

vminfo.pl --username XXXX --password XXXX --url XXXX --vmname XXXX --powerstatus ????

Thanks in advance!

Tags (3)
0 Kudos
8 Replies
StefanPahrmann

Hi,

my guess would be "poweredOn", "suspended" or "poweredOff". I just can't check it by myself.

Regards

Stefan

hicksj
Virtuoso
Virtuoso

If you use "powerstatus poweredOn" with vminfo.pl, it will only display the info on the VM if its on. Vice-versa, if you give it the poweredOff option, yet the VM is on, it will tell you that VM (specified by "vmname xxxx") is not found.

Essentially, you're providing a condition to the search with --powerstatus, and you must specify what you want that condition to be.

Regards, J

hicksj
Virtuoso
Virtuoso

if you want to see the "state" of the VM listed in the output, you may want to use guestinfo.pl instead. (remove the "powerstatus" option above, replace with "operation display")

$ guestinfo.pl --username xxx --password yyy --server zzz --vmname abc --operation display | grep guestState

or you can easily hack the code in guestinfo.pl to only return the vm power state (instead of using grep)

0 Kudos
Noman0118
Contributor
Contributor

Hello,

If the VM is OFF and i pass:

1) "poweredOn" : it returns VM not found

2) "poweredOff": it simple returns the VM information. Powerstatus is not displayed

Either way i do not know if the current power state of the VM....

0 Kudos
Noman0118
Contributor
Contributor

Hello,

1) If the VM is ON and i run the command:

( guestinfo.pl --username xxx --password yyy --server zzz --vmname abc --operation display | grep guestState) >> returns "VMNAME guestState: running"

2) If the VM is OFF and i run the below command:

( guestinfo.pl --username xxx --password yyy --server zzz --vmname abc --operation display | grep guestState) >> returns nothing

However, if i do it without " | grep guestState" I get the below:

For display, Virtual Machine 'VMNAME' under host X.X.X.X. should be powered ON

All i am looking for is the power state of the VM. Any thoughts?

Thanks,

0 Kudos
StefanPahrmann

Hi,

try the following code (just put all in a file, save it with a .pl extension and modify the first 4 lines):

my $service_url = "https://VC-server/sdk";
my $user_name = "";
my $password = "";
my $vmname = "";

use VMware::VIM2Runtime;
use VMware::VILib;

Vim::login(service_url => $service_url, user_name => $username, password => $password);

my $xvm_views = Vim::find_entity_views(view_type => "VirtualMachine",
                                                filter => {'name' => $vmname } );

foreach(@$xvm_views){
   $tempname=$_->name;
   if($_->runtime->powerState->val eq 'poweredOff'){
      print $tempname." is powered off\n";
   }
   if($_->runtime->powerState->val eq 'poweredOn'){
      print $tempname." is powered on\n";
   }
}

Vim::logout();

Regards

Stefan

0 Kudos
hicksj
Virtuoso
Virtuoso

> Either way i do not know if the current power state of the VM....

Yes, you do. Assuming the VM specified on the command line exists, if you see "VM not found" when using --powerstate poweredOn, you know that its powered off. If instead, the VM information is displayed, you know it is on. However, as I suggested, you could very easily modify the code from guestinfo.pl to provide a return of "poweredOn" or "poweredOff", which Stefan has nicely gone ahead and done for you.

0 Kudos
akshattambe123
Contributor
Contributor

Hi,

Not sure if you have got the answer but it is quite easy & have done it myself. I came across this need of getting Power State few days back.

I have done few things that I am mentioning here :

1. Added a if condition for PowerState under foreach() loop like below-

elsif($_ eq 'powerState') {

if (defined($vm_view->runtime) && defined ($vm_view->runtime->powerState)) {

print_log($vm_view->runtime->powerState->val,"powerState", "Power State ");

}

}

2. I have updated the powerstate definition under %opts

powerstate' => {

      type    => "=s",

      help    => "State of the virtual machine: poweredOn or poweredOff",

      required => 0,

  }

3. Added "powerState" to validate() >> @valid_properties

With this I am able to manage to get the below output with Power State of the VM in context-

<Root>

<VM>

<Name>vm001</Name>

<noCPU>2</noCPU>

<memorySize>4096</memorySize>

<virtualDisks>1</virtualDisks>

<template>0</template>

<vmPathName>[hfesx005-data] vm001/vm001.vmx</vmPathName>

<guestOS>CentOS 4/5/6/7 (64-bit)</guestOS>

<guestId>centos64Guest</guestId>

<hostName>localhost.localdomain</hostName>

<ipAddress>16.71.85.181</ipAddress>

<VMwareTools>VMware Tools is running and the version is current</VMwareTools>

<cpuUsage>0 MHz</cpuUsage>

<hostMemoryUsage>742 MB</hostMemoryUsage>

<guestMemoryUsage>1064 MB</guestMemoryUsage>

<powerState>poweredOn</powerState>

<overallStatus>The entity is OK</overallStatus>

</VM>

</Root>

Hope this will help.

Regards

Akshat Tambe

0 Kudos