Exemplo n.º 1
0
        private void LoadWatchers()
        {
            JLogger.LogInfo(this, "LoadWatchers()");
            var watchers = Directory.EnumerateFiles(SRMAgentPaths.SRMComponents, "*.dll").ToList();

            foreach (var watcher in watchers)
            {
                JLogger.LogDebug(this, "Component found: {0}", watcher);
                try
                {
                    JLogger.LogDebug(this, "Loading assembly from file");
                    var ptrAssembly = Assembly.LoadFile(watcher);
                    foreach (var item in ptrAssembly.GetTypes())
                    {
                        if (!item.IsClass)
                        {
                            continue;
                        }
                        JLogger.LogDebug(this, "Filter for correct interface");
                        if (item.GetInterfaces().Contains(typeof(IFactWatcher)))
                        {
                            var inst = (IFactWatcher)Activator.CreateInstance(item);
                            _watchers.Add(inst);
                            JLogger.LogDebug(this, "Watcher '{0}' added to list", inst.GetFactWatcherName());
                        }
                    }
                }
                catch (Exception ex)
                {
                    JLogger.LogError(this, "LoadWatchers() Exception found: {0}", ex.Message);
                }
            }
        }
Exemplo n.º 2
0
        protected override void OnStart(string[] args)
        {
            JLogger.LogInfo(this, "OnStart() Start");

            base.OnStart(args);
            _host?.Close();

            try
            {
                JLogger.LogDebug(this, "Se crea el service host con la direccion base.");
                var baseAddress = new Uri("http://localhost:8080/CommandService");
                _host = new ServiceHost(typeof(CommandService), baseAddress);

                JLogger.LogDebug(this, "Se configura para presentar el metadata.");
                var smb = new ServiceMetadataBehavior
                {
                    HttpGetEnabled   = true,
                    MetadataExporter = { PolicyVersion = PolicyVersion.Policy15 }
                };
                _host.Description.Behaviors.Add(smb);

                JLogger.LogDebug(this, "Se Abre la comunicacion.");
                _host.Open();
            }
            catch (Exception ex)
            {
                JLogger.LogError(this, "Error al crear el host.", ex);
            }

            JLogger.LogInfo(this, "OnStart() End");
        }
Exemplo n.º 3
0
        public void Start()
        {
            JLogger.LogInfo(this, "Start() In");

            //INIT REMOTE OPERATIONS
            _factHandlerClient = new SRMServerAccess();

            //Enroll Machine
            //JMachine.Id = _factHandlerClient.EnrollMachine(JMachine.GetMachine());

            //Get Configuration
            //_factHandlerClient.GetMachineConfiguration(_machine);

            //Start CommandService
            try
            {
                JLogger.LogDebug(this, "Se crea el service host con la direccion base.");
                var baseAddress = new Uri("http://localhost:8080/CommandService");
                _commandService = new ServiceHost(typeof(CommandService), baseAddress);

                JLogger.LogDebug(this, "Se configura para presentar el metadata.");
                var smb = new ServiceMetadataBehavior
                {
                    HttpGetEnabled   = true,
                    MetadataExporter = { PolicyVersion = PolicyVersion.Policy15 }
                };
                _commandService.Description.Behaviors.Add(smb);

                JLogger.LogDebug(this, "Se Abre la comunicacion.");
                _commandService.Open();
            }
            catch (Exception ex)
            {
                JLogger.LogError(this, "Error al crear el host.", ex);
            }

            //Start Watchers
            _watcherLoader = new WatcherLoader();
            _watcherLoader.StartWatchers();

            JLogger.LogInfo(this, "Stop() Out");
        }
Exemplo n.º 4
0
        private ICommand GetCommand(string id, string command)
        {
            JLogger.LogInfo(this, "getCommand(): id:{0} command:{1}", id, command);

            var assembly = Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + @"..\commands\" + id + ".dll");

            JLogger.LogDebug("assembly:{0}", assembly);

            try
            {
                var ptrAssembly = Assembly.LoadFile(assembly);
                //foreach (var item in ptrAssembly.GetTypes())
                //{
                //    if (!item.IsClass) continue;
                //    if (item.GetInterfaces().Contains(typeof (ICommand)))
                //    {
                //        if (((ICommand) Activator.CreateInstance(item)).GetCommandName().ToUpperInvariant() ==
                //            command.ToUpperInvariant())
                //        {
                //            return (ICommand) Activator.CreateInstance(item);
                //        }
                //    }
                //}
                foreach (var item in from item in ptrAssembly.GetTypes()
                         where item.IsClass
                         where item.GetInterfaces().Contains(typeof(ICommand))
                         where string.Equals(((ICommand)Activator.CreateInstance(item)).GetCommandName(), command, StringComparison.InvariantCultureIgnoreCase)
                         select item)
                {
                    return((ICommand)Activator.CreateInstance(item));
                }
            }
            catch (Exception ex)
            {
                JLogger.LogError(this, "Error trying to LoadAssembly:{0}", ex.Message);
            }

            return(null);
        }
