static bool createMapMessageFromDevice(MapMessage message, PerformanceBase data, TextWriter writer) { Device dev = _btSystem.GetDevice(data.MOId); if (null == dev) { writer.WriteLine("//找不到设备{0}", data.MOId); return false; } writer.Write("//IP="); writer.WriteLine(dev.IP); bool[] sa; if (null != _deviceList && _deviceList.TryGetValue(data.MOId, out sa) && !(sa[0] && sa[1])) { writer.WriteLine("//对象不在上传列表中"); return false; } string type = null; if (5 == dev.DeviceType) { if (!_osTypes.TryGetValue(dev.SystemType, out type)) type = "Computersystem"; } else { _deviceTypes.TryGetValue(dev.DeviceType, out type); } if (string.IsNullOrEmpty(type)) { writer.WriteLine("//设备{0}的类型不正确,不可识别 - '{1}:{2}'.", dev.Id, dev.DeviceType, dev.SystemType); return false; } string nanruiKey = string.Concat("OriginalKey=", _manufacturer, _prefixId, "Device-", dev.Id); string dataKey = data.StatusX; HistoryRule rule = _btSystem.GetHistoryRuleByStoreId(data.StoreId); if (null != rule) { writer.WriteLine("//查询历史记录规则 storeId='{0}',修正指标 old='{1}', new='{2}'.", data.StoreId, dataKey, rule.PKIs); dataKey = rule.PKIs; } Context context = new Context(); context["deviceType"] = dev.DeviceType.ToString(); context["deviceOS"] = dev.SystemType.ToString(); context["deviceId"] = dev.Id.ToString(); context["performanceKey"] = dataKey; string fieldKey = null; string fieldValue = null; foreach (IMatcher matcher in _serviceMatchers) { MatchResult matchResult = matcher.Match(dataKey); if (null != matchResult) { fieldKey = matchResult.Misc[0].Key; fieldValue = matchResult.Misc[0].Value; context["schema"] = matchResult.Schema; context["objectKey"] = fieldKey; context["objectId"] = fieldValue; context["performanceKey"] = matchResult.PKI; dataKey = matchResult.PKI; writer.WriteLine("//分解PKI, schema='{0}',performanceKey='{1}', '{2}'='{3}'." , matchResult.Schema , matchResult.PKI , fieldKey , fieldValue); break; } } DispatchResult result = _deviceDispatcher.Find(context); if (null == result) { writer.WriteLine("//设备{0}的性能参数不正确,不可识别 - '{1}'.", dev.Id, data.StatusX); return false; } if (string.IsNullOrEmpty(result.Alias) || 0 == string.Compare("null", result.Alias, true)) { writer.WriteLine("//设备{0}的性能参数不正确,不可识别,可能不用发送 - '{1}'.", dev.Id, data.StatusX); return false; } dataKey = result.Alias; string objectKey = null; if (!string.IsNullOrEmpty(fieldValue)) { if (string.IsNullOrEmpty(result.Separator)) { objectKey = string.Concat(nanruiKey, "-", fieldKey, "-", fieldValue); dataKey = string.Concat(result.Alias, ":", objectKey); } else if ("null" != result.Separator) { objectKey = string.Concat(nanruiKey, result.Separator, fieldValue); dataKey = string.Concat(result.Alias, ":", objectKey); } writer.WriteLine("//修正设备的性能参数 - '{0}'.", dataKey); } //SCENE 网管厂家 Betasoft(固定是这个) message.Body["MAINDATA"] = _encoding.GetBytes(nanruiKey); message.Body["SCENE"] = _encoding.GetBytes(_manufacturer); message.Body["CLASSNAME"] = _encoding.GetBytes(type); writer.Write("MAINDATA="); writer.WriteLine(nanruiKey); writer.Write("SCENE="); writer.WriteLine(_manufacturer); writer.Write("CLASSNAME="); writer.WriteLine(type); object performanceValue = data.GetValue(); if (data.StatusX == "Memory") { performanceValue = GetValue(data.GetValue()); dataKey = "MEMLOAD"; } else if (data.StatusX == "Cpu") { dataKey = string.Concat("CPULOAD:", nanruiKey, "-CPU1"); performanceValue = GetValue(data.GetValue()); } else if (data.StatusX == "Used_Per") { if (result.Alias == "FileSystemUsedRate") { performanceValue = GetValue(data.GetValue()); } else if (result.Alias == "DiskFreeRate") { performanceValue = GetValue(100 - ((PerformanceData)data).Value); writer.WriteLine("//原值为使用率,需要修正 - oldValue={0}, newValue={1}", data.GetValue(), performanceValue); } } else if (data.StatusX == "upmins") { if (100 == (long)(((PerformanceData)data).Value)) { writer.WriteLine("//原值单位为100,可能不正确 - oldValue={0}, Id={1}", data.GetValue(), data.Id); return false; } performanceValue = ((long)(((PerformanceData)data).Value * 60)).ToString(); writer.WriteLine("//原值单位为分钟,需要修正 - oldValue={0}, newValue={1}", data.GetValue(), performanceValue); } else if (result.Alias == "ComputerSystemRunningTime") { performanceValue = ((long)(((PerformanceData)data).Value * 60)).ToString(); writer.WriteLine("//原值单位为分钟,需要修正 - oldValue={0}, newValue={1}", data.GetValue(), performanceValue); } else if (0 == string.Compare("Up", data.GetValue().ToString(), true)) { // 此处将所有的 Up 或 Down这样的值改成 1 或 0 performanceValue = 1; writer.WriteLine("//原值需要修正 - oldValue=Up, newValue={1}", data.GetValue(), performanceValue); } else if (0 == string.Compare("Down", data.GetValue().ToString(), true)) { // 此处将所有的 Up 或 Down这样的值改成 1 或 0 performanceValue = 0; writer.WriteLine("//原值需要修正 - oldValue=Down, newValue={1}", data.GetValue(), performanceValue); } else if (0 == string.Compare("active", data.GetValue().ToString(), true)) { performanceValue = "1"; writer.WriteLine("//原值需要修正 - oldValue=Down, newValue={1}", data.GetValue(), performanceValue); } else if (0 == string.Compare("inactive", data.GetValue().ToString(), true)) { performanceValue = "0"; writer.WriteLine("//原值需要修正 - oldValue=Down, newValue={1}", data.GetValue(), performanceValue); } else if ("ProcessOccupyCPUTime" == result.Alias || "ProcessRunningTime" == result.Alias) { TimeSpan timeSpan; if (TimeSpan.TryParse(performanceValue.ToString().Replace('-', '.'), out timeSpan)) { performanceValue = GetValue(timeSpan.TotalSeconds); writer.WriteLine("//原值需要修正 - oldValue=Down, newValue={1}", data.GetValue(), performanceValue); } } //dataKey = dataKey.Trim(); message.Body[dataKey] = _encoding.GetBytes(performanceValue.ToString()); writer.Write("//PKI="); writer.WriteLine(dataKey); writer.Write(dataKey); writer.Write("="); writer.WriteLine(performanceValue.ToString()); return true; }
static bool createMapMessageFromServer(MapMessage message, PerformanceBase data, TextWriter writer) { ServiceStaticPropertiesData server = _btSystem.GetService(data.MOId); if (null == server) { writer.WriteLine("//找不到服务{0}", data.MOId); return false; } Device dev = server.Device; if (null == dev) { writer.WriteLine("//找不到服务[{0}:{1}]所在的设备{2}", data.MOId, server.CustomName, server.DeviceId); return false; } writer.Write("//IP="); writer.WriteLine(server.Device.IP); writer.Write("//serviceId="); writer.WriteLine(server.Id); writer.Write("//serviceType="); writer.WriteLine(server.Type); writer.Write("//serviceName="); writer.WriteLine(server.CustomName); bool[] sa; if (null != _serverList && _serverList.TryGetValue(data.MOId, out sa) && !(sa[0] && sa[1])) { writer.WriteLine("//对象不在上传列表中"); return false; } string type = null; _serverTypes.TryGetValue(server.Type, out type); if (string.IsNullOrEmpty(type)) { writer.WriteLine("//服务{0}的类型不正确,不可识别 - '{1}'.", server.Id, server.Type); return false; } //string nanruiKey = string.Concat("OriginalKey=", manufacturer, PrefixId, "Device-", dev.Id); string nanruiKey = string.Concat("OriginalKey=", _manufacturer, _prefixId, "SERVER-", data.MOId); string dataKey = data.StatusX; HistoryRule rule = _btSystem.GetHistoryRuleByStoreId(data.StoreId); if (null != rule) { writer.WriteLine("//查询历史记录规则 storeId='{0}',修正指标 old='{1}', new='{2}'.", data.StoreId, dataKey, rule.PKIs); dataKey = rule.PKIs; } Context context = new Context(); context["serviceType"] = server.Type.ToString(); context["serviceId"] = server.Id.ToString(); context["performanceKey"] = dataKey; string fieldKey = null; string fieldValue = null; foreach (IMatcher matcher in _serviceMatchers) { MatchResult matchResult = matcher.Match(dataKey); if (null != matchResult) { fieldKey = matchResult.Misc[0].Key; fieldValue = matchResult.Misc[0].Value; context["schema"] = matchResult.Schema; context["objectKey"] = fieldKey; context["objectId"] = fieldValue; context["performanceKey"] = matchResult.PKI; dataKey = matchResult.PKI; writer.WriteLine("//分解PKI, schema='{0}',performanceKey='{1}', '{2}'='{3}'." , matchResult.Schema , matchResult.PKI , fieldKey , fieldValue); break; } } DispatchResult result = _serviceDispatcher.Find(context); if (null == result) { writer.WriteLine("//服务{0}的性能参数不正确,不可识别 - '{1}'.", server.Id, data.StatusX); return false; } if (string.IsNullOrEmpty(result.Alias) || 0 == string.Compare("null", result.Alias, true)) { writer.WriteLine("//服务{0}的性能参数不正确,不可识别,可能不用发送 - '{1}'.", server.Id, data.StatusX); return false; } dataKey = result.Alias; string objectKey = null; if (!string.IsNullOrEmpty(fieldValue)) { if (string.IsNullOrEmpty(result.Separator)) { objectKey = string.Concat(nanruiKey, "-", fieldKey, "-", fieldValue); dataKey = string.Concat(result.Alias, ":", objectKey); } else if ("null" != result.Separator) { objectKey = string.Concat(nanruiKey, result.Separator, fieldValue); dataKey = string.Concat(result.Alias, ":", objectKey); } //else //{ // objectKey = serviceKey; // dataKey = string.Concat(result.Alias, ":", objectKey); //} writer.WriteLine("//修正服务的性能参数 - '{0}'.", dataKey); } //else //{ // objectKey = serviceKey; // dataKey = string.Concat(result.Alias, ":", objectKey); //} string performanceValue = GetValue(data.GetValue()); if (0 == string.Compare("open", performanceValue, true)) { performanceValue = "1"; writer.WriteLine("//原值需要修正 - oldValue=Down, newValue={1}", data.GetValue(), performanceValue); } else if (0 == string.Compare("close", performanceValue, true)) { performanceValue = "0"; writer.WriteLine("//原值需要修正 - oldValue=Down, newValue={1}", data.GetValue(), performanceValue); } else if (0 == string.Compare("up", performanceValue, true)) { performanceValue = "1"; writer.WriteLine("//原值需要修正 - oldValue=Down, newValue={1}", data.GetValue(), performanceValue); } else if (0 == string.Compare("down", performanceValue, true)) { performanceValue = "0"; writer.WriteLine("//原值需要修正 - oldValue=Down, newValue={1}", data.GetValue(), performanceValue); } else if (0 == string.Compare("running", performanceValue, true)) { performanceValue = "1"; writer.WriteLine("//原值需要修正 - oldValue=Down, newValue={1}", data.GetValue(), performanceValue); } else if (0 == string.Compare("active", performanceValue, true)) { performanceValue = "1"; writer.WriteLine("//原值需要修正 - oldValue=Down, newValue={1}", data.GetValue(), performanceValue); } else if (0 == string.Compare("inactive", performanceValue, true)) { performanceValue = "0"; writer.WriteLine("//原值需要修正 - oldValue=Down, newValue={1}", data.GetValue(), performanceValue); } else if (data.StatusX == "PendingRequestOldestTime") { try { DateTime timeValue; if (!tryParseDateTime(performanceValue, out timeValue)) { writer.WriteLine("//日期格式无法分析"); return false; } performanceValue = timeValue.ToFileTimeUtc().ToString(); } catch (Exception ex) { writer.WriteLine("//" + ex.ToString()); return false; } } message.Body["MAINDATA"] = _encoding.GetBytes(nanruiKey); message.Body["SCENE"] = _encoding.GetBytes(_manufacturer); message.Body["CLASSNAME"] = _encoding.GetBytes(type); message.Body[dataKey] = _encoding.GetBytes(performanceValue); writer.Write("//PKI="); writer.WriteLine(dataKey); writer.Write("MAINDATA="); writer.WriteLine(nanruiKey); writer.Write("SCENE="); writer.WriteLine(_manufacturer); writer.Write("CLASSNAME="); writer.WriteLine(type); writer.Write(dataKey); writer.Write("="); writer.WriteLine(performanceValue); return true; }
static int GetOriginalKey(PerformanceBase data) { return data.MOId; }
static MapMessage createMapMessage(Session producer, PerformanceBase data, TextWriter writer) { MapMessage message = new MapMessage(); message.Body["TIME"] = _encoding.GetBytes(DateTime.Now.ToString()); writer.Write("TIME="); writer.WriteLine(DateTime.Now.ToString()); writer.Write("//rawPKI="); writer.WriteLine(data.StatusX); writer.Write("//rawValue="); writer.WriteLine(data.GetValue()); //CPU/MEM if (data.MOType == "Device") { if (createMapMessageFromDevice(message, data, writer)) return message; } else if (data.MOType == "DevicePort") { if (createMapMessageFromPort(message, data, writer)) return message; } else if (data.MOType == "TopologyLink") { if (createMapMessageFromLink(message, data, writer)) return message; } else if (data.MOType == "Service") { if (createMapMessageFromServer(message, data, writer)) return message; } else { writer.WriteLine("//无法识别的对象类型"); } return null; }
static bool createMapMessageFromLink(MapMessage message, PerformanceBase data, TextWriter writer) { Link link = _btSystem.GetLink(data.MOId); if (null == link) { writer.WriteLine("//找不到链路连接{0}", data.MOId); return false; } PerformanceData performanceData = data as PerformanceData; if (null == performanceData) { writer.WriteLine("//链路连接{0}的值类型不正确.", data.MOId); return false; } writer.Write("//LinkId="); writer.WriteLine(link.Id); writer.Write("//LinkName="); writer.WriteLine(link.DisplayName); bool[] sa; if (null != _linkList && _linkList.TryGetValue(data.MOId, out sa) && !(sa[0] && sa[1])) { writer.WriteLine("//对象不在上传列表中"); return false; } string nanruiKey = string.Concat("OriginalKey=", _manufacturer, _prefixId, "L2Link-", data.MOId); message.Body["MAINDATA"] = _encoding.GetBytes(nanruiKey); message.Body["SCENE"] = _encoding.GetBytes(_manufacturer); string className = link.IsVirtual ? "LogicalLink" : "L2Link"; message.Body["CLASSNAME"] = _encoding.GetBytes(className); writer.Write("MAINDATA="); writer.WriteLine(nanruiKey); writer.Write("SCENE="); writer.WriteLine(_manufacturer); writer.WriteLine("CLASSNAME=" + className); switch (data.StatusX) { case "ifStatus": message.Body["L2LinkStatus"] = _encoding.GetBytes(GetIfStatus(data.GetValue())); writer.Write("L2LinkStatus="); writer.WriteLine(GetIfStatus(data.GetValue())); writer.Write("//PKI="); writer.WriteLine("L2LinkStatus"); break; //case "ifOutOctets": // message.Body["L2LinkOutFlow5MIN"] = _encoding.GetBytes(data.Value.ToString()); // break; //case "ifOutNUcastPkts": // message.Body["L2LinkOutBroadcastFlow5MIN"] = _encoding.GetBytes(data.Value.ToString()); // break; //case "ifOutDiscards": // message.Body["L2LinkOutDropPacketNum5MIN"] = _encoding.GetBytes(data.Value.ToString()); // break; //case "ifOutErrors": // message.Body["L2LinkOutErrorPacketNum5MIN"] = _encoding.GetBytes(data.Value.ToString()); // break; //case "ifOutPercent": // message.Body["L2LinkOutBwUsedRate5MIN"] = _encoding.GetBytes(data.Value.ToString()); // break; //case "ifInOctets": // message.Body["L2LinkInFlow5MIN"] = _encoding.GetBytes(data.Value.ToString()); // break; //case "ifInUcastPkts": // message.Body["L2LinkInBroadcastFlow5MIN"] = _encoding.GetBytes(data.Value.ToString()); // break; //case "ifInDiscards": // message.Body["L2LinkInDropPacketNum5MIN"] = _encoding.GetBytes(data.Value.ToString()); // break; //case "ifInErrors": // message.Body["L2LinkInErrorPacketNum5MIN"] = _encoding.GetBytes(data.Value.ToString()); // break; //case "ifInPercent": // message.Body["L2LinkInBwUsedRate5MIN"] = _encoding.GetBytes(data.Value.ToString()); // break; default: { string key; _performanceMapForLinks.TryGetValue(data.StatusX, out key); if (string.IsNullOrEmpty(key)) { writer.WriteLine("//线路{0}的性能参数不正确,不可识别 - '{1}'.", data.MOId, data.StatusX); return false; } writer.Write("//PKI="); writer.WriteLine(key); string performanceValue = GetValue(performanceData.Value); switch (data.StatusX) { case "ifOutOctets": { performanceValue = GetValue(performanceData.Value / 8); break; } case "ifInOctets": { performanceValue = GetValue(performanceData.Value / 8); break; } case "ifOctets": { performanceValue = GetValue(performanceData.Value / 8); break; } } message.Body[key] = _encoding.GetBytes(performanceValue); writer.Write(key); writer.Write("="); writer.WriteLine(performanceValue); break; } } return true; }
static bool createMapMessageFromPort(MapMessage message, PerformanceBase data, TextWriter writer) { Interface port = _btSystem.GetDevicePort(GetOriginalKey(data)); if (null == port) { writer.WriteLine("//找不到端口{0}", data.MOId); return false; } Device dev = port.Device; if (null == dev) { writer.WriteLine("//找不到设备{0}", port.DeviceId); return false; } writer.Write("//IP="); writer.WriteLine(dev.IP); writer.Write("//IfIndex="); writer.WriteLine(port.IfIndex); bool[] sa; if (null != _deviceList && _deviceList.TryGetValue(dev.Id, out sa) && !(sa[0] && sa[1])) { writer.WriteLine("//对象不在上传列表中"); return false; } string type = null; if (5 == dev.DeviceType) { if (!_osTypes.TryGetValue(dev.SystemType, out type)) type = "Computersystem"; } else { _deviceTypes.TryGetValue(dev.DeviceType, out type); } if (string.IsNullOrEmpty(type)) { writer.WriteLine("//设备{0}的类型不正确,不可识别 - '{1}:{2}'.", dev.Id, dev.DeviceType, dev.SystemType); return false; } PerformanceData performanceData = data as PerformanceData; if (null == performanceData) { writer.WriteLine("//设备{0}的值类型不正确.", dev.Id); return false; } string nanruiKey = string.Concat("OriginalKey=", _manufacturer, _prefixId, "Device-", port.DeviceId); message.Body["MAINDATA"] = _encoding.GetBytes(nanruiKey); message.Body["SCENE"] = _encoding.GetBytes(_manufacturer); message.Body["CLASSNAME"] = _encoding.GetBytes(type); writer.Write("MAINDATA="); writer.WriteLine(nanruiKey); writer.Write("SCENE="); writer.WriteLine(_manufacturer); writer.Write("CLASSNAME="); writer.WriteLine(type); string ifKey = string.Concat("OriginalKey=", _manufacturer, _prefixId, "IpInterface-", port.Id); switch (data.StatusX) { case "ifStatus": { string valueKey = string.Concat("IpInterfaceStatus:", ifKey); string tmp = GetIfStatus(data.GetValue()).ToString(); message.Body[valueKey] = _encoding.GetBytes(tmp); writer.Write(valueKey); writer.Write("="); writer.WriteLine(tmp); writer.Write("//PKI="); writer.WriteLine("IpInterfaceStatus"); } break; default: { string key; _performanceMapForPorts.TryGetValue(data.StatusX, out key); if (string.IsNullOrEmpty(key)) { writer.WriteLine("//端口{0}的性能参数不正确,不可识别 - '{1}'.", port.Id, data.StatusX); return false; } string valueKey = string.Concat(key, ":", ifKey); writer.Write("//PKI="); writer.WriteLine(valueKey); string performanceValue = GetValue(performanceData.Value); switch (data.StatusX) { case "ifOutOctets": { performanceValue = GetValue((performanceData.Value / 8)); break; } case "ifInOctets": { performanceValue = GetValue((performanceData.Value / 8)); break; } case "ifOctets": { performanceValue = GetValue((performanceData.Value / 8)); break; } } message.Body[valueKey] = _encoding.GetBytes(performanceValue); writer.Write(valueKey); writer.Write("="); writer.WriteLine(performanceValue); break; } } return true; }