/// <summary> /// Inserts the device change event. /// </summary> /// <param name="mpClass">The mp class.</param> /// <param name="eventData">The event data.</param> /// <param name="eSightIp">The e sight ip.</param> public void InsertDeviceChangeEvent(ManagementPackClass mpClass, DeviceChangeEventData eventData, string eSightIp) { try { var logger = HWLogger.GetESightSdkLogger(eventData.ESightIp); HWLogger.GetESightSdkLogger(eSightIp).Info($"Start Insert DeviceChangeEvent.[{JsonUtil.SerializeObject(eventData)}]"); MGroup.Instance.CheckConnection(); var obj = GetObjectByDeviceId(mpClass, eventData.DeviceId); if (obj == null) { logger.Warn($"InsertDeviceChangeEvent:Can not find the MonitoringObject:{eventData.DeviceId}"); return; } var isReady = CheckAndWaitHealthStateReady(mpClass, obj, eventData.DeviceId, eSightIp); if (!isReady) { logger.Warn($"InsertDeviceChangeEvent:The MonitoringObject state is uninitialized.Drop the event."); return; } obj?.InsertCustomMonitoringEvent(eventData.ToCustomMonitoringEvent()); } catch (Exception ex) { HWLogger.GetESightSdkLogger(eSightIp).Error(ex, "Insert DeviceChangeEvent Error"); } }
/// <summary> /// Deletes the servers on synchronize. /// </summary> /// <param name="eSightIp">The e sight ip.</param> /// <param name="newDeviceIds">The new device ids.</param> /// <param name="mpClass">The mp class.</param> public void RemoveServersOnSync(string eSightIp, List <string> newDeviceIds, ManagementPackClass mpClass) { try { HWLogger.GetESightSdkLogger(eSightIp).Info($"Check Removed Servers On Polling.[mpClass:{mpClass}][curQueryResult:{string.Join(",", newDeviceIds)}]"); MGroup.Instance.CheckConnection(); var criteria = new MonitoringObjectCriteria($"eSight = '{eSightIp}'", mpClass); var exsitDevices = MGroup.Instance.EntityObjects.GetObjectReader <MonitoringObject>(criteria, ObjectQueryOptions.Default).ToList(); var deleteDevices = exsitDevices.Where(x => newDeviceIds.All(newDeviceId => newDeviceId != x[this.HuaweiServerKey].Value.ToString())) .ToList(); var discovery = new IncrementalDiscoveryData(); deleteDevices.ForEach(deleteDevice => { discovery.Remove(deleteDevice); }); discovery.Commit(this.MontioringConnector); if (deleteDevices.Any()) { HWLogger.GetESightSdkLogger(eSightIp).Info($"RemoveServers OnSync:delete servers[{mpClass.Name}]:{string.Join(",", deleteDevices.Select(x => x[this.HuaweiServerKey].Value.ToString()).Distinct())} "); } } catch (Exception e) { HWLogger.GetESightSdkLogger(eSightIp).Error($"RemoveServers OnSync.", e); } }
/// <summary> /// The insert detials. /// </summary> /// <param name="model"> /// The model. /// </param> private void InsertDetials(RackServer model) { try { HWLogger.GetESightSdkLogger(model.ESight).Debug($"Insert Rack:{model.DN}"); var discoveryData = new IncrementalDiscoveryData(); var rackServer = this.CreateRackServer(model); discoveryData.Add(rackServer); discoveryData.Commit(this.MontioringConnector); } catch (Exception e) { HWLogger.GetESightSdkLogger(model.ESight).Error($"Insert (Rack Error:{model.DN}", e); } }
/// <summary> /// The update rack. /// </summary> /// <param name="model">The model.</param> /// <param name="isPolling">是否是轮询</param> /// <exception cref="System.Exception"></exception> /// <exception cref="Exception"></exception> public void UpdateRack(RackServer model, bool isPolling) { try { HWLogger.GetESightSdkLogger(model.ESight).Debug($"Start UpdateRack. [{model.DN}] [isPolling:{isPolling}]"); var oldBlade = this.GetRackServer(model.DeviceId); if (oldBlade == null) { throw new Exception($"Can not find the server:{model.DN}"); } var propertys = this.RackClass.PropertyCollection; // 获取到class的属性 var discoveryData = new IncrementalDiscoveryData(); oldBlade[this.DisplayNameField].Value = model.ServerName; if (model.Status != "-3") { oldBlade[propertys["Status"]].Value = model.StatusTxt; } oldBlade[propertys["iBMCIPv4Address"]].Value = model.iBMCIPv4Address; oldBlade[propertys["Type"]].Value = model.Type; oldBlade[propertys["UUID"]].Value = model.UUID; oldBlade[propertys["AveragePower"]].Value = model.AveragePower; oldBlade[propertys["PeakPower"]].Value = model.PeakPower; oldBlade[propertys["PowerConsumption"]].Value = model.PowerConsumption; oldBlade[propertys["DNSServerIP"]].Value = model.DNSServerIP; oldBlade[propertys["DNSName"]].Value = model.DNSName; oldBlade[propertys["ProductSn"]].Value = model.ProductSN; oldBlade[propertys["HostName"]].Value = model.HostName; oldBlade[propertys["CPUNums"]].Value = model.CPUNums; oldBlade[propertys["CPUCores"]].Value = model.CPUCores; oldBlade[propertys["CPUModel"]].Value = model.CPUModel; oldBlade[propertys["MemoryCapacity"]].Value = model.MemoryCapacity; oldBlade[propertys["AssertTag"]].Value = model.AssertTag; if (isPolling) { oldBlade[propertys["BMCVersion"]].Value = model.BMCVersion; } oldBlade[propertys["eSight"]].Value = model.ESight; discoveryData.Add(oldBlade); discoveryData.Overwrite(this.MontioringConnector); } catch (Exception e) { HWLogger.GetESightSdkLogger(model.ESight).Error($"Update UpdateRack Error.[{model.DN}] [isPolling:{isPolling}]", e); } }
/// <summary> /// 根据eSight删除Server以及父级的Computer /// </summary> /// <param name="mpClass">The class.</param> /// <param name="eSightIp">The eSightIp.</param> public void RemoverServersByESight(ManagementPackClass mpClass, string eSightIp) { try { HWLogger.GetESightSdkLogger(eSightIp).Info($"RemoverServersByESight.[{eSightIp}]"); MGroup.Instance.CheckConnection(); var criteria = new MonitoringObjectCriteria($"eSight = '{eSightIp}'", mpClass); var devices = MGroup.Instance.EntityObjects.GetObjectReader <MonitoringObject>(criteria, ObjectQueryOptions.Default).ToList(); if (devices.Any()) { var discovery = new IncrementalDiscoveryData(); devices.ForEach(device => discovery.Remove(device)); discovery.Commit(this.MontioringConnector); } } catch (Exception ex) { HWLogger.GetESightSdkLogger(eSightIp).Error("RemoverServersByESight", ex); } }
/// <summary> /// Removes the server by device identifier. /// </summary> /// <param name="eSightIp">The e sight ip.</param> /// <param name="deviceId">The device identifier.</param> public void RemoveServerByDeviceId(string eSightIp, string deviceId) { try { HWLogger.GetESightSdkLogger(eSightIp).Info($"RemoveServerByDeviceId.[{deviceId}]"); MGroup.Instance.CheckConnection(); var criteria = new MonitoringObjectCriteria($"DN = '{deviceId}'", HuaweiServerClass); var reader = MGroup.Instance.EntityObjects.GetObjectReader <MonitoringObject>(criteria, ObjectQueryOptions.Default); if (reader.Any()) { var existingObject = reader.First(); var discovery = new IncrementalDiscoveryData(); discovery.Remove(existingObject); discovery.Commit(this.MontioringConnector); } } catch (Exception e) { HWLogger.GetESightSdkLogger(eSightIp).Error($"RemoveServerByDeviceId.", e); } }
/// <summary> /// 插入告警时,等待新增的对象的healthState不再是Not Monitor /// </summary> /// <param name="mpClass">The mp class.</param> /// <param name="obj">The object.</param> /// <param name="deviceId">The device identifier.</param> /// <param name="eSightIp">The e sight ip.</param> /// <returns>PartialMonitoringObject.</returns> public bool CheckAndWaitHealthStateReady(ManagementPackClass mpClass, PartialMonitoringObject obj, string deviceId, string eSightIp) { var logger = HWLogger.GetESightSdkLogger(eSightIp); if (obj.StateLastModified == null) { //如果对象添加超过5分钟,仍然没有健康状态,防止阻塞只查询一次 if ((DateTime.Now - obj.TimeAdded).TotalMinutes > 5) { obj = GetObjectByDeviceId(mpClass, deviceId); if (obj.HealthState != HealthState.Uninitialized) { logger.Info($"{deviceId} first healthState is {obj.HealthState}."); return(true); } return(false); } #region 新增对象 logger.Info($"New Object:{deviceId}"); int i = 0; while (i < 48) { i++; // 重新查询obj状态 obj = GetObjectByDeviceId(mpClass, deviceId); if (obj.HealthState != HealthState.Uninitialized) { logger.Info($"{deviceId} first healthState is {obj.HealthState}."); Thread.Sleep(TimeSpan.FromSeconds(5)); return(true); } logger.Info($"wait {deviceId} first Initialized..."); Thread.Sleep(TimeSpan.FromSeconds(5)); } return(false); #endregion } return(true); }
/// <summary> /// Inserts the event. /// </summary> /// <param name="mpClass">The mp class.</param> /// <param name="eventData">The event data.</param> /// <param name="eSightIp">The e sight ip.</param> public void InsertEvent(ManagementPackClass mpClass, EventData eventData, string eSightIp) { var logger = HWLogger.GetESightSdkLogger(eventData.ESightIp); try { var sn = eventData.AlarmSn.ToString(); MGroup.Instance.CheckConnection(); var logPre = $"[Sn={sn}] [OptType={eventData.OptType}] [LevelId={eventData.LevelId.ToString()}] "; HWLogger.GetESightSdkLogger(eSightIp).Info($"Start Insert Event.[{JsonUtil.SerializeObject(eventData)}]"); var obj = GetObjectByDeviceId(mpClass, eventData.DeviceId); if (obj == null) { logger.Warn($"{logPre} Can not find the MonitoringObject:{eventData.DeviceId}"); return; } var isReady = CheckAndWaitHealthStateReady(mpClass, obj, eventData.DeviceId, eSightIp); if (!isReady) { logger.Warn($"{logPre} The MonitoringObject state is uninitialized.Drop the event."); return; } var eventHistory = obj.GetMonitoringEvents().Where(x => x.EventData.Contains("<AlarmData>")).ToList(); switch (eventData.OptType) { case 1: case 5: #region 新增/修改 告警-告警会重复上报 //如果不存在,则插入 //如果上次安装时的事件未清除,本次同步后,一个sn会存在两条数据,需要取最新添加的一条 var existEvent = eventHistory.OrderByDescending(x => x.TimeAdded).FirstOrDefault(x => x.GetAlarmData().AlarmSN.ToString() == sn); if (existEvent == null || existEvent.TimeAdded < MGroup.Instance.MpInstallTime) { obj.InsertCustomMonitoringEvent(eventData.ToCustomMonitoringEvent()); logger.Info($"{logPre}Insert new Event."); if (eventData.LevelId == EventLogEntryType.Error || eventData.LevelId == EventLogEntryType.Warning) { if (eventData.AlarmData.Cleared) //如果告警是清除状态 { logger.Info($"{logPre}Need to close Event when insert."); Task.Run(() => { int i = 0; while (i < 10) { i++; Thread.Sleep(TimeSpan.FromMinutes(1)); var alertToClose = obj.GetMonitoringAlerts().FirstOrDefault(x => x.CustomField6 == sn); if (alertToClose != null) { alertToClose.ResolutionState = this.CloseState.ResolutionState; var comment = eventData.AlarmData.ClearedType.ToString(); alertToClose.Update(comment); logger.Info($"{logPre}Close Event success."); break; } } }); } } } else { #region 存在则更新 var alertHistory = obj.GetMonitoringAlerts(); var alertToUpdate = alertHistory.FirstOrDefault(x => x.CustomField6 == sn); if (alertToUpdate != null) { alertToUpdate.CustomField2 = eventData.AlarmData.AdditionalInformation; alertToUpdate.CustomField3 = eventData.AlarmData.AdditionalText; alertToUpdate.CustomField4 = eventData.AlarmData.AlarmId.ToString(); alertToUpdate.CustomField5 = eventData.AlarmData.AlarmName; alertToUpdate.CustomField6 = eventData.AlarmData.AlarmSN.ToString(); alertToUpdate.CustomField7 = TimeHelper.StampToDateTime(eventData.AlarmData.ArrivedTime.ToString()).ToString(); alertToUpdate.CustomField8 = eventData.AlarmData.DevCsn.ToString(); alertToUpdate.CustomField9 = eventData.AlarmData.EventType.ToString(); alertToUpdate.CustomField10 = eventData.AlarmData.MoName; alertToUpdate.Update(eventData.AlarmData.AdditionalInformation); logger.Debug($"{logPre}Update Event."); if (eventData.AlarmData.Cleared) //如果告警是清除状态 { alertToUpdate.ResolutionState = this.CloseState.ResolutionState; alertToUpdate.Update(eventData.AlarmData.AdditionalInformation); logger.Info($"{logPre}Close Alert On Update Event."); } else { //如果原来的告警是关闭状态,本次是Open,则重新打开告警 if (alertToUpdate.ResolutionState == this.CloseState.ResolutionState) { alertToUpdate.ResolutionState = this.NewState.ResolutionState; alertToUpdate.Update(eventData.AlarmData.AdditionalInformation); logger.Info($"{logPre}Reopen Alert On Update Event."); } } } else { logger.Warn($"{logPre}Ingore Event.Can not find the alert."); } #endregion } #endregion break; case 2: #region 清除告警 var _alertHistory = obj.GetMonitoringAlerts(); var _alertToClose = _alertHistory.FirstOrDefault(x => x.CustomField6 == sn); if (_alertToClose != null) { _alertToClose.ResolutionState = this.CloseState.ResolutionState; var comment = eventData.AlarmData.ClearedType.ToString(); _alertToClose.Update(comment); logger.Info($"{logPre}Close Event."); } else { logger.Warn($"{logPre}Ingore Event.Can not find the alert."); } #endregion break; case 6: #region 插入事件 if (eventData.LevelId == EventLogEntryType.Information) { var existAlarmDatas = obj.GetMonitoringEvents().Where(x => x.EventData.StartsWith("<AlarmData")).Select(x => x.GetAlarmData()).ToList(); //插入事件 if (existAlarmDatas.All(x => x.AlarmSN.ToString() != sn)) { logger.Info($"{logPre}Insert new Event."); obj.InsertCustomMonitoringEvent(eventData.ToCustomMonitoringEvent()); } else { logger.Warn($"{logPre}Ignore Event.The event is exist."); //忽略已存在 } } else { logger.Warn($"{logPre}Ignore Event."); //忽略非事件 } break; #endregion default: HWLogger.GetESightSdkLogger(eSightIp).Error($"Unknown optType {eventData.OptType}"); break; } } catch (Exception ex) { HWLogger.GetESightSdkLogger(eSightIp).Error(ex); } }
/// <summary> /// The update kun lun. /// </summary> /// <param name="model">The model.</param> /// <param name="isPolling">是否是轮询</param> /// <exception cref="System.Exception"></exception> /// <exception cref="Exception">Can not find the server</exception> public void UpdateKunLun(KunLunServer model, bool isPolling) { try { HWLogger.GetESightSdkLogger(model.ESight).Debug($"Start UpdateKunLun.[{model.DN}] [isPolling:{isPolling}]"); var oldServer = this.GetKunLunServer(model.DeviceId); if (oldServer == null) { throw new Exception($"Can not find the server:{model.DN}"); } var propertys = this.KunLunClass.PropertyCollection; // 获取到class的属性 var discoveryData = new IncrementalDiscoveryData(); oldServer[this.DisplayNameField].Value = model.Name; if (model.Status != "-3") { oldServer[propertys["Status"]].Value = model.StatusTxt; } oldServer[propertys["Vendor"]].Value = "HUAWEI"; oldServer[propertys["UUID"]].Value = model.UUID; oldServer[propertys["IPAddress"]].Value = model.IpAddress; if (isPolling) { oldServer[propertys["Manufacturer"]].Value = model.Manufacturer; } // oldBlade[propertys["iBMCVersion"]].Value = model.Version; oldServer[propertys["CPLDVersion"]].Value = string.Empty; oldServer[propertys["UbootVersion"]].Value = string.Empty; oldServer[propertys["ProductSn"]].Value = model.ProductSN; oldServer[propertys["MemoryCapacity"]].Value = model.MemoryCapacity; oldServer[propertys["CPUNums"]].Value = model.CPUNums; oldServer[propertys["CPUCores"]].Value = model.CPUCores; // oldBlade[propertys["ServerName"]].Value = model.ServerName; oldServer[propertys["BMCMacAddr"]].Value = model.BmcMacAddr; oldServer[propertys["eSight"]].Value = model.ESight; discoveryData.Add(oldServer); var fanGroup = oldServer.GetRelatedMonitoringObjects(this.FanGroupClass).First(); discoveryData.Add(fanGroup); var relatedFanObjects = fanGroup.GetRelatedMonitoringObjects(this.FanClass); var deleteFan = relatedFanObjects.Where( x => model.FanList.All(y => y.UUID != x[this.FanClass.PropertyCollection["UUID"]].Value.ToString())) .ToList(); deleteFan.ForEach(x => { discoveryData.Remove(x); }); model.FanList.ForEach( x => { var oldFan = relatedFanObjects.FirstOrDefault(y => y[this.FanClass.PropertyCollection["UUID"]].Value.ToString() == x.UUID); if (oldFan == null) { var newFan = this.CreateFan(x); newFan[this.PartGroupKey].Value = fanGroup[this.PartGroupKey].Value; newFan[this.HuaweiServerKey].Value = model.DeviceId; discoveryData.Add(newFan); } else { this.UpdateFan(x, oldFan); discoveryData.Add(oldFan); } }); #region PSU var psuGroup = oldServer.GetRelatedMonitoringObjects(this.PowerSupplyGroupClass).First(); discoveryData.Add(psuGroup); var relatedPsuObjects = psuGroup.GetRelatedMonitoringObjects(this.PowerSupplyClass); var deletePsu = relatedPsuObjects.Where( x => model.PowerSupplyList.All( y => y.UUID != x[this.PowerSupplyClass.PropertyCollection["UUID"]].Value.ToString())).ToList(); deletePsu.ForEach(x => { discoveryData.Remove(x); }); model.PowerSupplyList.ForEach( x => { var oldPsu = relatedPsuObjects.FirstOrDefault(y => y[this.PowerSupplyClass.PropertyCollection["UUID"]].Value.ToString() == x.UUID); if (oldPsu == null) { var newpsu = this.CreatePowerSupply(x); newpsu[this.PartGroupKey].Value = psuGroup[this.PartGroupKey].Value; newpsu[this.HuaweiServerKey].Value = model.DeviceId; discoveryData.Add(newpsu); } else { this.UpdatePowerSupply(x, oldPsu); discoveryData.Add(oldPsu); } }); #endregion #region Disk var diskGroup = oldServer.GetRelatedMonitoringObjects(this.PhysicalDiskGroupClass).First(); discoveryData.Add(diskGroup); var relatedDiskObjects = diskGroup.GetRelatedMonitoringObjects(this.PhysicalDiskClass); var deleteDisk = relatedDiskObjects.Where( x => model.DiskList.All( y => y.UUID != x[this.PhysicalDiskClass.PropertyCollection["UUID"]].Value.ToString())).ToList(); deleteDisk.ForEach(x => { discoveryData.Remove(x); }); model.DiskList.ForEach( y => { var oldDisk = relatedDiskObjects.FirstOrDefault(z => z[this.PhysicalDiskClass.PropertyCollection["UUID"]].Value.ToString() == y.UUID); if (oldDisk == null) { var newDisk = this.CreateDisk(y); newDisk[this.HuaweiServerKey].Value = model.DeviceId; newDisk[this.PartGroupKey].Value = diskGroup[this.PartGroupKey].Value; discoveryData.Add(newDisk); } else { this.UpdateDisk(y, oldDisk); discoveryData.Add(oldDisk); } }); #endregion #region Raid var raidGroup = oldServer.GetRelatedMonitoringObjects(this.RaidGroupClass).First(); discoveryData.Add(raidGroup); var relatedRaidObjects = raidGroup.GetRelatedMonitoringObjects(this.RaidClass); var deleteRaid = relatedRaidObjects.Where( x => model.RaidList.All( y => y.UUID != x[this.RaidClass.PropertyCollection["UUID"]].Value.ToString())) .ToList(); deleteRaid.ForEach(x => { discoveryData.Remove(x); }); model.RaidList.ForEach( y => { var oldRaid = relatedRaidObjects.FirstOrDefault(z => z[this.RaidClass.PropertyCollection["UUID"]].Value.ToString() == y.UUID); if (oldRaid == null) { var newRaid = this.CreateRaidControl(y); newRaid[this.HuaweiServerKey].Value = model.DeviceId; newRaid[this.PartGroupKey].Value = raidGroup[this.PartGroupKey].Value; discoveryData.Add(newRaid); } else { this.UpdateRaidControl(y, oldRaid); discoveryData.Add(oldRaid); } }); #endregion discoveryData.Overwrite(this.MontioringConnector); } catch (Exception e) { HWLogger.GetESightSdkLogger(model.ESight).Error($"Update KunLun Error.[{model.DN}] [isPolling:{isPolling}]", e); } }
/// <summary> /// The insert detials. /// </summary> /// <param name="model"> /// The model. /// </param> private void InsertDetials(KunLunServer model) { try { HWLogger.GetESightSdkLogger(model.ESight).Debug($"Insert KunLun:{model.DN}"); var discoveryData = new IncrementalDiscoveryData(); #region KunLunServer var kunLunServer = this.CreateKunLunServer(model); discoveryData.Add(kunLunServer); #endregion #region Fan var fanGroup = this.CreateLogicalGroup(this.FanGroupClass, model.DeviceId); discoveryData.Add(fanGroup); model.FanList.ForEach( x => { var fan = this.CreateFan(x); fan[this.PartGroupKey].Value = fanGroup[this.PartGroupKey].Value; fan[this.HuaweiServerKey].Value = model.DeviceId; discoveryData.Add(fan); }); #endregion #region PSU var powerSupplyGroup = this.CreateLogicalGroup(this.PowerSupplyGroupClass, model.DeviceId); discoveryData.Add(powerSupplyGroup); model.PowerSupplyList.ForEach( x => { var powerSupply = this.CreatePowerSupply(x); powerSupply[this.PartGroupKey].Value = powerSupplyGroup[this.PartGroupKey].Value; powerSupply[this.HuaweiServerKey].Value = model.DeviceId; discoveryData.Add(powerSupply); }); #endregion #region Raid var raidGroup = this.CreateLogicalGroup(this.RaidGroupClass, model.DeviceId); discoveryData.Add(raidGroup); model.RaidList.ForEach( y => { var raid = this.CreateRaidControl(y); raid[this.PartGroupKey].Value = raidGroup[this.PartGroupKey].Value; raid[this.HuaweiServerKey].Value = model.DeviceId; discoveryData.Add(raid); }); #endregion #region Disk var diskGroup = this.CreateLogicalGroup(this.PhysicalDiskGroupClass, model.DeviceId); discoveryData.Add(diskGroup); model.DiskList.ForEach( x => { var disk = this.CreateDisk(x); disk[this.PartGroupKey].Value = diskGroup[this.PartGroupKey].Value; disk[this.HuaweiServerKey].Value = model.DeviceId; discoveryData.Add(disk); }); #endregion discoveryData.Commit(this.MontioringConnector); } catch (Exception e) { HWLogger.GetESightSdkLogger(model.ESight).Error($"Insert KunLun Error:{model.DN}", e); } }