VMware Cloud Community
qwert1235
Enthusiast
Enthusiast
Jump to solution

modify alarm setting (actions)

Hello:

I need to modify settings for my existing alarm by script. I need to add SNMP action to specific alarm (I know how to filter by name).

Can somebody please help me with it?

Thanks,

qwert

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I tested with an alarm that had 1 action (send email) and the adapted script below seems to work.

$entity = Get-View (Get-Folder -Name "Datacenters").ID 
$alarmMgr = get-view AlarmManager

$tgtAlarmName = "Test Alarm"

$alarms = $alarmMgr.GetAlarm($null)
$alarms | % {
	$alarm = Get-View $_
	if($alarm.Info.Name -eq $tgtAlarmName){
		$spec = New-Object VMware.Vim.AlarmSpec
		$spec.Expression = $alarm.Info.Expression
		$spec.Setting = $alarm.Info.Setting
		$spec.Name = $alarm.Info.Name
		$spec.Enabled = $alarm.Info.Enabled
		$spec.Description = $alarm.Info.Description

		$spec.Action = New-Object VMware.Vim.GroupAlarmAction
		$action = New-Object VMware.Vim.AlarmTriggeringAction
		$action.Action = New-Object VMware.Vim.SendSNMPAction
		$trans = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec
		$trans.StartState = "yellow"
		$trans.FinalState = "red"
		$action.TransitionSpecs += $trans

		$spec.Action.Action = $alarm.Info.Action.Action
		$spec.Action.Action += $action
		
		$alarm.ReconfigureAlarm($spec)
	}
}

Note1: you don't need to do the LoadWithPartialName anymore since VITK 1.5

Note2: for the AlarmManager, the Get-View has a direct pointer. No need to go via ServiceInstance anymore.

Note3: first I copy over the existing actions from $alarm.Info.Action and then I add the new $action


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

Have a look at my scripts in CreateAlarm not (always) compatible with the vSphere client.

In there I use a SendSNMPAction action.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

qwert1235
Enthusiast
Enthusiast
Jump to solution

Luc,

I know how to create Alarm that does NOT exist yet with action - thanks to your post (see attachment).

However, I cannot figure out how I can just add action to the existing alarm...

I guess, I have to use EditAlarm or ReconfigureAlarm, but have problem with it...

I really appreciate the help.

Thanks,

qwert

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You have to use the ReconfigureAlarm method

Something like this

$tgtAlarmName = <alarm-name>
$alarmMgr = Get-View AlarmManager
$alarms = $alarmMgr.GetAlarm($null)
$alarms | %{
	$alarm = Get-View $_
	if($alarm.Info.Name -eq $tgtAlarmName){
		$spec = New-Object VMware.Vim.AlarmSpec
		$spec.Action = $alarm.Info.Action
		$spec.Expression = $alarm.Info.Expression
		$spec.Setting = $alarm.Info.Setting
		$spec.Name = $alarm.Info.Name
		$spec.Description = $alarm.Info.Description
		$alarmTrigger = New-Object VMware.Vim.AlarmTriggeringAction
		$alarmTrigger.TransitionSpecs =  New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec
		$alarmTrigger.TransitionSpecs[0].StartState = $spec.Action.Action[0].TransitionSpecs[0].StartState
		$alarmTrigger.TransitionSpecs[0].FinalState = $spec.Action.Action[0].TransitionSpecs[0].FinalState
		$alarmTrigger.Action = New-Object VMware.Vim.SendEmailAction
		$alarmTrigger.Action.toList = "luc.dekens@lucd.info"
		$alarmTrigger.Action.CcList = ""
		$alarmTrigger.Action.Subject = "Alarm subject"
		$alarmTrigger.Action.Body = ""
		$spec.Action.Action += $alarmTrigger
		$alarm.ReconfigureAlarm($spec)
	}
}

I hope this puts you on the right track.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

qwert1235
Enthusiast
Enthusiast
Jump to solution

Luc,

Thank you very much for your help. Unfortunately, it’s still does not work.

You code gives error “$alarmTrigger.TransitionSpecs[0].StartState = $spec.Action.Action[ <<<< 0].TransitionSpecs[0].StartState”

Also, looks like I have to add line $spec.Enabled = $alarm.Info.Enabled for the alarm to stay enabled.

I tried to modify the code and in my last draft it looks like this:

http://Reflection.Assembly::LoadWithPartialName("vmware.vim")

$svcRef = new-object VMware.Vim.ManagedObjectReference

