johnswb
Enthusiast
Enthusiast

VMinfo Script

Jump to solution

Hello all,

I'm in need of a script that will tell me what how much resources each VM is using. For example: testvm 2gig memory 15gig vmdk 30gig vmdk and so on.

I am new to scripting so go easy on me. I just know scripting will make life easier.

Hosts are on 3.5 and VC is on 2.5.

Thanks 4 any help.

Will Johnson VCP on VI3 / VI4 vSphere 5
0 Kudos
1 Solution

Accepted Solutions
akutz
Hot Shot
Hot Shot

Change

use VMware::VIM2Runtime;

to

use VMware::VIRuntime;

View solution in original post

0 Kudos
16 Replies

Hi,

Following code will list memory and disk-usage. Remember to fix the first 3 lines with appropriate values for your Virtual Center.

$service-url="https://localhost/sdk";
$username="";
$password="";

use VMware::VIM2Runtime;
use VMware::VILib;
use Data::Dumper;

Vim::login(service_url => $service_url, user_name => $username, password => $password);
my $vm_views = Vim::find_entity_views(view_type => "VirtualMachine" );

foreach(@$vm_views){

     my $memory = $_->summary->config->memorySizeMB;
     print $_->name." ".$memory." mb Mem ";
     if (defined $_->guest->disk) {
            my $disk_len = @{$_->guest->disk};
            my $cnt = 0;
            while ($cnt < $disk_len) {
                if (defined $_->guest->disk->[$cnt]->capacity) {
                    print "Disk". $cnt ." ". int($_->guest->disk->[$cnt]->capacity / 1048576000) . " gb ";
                }
                else {
                    print "Disk". $cnt ." Not Known";
                }
                $cnt++;
           }
      }elsif(defined ($_->summary->config->numVirtualDisks)) {
           print "Antal virtuelle diske: ".$_->summary->config->numVirtualDisks;
      }
      print "\n";
}

Vim::logout();

Regards

Stefan

###############

If you found this information useful, please consider awarding points for "Correct" or "Helpful". Thanks!!!

johnswb
Enthusiast
Enthusiast

Stefan,

Thank you for the info. I'm sorry but I'm a BIG noob when it comes to scripting. I need to know what file type do I need to save this as? I know if it was a VB script I would do .vbs. Also, should I run this script from Windows or Linux? I prefer Linux, but I will take what ever help you can give. Thanks again.

Will Johnson VCP on VI3 / VI4 vSphere 5
0 Kudos

Oh, I'm sorry. This is a perl-script, so the extension should be ".pl". You also need to install the VI Perl toolkit, which you can download here:

http://www.vmware.com/download/sdk/

Regards

Stefan

johnswb
Enthusiast
Enthusiast

StefanPahrmann,

Now that I can spend more time working on scripting, I would like to ask you a question on this one. I ran the script and got the following.

./vminfo.pl: 1: -url=https://vc.server.name.com/sdk: not found

./vminfo.pl: 2: =admin01: not found

./vminfo.pl: 3: =Passw0rd: not found

./vminfo.pl: 5: use: not found

./vminfo.pl: 6: use: not found

./vminfo.pl: 7: use: not found

./vminfo.pl: 9: Syntax error: word unexpected (expecting ")")

I'm am a Linux user and not a Windows user. I've gone to the link you posted and installed VI Perl for Linux. Can you tell me what I'm doing wrong?

Will Johnson VCP on VI3 / VI4 vSphere 5
0 Kudos
akutz
Hot Shot
Hot Shot

It looks like the script is not being interpreted by perl and is being, or attempted, executed line by line and failing. Are you running this in Windows or some Unix environment?

0 Kudos
johnswb
Enthusiast
Enthusiast

I am running the command from Ubuntu Linux desktop.

I created a file called vminfo.pl and added the script to it.

I then did the following to it - "sudo chmod +x vminfo.pl" so that I could run it.

Will Johnson VCP on VI3 / VI4 vSphere 5
0 Kudos
akutz
Hot Shot
Hot Shot

