-
Notifications
You must be signed in to change notification settings - Fork 503
Expand file tree
/
Copy pathgetVINData.pl
More file actions
executable file
·98 lines (79 loc) · 3.65 KB
/
getVINData.pl
File metadata and controls
executable file
·98 lines (79 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/perl
# Author: William Lam
# Website: www.williamlam.com
# Reference: http://www.williamlam.com/2012/11/extracting-information-from-vin-vsphere_8.html
use strict;
use JSON;
use JMX::Jmx4Perl;
use JMX::Jmx4Perl::Request;
# command line args
if($#ARGV != 1) {
print "\n\tUsage: $0 [VIN_HOST] [VM_NAME]\n\n";
exit 1;
}
# global vars
my $vin_host = $ARGV[0];
my $vmname = $ARGV[1];
my ($jmx,$request,$response,$vm,$ret);
# connect to VIN jmx
$jmx = new JMX::Jmx4Perl(url => "http://$vin_host:8080/jolokia");
# query vCenter Server that VIN is connected to
$request = new JMX::Jmx4Perl::Request({type => READ,
mbean => "com.vmware.vadm:name=inceptionConfigurationMBean,type=ConfigurationMBean",
attribute => "vc.credentials.host"});
$response = $jmx->request($request);
print "\nVIN is connected to vCenter Server: " . $response->value() . "\n";
# query the VM given using getVmByName() given by the user
$vm = $jmx->execute("com.vmware.vadm:name=vcInventory,type=VcConnector","getVmByName",$vmname);
if(!defined($vm)) {
print "Unable to locate VM: " . $vmname . "\n\n";
exit 1;
}
# VM info
print "\nVM Hostname: " . $vm->{'vm_hostname'} . "\n";
print "IP Addresses: " . $vm->{'vm_all_ips'} . "\n";
print "Power State: " . $vm->{'vm_power_state'} . "\n";
print "OS: " . $vm->{'vm_os_name'} . "\n";
print "MoRef ID: " . $vm->{'vm_moid'} . "\n";
# input to other methods require JSON format for VM's moref
my %moref = ("moid" => $vm->{'vm_moid'});
my $moref_array = [ {"moid" => $vm->{'vm_moid'}} ];
print "\nApplications\n\n";
# query applications running on VM using findApplicationComponentsByInfrastructureElementBusinessKeys() & moref input
$ret = $jmx->execute("com.vmware.vadm:name=ApplicationService,type=ApplicationService","findApplicationComponentsByInfrastructureElementBusinessKeys",to_json($moref_array));
# loop through each application
foreach(@$ret) {
if(defined($_->{'parentBusinessKey'})) {
if($_->{'parentBusinessKey'} eq $vm->{'vm_moid'}) {
if(defined($_->{'servicePort'})) {
print "Product Name: " . $_->{'productName'} . "\n";
print "Category: " . $_->{'category'} . "\n";
print "Vendor: " . $_->{'vendor'} . "\n";
print "Version: " . $_->{'version'} . "\n";
print "Process Name: " . $_->{'processName'} . "\n";
print "Install Path: " . $_->{'installPath'} . "\n";
print "Ports: " . join(',',@{$_->{'servicePort'}}) . "\n\n";
}
}
}
}
print "Dependencies\n\n";
# query outgoing dependencies using of VM using findOutgoingDependentInfrastructureElements() & moref input
$ret = $jmx->execute("com.vmware.vadm:name=ApplicationService,type=ApplicationService","findOutgoingDependentInfrastructureElements",to_json(\%moref),to_json($moref_array));
# loop through each dependency
foreach(@$ret) {
my $vmObj = getVmByMoid($_->{'businessKey'});
if($vmObj ne "N/A") {
print "VM: " . $vmObj->{'vm_name'} . "\t Hostname: " . $vmObj->{'vm_hostname'} . "\t MoRef: " . $vmObj->{'vm_moid'} . "\n";
}
}
print "\n";
# helper method to query VM based on moref using getVmByMoid()
sub getVmByMoid {
my ($moref) = @_;
my $name = $jmx->execute("com.vmware.vadm:name=vcInventory,type=VcConnector","getVmByMoid",$moref);
if(!defined($name)) {
$name = "N/A";
}
return $name;
}