读取中兴3G告警log告警文件到集合

时间:2021-03-20 17:19:25

1、文件格式

ALARM_ID=102305_404205
EVENT_TIME=2016-08-16 10:00:09
NOTIFICATION_TYPE=1
MANAGED_OBJECT_INSTANCE=NodeId=102305,BssId=0,BtsId=470,RackId=2
PERCEIVED_SEVERITY=4
ALARM_TYPE=4
PROBABLE_CAUSE=550
SPECIFIC_PROBLEM=直流电压低于47V(RRU干结点监控)
ADDITIONAL_TEXT=AlarmCode(1005020),AlarmInfo(The subsystem No. is 5, RRU_INFO=[RRU ID=0, Alias=武隆中兴隧道RRU0_江口支局, Type=RRU])

 

 2、创建实体类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LteAlarmAnalysis.Entities
{
    /// <summary>
    /// 中兴 3G 告警Ftp下载数据实体
    /// 日期 : 2016年8月16日
    /// 作者 : 老郑
    /// 
    /// ALARM_ID=102301_928516
    /// EVENT_TIME=2016-08-16 09:59:35
    /// NOTIFICATION_TYPE=5
    /// MANAGED_OBJECT_INSTANCE=NodeId=102301,BssId=0,BtsId=38,RackId=2
    /// PERCEIVED_SEVERITY=6
    /// ALARM_TYPE=4
    /// PROBABLE_CAUSE=550
    /// SPECIFIC_PROBLEM=直流电压低于51V或交流故障(RRU干结点监控)
    /// ADDITIONAL_TEXT=AlarmCode(1005019),AlarmInfo(The subsystem No. is 9, RRU_INFO=[RRU ID=4, Alias=万州石桥毛岭RRU_武陵支局, Type=RRU])
    /// </summary>
    public class AlarmEntities3GZte
    {

        public string ALARM_ID { get; set; }

        public string EVENT_TIME { get; set; }

        public string NOTIFICATION_TYPE { get; set; }

        public string MANAGED_OBJECT_INSTANCE { get; set; }

        public string PERCEIVED_SEVERITY { get; set; }

        public string ALARM_TYPE { get; set; }

        public string PROBABLE_CAUSE { get; set; }

        public string SPECIFIC_PROBLEM { get; set; }

        public string ADDITIONAL_TEXT { get; set; }
    }
}

 

3、数据读取到List集合

        /// <summary>
        /// 读取告警转换为集合
        /// </summary>
        private IList<AlarmEntities3GZte> Alarm2AlarmEntities3GZte()
        {
            IList<AlarmEntities3GZte> list = new List<AlarmEntities3GZte>();
            try
            {
                TaskConfig.SpanTime = Stopwatch.StartNew();
                var reAlarmFiles = Directory.GetFiles(TaskConfig.SavePath, TaskConfig.FileMatch);

                AlarmEntities3GZte alarm = null;

                string[] lines = System.IO.File.ReadAllLines(reAlarmFiles[0], Encoding.Default);
                foreach (string line in lines)
                {
                    if (line.IndexOf("ALARM_ID") > -1)
                    {
                        alarm = new AlarmEntities3GZte();
                    }

                    if (line != null && !"".Equals(line.Trim()))
                    {
                        alarm = Line2Property(line, alarm);
                    }

                    if (line.IndexOf("ADDITIONAL_TEXT") > -1 && alarm != null)
                    {
                        list.Add(alarm);
                    }
                }

                
                WriteMessage.PrintMassage(MessageType.Success, String.Format("解析中兴告警文件成功,共解析出小区发生不同告警数据量:{0} 条,总数据量为:{1} 条,共耗时:{2}毫秒"
                    , list.Count, list.Count, TaskConfig.SpanTime.ElapsedMilliseconds));
            }
            catch (Exception ex)
            {
                WriteMessage.PrintMassage(MessageType.Error, String.Format("解析中兴告警文件失败,错误信息:{0}", ex));
                Environment.Exit(-1);
            }
            return list;
        }

        private AlarmEntities3GZte Line2Property(string line, AlarmEntities3GZte alarm) 
        {

            if (alarm == null)
            {
                alarm = new AlarmEntities3GZte();
            }
            System.Reflection.PropertyInfo[] properties = alarm.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);

            if (properties.Length <= 0)
            {
                return alarm;
            }
            foreach (System.Reflection.PropertyInfo item in properties)
            {
                string name = item.Name;
                object value = item.GetValue(alarm, null);
                if (line.IndexOf(name) > -1)
                {
                    item.SetValue(alarm, line.Replace(name + "=", ""), null);
                    break;
                }
            }

            return alarm;
        }