<commit_before>﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using NLog;
using System.Text.RegularExpressions;
using BloombergFLP.CollectdWin;
using System.ServiceProcess;
using System.Linq;

namespace Netuitive.CollectdWin
{

    internal class ReadSystemChecksPlugin : ICollectdReadPlugin
    {
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
        private IList<SystemCheckConfig> _checks;
        private string _hostName;
        private int _interval;
        private bool _sendAgentHeartbeat;
        private int _heartbeatInterval;
        public ReadSystemChecksPlugin()
        {
            _checks = new List<SystemCheckConfig>();
        }

        public void Configure()
        {
            var config = ConfigurationManager.GetSection("ReadSystemChecks") as ReadSystemChecksPluginConfig;
            if (config == null)
            {
                throw new Exception("Cannot get configuration section : ReadWindowsAttributes");
            }

            var baseConfig = CollectdWinConfig.GetConfig();

            _interval = baseConfig.GeneralSettings.Interval;

            _sendAgentHeartbeat = config.EnableAgentHeartbeat;
            _heartbeatInterval = (int)Math.Ceiling(_interval * Math.Max(1.0,config.HeartbeatTTLMultiplier));
            Logger.Info("Agent heartbeat enabled: {0}, interval: {1}secs", _sendAgentHeartbeat, _heartbeatInterval);


            foreach (SystemCheckConfig check in config.Checks)
            {
                _checks.Add(check);
                Logger.Info("Added {0} '{1}' as '{2}' with interval {3} secs", check.GetType().Name , check.Name, check.Alias, check.GetTTL(_interval));
            }

            _hostName = Util.GetHostName();

            Logger.Info("ReadSystemChecks plugin configured");
        }

        public void Start()
        {
            Logger.Info("ReadSystemChecks plugin started");
        }

        public void Stop()
        {
            Logger.Info("ReadSystemChecks plugin stopped");
        }

        public IList<CollectableValue> Read()
        {
            var checkList = new List<CollectableValue>();

            if (_sendAgentHeartbeat)
            {
                checkList.Add(createCheck("heartbeat", _heartbeatInterval));
            }

            checkList.AddRange(CheckServices());
            checkList.AddRange(CheckProcesses());

            return checkList;
        }

        private List<CollectableValue> CheckServices()
        {
            List<CollectableValue> checkList = new List<CollectableValue>();

            List<ServiceCheckConfig> serviceChecks = _checks.OfType<ServiceCheckConfig>().ToList();

            ServiceController[] serviceList = ServiceController.GetServices();

                foreach (SystemCheckConfig checkConfig in serviceChecks)
            {
                Regex regex = new Regex(checkConfig.Name, RegexOptions.None);

                List<ServiceController> matches = serviceList.Where(service => regex.IsMatch(service.ServiceName)).ToList();
                foreach(ServiceController foundService in matches)
                {
                    if (foundService.Status == ServiceControllerStatus.Running)
                    {
                        Logger.Debug("Creating check for service: {0}", checkConfig.Name);
                        checkList.Add(createCheck(checkConfig.Alias, checkConfig.Interval)); 
                            checkList.Add(createCheck(checkConfig.Alias, checkConfig.GetTTL(_interval)));
                    }
                    else
                    {
                        Logger.Warn("Service '{0}' not running. Check not created.", checkConfig.Name);
                    }
                }

                if (matches.Count == 0)
                {
                    Logger.Warn("No services matching '{0}' were found. Check not created.", checkConfig.Name);
                }
            }
            return checkList;
        }

        private List<CollectableValue> CheckProcesses()
        {
            List<CollectableValue> checkList = new List<CollectableValue>();
            

            List<ProcessCheckConfig> processChecks = _checks.OfType<ProcessCheckConfig>().ToList();

            Process[] processList = Process.GetProcesses();

                foreach (SystemCheckConfig checkConfig in processChecks)
            {
                Regex regex = new Regex(checkConfig.Name, RegexOptions.None);

                List<Process> matches = processList.Where(process =>
                {
                    return regex.IsMatch(process.ProcessName);
                }).ToList();

                foreach (Process foundProcess in matches)
                {
                    Logger.Debug("Creating check for process: {0}", checkConfig.Name);
                    checkList.Add(createCheck(checkConfig.Alias, checkConfig.Interval));
                        checkList.Add(createCheck(checkConfig.Alias, checkConfig.GetTTL(_interval)));
                }

                if (matches.Count == 0)
                {
                    Logger.Warn("No processes matching '{0}' were found. Check not created.", checkConfig.Name);
                }
                
            }

            return checkList;
        }

        private CheckValue createCheck(string name, int interval)
        {
            string cleanName = name.Replace(",", "_");
            CheckValue check = new CheckValue
            {
                HostName = _hostName,
                Name = cleanName,
                CheckInterval = interval
            };

            return check;

        }
    }<commit_msg>Only get process and service lists if any checks are defined.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using NLog;
using System.Text.RegularExpressions;
using BloombergFLP.CollectdWin;
using System.ServiceProcess;
using System.Linq;

namespace Netuitive.CollectdWin
{

