PowerCLI

 View Only
Expand all | Collapse all

How to generate alarms or events using any scripts?

  • 1.  How to generate alarms or events using any scripts?

    Posted May 09, 2013 12:22 PM

    Hi

    I want to test a network management application which also manages the VMs and hosts in the device. It also displays the alarms and events generated by the vCenter.

    Is it possible to generate the vm events using any scripts so that I can directly trigger the events into the application instead of changing any configuration of the VMs?

    Thanks in advance.

    Prateek



  • 2.  RE: How to generate alarms or events using any scripts?

    Posted May 09, 2013 01:48 PM

    To generate alarms will be a matter of setting an alarm, for example CPU usage, on a specific VM, with a sufficiently low threshold.

    Events are generated by almost any action inside vSphere.

    So you could create or move a VM, logon/logoff from vCenter....

    You can also fire a user specified event with the LogUserEvent method.

    Let me know if you a sample of that ?



  • 3.  RE: How to generate alarms or events using any scripts?

    Posted May 10, 2013 04:16 AM

    Thanks LucD.

    If you can share a sample as you had mentioned it would be helpful!

    I have a scenario here. I have an application which will display all the events generated by the vCenter. I have to test the application. The test scenario is that I need to fetch events generated by many VMs altogether(say 100) to the application and test if the application is displaying the events as required.

    Another test scenario is that if I want to fetch the same event 100 times from the same VM to the application.

    Is this possible if we use any scripts or in any way?

    Thanks in advance.



  • 4.  RE: How to generate alarms or events using any scripts?

    Posted May 10, 2013 07:27 AM

    Sure, try something like this

    .In this example I generate the event against a random VM, but you can change this to any other vSphere object.

    $evtMgr = Get-View EVentManager
    $entity = Get-VM | Get-Random
    $evtMgr.LogUserEvent($entity.ExtensionData.MoRef,"User test event")


  • 5.  RE: How to generate alarms or events using any scripts?

    Posted May 13, 2013 09:42 AM

    Thanks LucD for the reply.

    Actually I am very new to PowerShell and PowerCLI. Have just started to learn.

    Can u please post a sample scenario full script (From making a connection to host to generating a event at any VM/Host)?

    Thanks again!



  • 6.  RE: How to generate alarms or events using any scripts?

    Posted May 13, 2013 12:08 PM

    Are you connecting to an ESXi server or a vCenter ?

    In case of an ESXi server you can do

    $esxName = "MyEsx"
    $esxUser = "root"
    $esxPswd = "MyPswd"

    Connect-VIServer -Server $esxName -user $esxUser -Password $esxPswd

    $si = Get-View ServiceInstance
    $evtMgr = Get-View $si.Content.EventManager
    $entity = Get-VMHost -Name $esxName
    $evtMgr.LogUserEvent($entity.ExtensionData.MoRef,"User test event")

    Disconnect-VIServer -Server $esxName -Confirm:$false


  • 7.  RE: How to generate alarms or events using any scripts?

    Posted May 14, 2013 05:14 AM

    Thanks LucD!

    I am using ESXi server and the above code was very helpful and is running absolutely fine.

    So I can create user created events and can find the event in the log as type 'user'. Is it possible to generate system defined events by the user?

    For example,

    Event: VmPoweredOffEvent (Type: Info)

              VmFailedToPowerOnEvent (Type: Minor)



  • 8.  RE: How to generate alarms or events using any scripts?

    Posted May 14, 2013 05:53 AM

    That is possible with the PostEvent method.

    The difficulty is that you need to know how to compose the specific event you want to create.

    The following script will generate a VmFailedToPowerOnEvent, where I only populated the required properties.

    As you can see, not as simple as generating a user event, but possible :smileygrin:

    $vmName = "MyVM"
    $userName = "testuser"

    $evtMgr = Get-View EVentManager

    $event = New-Object VMware.Vim.VmFailedToPowerOnEvent
    $event.chainId = 1
    $event.CreatedTime = Get-Date
    $event.Key = 1
    $event.Username = $userName
    $event.Template = $false
    $event.Reason = New-Object VMware.Vim.LocalizedMethodFault
    $event.Reason.fault = New-Object VMware.Vim.InvalidPowerState
    $event.Reason.fault.existingState = "poweredOn"
    $event.Reason.fault.requestedState = "poweredOff"
    $event.Reason.LocalizedMessage = "Testing event"
    $event.VM = New-Object VMware.Vim.VmEventArgument
    $event.Vm.Name = $vmName
    $event.VM.vm = (Get-VM -Name $vmName).ExtensionData.MoRef

    $evtMgr.PostEvent($event,$null)


  • 9.  RE: How to generate alarms or events using any scripts?

    Posted May 14, 2013 09:51 AM

    When I try to run the above script using $evtMgr = Get-View EVentManager

    I am getting the following error:

    Get-View        View with Id

    'EventManager-EventManager' was not found on the server(s).

    At C:\Myscripts\vmfailedtopoweron.ps1:3 char:11

    + $evtMgr = Get-View EventManager

    +           ~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : ObjectNotFound: (:) [Get-View], VimException

        + FullyQualifiedErrorId : Core_GetView_WriteNotFoundError,VMware.VimAutoma

       tion.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

    You cannot call a method on a null-valued expression.

    At C:\Myscripts\vmfailedtopoweron.ps1:18 char:1

    + $evtMgr.PostEvent($event,$null)

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

        + FullyQualifiedErrorId : InvokeMethodOnNull

    But when I try with

    $si = Get-View ServiceInstance

    $evtMgr = Get-View $si.Content.EventManager

    I don't get any error but I don't see the event generated in the specified VM. Why is it so?

    And how do i figure out how to compose the specific event that I want to create? :smileyhappy:



  • 10.  RE: How to generate alarms or events using any scripts?

    Posted May 14, 2013 09:54 AM

    Are you connected (Connect-VIServer) to a vCenter or an ESXi host ?



  • 11.  RE: How to generate alarms or events using any scripts?

    Posted May 14, 2013 09:57 AM

    I am connected to an ESXi host



  • 12.  RE: How to generate alarms or events using any scripts?

    Posted May 14, 2013 10:41 AM

    In that case try replacing

    $evtMgr = Get-View EVentManager

    with

    $si = Get-View ServiceInstance

    $evtMgr = Get-View $si.Content.EventManager



  • 13.  RE: How to generate alarms or events using any scripts?

    Posted May 14, 2013 10:58 AM

    That is what I tried with. I don't get any errors but there is no event generated at the VM that I had specified in the script



  • 14.  RE: How to generate alarms or events using any scripts?

    Posted May 14, 2013 11:20 AM

    Just tried it myself while connected to an ESXi server, I see the same behavior as you.

    When connected to a vCenter it works. And that eventtype exists on an ESXi server.

    I'll do some further digging.



  • 15.  RE: How to generate alarms or events using any scripts?

    Posted May 15, 2013 12:09 PM

    Just for clarification. What you are doing in the above script is that you are creating a VmFailedToPowerOnEvent object. And to specify the reason for the event you are creating another object LocalizedMethodFault. In this case, you are trying to poweron the VM but since it is already poweredon an error is thrown. Am I right?

    So for every event I have to generate event by manipulating the scenarios rather than directly generating event(if possible)?



  • 16.  RE: How to generate alarms or events using any scripts?

    Posted May 15, 2013 03:25 PM

    That is correct.

    And yes, to use with the PostEvent method you will have to construct the event object.