$svcRef.Type = "ServiceInstance"

$svcRef.Value = "ServiceInstance"

$serviceInstance = get-view $svcRef

$entity = Get-View (Get-Folder -Name "Datacenters").ID $alarmMgr = get-view $serviceInstance.Content.alarmManager

$tgtAlarmName = "TEST_ALARM"

$alarmMgr = get-view $serviceInstance.Content.alarmManager

$alarms = $alarmMgr.GetAlarm($null)

$alarms | %{

$alarm = Get-View $_

if($alarm.Info.Name -eq $tgtAlarmName){

$spec = New-Object VMware.Vim.AlarmSpec

$spec.Expression = $alarm.Info.Expression

$spec.Setting = $alarm.Info.Setting

$spec.Name = $alarm.Info.Name

$spec.Enabled = $alarm.Info.Enabled

$spec.Description = $alarm.Info.Description

$spec.Action = New-Object VMware.Vim.GroupAlarmAction

$action = New-Object VMware.Vim.AlarmTriggeringAction

$action.Action = New-Object VMware.Vim.SendSNMPAction

$trans = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec

$trans.StartState = "yellow"

$trans.FinalState = "red"

$action.TransitionSpecs += $trans

$alarm.ReconfigureAlarm($spec)

}

}

But when I am running it I got error on my last line:

“Exception calling "ReconfigureAlarm" with "1" argument(s): "Not initialized: vim.alarm.AlarmAction[] action" $alarm.ReconfigureAlarm <<<< ($spec)”

Can you please point me what to mistakes in this code?

Thanks,

qwert

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I tested with an alarm that had 1 action (send email) and the adapted script below seems to work.

$entity = Get-View (Get-Folder -Name "Datacenters").ID 
$alarmMgr = get-view AlarmManager

$tgtAlarmName = "Test Alarm"

$alarms = $alarmMgr.GetAlarm($null)
$alarms | % {
	$alarm = Get-View $_
	if($alarm.Info.Name -eq $tgtAlarmName){
		$spec = New-Object VMware.Vim.AlarmSpec
		$spec.Expression = $alarm.Info.Expression
		$spec.Setting = $alarm.Info.Setting
		$spec.Name = $alarm.Info.Name
		$spec.Enabled = $alarm.Info.Enabled
		$spec.Description = $alarm.Info.Description

		$spec.Action = New-Object VMware.Vim.GroupAlarmAction
		$action = New-Object VMware.Vim.AlarmTriggeringAction
		$action.Action = New-Object VMware.Vim.SendSNMPAction
		$trans = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec
		$trans.StartState = "yellow"
		$trans.FinalState = "red"
		$action.TransitionSpecs += $trans

		$spec.Action.Action = $alarm.Info.Action.Action
		$spec.Action.Action += $action
		
		$alarm.ReconfigureAlarm($spec)
	}
}

Note1: you don't need to do the LoadWithPartialName anymore since VITK 1.5

Note2: for the AlarmManager, the Get-View has a direct pointer. No need to go via ServiceInstance anymore.

Note3: first I copy over the existing actions from $alarm.Info.Action and then I add the new $action


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
qwert1235
Enthusiast
Enthusiast
Jump to solution

Luc,

Thank you very much for your help!!!

It does work!!!

Thanks again,

qwert

0 Kudos
apapap
Enthusiast
Enthusiast
Jump to solution

Hey LucD, can u help me creating the VI Java equivalent of this ?

Here is my code :

<Code>