    internal class ReadSystemChecksPlugin : ICollectdReadPlugin
    {
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
        private IList<SystemCheckConfig> _checks;
        private string _hostName;
        private int _interval;
        private bool _sendAgentHeartbeat;
        private int _heartbeatInterval;
        public ReadSystemChecksPlugin()
        {
            _checks = new List<SystemCheckConfig>();
        }

        public void Configure()
        {
            var config = ConfigurationManager.GetSection("ReadSystemChecks") as ReadSystemChecksPluginConfig;
            if (config == null)
            {
                throw new Exception("Cannot get configuration section : ReadWindowsAttributes");
            }

            var baseConfig = CollectdWinConfig.GetConfig();

            _interval = baseConfig.GeneralSettings.Interval;

            _sendAgentHeartbeat = config.EnableAgentHeartbeat;
            _heartbeatInterval = (int)Math.Ceiling(_interval * Math.Max(1.0,config.HeartbeatTTLMultiplier));
            Logger.Info("Agent heartbeat enabled: {0}, interval: {1}secs", _sendAgentHeartbeat, _heartbeatInterval);


            foreach (SystemCheckConfig check in config.Checks)
            {
                _checks.Add(check);
                Logger.Info("Added {0} '{1}' as '{2}' with interval {3} secs", check.GetType().Name , check.Name, check.Alias, check.GetTTL(_interval));
            }

            _hostName = Util.GetHostName();

            Logger.Info("ReadSystemChecks plugin configured");
        }

        public void Start()
        {
            Logger.Info("ReadSystemChecks plugin started");
        }

        public void Stop()
        {
            Logger.Info("ReadSystemChecks plugin stopped");
        }

        public IList<CollectableValue> Read()
        {
            var checkList = new List<CollectableValue>();

            if (_sendAgentHeartbeat)
            {
                checkList.Add(createCheck("heartbeat", _heartbeatInterval));
            }

            checkList.AddRange(CheckServices());
            checkList.AddRange(CheckProcesses());

            return checkList;
        }

        private List<CollectableValue> CheckServices()
        {
            List<CollectableValue> checkList = new List<CollectableValue>();

            List<ServiceCheckConfig> serviceChecks = _checks.OfType<ServiceCheckConfig>().ToList();

            if (serviceChecks.Count > 0)
            {
            ServiceController[] serviceList = ServiceController.GetServices();

                foreach (SystemCheckConfig checkConfig in serviceChecks)
            {
                Regex regex = new Regex(checkConfig.Name, RegexOptions.None);

                List<ServiceController> matches = serviceList.Where(service => regex.IsMatch(service.ServiceName)).ToList();
                foreach(ServiceController foundService in matches)
                {
                    if (foundService.Status == ServiceControllerStatus.Running)
                    {
                        Logger.Debug("Creating check for service: {0}", checkConfig.Name);
                        checkList.Add(createCheck(checkConfig.Alias, checkConfig.Interval)); 
                            checkList.Add(createCheck(checkConfig.Alias, checkConfig.GetTTL(_interval)));
                    }
                    else
                    {
                        Logger.Warn("Service '{0}' not running. Check not created.", checkConfig.Name);
                    }
                }

                if (matches.Count == 0)
                {
                    Logger.Warn("No services matching '{0}' were found. Check not created.", checkConfig.Name);
                }
            }
            }
            return checkList;
        }

        private List<CollectableValue> CheckProcesses()
        {
            List<CollectableValue> checkList = new List<CollectableValue>();
            

            List<ProcessCheckConfig> processChecks = _checks.OfType<ProcessCheckConfig>().ToList();

            if (processChecks.Count > 0)
            {
            Process[] processList = Process.GetProcesses();

                foreach (SystemCheckConfig checkConfig in processChecks)
            {
                Regex regex = new Regex(checkConfig.Name, RegexOptions.None);

                List<Process> matches = processList.Where(process =>
                {
                    return regex.IsMatch(process.ProcessName);
                }).ToList();

                foreach (Process foundProcess in matches)
                {
                    Logger.Debug("Creating check for process: {0}", checkConfig.Name);
                    checkList.Add(createCheck(checkConfig.Alias, checkConfig.Interval));
                        checkList.Add(createCheck(checkConfig.Alias, checkConfig.GetTTL(_interval)));
                }

                if (matches.Count == 0)
                {
                    Logger.Warn("No processes matching '{0}' were found. Check not created.", checkConfig.Name);
                }
                
            }
            }
            return checkList;
        }

        private CheckValue createCheck(string name, int interval)
        {
            string cleanName = name.Replace(",", "_");
            CheckValue check = new CheckValue
            {
                HostName = _hostName,
                Name = cleanName,
                CheckInterval = interval
            };

            return check;

        }
    }
{'fraction_non_alphanumeric': 0.08879510808715872, 'fraction_numerical': 0.0027560072345189906, 'mean_word_length': 1.7879951980792317, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1078598', 'n_tokens_mistral': 2826, 'n_tokens_neox': 2643, 'n_words': 586}