VMware Cloud Community
brtlvrs
Enthusiast
Enthusiast
Jump to solution

Howto log a custom message / event in VM virtual center

Is there a way to log a custom message to VM virtual center ?

I use a vCLI script to reset a vm. I want to report from where (which PC ) this script is initiated.

And it would be great if it is possible to report this to the event or task list of the VM which is going to be reset.

Please consider marking this answer "CORRECT" or "Helpful" if you think your question have been answered correctly.
Tags (3)
0 Kudos
1 Solution

Accepted Solutions
VTsukanov
Virtuoso
Virtuoso
Jump to solution

You can report from where and who this script is initiated by following script

#!/usr/bin/perl -w                                                                                
# CustomEvent.pl

# 
use strict;
use warnings;
use VMware::VILib;
use VMware::VIRuntime;

my %opts = (
   vmname => {
      type => "=s",
      help => "Name the virtual machine associated with the event",
      required => 1,
   },    
);

Opts::add_options(%opts);

Opts::parse();
Opts::validate();
Util::connect();

my $vmname  = Opts::get_option('vmname');
my $username, my $compname;
($username = `echo %USERNAME%`) =~ /(\S+)/; $username = $1;
($compname = `echo %COMPUTERNAME%`) =~ /(\S+)/; $compname = $1;

my $EventManager = Vim::get_service_content()->eventManager;
my $EventManager_view = Vim::get_view(mo_ref => $EventManager);
my $vm_ref = Vim::find_entity_view(view_type => 'VirtualMachine', filter => {'name' => $vmname});
eval {
    $EventManager_view->LogUserEvent(entity => $vm_ref, msg => "CE_RESTARTVM : VM $vmname restarted by $username from $compname.");
    Util::trace(0, "\n CUSTOMEVENT \'VM $vmname restarted by $username from $compname.\' post Successfully.\n");
};
if ($@) {print "Error: ". $@ . "\n";}
Util::disconnect();

View solution in original post

0 Kudos
3 Replies
VTsukanov
Virtuoso
Virtuoso
Jump to solution

You can report from where and who this script is initiated by following script

#!/usr/bin/perl -w                                                                                
# CustomEvent.pl

# 
use strict;
use warnings;
use VMware::VILib;
use VMware::VIRuntime;

my %opts = (
   vmname => {
      type => "=s",
      help => "Name the virtual machine associated with the event",
      required => 1,
   },    
);

Opts::add_options(%opts);

Opts::parse();
Opts::validate();
Util::connect();

my $vmname  = Opts::get_option('vmname');
my $username, my $compname;
($username = `echo %USERNAME%`) =~ /(\S+)/; $username = $1;
($compname = `echo %COMPUTERNAME%`) =~ /(\S+)/; $compname = $1;

my $EventManager = Vim::get_service_content()->eventManager;
my $EventManager_view = Vim::get_view(mo_ref => $EventManager);
my $vm_ref = Vim::find_entity_view(view_type => 'VirtualMachine', filter => {'name' => $vmname});
eval {
    $EventManager_view->LogUserEvent(entity => $vm_ref, msg => "CE_RESTARTVM : VM $vmname restarted by $username from $compname.");
    Util::trace(0, "\n CUSTOMEVENT \'VM $vmname restarted by $username from $compname.\' post Successfully.\n");
};
if ($@) {print "Error: ". $@ . "\n";}
Util::disconnect();

0 Kudos
brtlvrs
Enthusiast
Enthusiast
Jump to solution

thanks....

sry for the response time... but vacation ..... what can I say...

Please consider marking this answer "CORRECT" or "Helpful" if you think your question have been answered correctly.
0 Kudos
brtlvrs
Enthusiast
Enthusiast
Jump to solution

thanks....

sry for the response time... but vacation ..... what can I say...

Please consider marking this answer "CORRECT" or "Helpful" if you think your question have been answered correctly.
0 Kudos