import java.net.URL;
import com.vmware.vim25.Action;
import com.vmware.vim25.AlarmAction;
import com.vmware.vim25.AlarmSetting;
import com.vmware.vim25.AlarmSpec;
import com.vmware.vim25.AlarmTriggeringAction;
import com.vmware.vim25.AlarmTriggeringActionTransitionSpec;
import com.vmware.vim25.EventAlarmExpression;
import com.vmware.vim25.GroupAlarmAction;
import com.vmware.vim25.ManagedEntityEventArgument;
import com.vmware.vim25.ManagedEntityStatus;
import com.vmware.vim25.MethodAction;
import com.vmware.vim25.MethodActionArgument;
import com.vmware.vim25.SendEmailAction;
import com.vmware.vim25.SendSNMPAction;
import com.vmware.vim25.StateAlarmExpression;
import com.vmware.vim25.StateAlarmOperator;
import com.vmware.vim25.mo.AlarmManager;
import com.vmware.vim25.mo.Datacenter;
import com.vmware.vim25.mo.HostSystem;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.ManagedEntity;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.VirtualMachine;
public class CreateDcAlarm
{
public static void main(String[] args) throws Exception
{
ServiceInstance si = new ServiceInstance(new URL("https://<VC-SERVER IP>/sdk"), "Administrator", "Password", true);
InventoryNavigator inv = new InventoryNavigator(si.getRootFolder());
ManagedEntity[] mes =  new InventoryNavigator(si.getRootFolder()).searchManagedEntities("HostSystem");
   HostSystem host = (HostSystem) mes[0];
   Datacenter dc = (Datacenter) host.getParent().getParent().getParent();
   String dcName = dc.getName();
   System.out.println("Datacenter Name : "+dcName);
AlarmManager alarmMgr = si.getAlarmManager();
AlarmSpec spec = new AlarmSpec();
EventAlarmExpression exp = createEventAlarmExpression();
AlarmTriggeringAction trigger = new AlarmTriggeringAction();
AlarmTriggeringActionTransitionSpec trans1a = new AlarmTriggeringActionTransitionSpec();
trans1a.setStartState(ManagedEntityStatus.green) ;
trans1a.setFinalState(ManagedEntityStatus.yellow) ;
AlarmTriggeringActionTransitionSpec trans1b = new AlarmTriggeringActionTransitionSpec();
trans1b.setStartState(ManagedEntityStatus.yellow) ;
trans1b.setFinalState(ManagedEntityStatus.red) ;
trigger.transitionSpecs = new AlarmTriggeringActionTransitionSpec();
GroupAlarmAction gaa = new GroupAlarmAction();
gaa.setAction(new AlarmAction[]{trigger});
spec.setName("DcAlarm");
spec.setDescription("Monitor DC state");
spec.setEnabled(true);
spec.setExpression(exp);
spec.setAction(gaa);
AlarmSetting as = new AlarmSetting();
as.setReportingFrequency(0); //as often as possible
as.setToleranceRange(0);
spec.setSetting(as);
alarmMgr.createAlarm(dc, spec);
si.getServerConnection().logout();
}
static EventAlarmExpression createEventAlarmExpression() {
// TODO Auto-generated method stub
EventAlarmExpression exp = new EventAlarmExpression();
exp.setEventType("DatacenterRenamedEvent");
exp.setObjectType("Datacenter");
exp.setStatus(ManagedEntityStatus.yellow) ;
return exp;
}
}

</Code>

Please help

Thanks in advance.

Message was edited by: apapap

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sorry to disappoint you, but my Java knowledge is $null.

Why don't you try your luck in the VI Java forum ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
apapap
Enthusiast
Enthusiast
Jump to solution

Ok. Thanks for the quick response.

0 Kudos
qwert1235
Enthusiast
Enthusiast
Jump to solution

Luc,

Can you please help me figure out what am I missing on my server that preventing me to run "Get-View AlarmManager"?

Here is the error I am getting:

PS C:\temp> $alarmManager = Get-View AlarmManager
Get-View : 5/23/2012 1:00:14 PM    Get-View
At line:1 char:25
+ $alarmManager = Get-View <<<<  AlarmManager
    + CategoryInfo          : NotSpecified: (:) [Get-View], NotAuthenticated
    + FullyQualifiedErrorId : Client20_MoServiceImpl_GetNetInteropView_ViError
   ,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

Get-View : 5/23/2012 1:00:14 PM    Get-View        View with Id  'AlarmManager-
AlarmManager' was not found on the server(s).
At line:1 char:25
+ $alarmManager = Get-View <<<<  AlarmManager
    + CategoryInfo          : ObjectNotFound: (:) [Get-View], VimException
    + FullyQualifiedErrorId : Core_GetView_WriteNotFoundError,VMware.VimAutoma
   tion.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

PS C:\temp>

I am using vSphere PowerCLI 5.0.1.4431 and PowerShell 2.0.  It's working for me on another server wher I am running the same version of PowerCLI and PowerShell... I think I am missing something; but what?

Thank you,

qwert

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you perhaps connected to a vCenter ?

That error normally appears when connected to an ESXi server.

Btw, in the current PowerCLI version there is no need anymore to use the SDK methods to change Alarm actions.

There are now cmdlets for that, see New-AlarmAction.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
qwert1235
Enthusiast
Enthusiast
Jump to solution

Luc,

Thanks a lot!

No, I connected to VC, not to ESX host, but you were right it was connection issue.

Thanks again,

qwert

0 Kudos