VMware Cloud Community
Benomatic
Contributor
Contributor

Script performed from Alarm failed

Hi Folks,

I hope anyone can help me on a Powershell script that will perform from the Alarm environmen.

-CODE-

$output = "C:\Scripts\send2tec\env.txt"

$tecServer = "my.tivoli.server.com"

$tecClass = "VMWARE_Alarm"

if( Test-Path ENV:VMWARE_ALARM_NAME )

{

$env = @{}

$vname = Get-ChildItem ENV:VMWARE_ALARM_NAME

$env[[$vname.Name]|http://$vname.name/] = $vname.Value

$tname = Get-ChildItem ENV:VMWARE_ALARM_TARGET_NAME

$env[[$tname.Name]|http://$tname.name/%5D] = $tname.Value

$severity = Get-ChildItem ENV:VMWARE_ALARM_NEWSTATUS

$description = Get-ChildItem ENV:VMWARE_ALARM_EVENTDESCRIPTION

$env[[$description.Id]|http://$description.id/%5D] = $description.Value

switch( $severity.Value )

{

"Green" {$env[[$severity.Name|http://$severity.name/%5D]] = "HARMLESS"}

"Yellow" {$env[[$severity.Name]|http://$severity.name/] = "WARNING"}

"Red" {$env[[$severity.Name]|http://$severity.name/%5D] = "CRITICAL"}

default

}

postemsg -S $tecServer -r $env[[$severity.Name]|http://$severity.name/] -m "`"$env`"" hostname="$env[$tecClass] Sentry

$env | Format-Table Name, @{expression="Value";width=45} -Wrap | Out-File -Append $output

if($error[0] -ne $null)

{

$error[0] | Out-File -Append "C:\Scripts\send2tec\error.log"

}

}

-CODE-

I piped the postemsg to a Text file and get following string:

-CODE-

postemsg -S my.tivoli.server.com -r System.Collections.Hashtable[VMWARE_ALARM_NEWSTATUS] -m "System.Collections.Hashtable[[VMWARE_ALARM_EVENTDESCRIPTION]|http://communities.vmware.com/community-document-picker.jspa?communityID=&subject=VMWARE_ALARM_EVENTDESCRIPTION]" hostname=System.Collections.Hashtable[[VMWARE_ALARM_TARGET_NAME|http://communities.vmware.com/community-document-picker.jspa?communityID=&subject=VMWARE_ALARM_TARGET_NAME]] VMWARE_Alarm Sentry

how can I use hashkeys in a command String ?

I hope that anyone have an idea

kind regards

benomatic

0 Kudos
8 Replies
harkamal
Expert
Expert

Hi Benomatic,

I have been creating alarms and kicking off scripts as alarm actions using powershell.

But I was not able to use the alarm variables in scripts...how does that work, can you guide.

ENV:VMWARE_ALARM_NAME ??

ENV:VMWARE_ALARM_TARGET_NAME ??

Code to create a alarm and kick off script is as follows....

$si = Get-View ServiceInstance
$alm = Get-View $si.Content.AlarmManager


$sae = New-Object VMware.Vim.StateAlarmExpression
$sae.Operator = "isEqual"
$sae.Type = "HostSystem"
$sae.StatePath = "runtime.connectionState"
$sae.Red = "connected"


$oae = New-Object VMware.Vim.OrAlarmExpression
$oae.Expression += $sae 


$rsa = New-Object VMware.Vim.RunScriptAction
$rsa.script = "cmd.exe /c c:\run.bat "{eventDescription}" "{targetName}" "{alarmName}" """


$atats = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec
$atats.StartState = "yellow"
$atats.FinalState = "red"
$atats.Repeats = $false


$ata = New-Object VMware.Vim.AlarmTriggeringAction
$ata.Action = $rsa
$ata.TransitionSpecs = $atats


$gaa = New-Object VMware.Vim.GroupAlarmAction
$gaa.Action += $ata


$alarm = New-Object VMware.Vim.AlarmSpec
$alarm.Name = "my_Alarm2009"
$alarm.Description = "myDesc"
$alarm.Enabled = $true
$alarm.Expression = $oae #
$alarm.Action = $gaa
$alarm.ActionFrequency = 0
$alarm.Setting = New-Object VMware.Vim.AlarmSetting


$alm.CreateAlarm($si.Content.RootFolder,$alarm)


0 Kudos
Benomatic
Contributor
Contributor

Hi Harkamal,

after a Alarm the VC Server set some Enviromental Variables that u can use to fill out your script with some information:

VMWARE_ALARM_TARGET_NAME

VMWARE_ALARM_NEWSTATUS

VMWARE_ALARM_EVENTDESCRIPTION

My Script will start after alarm and the script check if one ENV Variable is exist, after them all needed Variable will collect in a hash. And here start my problem how can I use hashkeys like above in a command string.

cheers

0 Kudos
harkamal
Expert
Expert

Well i am still not not getting it.

I coded alarm that activates when i attach a host to vc and that kicks off a batch file.

After the alarm rings, i don't see any alarm variable , neither from command prompt not from powershell/powercli on virtual center

cmd.exe > set -no variable with name vmware*

powershell > cd env: > dir -no variable with name vmware*

look forward to hear from you

0 Kudos
Benomatic
Contributor
Contributor

you can create ps1 Script with following code:

Get-ChildItem Env: | Out-File -Append C:\env.txt

and add to your batch script following line:

powershell -Command "& C:\yourPowershellScript.ps1"

you will see that after alarm you get some ENV Variables with all information about the alarm

I hope any one can help me with my hash table issue

cheers

0 Kudos
Benomatic
Contributor
Contributor

so I have now the correct String for the postemessage command:

& {"postemsg -S "$tecServer" -r "$env[http://$severity.Name]" -m ""`""$env[http://$description.Name]"`""" hostname="$env[http://$tname.Name]" "$tecClass" Sentry"}

but it seems that the postemsg command will not excecute from the Powershell. The postemsg.exe is the C:\Windows\system32\ folder and from the commandline works fine, only performed from VMware Alarm Envoirement will not work.

Any Idea ?

btw: the forum editor here is crap, how can I get code without that the editor broken my code ?

0 Kudos
harkamal
Expert
Expert

Try dotsourcing the executable while running from powershell.

.\postemsg

0 Kudos
harkamal
Expert
Expert

This is how i create collections...

$colEnv = @()

1..5 | %{ 

$env = "" | Select vmname, tsname, description
$env.vmname = "vmname" + $_ 
$env.tsname = "tsname" + $_
$env.description = "description" + $_
$colEnv += $env

}

$colEnv

0 Kudos
harkamal
Expert
Expert

was that of any help ?

0 Kudos