public static void main(String[] args) throws Exception { if(args.length != 4) { System.out.println("Usage: java CreateVmAlarm " + "<url> <username> <password> <vmname>"); return; } ServiceInstance si = new ServiceInstance( new URL(args[0]), args[1], args[2], true); String vmname = args[3]; InventoryNavigator inv = new InventoryNavigator( si.getRootFolder()); VirtualMachine vm = (VirtualMachine)inv.searchManagedEntity( "VirtualMachine", vmname); if(vm==null) { System.out.println("Cannot find the VM " + vmname + "\nExisting..."); si.getServerConnection().logout(); return; } AlarmManager alarmMgr = si.getAlarmManager(); AlarmSpec spec = new AlarmSpec(); StateAlarmExpression expression = createStateAlarmExpression(); AlarmAction emailAction = createAlarmTriggerAction( createEmailAction()); AlarmAction methodAction = createAlarmTriggerAction( createPowerOnAction()); GroupAlarmAction gaa = new GroupAlarmAction(); gaa.setAction(new AlarmAction[]{emailAction, methodAction}); spec.setAction(gaa); spec.setExpression(expression); spec.setName("VmPowerStateAlarm"); spec.setDescription("Monitor VM state and send email " + "and power it on if VM powers off"); spec.setEnabled(true); AlarmSetting as = new AlarmSetting(); as.setReportingFrequency(0); //as often as possible as.setToleranceRange(0); spec.setSetting(as); alarmMgr.createAlarm(vm, spec); si.getServerConnection().logout(); }
Alarm createAlarm(ManagedEntity me, AlarmSpec as) throws InvalidName, DuplicateName, RuntimeFault, RemoteException;