使用VMware VSphere WebService SDK进行开发 (六)——检测告警信息
来源:互联网 发布:pe windows 编辑:程序博客网 时间:2024/06/06 00:49
获取告警信息相对而言比较简单点,这里先陈述告警信息的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的告警就不一一赘述了,相信看得懂上面代码的小伙伴一定能很快的自己写出实现的方法。
这个系列暂时到此为止,如有意见或者建议的小伙伴记得在下方留言哦~
2 1
- 使用VMware VSphere WebService SDK进行开发 (六)——检测告警信息
- 使用VMware VSphere WebService SDK进行开发 (四)——获取集群(Cluster, ComputeResource)的相关信息
- 使用VMware VSphere WebService SDK进行开发 (一)——基本信息阐述
- 使用VMware VSphere WebService SDK进行开发 (一)——基本信息阐述
- 使用VMware VSphere WebService SDK进行开发 (二)——获取虚拟机cpu的使用情况
- 使用VMware VSphere WebService SDK进行开发 (三)——获取主机(HostSystem)的基本信息
- 使用VMware VSphere WebService SDK进行开发 (七)——获取数据中心、集群、主机、虚拟机的目录结构
- 使用VMware VSphere WebService SDK进行开发 (五)——根据虚拟机的名称获取对应主机的IP地址
- VMware vSphere Web Services SDK编程指南(六)- 6 Datacenter Inventory
- VMware vSphere Web Services SDK编程指南(三)- VMware vSphere 和 vSphere Management APIs
- VMware vSphere Web Services SDK编程指南(二)-Java 开发设置
- VMware vSphere Web Services SDK编程指南(八)- 8.5 使用 AuthorizationManager 管理角色和权限
- VMware vSphere Web Services SDK编程指南(八)- 8.7 使用凭据库自动登录
- VMware vSphere Web Services SDK编程指南(八)- 8.8 使用 LicenseManager 管理许可证
- VMware vSphere Web Services SDK编程指南(十一)- 11.2 使用分布式虚拟交换机
- VMware vSphere Web Services SDK编程指南(八)- 8.3 从 UserDirectory 获取用户和组信息
- VMware vSphere Web Services SDK编程指南(四)- vSphere API 编程模型(一)
- VMware vSphere Web Services SDK编程指南(四)- vSphere API 编程模型(二)
- iOS网络编程
- gfirefly 学习之ubuntu 15 python 环境熟悉配置ssh (1)
- java解析ipa工具类
- Maven学习总结(3)——使用Maven构建项目
- 负载平衡
- 使用VMware VSphere WebService SDK进行开发 (六)——检测告警信息
- Eclipse 常用快捷键
- Java并发包中的同步队列SynchronousQueue实现原理
- sicily 1155. Can I Post the lette
- Menu comments
- Spring在非web应用中关闭IoC容器 (registerShutdownHook)
- 百度翻译新版API的VB实现代码
- Maven学习总结(4)——Maven核心概念
- C# winForm webBrowser页面中js调用winForm类方法