Try running the script with "perl -w vminfo.pl"

0 Kudos
akutz
Hot Shot
Hot Shot

Wait, you added the script verbatim from what was posted in this thread? The post was missing the script interpreter line at the beginning. Without the shell does not know which script engine to execute the script with. Put this at the very top of the script:

#!/usr/bin/perl -w

0 Kudos
johnswb
Enthusiast
Enthusiast

do I still need to run it as perl -w vminfo.pl for the command line? Never mind I get the following now... now matter how I run it.

$ perl -w vminfo.pl

Can't modify subtraction :smileyminus: in scalar assignment at vminfo.pl line 2, near ""https://vc.server.name.com/sdk";"

BEGIN not safe after errors--compilation aborted at vminfo.pl line 6.

Will Johnson VCP on VI3 / VI4 vSphere 5
0 Kudos
akutz
Hot Shot
Hot Shot

No.

0 Kudos
stumpr
Virtuoso
Virtuoso

Couple things, one which Akutz hit on.

You need #!/usr/bin/perl as the first line of your script and you need to set the execute bit on the file before you can run it by just typing './vminfo.pl'. Or you can just pass it as an argument to perl.... 'perl vminfo.pl' and then the shebang and exe bit aren't required.

Second, the first line has a minus '-' in the variable name. No no. Change that to an underscore. "$service_url". The rest looks like the your shell interpreter kicking out single line commands as akutz pointed out.

Also, if you copy/pasted it from the web page here, you may see \xC2 character errors. I believe this is just Safari and IE that interpret &nbsp with ASCII 194 or 202 instead of ASCII 32. You'll have to retype it or just delete whitespace around the line in question and reapply your spaces/tabs.

The "-w" at the end of the shebang line is to enable warnings in Perl. Usually a good idea, or you can just say 'use warnings'; in your script after the shebang.

R.

Reuben Stump | http://www.virtuin.com | @ReubenStump
0 Kudos
johnswb
Enthusiast
Enthusiast

stumpr, I made the changes both you and Akutz said and now I get the following.

$ perl vminfo.pl

Error: Server version unavailable at 'https://vc.server.name.com/sdk/vimService.wsdl' :1: parser error : Start tag expected, '&lt;' not found

LWP will support https URLs if the Crypt::SSLeay module is installed.

^ at /usr/share/perl/5.8/VMware/VICommon.pm line 267

Will Johnson VCP on VI3 / VI4 vSphere 5
0 Kudos
akutz
Hot Shot
Hot Shot

sudo cpan Crypt::SSLeay

0 Kudos
johnswb
Enthusiast
Enthusiast

I did "sudo cpan Crypt::SSLeay" and that went fine. I'm now getting the following.

$ perl vminfo.pl

The component 'privilegeList' has already been installed in class &gt;AuthorizationManager&lt; as a HASH(0x848b028)

(this time a scalar)

at /usr/lib/perl/5.8/Class/MethodMaker.pm line 20

BEGIN failed--compilation aborted at /usr/share/perl/5.8/VMware/VIM25Runtime.pm line 17.

Compilation failed in require at (eval 27) line 3.

Will Johnson VCP on VI3 / VI4 vSphere 5
0 Kudos
akutz
Hot Shot
Hot Shot

Change

use VMware::VIM2Runtime;

to

use VMware::VIRuntime;

View solution in original post

0 Kudos
johnswb
Enthusiast
Enthusiast

akutz, that did it thx a mill.

One last Noob question... do you know I can get the results to out put to a file? example .xls or .pdf

Never mind... I figured it out... I did the follwoing " $ ./vminfo.pl &gt; vminfo.xls "

The real question is can I use the same script to get the Host info as well? example Disk-used-free-total Memory-used-free-total CPU-used-free-total

I have ESX servers with SAN attached LUN's that I would like to run a script against if possible.

Will Johnson VCP on VI3 / VI4 vSphere 5
0 Kudos