使用VMware VSphere WebService SDK进行开发 (六)——检测告警信息

欢迎支持笔者新作:《深入理解Kafka:核心设计与实践原理》和《RabbitMQ实战指南》,同时欢迎关注笔者的微信公众号:朱小厮的博客。

欢迎跳转到本文的原文链接:https://honeypps.com/backend/vmware-vsphere-webservice-sdk-warn-info/

 

获取告警信息相对而言比较简单点,这里先陈述告警信息的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的告警就不一一赘述了,相信看得懂上面代码的小伙伴一定能很快的自己写出实现的方法。

 

这个系列暂时到此为止,如有意见或者建议的小伙伴记得在下方留言哦~

 

欢迎跳转到本文的原文链接:https://honeypps.com/backend/vmware-vsphere-webservice-sdk-warn-info/

欢迎支持笔者新作:《深入理解Kafka:核心设计与实践原理》和《RabbitMQ实战指南》,同时欢迎关注笔者的微信公众号:朱小厮的博客。

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页