Exemplo n.º 5
0
        public void Start()
        {
            JLogger.LogInfo(this, "Start()");

            //GET BASE CONFIGURATION
            string[] savedEventLogsName = Config.GetValueByKey("EVENT_LOGS")?.Split(',');
            int      savedLastEntryIndex; //Latest index value processed and saved

            int.TryParse(Persistence.GetValueByKey("LAST_ENTRY_INDEX"), out savedLastEntryIndex);
            var lastEntryIndex = 0; //Latest index value from actual event logs

            DateTime lastEntryDatetime = new DateTime();
            DateTime savedLastEntryDatetime;

            DateTime.TryParse(Persistence.GetValueByKey("LAST_ENTRY_DATETIME"), out savedLastEntryDatetime);

            //CREATE EVENT LOG LIST
            _eventLogs = new List <EventLog>();

            //VALIDATE AND ENABLE EVENT CATCH
            if (savedEventLogsName != null)
            {
                foreach (var eventLogName in savedEventLogsName)
                {
                    JLogger.LogDebug(this, "Checking if EventLog {0} has registry configuration", eventLogName);
                    //CHECK FOR LOGFILE
                    var regEventLog =
                        Registry.LocalMachine.OpenSubKey("System\\CurrentControlSet\\Services\\EventLog\\" + eventLogName);
                    if (regEventLog != null)
                    {
                        JLogger.LogDebug(this, "EventLog {0} has registry configuration", eventLogName);
                        var temp = regEventLog.GetValue("File");
                        if (temp != null)
                        {
                            JLogger.LogDebug(this, "EventLog {0} has EventLog file created", eventLogName);

                            //CREATING AN EVENTLOG OBJECT FROM THE VALIDATED STRING
                            JLogger.LogDebug(this, "Creating EventLog object for {0}", eventLogName);
                            var eventLog = new EventLog(eventLogName);
                            _eventLogs.Add(eventLog);

                            //ATTACH EVENT
                            JLogger.LogDebug(this, "Attaching event handler to EventLog {0}", eventLogName);
                            eventLog.EntryWritten       += OnEntryWritten;
                            eventLog.EnableRaisingEvents = true;

                            //GETTING THE LAST ENTRY FROM THE EVENTLOG
                            JLogger.LogDebug(this, "Getting the last entry from the EventLog {0}", eventLogName);
                            var lastEntry =
                                (from EventLogEntry ent in eventLog.Entries orderby ent.TimeGenerated select ent)
                                .LastOrDefault();
                            if (lastEntry != null && lastEntry.Index > lastEntryIndex)
                            {
                                JLogger.LogDebug(this, "EventLog {0} last entry was {1}", eventLogName, lastEntry.Message);
                                lastEntryIndex = lastEntry.Index;
                            }

                            if (lastEntry != null && lastEntry.TimeGenerated >= lastEntryDatetime)
                            {
                                lastEntryDatetime = lastEntry.TimeGenerated;
                            }
                        }
                        else
                        {
                            //NO FILE ASSIGNED FOR THE EVENTLOG
                            JLogger.LogError(this, "EventLog {0} has not file assigned", eventLogName);
                        }
                    }
                    else
                    {
                        //NO REGISTRY KEY CREATED FOR THE EVENTLOG
                        JLogger.LogError(this, "EventLog {0} has not registry key created", eventLogName);
                    }
                }
            }

            //PROCESS ALL EVENTS
            JLogger.LogDebug(this, "Processing events in chequed EventLogs");
            foreach (var eventLog in _eventLogs)
            {
                JLogger.LogDebug(this, "Processing events in EventLog {0}", eventLog.LogDisplayName);
                var filteredEventEntrys = (from EventLogEntry ent
                                           in
                                           eventLog.Entries.Cast <EventLogEntry>()
                                           .Where(x => x.TimeGenerated <= lastEntryDatetime && x.TimeGenerated >= savedLastEntryDatetime)
                                           orderby ent.TimeGenerated
                                           select ent);
                //var filteredEventEntrys = (from EventLogEntry ent
                //           in eventLog.Entries.Cast<EventLogEntry>().Where(x => x.Index <= lastEntryIndex && x.Index > savedLastEntryIndex)
                //                           orderby ent.Index
                //                           select ent);

                JLogger.LogDebug(this, "eventslog:{0} FILTERED:{1} TOTAL:{2}",
                                 eventLog.LogDisplayName, filteredEventEntrys.Count(), eventLog.Entries.Count);
                foreach (var filteredEventEntry in filteredEventEntrys)
                {
                    JLogger.LogDebug(this, "Start() Processing entry: {0}", filteredEventEntry.Index);
                    var fieldSeparator = (char)28;
                    var sbFactMessage  = new StringBuilder();
                    sbFactMessage.AppendFormat("{0}{1}{2}{3}{4}{5}{6}{7}{8}",
                                               eventLog.LogDisplayName, fieldSeparator,
                                               filteredEventEntry.Index, fieldSeparator,
                                               filteredEventEntry.Source, fieldSeparator,
                                               filteredEventEntry.TimeGenerated, fieldSeparator,
                                               filteredEventEntry.Message);

                    //FIRE NEW EVENT
                    JLogger.LogDebug(this, "Start() Firing Event: {0}", sbFactMessage.ToString());
                    OnNewFact?.Invoke(this,
                                      new FactWatcherEventArgs(GetFactWatcherName(), filteredEventEntry.TimeGenerated,
                                                               sbFactMessage.ToString()));

                    //SAVE LOG FACTS
                    JLogger.LogDebug(this, "Validate if is configured fact file");
                    if (!string.IsNullOrEmpty(_savedFactFileName))
                    {
                        SaveFactToLog(sbFactMessage.ToString());
                    }

                    //STORE THE LATEST INDEX PROCESSED
                    Persistence.SetValueByKey("LAST_ENTRY_INDEX", filteredEventEntry.Index.ToString());
                    Persistence.SetValueByKey("LAST_ENTRY_DATETIME", filteredEventEntry.TimeGenerated.ToString(CultureInfo.InvariantCulture));
                    //Config.Save();
                }
            }
        }