获取告警信息相对而言比较简单点,这里先陈述告警信息的pojo类,作为存储告警信息的源头(省略getter和setter方法):
public class AlarmItem { //对象 private String ObjectName; //状态 private ManagedEntityStatus overallStatus; //名称 private String alarmName; //触发时间 private Date time; //确认时间 private Date acknowledgedTime; //确认者 private String acknowledgedByUser; //是否被确认 private Boolean acknowledged; }
以获取主机告警信息为例:
public static List<AlarmItem> getHostAlarms(String hostName) throws Exception { List<AlarmItem> ansList = null; ManagedObjectReference hostmor = getHostByHostName(hostName); ansList = getAlarms(hostmor); return ansList; }
这里先根据主机名称获取主机的对象(HostSystem),相关代码可以参考前面的相关文章。
接下去就是获取告警的方法getAlarms(ManagedObjectReference mor):
private static List<AlarmItem> getAlarms(ManagedObjectReference mor) throws Exception { List<AlarmItem> ansList = new ArrayList<AlarmItem>(); List<AlarmState> list = vimPort.getAlarmState(alarmManager, mor); for(AlarmState alarmState : list) { if(alarmState.getOverallStatus().toString().equals("YELLOW") || alarmState.getOverallStatus().toString().equals("RED")) { AlarmItem alarmItem = new AlarmItem(); ManagedObjectReference alarm = alarmState.getAlarm(); if(alarm!=null) { PropertyFilterSpec alarmFilterSpec = createAlarmFilterSpec(alarm); ArrayList<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(); listpfs.add(alarmFilterSpec); List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs); if(listobjcont != null) { ObjectContent oc = (ObjectContent) listobjcont.get(0); AlarmInfo alarmInfo = (AlarmInfo) oc.getPropSet().get(0).getVal(); alarmItem.setAlarmName(alarmInfo.getName()); } } alarmItem.setAcknowledged(alarmState.isAcknowledged()); alarmItem.setAcknowledgedByUser(alarmState.getAcknowledgedByUser()); alarmItem.setAcknowledgedTime(DateConvert.convertToDate(alarmState.getAcknowledgedTime())); alarmItem.setOverallStatus(alarmState.getOverallStatus()); alarmItem.setTime(DateConvert.convertToDate(alarmState.getTime())); alarmItem.setObjectName(getObjectName(alarmState.getEntity())); ansList.add(alarmItem); } } return ansList; }
private static PropertyFilterSpec createAlarmFilterSpec(ManagedObjectReference eventHistoryCollectorRef) { PropertySpec propSpec = new PropertySpec(); propSpec.setAll(new Boolean(false)); propSpec.getPathSet().add("info"); propSpec.setType(eventHistoryCollectorRef.getType()); ObjectSpec objSpec = new ObjectSpec(); objSpec.setObj(eventHistoryCollectorRef); objSpec.setSkip(new Boolean(false)); PropertyFilterSpec spec = new PropertyFilterSpec(); spec.getPropSet().add(propSpec); spec.getObjectSet().add(objSpec); return spec; } private static String getObjectName(ManagedObjectReference mor) throws Exception { String objectName = null; PropertySpec propSpec = new PropertySpec(); propSpec.setAll(new Boolean(false)); propSpec.getPathSet().add("name"); propSpec.setType(mor.getType()); ObjectSpec objSpec = new ObjectSpec(); objSpec.setObj(mor); objSpec.setSkip(new Boolean(false)); PropertyFilterSpec spec = new PropertyFilterSpec(); spec.getPropSet().add(propSpec); spec.getObjectSet().add(objSpec); ArrayList<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(); listpfs.add(spec); List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs); if(listobjcont != null) { ObjectContent oc = (ObjectContent) listobjcont.get(0); objectName = (String) oc.getPropSet().get(0).getVal(); } return objectName; }
这样就可以获取相关的主机告警信息。
对于其他的,比如虚拟机的告警信息就可以这样:
public static List<AlarmItem> getVmAlarms(String vmName) throws Exception { List<AlarmItem> ansList = null; ManagedObjectReference hostmor = getVmByVmName(vmName); ansList = getAlarms(hostmor); return ansList; }
对于其他的譬如vCenter, Cluster, Datacenter的告警就不一一赘述了,相信看得懂上面代码的小伙伴一定能很快的自己写出实现的方法。
这个系列暂时到此为止,如有意见或者建议的小伙伴记得在下方留言哦~
时间: 2024-09-22 07:10:13