Beispiel #1
0
        private void populateApplicationInfo(JObject applicationJSON, ControllerApplication controllerApplication)
        {
            controllerApplication.ApplicationName = getStringValueFromJToken(applicationJSON, "name");
            controllerApplication.Description     = getStringValueFromJToken(applicationJSON, "description");
            controllerApplication.ApplicationID   = getLongValueFromJToken(applicationJSON, "id");
            if (isTokenPropertyNull(applicationJSON, "applicationTypeInfo") == false &&
                isTokenPropertyNull(applicationJSON["applicationTypeInfo"], "applicationTypes") == false)
            {
                controllerApplication.Types = applicationJSON["applicationTypeInfo"]["applicationTypes"].ToString(Newtonsoft.Json.Formatting.None);
            }

            controllerApplication.CreatedBy    = getStringValueFromJToken(applicationJSON, "createdBy");
            controllerApplication.CreatedOnUtc = UnixTimeHelper.ConvertFromUnixTimestamp(getLongValueFromJToken(applicationJSON, "createdOn"));
            try { controllerApplication.CreatedOn = controllerApplication.CreatedOnUtc.ToLocalTime(); } catch { }
            controllerApplication.UpdatedBy    = getStringValueFromJToken(applicationJSON, "modifiedBy");
            controllerApplication.UpdatedOnUtc = UnixTimeHelper.ConvertFromUnixTimestamp(getLongValueFromJToken(applicationJSON, "modifiedOn"));
            try { controllerApplication.UpdatedOn = controllerApplication.UpdatedOnUtc.ToLocalTime(); } catch { }
        }
Beispiel #2
0
        public override bool Execute(ProgramOptions programOptions, JobConfiguration jobConfiguration)
        {
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            StepTiming stepTimingFunction = new StepTiming();

            stepTimingFunction.JobFileName = programOptions.OutputJobFilePath;
            stepTimingFunction.StepName    = jobConfiguration.Status.ToString();
            stepTimingFunction.StepID      = (int)jobConfiguration.Status;
            stepTimingFunction.StartTime   = DateTime.Now;
            stepTimingFunction.NumEntities = jobConfiguration.Target.Count;

            this.DisplayJobStepStartingStatus(jobConfiguration);

            FilePathMap = new FilePathMap(programOptions, jobConfiguration);

            try
            {
                if (this.ShouldExecute(programOptions, jobConfiguration) == false)
                {
                    return(true);
                }

                if (jobConfiguration.Target.Count(t => t.Type == APPLICATION_TYPE_MOBILE) == 0)
                {
                    logger.Warn("No {0} targets to process", APPLICATION_TYPE_MOBILE);
                    loggerConsole.Warn("No {0} targets to process", APPLICATION_TYPE_MOBILE);

                    return(true);
                }

                #region Template comparisons

                if (jobConfiguration.Input.ConfigurationComparisonReferenceMOBILE.Controller == BLANK_APPLICATION_CONTROLLER &&
                    jobConfiguration.Input.ConfigurationComparisonReferenceMOBILE.Application == BLANK_APPLICATION_MOBILE)
                {
                    jobConfiguration.Target.Add(jobConfiguration.Input.ConfigurationComparisonReferenceMOBILE);
                }
                else
                {
                    // Check if there is a valid reference application
                    JobTarget jobTargetReferenceApp = jobConfiguration.Target.Where(t =>
                                                                                    t.Type == APPLICATION_TYPE_MOBILE &&
                                                                                    String.Compare(t.Controller, jobConfiguration.Input.ConfigurationComparisonReferenceMOBILE.Controller, StringComparison.InvariantCultureIgnoreCase) == 0 &&
                                                                                    String.Compare(t.Application, jobConfiguration.Input.ConfigurationComparisonReferenceMOBILE.Application, StringComparison.InvariantCultureIgnoreCase) == 0).FirstOrDefault();
                    if (jobTargetReferenceApp == null)
                    {
                        // No valid reference, fall back to comparing against template
                        logger.Warn("Unable to find reference target {0}, will index default template", jobConfiguration.Input.ConfigurationComparisonReferenceMOBILE);
                        loggerConsole.Warn("Unable to find reference target {0}, will index default template", jobConfiguration.Input.ConfigurationComparisonReferenceMOBILE);

                        jobConfiguration.Input.ConfigurationComparisonReferenceMOBILE.Controller  = BLANK_APPLICATION_CONTROLLER;
                        jobConfiguration.Input.ConfigurationComparisonReferenceMOBILE.Application = BLANK_APPLICATION_MOBILE;
                        jobConfiguration.Input.ConfigurationComparisonReferenceMOBILE.Type        = APPLICATION_TYPE_MOBILE;

                        jobConfiguration.Target.Add(jobConfiguration.Input.ConfigurationComparisonReferenceMOBILE);
                    }
                }

                #endregion

                bool reportFolderCleaned = false;

                // Process each target
                for (int i = 0; i < jobConfiguration.Target.Count; i++)
                {
                    Stopwatch stopWatchTarget = new Stopwatch();
                    stopWatchTarget.Start();

                    JobTarget jobTarget = jobConfiguration.Target[i];

                    if (jobTarget.Type != null && jobTarget.Type.Length > 0 && jobTarget.Type != APPLICATION_TYPE_MOBILE)
                    {
                        continue;
                    }

                    StepTiming stepTimingTarget = new StepTiming();
                    stepTimingTarget.Controller      = jobTarget.Controller;
                    stepTimingTarget.ApplicationName = jobTarget.Application;
                    stepTimingTarget.ApplicationID   = jobTarget.ApplicationID;
                    stepTimingTarget.JobFileName     = programOptions.OutputJobFilePath;
                    stepTimingTarget.StepName        = jobConfiguration.Status.ToString();
                    stepTimingTarget.StepID          = (int)jobConfiguration.Status;
                    stepTimingTarget.StartTime       = DateTime.Now;

                    try
                    {
                        this.DisplayJobTargetStartingStatus(jobConfiguration, jobTarget, i + 1);

                        #region Preload list of detected entities

                        // For later cross-reference
                        List <ControllerApplication> controllerApplicationList = FileIOHelper.ReadListFromCSVFile <ControllerApplication>(FilePathMap.ControllerApplicationsIndexFilePath(jobTarget), new ControllerApplicationReportMap());

                        #endregion

                        #region Application Summary

                        MOBILEApplicationConfiguration applicationConfiguration = new MOBILEApplicationConfiguration();

                        loggerConsole.Info("Application Summary");

                        applicationConfiguration.Controller      = jobTarget.Controller;
                        applicationConfiguration.ControllerLink  = String.Format(DEEPLINK_CONTROLLER, applicationConfiguration.Controller, DEEPLINK_TIMERANGE_LAST_15_MINUTES);
                        applicationConfiguration.ApplicationName = jobTarget.Application;
                        applicationConfiguration.ApplicationID   = jobTarget.ApplicationID;
                        applicationConfiguration.ApplicationLink = String.Format(DEEPLINK_APM_APPLICATION, applicationConfiguration.Controller, applicationConfiguration.ApplicationID, DEEPLINK_TIMERANGE_LAST_15_MINUTES);
                        if (controllerApplicationList != null)
                        {
                            ControllerApplication controllerApplication = controllerApplicationList.Where(a => a.Type == APPLICATION_TYPE_MOBILE && a.ApplicationID == jobTarget.ApplicationID).FirstOrDefault();
                            if (controllerApplication != null)
                            {
                                applicationConfiguration.ApplicationDescription = controllerApplication.Description;
                            }
                        }

                        // Application Key
                        JObject appKeyObject = FileIOHelper.LoadJObjectFromFile(FilePathMap.MOBILEApplicationKeyDataFilePath(jobTarget));
                        if (appKeyObject != null)
                        {
                            applicationConfiguration.ApplicationKey = getStringValueFromJToken(appKeyObject, "appKey");
                        }

                        // Monitoring State
                        string monitoringState = FileIOHelper.ReadFileFromPath(FilePathMap.MOBILEApplicationMonitoringStateDataFilePath(jobTarget));
                        if (monitoringState != String.Empty)
                        {
                            bool parsedBool = false;
                            Boolean.TryParse(monitoringState, out parsedBool);
                            applicationConfiguration.IsEnabled = parsedBool;
                        }

                        // Configuration Settings
                        JObject configurationSettingsObject = FileIOHelper.LoadJObjectFromFile(FilePathMap.MOBILEAgentPageSettingsRulesDataFilePath(jobTarget));
                        if (configurationSettingsObject != null)
                        {
                            if (isTokenPropertyNull(configurationSettingsObject, "thresholds") == false)
                            {
                                applicationConfiguration.SlowThresholdType = getStringValueFromJToken(configurationSettingsObject["thresholds"]["slowThreshold"], "type");
                                applicationConfiguration.SlowThreshold     = getIntValueFromJToken(configurationSettingsObject["thresholds"]["slowThreshold"], "value");

                                applicationConfiguration.VerySlowThresholdType = getStringValueFromJToken(configurationSettingsObject["thresholds"]["verySlowThreshold"], "type");
                                applicationConfiguration.VerySlowThreshold     = getIntValueFromJToken(configurationSettingsObject["thresholds"]["verySlowThreshold"], "value");

                                applicationConfiguration.StallThresholdType = getStringValueFromJToken(configurationSettingsObject["thresholds"]["stallThreshold"], "type");
                                applicationConfiguration.StallThreshold     = getIntValueFromJToken(configurationSettingsObject["thresholds"]["stallThreshold"], "value");
                            }
                            applicationConfiguration.Percentiles    = getStringValueOfObjectFromJToken(configurationSettingsObject, "percentileMetrics", true);
                            applicationConfiguration.SessionTimeout = getIntValueFromJToken(configurationSettingsObject["sessionsMonitor"], "sessionTimeoutMins");

                            applicationConfiguration.CrashThreshold = getIntValueFromJToken(configurationSettingsObject["crashAlerts"], "threshold");

                            applicationConfiguration.IsIPDisplayed    = getBoolValueFromJToken(configurationSettingsObject, "ipAddressDisplayed");
                            applicationConfiguration.EnableScreenshot = getBoolValueFromJToken(configurationSettingsObject["agentConfigData"], "enableScreenshot");
                            applicationConfiguration.AutoScreenshot   = getBoolValueFromJToken(configurationSettingsObject["agentConfigData"], "autoScreenshot");
                            applicationConfiguration.UseCellular      = getBoolValueFromJToken(configurationSettingsObject["agentConfigData"], "screenshotUseCellular");
                        }

                        #endregion

                        #region Network Requests

                        List <MOBILENetworkRequestRule> networkRequestRulesList = new List <MOBILENetworkRequestRule>(128);

                        JObject networkRequestRulesObject = FileIOHelper.LoadJObjectFromFile(FilePathMap.MOBILEAgentNetworkRequestsRulesDataFilePath(jobTarget));
                        if (networkRequestRulesObject != null)
                        {
                            if (isTokenPropertyNull(networkRequestRulesObject, "customNamingIncludeRules") == false)
                            {
                                JArray includeRulesArray = (JArray)networkRequestRulesObject["customNamingIncludeRules"];
                                foreach (JObject includeRuleObject in includeRulesArray)
                                {
                                    MOBILENetworkRequestRule networkRequestRule = fillNetworkRequestRule(includeRuleObject, jobTarget);
                                    if (networkRequestRule != null)
                                    {
                                        networkRequestRule.DetectionType = "INCLUDE";

                                        networkRequestRulesList.Add(networkRequestRule);
                                    }
                                }
                            }

                            if (isTokenPropertyNull(networkRequestRulesObject, "customNamingExcludeRules") == false)
                            {
                                JArray excludeRulesArray = (JArray)networkRequestRulesObject["customNamingExcludeRules"];
                                foreach (JObject excludeRuleObject in excludeRulesArray)
                                {
                                    MOBILENetworkRequestRule networkRequestRule = fillNetworkRequestRule(excludeRuleObject, jobTarget);
                                    if (networkRequestRule != null)
                                    {
                                        networkRequestRule.DetectionType = "EXCLUDE";

                                        networkRequestRulesList.Add(networkRequestRule);
                                    }
                                }
                            }
                        }

                        // Sort them
                        networkRequestRulesList = networkRequestRulesList.OrderBy(o => o.DetectionType).ThenBy(o => o.Priority).ToList();
                        FileIOHelper.WriteListToCSVFile(networkRequestRulesList, new MOBILENetworkRequestRuleReportMap(), FilePathMap.MOBILENetworkRequestRulesIndexFilePath(jobTarget));

                        loggerConsole.Info("Completed {0} Rules", networkRequestRulesList.Count);


                        #endregion

                        #region Application Settings

                        if (networkRequestRulesList != null)
                        {
                            applicationConfiguration.NumNetworkRulesInclude = networkRequestRulesList.Count(r => r.DetectionType == "INCLUDE");
                            applicationConfiguration.NumNetworkRulesExclude = networkRequestRulesList.Count(r => r.DetectionType == "EXCLUDE");
                        }

                        List <MOBILEApplicationConfiguration> applicationConfigurationsList = new List <MOBILEApplicationConfiguration>(1);
                        applicationConfigurationsList.Add(applicationConfiguration);
                        FileIOHelper.WriteListToCSVFile(applicationConfigurationsList, new MOBILEApplicationConfigurationReportMap(), FilePathMap.MOBILEApplicationConfigurationIndexFilePath(jobTarget));

                        stepTimingTarget.NumEntities = stepTimingTarget.NumEntities + applicationConfigurationsList.Count;

                        #endregion

                        #region Combine All for Report CSV

                        // If it is the first one, clear out the combined folder
                        if (reportFolderCleaned == false)
                        {
                            FileIOHelper.DeleteFolder(FilePathMap.MOBILEConfigurationReportFolderPath());
                            Thread.Sleep(1000);
                            FileIOHelper.CreateFolder(FilePathMap.WEBConfigurationReportFolderPath());
                            reportFolderCleaned = true;
                        }

                        // Append all the individual report files into one
                        if (File.Exists(FilePathMap.MOBILEApplicationConfigurationIndexFilePath(jobTarget)) == true && new FileInfo(FilePathMap.MOBILEApplicationConfigurationIndexFilePath(jobTarget)).Length > 0)
                        {
                            FileIOHelper.AppendTwoCSVFiles(FilePathMap.MOBILEApplicationConfigurationReportFilePath(), FilePathMap.MOBILEApplicationConfigurationIndexFilePath(jobTarget));
                        }
                        if (File.Exists(FilePathMap.MOBILENetworkRequestRulesIndexFilePath(jobTarget)) == true && new FileInfo(FilePathMap.MOBILENetworkRequestRulesIndexFilePath(jobTarget)).Length > 0)
                        {
                            FileIOHelper.AppendTwoCSVFiles(FilePathMap.MOBILENetworkRequestRulesReportFilePath(), FilePathMap.MOBILENetworkRequestRulesIndexFilePath(jobTarget));
                        }

                        #endregion
                    }
                    catch (Exception ex)
                    {
                        logger.Warn(ex);
                        loggerConsole.Warn(ex);

                        return(false);
                    }
                    finally
                    {
                        stopWatchTarget.Stop();

                        this.DisplayJobTargetEndedStatus(jobConfiguration, jobTarget, i + 1, stopWatchTarget);

                        stepTimingTarget.EndTime    = DateTime.Now;
                        stepTimingTarget.Duration   = stopWatchTarget.Elapsed;
                        stepTimingTarget.DurationMS = stopWatchTarget.ElapsedMilliseconds;

                        List <StepTiming> stepTimings = new List <StepTiming>(1);
                        stepTimings.Add(stepTimingTarget);
                        FileIOHelper.WriteListToCSVFile(stepTimings, new StepTimingReportMap(), FilePathMap.StepTimingReportFilePath(), true);
                    }
                }

                // Remove all templates from the list
                jobConfiguration.Target.RemoveAll(t => t.Controller == BLANK_APPLICATION_CONTROLLER);

                return(true);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                loggerConsole.Error(ex);

                return(false);
            }
            finally
            {
                stopWatch.Stop();

                this.DisplayJobStepEndedStatus(jobConfiguration, stopWatch);

                stepTimingFunction.EndTime    = DateTime.Now;
                stepTimingFunction.Duration   = stopWatch.Elapsed;
                stepTimingFunction.DurationMS = stopWatch.ElapsedMilliseconds;

                List <StepTiming> stepTimings = new List <StepTiming>(1);
                stepTimings.Add(stepTimingFunction);
                FileIOHelper.WriteListToCSVFile(stepTimings, new StepTimingReportMap(), FilePathMap.StepTimingReportFilePath(), true);
            }
        }
Beispiel #3
0
        public override bool Execute(ProgramOptions programOptions, JobConfiguration jobConfiguration)
        {
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            StepTiming stepTimingFunction = new StepTiming();

            stepTimingFunction.JobFileName = programOptions.OutputJobFilePath;
            stepTimingFunction.StepName    = jobConfiguration.Status.ToString();
            stepTimingFunction.StepID      = (int)jobConfiguration.Status;
            stepTimingFunction.StartTime   = DateTime.Now;
            stepTimingFunction.NumEntities = jobConfiguration.Target.Count;

            this.DisplayJobStepStartingStatus(jobConfiguration);

            FilePathMap = new FilePathMap(programOptions, jobConfiguration);

            try
            {
                if (this.ShouldExecute(jobConfiguration) == false)
                {
                    return(true);
                }

                bool reportFolderCleaned = false;

                // Process each Controller once
                int i           = 0;
                var controllers = jobConfiguration.Target.GroupBy(t => t.Controller);
                foreach (var controllerGroup in controllers)
                {
                    Stopwatch stopWatchTarget = new Stopwatch();
                    stopWatchTarget.Start();

                    JobTarget jobTarget = controllerGroup.ToList()[0];

                    StepTiming stepTimingTarget = new StepTiming();
                    stepTimingTarget.Controller      = jobTarget.Controller;
                    stepTimingTarget.ApplicationName = jobTarget.Application;
                    stepTimingTarget.ApplicationID   = jobTarget.ApplicationID;
                    stepTimingTarget.JobFileName     = programOptions.OutputJobFilePath;
                    stepTimingTarget.StepName        = jobConfiguration.Status.ToString();
                    stepTimingTarget.StepID          = (int)jobConfiguration.Status;
                    stepTimingTarget.StartTime       = DateTime.Now;

                    stepTimingTarget.NumEntities = 0;

                    try
                    {
                        this.DisplayJobTargetStartingStatus(jobConfiguration, jobTarget, i + 1);

                        #region Controller Version

                        loggerConsole.Info("Controller Version");

                        // Create this row
                        ControllerSummary controllerSummary = new ControllerSummary();
                        controllerSummary.Controller     = jobTarget.Controller;
                        controllerSummary.ControllerLink = String.Format(DEEPLINK_CONTROLLER, controllerSummary.Controller, DEEPLINK_TIMERANGE_LAST_15_MINUTES);

                        // Lookup version
                        // Load the configuration.xml from the child to parse the version
                        XmlDocument configXml = FileIOHelper.LoadXmlDocumentFromFile(FilePathMap.ControllerVersionDataFilePath(jobTarget));
                        if (configXml != null)
                        {
                            //<serverstatus version="1" vendorid="">
                            //    <available>true</available>
                            //    <serverid/>
                            //    <serverinfo>
                            //        <vendorname>AppDynamics</vendorname>
                            //        <productname>AppDynamics Application Performance Management</productname>
                            //        <serverversion>004-004-001-000</serverversion>
                            //        <implementationVersion>Controller v4.4.1.0 Build 164 Commit 6e1fd94d18dc87c1ecab2da573f98cea49d31c3a</implementationVersion>
                            //    </serverinfo>
                            //    <startupTimeInSeconds>19</startupTimeInSeconds>
                            //</serverstatus>
                            string   controllerVersion         = configXml.SelectSingleNode("serverstatus/serverinfo/serverversion").InnerText;
                            string[] controllerVersionArray    = controllerVersion.Split('-');
                            int[]    controllerVersionArrayNum = new int[controllerVersionArray.Length];
                            for (int j = 0; j < controllerVersionArray.Length; j++)
                            {
                                controllerVersionArrayNum[j] = Convert.ToInt32(controllerVersionArray[j]);
                            }
                            controllerVersion               = String.Join(".", controllerVersionArrayNum);
                            controllerSummary.Version       = controllerVersion;
                            controllerSummary.VersionDetail = configXml.SelectSingleNode("serverstatus/serverinfo/implementationVersion").InnerText;
                            controllerSummary.StartupTime   = Convert.ToInt32(configXml.SelectSingleNode("serverstatus/startupTimeInSeconds").InnerText);
                        }
                        else
                        {
                            controllerSummary.Version = "No config data";
                        }

                        #endregion

                        #region All Applications

                        JObject allApplicationsContainerObject = FileIOHelper.LoadJObjectFromFile(FilePathMap.AllApplicationsDataFilePath(jobTarget));
                        JArray  mobileApplicationsArray        = FileIOHelper.LoadJArrayFromFile(FilePathMap.MOBILEApplicationsDataFilePath(jobTarget));

                        List <ControllerApplication> controllerApplicationsList = new List <ControllerApplication>(100);

                        if (isTokenPropertyNull(allApplicationsContainerObject, "apmApplications") == false)
                        {
                            loggerConsole.Info("Index List of APM Applications");

                            foreach (JObject applicationObject in allApplicationsContainerObject["apmApplications"])
                            {
                                ControllerApplication controllerApplication = new ControllerApplication();
                                controllerApplication.Controller = jobTarget.Controller;

                                populateApplicationInfo(applicationObject, controllerApplication);

                                controllerApplication.Type = APPLICATION_TYPE_APM;

                                controllerApplicationsList.Add(controllerApplication);
                            }
                        }

                        if (isTokenPropertyNull(allApplicationsContainerObject, "eumWebApplications") == false)
                        {
                            loggerConsole.Info("Index List of WEB Applications");

                            foreach (JObject applicationObject in allApplicationsContainerObject["eumWebApplications"])
                            {
                                ControllerApplication controllerApplication = new ControllerApplication();
                                controllerApplication.Controller = jobTarget.Controller;

                                populateApplicationInfo(applicationObject, controllerApplication);

                                controllerApplication.Type = APPLICATION_TYPE_WEB;

                                controllerApplicationsList.Add(controllerApplication);
                            }
                        }

                        if (isTokenPropertyNull(allApplicationsContainerObject, "iotApplications") == false)
                        {
                            loggerConsole.Info("Index List of IOT Applications");

                            foreach (JObject applicationObject in allApplicationsContainerObject["iotApplications"])
                            {
                                ControllerApplication controllerApplication = new ControllerApplication();
                                controllerApplication.Controller = jobTarget.Controller;

                                populateApplicationInfo(applicationObject, controllerApplication);

                                controllerApplication.Type = APPLICATION_TYPE_IOT;

                                controllerApplicationsList.Add(controllerApplication);
                            }
                        }

                        if (isTokenPropertyNull(allApplicationsContainerObject, "mobileAppContainers") == false)
                        {
                            loggerConsole.Info("Index List of MOBILE Applications");

                            foreach (JObject applicationObject in allApplicationsContainerObject["mobileAppContainers"])
                            {
                                ControllerApplication controllerApplication = new ControllerApplication();
                                controllerApplication.Controller = jobTarget.Controller;

                                populateApplicationInfo(applicationObject, controllerApplication);

                                controllerApplication.Type = APPLICATION_TYPE_MOBILE;

                                if (controllerApplicationsList.Where(a => a.ApplicationID == controllerApplication.ApplicationID).Count() == 0)
                                {
                                    controllerApplicationsList.Add(controllerApplication);
                                }

                                // Now go through children
                                if (mobileApplicationsArray != null)
                                {
                                    JToken mobileApplicationContainerObject = mobileApplicationsArray.Where(a => getLongValueFromJToken(a, "applicationId") == controllerApplication.ApplicationID).FirstOrDefault();
                                    if (mobileApplicationContainerObject != null)
                                    {
                                        foreach (JObject mobileApplicationChildJSON in mobileApplicationContainerObject["children"])
                                        {
                                            ControllerApplication controllerApplicationChild = controllerApplication.Clone();
                                            controllerApplicationChild.ParentApplicationID = controllerApplicationChild.ApplicationID;

                                            controllerApplicationChild.ApplicationName = getStringValueFromJToken(mobileApplicationChildJSON, "name");
                                            controllerApplicationChild.ApplicationID   = getLongValueFromJToken(mobileApplicationChildJSON, "mobileAppId");

                                            controllerApplicationsList.Add(controllerApplicationChild);
                                        }
                                    }
                                }
                            }
                        }

                        if (isTokenPropertyNull(allApplicationsContainerObject, "dbMonApplication") == false)
                        {
                            loggerConsole.Info("Index DB Application");

                            JObject applicationObject = (JObject)allApplicationsContainerObject["dbMonApplication"];

                            ControllerApplication controllerApplication = new ControllerApplication();
                            controllerApplication.Controller = jobTarget.Controller;

                            populateApplicationInfo(applicationObject, controllerApplication);

                            controllerApplication.Type = APPLICATION_TYPE_DB;

                            controllerApplicationsList.Add(controllerApplication);
                        }

                        if (isTokenPropertyNull(allApplicationsContainerObject, "simApplication") == false)
                        {
                            loggerConsole.Info("Index SIM Application");

                            JObject applicationObject = (JObject)allApplicationsContainerObject["simApplication"];

                            ControllerApplication controllerApplication = new ControllerApplication();
                            controllerApplication.Controller = jobTarget.Controller;

                            populateApplicationInfo(applicationObject, controllerApplication);

                            controllerApplication.Type = APPLICATION_TYPE_SIM;

                            controllerApplicationsList.Add(controllerApplication);
                        }

                        if (isTokenPropertyNull(allApplicationsContainerObject, "analyticsApplication") == false)
                        {
                            loggerConsole.Info("Index BIQ Application");

                            JObject applicationObject = (JObject)allApplicationsContainerObject["analyticsApplication"];

                            ControllerApplication controllerApplication = new ControllerApplication();
                            controllerApplication.Controller = jobTarget.Controller;

                            populateApplicationInfo(applicationObject, controllerApplication);

                            controllerApplication.Type = APPLICATION_TYPE_BIQ;

                            controllerApplicationsList.Add(controllerApplication);
                        }

                        // Sort them
                        controllerApplicationsList = controllerApplicationsList.OrderBy(o => o.Type).ThenBy(o => o.ApplicationName).ToList();
                        FileIOHelper.WriteListToCSVFile(controllerApplicationsList, new ControllerApplicationReportMap(), FilePathMap.ControllerApplicationsIndexFilePath(jobTarget));

                        controllerSummary.NumApps       = controllerApplicationsList.Count;
                        controllerSummary.NumAPMApps    = controllerApplicationsList.Where(a => a.Type == APPLICATION_TYPE_APM).Count();
                        controllerSummary.NumWEBApps    = controllerApplicationsList.Where(a => a.Type == APPLICATION_TYPE_WEB).Count();
                        controllerSummary.NumMOBILEApps = controllerApplicationsList.Where(a => a.Type == APPLICATION_TYPE_MOBILE).Count();
                        controllerSummary.NumSIMApps    = controllerApplicationsList.Where(a => a.Type == APPLICATION_TYPE_SIM).Count();
                        controllerSummary.NumDBApps     = controllerApplicationsList.Where(a => a.Type == APPLICATION_TYPE_DB).Count();
                        controllerSummary.NumBIQApps    = controllerApplicationsList.Where(a => a.Type == APPLICATION_TYPE_BIQ).Count();
                        controllerSummary.NumIOTApps    = controllerApplicationsList.Where(a => a.Type == APPLICATION_TYPE_IOT).Count();

                        List <ControllerSummary> controllerList = new List <ControllerSummary>(1);
                        controllerList.Add(controllerSummary);
                        FileIOHelper.WriteListToCSVFile(controllerList, new ControllerSummaryReportMap(), FilePathMap.ControllerSummaryIndexFilePath(jobTarget));

                        stepTimingTarget.NumEntities = stepTimingTarget.NumEntities + controllerApplicationsList.Count;

                        #endregion

                        #region Combine All for Report CSV

                        // If it is the first one, clear out the combined folder
                        if (reportFolderCleaned == false)
                        {
                            FileIOHelper.DeleteFolder(FilePathMap.ControllerEntitiesReportFolderPath());
                            Thread.Sleep(1000);
                            FileIOHelper.CreateFolder(FilePathMap.ControllerEntitiesReportFolderPath());
                            reportFolderCleaned = true;
                        }

                        // Append all the individual report files into one
                        if (File.Exists(FilePathMap.ControllerSummaryIndexFilePath(jobTarget)) == true && new FileInfo(FilePathMap.ControllerSummaryIndexFilePath(jobTarget)).Length > 0)
                        {
                            FileIOHelper.AppendTwoCSVFiles(FilePathMap.ControllerSummaryReportFilePath(), FilePathMap.ControllerSummaryIndexFilePath(jobTarget));
                        }
                        if (File.Exists(FilePathMap.ControllerApplicationsIndexFilePath(jobTarget)) == true && new FileInfo(FilePathMap.ControllerApplicationsIndexFilePath(jobTarget)).Length > 0)
                        {
                            FileIOHelper.AppendTwoCSVFiles(FilePathMap.ControllerApplicationsReportFilePath(), FilePathMap.ControllerApplicationsIndexFilePath(jobTarget));
                        }

                        #endregion
                    }
                    catch (Exception ex)
                    {
                        logger.Warn(ex);
                        loggerConsole.Warn(ex);

                        return(false);
                    }
                    finally
                    {
                        stopWatchTarget.Stop();

                        this.DisplayJobTargetEndedStatus(jobConfiguration, jobTarget, i + 1, stopWatchTarget);

                        stepTimingTarget.EndTime    = DateTime.Now;
                        stepTimingTarget.Duration   = stopWatchTarget.Elapsed;
                        stepTimingTarget.DurationMS = stopWatchTarget.ElapsedMilliseconds;

                        List <StepTiming> stepTimings = new List <StepTiming>(1);
                        stepTimings.Add(stepTimingTarget);
                        FileIOHelper.WriteListToCSVFile(stepTimings, new StepTimingReportMap(), FilePathMap.StepTimingReportFilePath(), true);
                    }

                    i++;
                }

                return(true);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                loggerConsole.Error(ex);

                return(false);
            }
            finally
            {
                stopWatch.Stop();

                this.DisplayJobStepEndedStatus(jobConfiguration, stopWatch);

                stepTimingFunction.EndTime    = DateTime.Now;
                stepTimingFunction.Duration   = stopWatch.Elapsed;
                stepTimingFunction.DurationMS = stopWatch.ElapsedMilliseconds;

                List <StepTiming> stepTimings = new List <StepTiming>(1);
                stepTimings.Add(stepTimingFunction);
                FileIOHelper.WriteListToCSVFile(stepTimings, new StepTimingReportMap(), FilePathMap.StepTimingReportFilePath(), true);
            }
        }
Beispiel #4
0
        public override bool Execute(ProgramOptions programOptions, JobConfiguration jobConfiguration)
        {
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            StepTiming stepTimingFunction = new StepTiming();

            stepTimingFunction.JobFileName = programOptions.OutputJobFilePath;
            stepTimingFunction.StepName    = jobConfiguration.Status.ToString();
            stepTimingFunction.StepID      = (int)jobConfiguration.Status;
            stepTimingFunction.StartTime   = DateTime.Now;
            stepTimingFunction.NumEntities = jobConfiguration.Target.Count;

            this.DisplayJobStepStartingStatus(jobConfiguration);

            FilePathMap = new FilePathMap(programOptions, jobConfiguration);

            try
            {
                if (this.ShouldExecute(programOptions, jobConfiguration) == false)
                {
                    return(true);
                }

                bool reportFolderCleaned = false;

                // Process each Controller once
                int i           = 0;
                var controllers = jobConfiguration.Target.GroupBy(t => t.Controller);
                foreach (var controllerGroup in controllers)
                {
                    Stopwatch stopWatchTarget = new Stopwatch();
                    stopWatchTarget.Start();

                    JobTarget jobTarget = controllerGroup.ToList()[0];

                    StepTiming stepTimingTarget = new StepTiming();
                    stepTimingTarget.Controller      = jobTarget.Controller;
                    stepTimingTarget.ApplicationName = jobTarget.Application;
                    stepTimingTarget.ApplicationID   = jobTarget.ApplicationID;
                    stepTimingTarget.JobFileName     = programOptions.OutputJobFilePath;
                    stepTimingTarget.StepName        = jobConfiguration.Status.ToString();
                    stepTimingTarget.StepID          = (int)jobConfiguration.Status;
                    stepTimingTarget.StartTime       = DateTime.Now;

                    stepTimingTarget.NumEntities = 0;

                    try
                    {
                        this.DisplayJobTargetStartingStatus(jobConfiguration, jobTarget, i + 1);

                        #region Prepare time range

                        long   fromTimeUnix            = UnixTimeHelper.ConvertToUnixTimestamp(jobConfiguration.Input.TimeRange.From);
                        long   toTimeUnix              = UnixTimeHelper.ConvertToUnixTimestamp(jobConfiguration.Input.TimeRange.To);
                        int    differenceInMinutes     = (int)(jobConfiguration.Input.TimeRange.To - jobConfiguration.Input.TimeRange.From).Duration().TotalMinutes;
                        string DEEPLINK_THIS_TIMERANGE = String.Format(DEEPLINK_TIMERANGE_BETWEEN_TIMES, toTimeUnix, fromTimeUnix, differenceInMinutes);

                        #endregion

                        #region Account Summary

                        loggerConsole.Info("Account Summary");

                        LicenseAccountSummary licenseAccountSummary = new LicenseAccountSummary();

                        JObject accountSummaryContainerObject = FileIOHelper.LoadJObjectFromFile(FilePathMap.LicenseAccountDataFilePath(jobTarget));
                        if (accountSummaryContainerObject != null && isTokenPropertyNull(accountSummaryContainerObject, "account") == false)
                        {
                            JObject accountSummaryLicenseObject = (JObject)accountSummaryContainerObject["account"];

                            licenseAccountSummary.Controller = jobTarget.Controller;

                            licenseAccountSummary.AccountID         = getLongValueFromJToken(accountSummaryLicenseObject, "id");
                            licenseAccountSummary.AccountName       = getStringValueFromJToken(accountSummaryLicenseObject, "name");
                            licenseAccountSummary.AccountNameGlobal = getStringValueFromJToken(accountSummaryLicenseObject, "globalAccountName");
                            licenseAccountSummary.AccountNameEUM    = getStringValueFromJToken(accountSummaryLicenseObject, "eumAccountName");

                            licenseAccountSummary.AccessKey1    = getStringValueFromJToken(accountSummaryLicenseObject, "accessKey");
                            licenseAccountSummary.AccessKey2    = getStringValueFromJToken(accountSummaryLicenseObject, "key");
                            licenseAccountSummary.LicenseKeyEUM = getStringValueFromJToken(accountSummaryLicenseObject, "eumCloudLicenseKey");
                            licenseAccountSummary.ServiceKeyES  = getStringValueFromJToken(accountSummaryLicenseObject, "eumEventServiceKey");

                            licenseAccountSummary.ExpirationDate = UnixTimeHelper.ConvertFromUnixTimestamp(getLongValueFromJToken(accountSummaryLicenseObject, "expirationDate"));

                            licenseAccountSummary.LicenseLink = String.Format(DEEPLINK_LICENSE, licenseAccountSummary.Controller, DEEPLINK_THIS_TIMERANGE);

                            List <LicenseAccountSummary> licenseAccountSummaryList = new List <LicenseAccountSummary>(1);
                            licenseAccountSummaryList.Add(licenseAccountSummary);
                            FileIOHelper.WriteListToCSVFile(licenseAccountSummaryList, new LicenseAccountSummaryReportMap(), FilePathMap.LicenseAccountIndexFilePath(jobTarget));

                            stepTimingTarget.NumEntities = stepTimingTarget.NumEntities + licenseAccountSummaryList.Count;
                        }

                        #endregion

                        #region Account Level License Entitlements and Usage

                        loggerConsole.Info("Account License Summary and Usage");

                        List <License> licensesList = new List <License>(24);

                        // Typically there are 12 values per hour, if we extracted every 5 minute things
                        List <LicenseValue> licenseValuesGlobalList = new List <LicenseValue>(jobConfiguration.Input.HourlyTimeRanges.Count * 12 * licensesList.Count);

                        JObject licenseModulesContainer = FileIOHelper.LoadJObjectFromFile(FilePathMap.LicenseModulesDataFilePath(jobTarget));
                        if (licenseModulesContainer != null &&
                            isTokenPropertyNull(licenseModulesContainer, "modules") == false)
                        {
                            JArray licenseModulesArray = (JArray)licenseModulesContainer["modules"];
                            foreach (JObject licenseModuleObject in licenseModulesArray)
                            {
                                string licenseModuleName = getStringValueFromJToken(licenseModuleObject, "name");

                                License license = new License();

                                license.Controller  = jobTarget.Controller;
                                license.AccountID   = licenseAccountSummary.AccountID;
                                license.AccountName = licenseAccountSummary.AccountName;

                                // Duration
                                license.Duration = (int)(jobConfiguration.Input.TimeRange.To - jobConfiguration.Input.TimeRange.From).Duration().TotalMinutes;
                                license.From     = jobConfiguration.Input.TimeRange.From.ToLocalTime();
                                license.To       = jobConfiguration.Input.TimeRange.To.ToLocalTime();
                                license.FromUtc  = jobConfiguration.Input.TimeRange.From;
                                license.ToUtc    = jobConfiguration.Input.TimeRange.To;

                                license.AgentType = licenseModuleName;

                                JObject licenseModulePropertiesContainer = FileIOHelper.LoadJObjectFromFile(FilePathMap.LicenseModulePropertiesDataFilePath(jobTarget, licenseModuleName));
                                if (licenseModulePropertiesContainer != null &&
                                    isTokenPropertyNull(licenseModulePropertiesContainer, "properties") == false)
                                {
                                    foreach (JObject licensePropertyObject in licenseModulePropertiesContainer["properties"])
                                    {
                                        string valueOfProperty = getStringValueFromJToken(licensePropertyObject, "value");
                                        switch (getStringValueFromJToken(licensePropertyObject, "name"))
                                        {
                                        case "expiry-date":
                                            long expiryDateLong = 0;
                                            if (long.TryParse(valueOfProperty, out expiryDateLong) == true)
                                            {
                                                license.ExpirationDate = UnixTimeHelper.ConvertFromUnixTimestamp(expiryDateLong);
                                            }
                                            break;

                                        case "licensing-model":
                                            license.Model = valueOfProperty;
                                            break;

                                        case "edition":
                                            license.Edition = valueOfProperty;
                                            break;

                                        case "number-of-provisioned-licenses":
                                            long numberOfLicensesLong = 0;
                                            if (long.TryParse(valueOfProperty, out numberOfLicensesLong) == true)
                                            {
                                                license.Provisioned = numberOfLicensesLong;
                                            }
                                            break;

                                        case "maximum-allowed-licenses":
                                            long maximumNumberOfLicensesLong = 0;
                                            if (long.TryParse(valueOfProperty, out maximumNumberOfLicensesLong) == true)
                                            {
                                                license.MaximumAllowed = maximumNumberOfLicensesLong;
                                            }
                                            break;

                                        case "data-retention-period":
                                            int dataRetentionPeriodInt = 0;
                                            if (int.TryParse(valueOfProperty, out dataRetentionPeriodInt) == true)
                                            {
                                                license.Retention = dataRetentionPeriodInt;
                                            }
                                            break;

                                        default:
                                            break;
                                        }
                                    }
                                }

                                List <LicenseValue> licenseValuesList = new List <LicenseValue>(jobConfiguration.Input.HourlyTimeRanges.Count * 12);

                                JObject licenseModuleUsagesContainer = FileIOHelper.LoadJObjectFromFile(FilePathMap.LicenseModuleUsagesDataFilePath(jobTarget, licenseModuleName));
                                if (licenseModuleUsagesContainer != null &&
                                    isTokenPropertyNull(licenseModuleUsagesContainer, "usages") == false)
                                {
                                    foreach (JObject licenseUsageObject in licenseModuleUsagesContainer["usages"])
                                    {
                                        LicenseValue licenseValue = new LicenseValue();

                                        licenseValue.Controller  = license.Controller;
                                        licenseValue.AccountName = license.AccountName;
                                        licenseValue.AccountID   = license.AccountID;

                                        licenseValue.AgentType = license.AgentType;

                                        licenseValue.RuleName = "Account";

                                        licenseValue.LicenseEventTimeUtc = UnixTimeHelper.ConvertFromUnixTimestamp(getLongValueFromJToken(licenseUsageObject, "createdOn"));
                                        licenseValue.LicenseEventTime    = licenseValue.LicenseEventTimeUtc.ToLocalTime();

                                        licenseValue.Min     = getLongValueFromJToken(licenseUsageObject, "minUnitsUsed");
                                        licenseValue.Max     = getLongValueFromJToken(licenseUsageObject, "maxUnitsUsed");
                                        licenseValue.Average = getLongValueFromJToken(licenseUsageObject, "avgUnitsUsed");
                                        licenseValue.Total   = getLongValueFromJToken(licenseUsageObject, "totalUnitsUsed");
                                        licenseValue.Samples = getLongValueFromJToken(licenseUsageObject, "sampleCount");

                                        licenseValuesList.Add(licenseValue);
                                    }
                                }

                                // Do the counts and averages from per hour consumption to the total line
                                if (licenseValuesList.Count > 0)
                                {
                                    license.Average = (long)Math.Round((double)((double)licenseValuesList.Sum(mv => mv.Average) / (double)licenseValuesList.Count), 0);
                                    license.Min     = licenseValuesList.Min(l => l.Min);
                                    license.Max     = licenseValuesList.Max(l => l.Max);
                                }

                                licensesList.Add(license);
                                licenseValuesGlobalList.AddRange(licenseValuesList);
                            }
                        }

                        stepTimingTarget.NumEntities = stepTimingTarget.NumEntities + licensesList.Count;

                        licensesList = licensesList.OrderBy(l => l.AgentType).ToList();
                        FileIOHelper.WriteListToCSVFile(licensesList, new LicenseReportMap(), FilePathMap.LicensesIndexFilePath(jobTarget));

                        FileIOHelper.WriteListToCSVFile(licenseValuesGlobalList, new LicenseValueReportMap(), FilePathMap.LicenseUsageAccountIndexFilePath(jobTarget));

                        #endregion

                        #region License Rules

                        loggerConsole.Info("License Rules");

                        #region Preload the application and machine mapping

                        Dictionary <string, string> applicationsUsedByRules = null;
                        Dictionary <string, string> simMachinesUsedByRules  = null;

                        JArray licenseApplicationsReferenceArray = FileIOHelper.LoadJArrayFromFile(FilePathMap.LicenseApplicationsDataFilePath(jobTarget));
                        JArray licenseSIMMachinesReferenceArray  = FileIOHelper.LoadJArrayFromFile(FilePathMap.LicenseSIMMachinesDataFilePath(jobTarget));

                        if (licenseApplicationsReferenceArray == null)
                        {
                            applicationsUsedByRules = new Dictionary <string, string>(0);
                        }
                        else
                        {
                            applicationsUsedByRules = new Dictionary <string, string>(licenseApplicationsReferenceArray.Count);
                            foreach (JObject applicationObject in licenseApplicationsReferenceArray)
                            {
                                applicationsUsedByRules.Add(getStringValueFromJToken(applicationObject["objectReference"], "id"), getStringValueFromJToken(applicationObject, "name"));
                            }
                        }

                        if (licenseSIMMachinesReferenceArray == null)
                        {
                            simMachinesUsedByRules = new Dictionary <string, string>(0);
                        }
                        else
                        {
                            simMachinesUsedByRules = new Dictionary <string, string>(licenseSIMMachinesReferenceArray.Count);
                            foreach (JObject machineObject in licenseSIMMachinesReferenceArray)
                            {
                                simMachinesUsedByRules.Add(getStringValueFromJToken(machineObject["objectReference"], "id"), getStringValueFromJToken(machineObject, "name"));
                            }
                        }

                        List <ControllerApplication> controllerApplicationsList = FileIOHelper.ReadListFromCSVFile <ControllerApplication>(FilePathMap.ControllerApplicationsIndexFilePath(jobTarget), new ControllerApplicationReportMap());

                        #endregion

                        List <LicenseRule>      licenseRulesList      = new List <LicenseRule>(10);
                        List <LicenseRuleScope> licenseRuleScopesList = new List <LicenseRuleScope>(100);

                        JArray licenseRulesArray = FileIOHelper.LoadJArrayFromFile(FilePathMap.LicenseRulesDataFilePath(jobTarget));
                        if (licenseRulesArray != null)
                        {
                            foreach (JObject licenseRuleObject in licenseRulesArray)
                            {
                                string ruleID   = getStringValueFromJToken(licenseRuleObject, "id");
                                string ruleName = getStringValueFromJToken(licenseRuleObject, "name");

                                JObject licenseRuleDetailsObject = FileIOHelper.LoadJObjectFromFile(FilePathMap.LicenseRuleConfigurationDataFilePath(jobTarget, ruleName, ruleID));
                                if (licenseRuleDetailsObject != null)
                                {
                                    LicenseRule licenseRuleTemplate = new LicenseRule();

                                    licenseRuleTemplate.Controller  = jobTarget.Controller;
                                    licenseRuleTemplate.AccountID   = licenseAccountSummary.AccountID;
                                    licenseRuleTemplate.AccountName = licenseAccountSummary.AccountName;

                                    // Duration
                                    licenseRuleTemplate.Duration = (int)(jobConfiguration.Input.TimeRange.To - jobConfiguration.Input.TimeRange.From).Duration().TotalMinutes;
                                    licenseRuleTemplate.From     = jobConfiguration.Input.TimeRange.From.ToLocalTime();
                                    licenseRuleTemplate.To       = jobConfiguration.Input.TimeRange.To.ToLocalTime();
                                    licenseRuleTemplate.FromUtc  = jobConfiguration.Input.TimeRange.From;
                                    licenseRuleTemplate.ToUtc    = jobConfiguration.Input.TimeRange.To;

                                    licenseRuleTemplate.RuleName = getStringValueFromJToken(licenseRuleObject, "name");
                                    licenseRuleTemplate.RuleID   = getStringValueFromJToken(licenseRuleObject, "id");

                                    licenseRuleTemplate.AccessKey = getStringValueFromJToken(licenseRuleObject, "access_key");

                                    licenseRuleTemplate.RuleLicenses = getLongValueFromJToken(licenseRuleObject, "total_licenses");
                                    licenseRuleTemplate.RulePeak     = getLongValueFromJToken(licenseRuleObject, "peak_usage");

                                    if (isTokenPropertyNull(licenseRuleDetailsObject, "constraints") == false)
                                    {
                                        JArray licenseRuleConstraintsArray = (JArray)licenseRuleDetailsObject["constraints"];
                                        foreach (JObject licenseConstraintObject in licenseRuleConstraintsArray)
                                        {
                                            LicenseRuleScope licenseRuleScope = new LicenseRuleScope();

                                            licenseRuleScope.Controller  = licenseRuleTemplate.Controller;
                                            licenseRuleScope.AccountID   = licenseRuleTemplate.AccountID;
                                            licenseRuleScope.AccountName = licenseRuleTemplate.AccountName;

                                            licenseRuleScope.RuleName = licenseRuleTemplate.RuleName;
                                            licenseRuleScope.RuleID   = licenseRuleTemplate.RuleID;

                                            licenseRuleScope.ScopeSelector = getStringValueFromJToken(licenseConstraintObject, "constraint_type");

                                            string scopeType = getStringValueFromJToken(licenseConstraintObject, "entity_type_id");
                                            if (scopeType == "com.appdynamics.modules.apm.topology.impl.persistenceapi.model.ApplicationEntity")
                                            {
                                                licenseRuleScope.EntityType = "Application";
                                            }
                                            else if (scopeType == "com.appdynamics.modules.apm.topology.impl.persistenceapi.model.MachineEntity")
                                            {
                                                licenseRuleScope.EntityType = "Machine";
                                            }

                                            if (licenseRuleScope.ScopeSelector == "ALLOW_ALL")
                                            {
                                                licenseRuleScope.MatchType = "EQUALS";
                                                if (scopeType == "com.appdynamics.modules.apm.topology.impl.persistenceapi.model.ApplicationEntity")
                                                {
                                                    licenseRuleScope.EntityName = "[Any Application]";
                                                    licenseRuleTemplate.NumApplications++;
                                                }
                                                else if (scopeType == "com.appdynamics.modules.apm.topology.impl.persistenceapi.model.MachineEntity")
                                                {
                                                    licenseRuleScope.EntityName = "[Any Machine]";
                                                    licenseRuleTemplate.NumServers++;
                                                }
                                                licenseRuleScopesList.Add(licenseRuleScope);
                                            }
                                            else if (isTokenPropertyNull(licenseConstraintObject, "match_conditions") == false)
                                            {
                                                JArray licenseRuleMatcheConditionsArray = (JArray)licenseConstraintObject["match_conditions"];

                                                foreach (JObject licenseRuleMatchConditionObject in licenseRuleMatcheConditionsArray)
                                                {
                                                    LicenseRuleScope licenseRuleScopeMatchCondition = licenseRuleScope.Clone();

                                                    licenseRuleScopeMatchCondition.MatchType = getStringValueFromJToken(licenseRuleMatchConditionObject, "match_type");
                                                    if (licenseRuleScopeMatchCondition.MatchType == "EQUALS" &&
                                                        getStringValueFromJToken(licenseRuleMatchConditionObject, "attribute_type") == "ID")
                                                    {
                                                        string applicationID = getStringValueFromJToken(licenseRuleMatchConditionObject, "match_string");

                                                        if (scopeType == "com.appdynamics.modules.apm.topology.impl.persistenceapi.model.ApplicationEntity")
                                                        {
                                                            if (applicationsUsedByRules.ContainsKey(applicationID) == true &&
                                                                controllerApplicationsList != null)
                                                            {
                                                                ControllerApplication controllerApplication = controllerApplicationsList.Where(a => a.ApplicationName == applicationsUsedByRules[applicationID]).FirstOrDefault();
                                                                if (controllerApplication != null)
                                                                {
                                                                    licenseRuleScopeMatchCondition.EntityName = controllerApplication.ApplicationName;
                                                                    licenseRuleScopeMatchCondition.EntityType = controllerApplication.Type;
                                                                    licenseRuleScopeMatchCondition.EntityID   = controllerApplication.ApplicationID;
                                                                }
                                                            }
                                                            licenseRuleTemplate.NumApplications++;
                                                        }
                                                        else if (scopeType == "com.appdynamics.modules.apm.topology.impl.persistenceapi.model.MachineEntity")
                                                        {
                                                            if (simMachinesUsedByRules.ContainsKey(applicationID) == true)
                                                            {
                                                                licenseRuleScopeMatchCondition.EntityName = simMachinesUsedByRules[applicationID];
                                                            }
                                                            licenseRuleTemplate.NumServers++;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        licenseRuleScopeMatchCondition.EntityName = getStringValueFromJToken(licenseRuleMatchConditionObject, "match_string");
                                                        licenseRuleScopeMatchCondition.EntityID   = -1;
                                                        if (scopeType == "com.appdynamics.modules.apm.topology.impl.persistenceapi.model.ApplicationEntity")
                                                        {
                                                            licenseRuleTemplate.NumApplications++;
                                                        }
                                                        else if (scopeType == "com.appdynamics.modules.apm.topology.impl.persistenceapi.model.MachineEntity")
                                                        {
                                                            licenseRuleTemplate.NumServers++;
                                                        }
                                                    }

                                                    licenseRuleScopesList.Add(licenseRuleScopeMatchCondition);
                                                }
                                            }
                                        }
                                    }

                                    if (isTokenPropertyNull(licenseRuleDetailsObject, "entitlements") == false)
                                    {
                                        JArray licenseRuleEntitlementsArray = (JArray)licenseRuleDetailsObject["entitlements"];
                                        foreach (JObject licenseEntitlementObject in licenseRuleEntitlementsArray)
                                        {
                                            LicenseRule licenseRule = licenseRuleTemplate.Clone();

                                            licenseRule.Licenses = getLongValueFromJToken(licenseEntitlementObject, "number_of_licenses");

                                            licenseRule.AgentType = getStringValueFromJToken(licenseEntitlementObject, "license_module_type");

                                            licenseRulesList.Add(licenseRule);
                                        }
                                    }
                                }
                            }

                            licenseRulesList = licenseRulesList.OrderBy(l => l.RuleName).ThenBy(l => l.AgentType).ToList();
                            FileIOHelper.WriteListToCSVFile(licenseRulesList, new LicenseRuleReportMap(), FilePathMap.LicenseRulesIndexFilePath(jobTarget));

                            licenseRuleScopesList = licenseRuleScopesList.OrderBy(l => l.RuleName).ThenBy(l => l.ScopeSelector).ThenBy(l => l.EntityType).ThenBy(l => l.EntityName).ToList();
                            FileIOHelper.WriteListToCSVFile(licenseRuleScopesList, new LicenseRuleScopeReportMap(), FilePathMap.LicenseRuleScopesIndexFilePath(jobTarget));
                        }

                        #endregion

                        #region License Rule consumption details

                        loggerConsole.Info("Rules License Consumption");

                        // Typically there are 12 values per hour
                        // Assume there will be 16 different license types
                        List <LicenseValue> licenseValuesRulesList = new List <LicenseValue>(jobConfiguration.Input.HourlyTimeRanges.Count * 12 * 16 * licenseRulesList.Count);

                        foreach (LicenseRule licenseRule in licenseRulesList)
                        {
                            JObject licenseValuesForRuleContainerObject = FileIOHelper.LoadJObjectFromFile(FilePathMap.LicenseRuleUsageDataFilePath(jobTarget, licenseRule.RuleName, licenseRule.RuleID));

                            if (licenseValuesForRuleContainerObject != null)
                            {
                                if (isTokenPropertyNull(licenseValuesForRuleContainerObject, "apmStackGraphViewData") == false)
                                {
                                    // Parse the APM results first
                                    JArray licenseValuesForRuleAPMContainerArray = (JArray)licenseValuesForRuleContainerObject["apmStackGraphViewData"];
                                    if (licenseValuesForRuleAPMContainerArray != null)
                                    {
                                        foreach (JObject licenseValuesContainerObject in licenseValuesForRuleAPMContainerArray)
                                        {
                                            string licenseType = getStringValueFromJToken(licenseValuesContainerObject, "licenseModuleType");

                                            // If we have the license looked up, look through the values
                                            if (licenseType != String.Empty &&
                                                isTokenPropertyNull(licenseValuesContainerObject, "graphPoints") == false)
                                            {
                                                // Looks like [[ 1555484400000, 843 ], [ 1555488000000, 843 ]]
                                                JArray licenseValuesAPMArray = (JArray)licenseValuesContainerObject["graphPoints"];

                                                List <LicenseValue> licenseValuesList = parseLicenseValuesArray(licenseValuesAPMArray, licenseRule, licenseType);
                                                if (licenseValuesList != null)
                                                {
                                                    licenseValuesRulesList.AddRange(licenseValuesList);
                                                }
                                            }
                                        }
                                    }
                                }
                                if (isTokenPropertyNull(licenseValuesForRuleContainerObject, "nonApmModuleDetailViewData") == false)
                                {
                                    // Parse the non-APM results second
                                    JArray licenseValuesForRuleNonAPMContainerArray = (JArray)licenseValuesForRuleContainerObject["nonApmModuleDetailViewData"];
                                    if (licenseValuesForRuleNonAPMContainerArray != null)
                                    {
                                        foreach (JObject licenseValuesContainerObject in licenseValuesForRuleNonAPMContainerArray)
                                        {
                                            string licenseType = getStringValueFromJToken(licenseValuesContainerObject, "licenseModuleType");

                                            // If we have the license looked up, look through the values
                                            if (licenseType != String.Empty &&
                                                isTokenPropertyNull(licenseValuesContainerObject, "graphPoints") == false)
                                            {
                                                // Looks like [[ 1555484400000, 843 ], [ 1555488000000, 843 ]]
                                                JArray licenseValuesAPMArray = (JArray)licenseValuesContainerObject["graphPoints"];

                                                List <LicenseValue> licenseValuesList = parseLicenseValuesArray(licenseValuesAPMArray, licenseRule, licenseType);
                                                if (licenseValuesList != null)
                                                {
                                                    licenseValuesRulesList.AddRange(licenseValuesList);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        FileIOHelper.WriteListToCSVFile(licenseValuesRulesList, new LicenseRuleValueReportMap(), FilePathMap.LicenseUsageRulesIndexFilePath(jobTarget));

                        #endregion

                        #region Combine All for Report CSV

                        // If it is the first one, clear out the combined folder
                        if (reportFolderCleaned == false)
                        {
                            FileIOHelper.DeleteFolder(FilePathMap.ControllerLicensesReportFolderPath());
                            Thread.Sleep(1000);
                            FileIOHelper.CreateFolder(FilePathMap.ControllerLicensesReportFolderPath());
                            reportFolderCleaned = true;
                        }

                        // Append all the individual report files into one
                        if (File.Exists(FilePathMap.LicenseAccountIndexFilePath(jobTarget)) == true && new FileInfo(FilePathMap.LicenseAccountIndexFilePath(jobTarget)).Length > 0)
                        {
                            FileIOHelper.AppendTwoCSVFiles(FilePathMap.LicenseAccountReportFilePath(), FilePathMap.LicenseAccountIndexFilePath(jobTarget));
                        }
                        if (File.Exists(FilePathMap.LicensesIndexFilePath(jobTarget)) == true && new FileInfo(FilePathMap.LicensesIndexFilePath(jobTarget)).Length > 0)
                        {
                            FileIOHelper.AppendTwoCSVFiles(FilePathMap.LicensesReportFilePath(), FilePathMap.LicensesIndexFilePath(jobTarget));
                        }
                        if (File.Exists(FilePathMap.LicenseRulesIndexFilePath(jobTarget)) == true && new FileInfo(FilePathMap.LicenseRulesIndexFilePath(jobTarget)).Length > 0)
                        {
                            FileIOHelper.AppendTwoCSVFiles(FilePathMap.LicenseRulesReportFilePath(), FilePathMap.LicenseRulesIndexFilePath(jobTarget));
                        }
                        if (File.Exists(FilePathMap.LicenseUsageAccountIndexFilePath(jobTarget)) == true && new FileInfo(FilePathMap.LicenseUsageAccountIndexFilePath(jobTarget)).Length > 0)
                        {
                            FileIOHelper.AppendTwoCSVFiles(FilePathMap.LicenseUsageAccountReportFilePath(), FilePathMap.LicenseUsageAccountIndexFilePath(jobTarget));
                        }
                        if (File.Exists(FilePathMap.LicenseUsageRulesIndexFilePath(jobTarget)) == true && new FileInfo(FilePathMap.LicenseUsageRulesIndexFilePath(jobTarget)).Length > 0)
                        {
                            FileIOHelper.AppendTwoCSVFiles(FilePathMap.LicenseUsageRulesReportFilePath(), FilePathMap.LicenseUsageRulesIndexFilePath(jobTarget));
                        }
                        if (File.Exists(FilePathMap.LicenseRuleScopesIndexFilePath(jobTarget)) == true && new FileInfo(FilePathMap.LicenseRuleScopesIndexFilePath(jobTarget)).Length > 0)
                        {
                            FileIOHelper.AppendTwoCSVFiles(FilePathMap.LicenseRuleScopesReportFilePath(), FilePathMap.LicenseRuleScopesIndexFilePath(jobTarget));
                        }

                        #endregion
                    }
                    catch (Exception ex)
                    {
                        logger.Warn(ex);
                        loggerConsole.Warn(ex);

                        return(false);
                    }
                    finally
                    {
                        stopWatchTarget.Stop();

                        this.DisplayJobTargetEndedStatus(jobConfiguration, jobTarget, i + 1, stopWatchTarget);

                        stepTimingTarget.EndTime    = DateTime.Now;
                        stepTimingTarget.Duration   = stopWatchTarget.Elapsed;
                        stepTimingTarget.DurationMS = stopWatchTarget.ElapsedMilliseconds;

                        List <StepTiming> stepTimings = new List <StepTiming>(1);
                        stepTimings.Add(stepTimingTarget);
                        FileIOHelper.WriteListToCSVFile(stepTimings, new StepTimingReportMap(), FilePathMap.StepTimingReportFilePath(), true);
                    }

                    i++;
                }

                return(true);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                loggerConsole.Error(ex);

                return(false);
            }
            finally
            {
                stopWatch.Stop();

                this.DisplayJobStepEndedStatus(jobConfiguration, stopWatch);

                stepTimingFunction.EndTime    = DateTime.Now;
                stepTimingFunction.Duration   = stopWatch.Elapsed;
                stepTimingFunction.DurationMS = stopWatch.ElapsedMilliseconds;

                List <StepTiming> stepTimings = new List <StepTiming>(1);
                stepTimings.Add(stepTimingFunction);
                FileIOHelper.WriteListToCSVFile(stepTimings, new StepTimingReportMap(), FilePathMap.StepTimingReportFilePath(), true);
            }
        }
Beispiel #5
0
        public override bool Execute(ProgramOptions programOptions, JobConfiguration jobConfiguration)
        {
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            StepTiming stepTimingFunction = new StepTiming();

            stepTimingFunction.JobFileName = programOptions.OutputJobFilePath;
            stepTimingFunction.StepName    = jobConfiguration.Status.ToString();
            stepTimingFunction.StepID      = (int)jobConfiguration.Status;
            stepTimingFunction.StartTime   = DateTime.Now;
            stepTimingFunction.NumEntities = jobConfiguration.Target.Count;

            this.DisplayJobStepStartingStatus(jobConfiguration);

            FilePathMap = new FilePathMap(programOptions, jobConfiguration);

            try
            {
                if (this.ShouldExecute(jobConfiguration) == false)
                {
                    return(true);
                }

                bool reportFolderCleaned = false;

                // Process each Controller once
                int i           = 0;
                var controllers = jobConfiguration.Target.GroupBy(t => t.Controller);
                foreach (var controllerGroup in controllers)
                {
                    Stopwatch stopWatchTarget = new Stopwatch();
                    stopWatchTarget.Start();

                    JobTarget jobTarget = controllerGroup.ToList()[0];

                    StepTiming stepTimingTarget = new StepTiming();
                    stepTimingTarget.Controller      = jobTarget.Controller;
                    stepTimingTarget.ApplicationName = jobTarget.Application;
                    stepTimingTarget.ApplicationID   = jobTarget.ApplicationID;
                    stepTimingTarget.JobFileName     = programOptions.OutputJobFilePath;
                    stepTimingTarget.StepName        = jobConfiguration.Status.ToString();
                    stepTimingTarget.StepID          = (int)jobConfiguration.Status;
                    stepTimingTarget.StartTime       = DateTime.Now;

                    stepTimingTarget.NumEntities = 0;

                    try
                    {
                        this.DisplayJobTargetStartingStatus(jobConfiguration, jobTarget, i + 1);

                        #region Users

                        JArray usersArray = FileIOHelper.LoadJArrayFromFile(FilePathMap.UsersDataFilePath(jobTarget));

                        List <RBACUser> usersList = null;

                        if (usersArray != null && usersArray.Count > 0)
                        {
                            loggerConsole.Info("Index List of Users ({0} entities)", usersArray.Count);

                            usersList = new List <RBACUser>(usersArray.Count);

                            foreach (JToken userToken in usersArray)
                            {
                                RBACUser user = new RBACUser();
                                user.Controller = jobTarget.Controller;

                                user.UserName         = getStringValueFromJToken(userToken, "name");
                                user.DisplayName      = getStringValueFromJToken(userToken, "displayName");
                                user.Email            = getStringValueFromJToken(userToken, "email");
                                user.SecurityProvider = getStringValueFromJToken(userToken, "securityProviderType");

                                user.UserID = getLongValueFromJToken(userToken, "id");

                                user.CreatedBy    = getStringValueFromJToken(userToken, "createdBy");
                                user.CreatedOnUtc = UnixTimeHelper.ConvertFromUnixTimestamp(getLongValueFromJToken(userToken, "createdOn"));
                                try { user.CreatedOn = user.CreatedOnUtc.ToLocalTime(); } catch { }
                                user.UpdatedBy    = getStringValueFromJToken(userToken, "modifiedBy");
                                user.UpdatedOnUtc = UnixTimeHelper.ConvertFromUnixTimestamp(getLongValueFromJToken(userToken, "modifiedOn"));
                                try { user.UpdatedOn = user.UpdatedOnUtc.ToLocalTime(); } catch { }

                                usersList.Add(user);
                            }

                            // Sort them
                            usersList = usersList.OrderBy(o => o.SecurityProvider).ThenBy(o => o.UserName).ToList();
                            FileIOHelper.WriteListToCSVFile(usersList, new RBACUserReportMap(), FilePathMap.UsersIndexFilePath(jobTarget));

                            stepTimingTarget.NumEntities = stepTimingTarget.NumEntities + usersList.Count;
                        }

                        #endregion

                        #region Groups

                        JArray groupsArray = FileIOHelper.LoadJArrayFromFile(FilePathMap.GroupsDataFilePath(jobTarget));

                        List <RBACGroup> groupsList = null;

                        if (groupsArray != null && groupsArray.Count > 0)
                        {
                            loggerConsole.Info("Index List of Groups ({0} entities)", groupsArray.Count);

                            groupsList = new List <RBACGroup>(groupsArray.Count);

                            foreach (JToken groupToken in groupsArray)
                            {
                                RBACGroup group = new RBACGroup();
                                group.Controller = jobTarget.Controller;

                                group.GroupName        = getStringValueFromJToken(groupToken, "name");
                                group.Description      = getStringValueFromJToken(groupToken, "description");
                                group.SecurityProvider = getStringValueFromJToken(groupToken, "securityProviderType");

                                group.GroupID = getLongValueFromJToken(groupToken, "id");

                                group.CreatedBy    = getStringValueFromJToken(groupToken, "createdBy");
                                group.CreatedOnUtc = UnixTimeHelper.ConvertFromUnixTimestamp(getLongValueFromJToken(groupToken, "createdOn"));
                                try { group.CreatedOn = group.CreatedOnUtc.ToLocalTime(); } catch { }
                                group.UpdatedBy    = getStringValueFromJToken(groupToken, "modifiedBy");
                                group.UpdatedOnUtc = UnixTimeHelper.ConvertFromUnixTimestamp(getLongValueFromJToken(groupToken, "modifiedOn"));
                                try { group.UpdatedOn = group.UpdatedOnUtc.ToLocalTime(); } catch { }

                                groupsList.Add(group);
                            }

                            // Sort them
                            groupsList = groupsList.OrderBy(o => o.SecurityProvider).ThenBy(o => o.GroupName).ToList();
                            FileIOHelper.WriteListToCSVFile(groupsList, new RBACGroupReportMap(), FilePathMap.GroupsIndexFilePath(jobTarget));

                            stepTimingTarget.NumEntities = stepTimingTarget.NumEntities + groupsList.Count;
                        }

                        #endregion

                        #region Roles

                        JArray rolesArray = FileIOHelper.LoadJArrayFromFile(FilePathMap.RolesDataFilePath(jobTarget));

                        List <RBACRole>       rolesList       = null;
                        List <RBACPermission> permissionsList = null;

                        List <ControllerApplication> applicationsList = FileIOHelper.ReadListFromCSVFile <ControllerApplication>(FilePathMap.ControllerApplicationsIndexFilePath(jobTarget), new ControllerApplicationReportMap());

                        if (rolesArray != null && rolesArray.Count > 0)
                        {
                            loggerConsole.Info("Index List of Roles ({0} entities)", rolesArray.Count);

                            rolesList       = new List <RBACRole>(rolesArray.Count);
                            permissionsList = new List <RBACPermission>(rolesArray.Count * 32);

                            foreach (JToken roleToken in rolesArray)
                            {
                                RBACRole role = new RBACRole();
                                role.Controller = jobTarget.Controller;

                                role.RoleName    = getStringValueFromJToken(roleToken, "name");
                                role.Description = getStringValueFromJToken(roleToken, "description");
                                role.ReadOnly    = getBoolValueFromJToken(roleToken, "readonly");

                                role.RoleID = getLongValueFromJToken(roleToken, "id");

                                role.CreatedBy    = getStringValueFromJToken(roleToken, "createdBy");
                                role.CreatedOnUtc = UnixTimeHelper.ConvertFromUnixTimestamp(getLongValueFromJToken(roleToken, "createdOn"));
                                try { role.CreatedOn = role.CreatedOnUtc.ToLocalTime(); } catch { }
                                role.UpdatedBy    = getStringValueFromJToken(roleToken, "modifiedBy");
                                role.UpdatedOnUtc = UnixTimeHelper.ConvertFromUnixTimestamp(getLongValueFromJToken(roleToken, "modifiedOn"));
                                try { role.UpdatedOn = role.UpdatedOnUtc.ToLocalTime(); } catch { }

                                // Permissions from role detail
                                JObject roleDetailObject = FileIOHelper.LoadJObjectFromFile(FilePathMap.RoleDataFilePath(jobTarget, role.RoleName, role.RoleID));
                                if (roleDetailObject != null)
                                {
                                    foreach (JToken permissionToken in roleDetailObject["permissions"])
                                    {
                                        RBACPermission permission = new RBACPermission();
                                        permission.Controller = role.Controller;

                                        permission.RoleName = role.RoleName;
                                        permission.RoleID   = role.RoleID;

                                        permission.PermissionName = getStringValueFromJToken(permissionToken, "action");
                                        permission.Allowed        = getBoolValueFromJToken(permissionToken, "allowed");

                                        permission.PermissionID = getLongValueFromJToken(permissionToken, "id");

                                        if (isTokenPropertyNull(permissionToken, "affectedEntity") == false)
                                        {
                                            permission.EntityType = getStringValueFromJToken(permissionToken["affectedEntity"], "entityType");
                                            permission.EntityID   = getLongValueFromJToken(permissionToken["affectedEntity"], "entityId");
                                        }

                                        // Lookup the application
                                        if (permission.EntityType == "APPLICATION" && permission.EntityID != 0)
                                        {
                                            if (applicationsList != null)
                                            {
                                                ControllerApplication application = applicationsList.Where(e => e.ApplicationID == permission.EntityID).FirstOrDefault();
                                                if (application != null)
                                                {
                                                    permission.EntityName = application.ApplicationName;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            permission.EntityType = "";
                                        }

                                        role.NumPermissions++;

                                        permissionsList.Add(permission);
                                    }
                                }

                                rolesList.Add(role);
                            }

                            // Sort them
                            rolesList = rolesList.OrderBy(o => o.RoleName).ToList();
                            FileIOHelper.WriteListToCSVFile(rolesList, new RBACRoleReportMap(), FilePathMap.RolesIndexFilePath(jobTarget));

                            permissionsList = permissionsList.OrderBy(o => o.RoleName).ThenBy(o => o.EntityName).ThenBy(o => o.PermissionName).ToList();
                            FileIOHelper.WriteListToCSVFile(permissionsList, new RBACPermissionReportMap(), FilePathMap.PermissionsIndexFilePath(jobTarget));

                            stepTimingTarget.NumEntities = stepTimingTarget.NumEntities + rolesList.Count;
                        }

                        #endregion

                        #region Groups and Users in Roles

                        List <RBACRoleMembership> roleMembershipsList = new List <RBACRoleMembership>();

                        // Users in Roles
                        if (usersList != null && rolesList != null)
                        {
                            loggerConsole.Info("Index Users in Roles ({0} entities)", usersList.Count);

                            foreach (RBACUser user in usersList)
                            {
                                JObject userDetailObject = FileIOHelper.LoadJObjectFromFile(FilePathMap.UserDataFilePath(jobTarget, user.UserName, user.UserID));
                                if (userDetailObject != null)
                                {
                                    foreach (JToken roleIDToken in userDetailObject["accountRoleIds"])
                                    {
                                        long roleID = (long)roleIDToken;

                                        RBACRole role = rolesList.Where(r => r.RoleID == roleID).FirstOrDefault();
                                        if (role != null)
                                        {
                                            RBACRoleMembership roleMembership = new RBACRoleMembership();
                                            roleMembership.Controller = user.Controller;

                                            roleMembership.RoleName = role.RoleName;
                                            roleMembership.RoleID   = role.RoleID;

                                            roleMembership.EntityName = user.UserName;
                                            roleMembership.EntityID   = user.UserID;
                                            roleMembership.EntityType = "User";

                                            roleMembershipsList.Add(roleMembership);
                                        }
                                    }
                                }
                            }
                        }

                        // Groups in Roles
                        if (groupsList != null && rolesList != null)
                        {
                            loggerConsole.Info("Index Groups in Roles ({0} entities)", groupsList.Count);

                            foreach (RBACGroup group in groupsList)
                            {
                                JObject groupDetailJSON = FileIOHelper.LoadJObjectFromFile(FilePathMap.GroupDataFilePath(jobTarget, group.GroupName, group.GroupID));
                                if (groupDetailJSON != null)
                                {
                                    foreach (JToken roleIDToken in groupDetailJSON["accountRoleIds"])
                                    {
                                        long roleID = (long)roleIDToken;

                                        RBACRole role = rolesList.Where(r => r.RoleID == roleID).FirstOrDefault();
                                        if (role != null)
                                        {
                                            RBACRoleMembership roleMembership = new RBACRoleMembership();
                                            roleMembership.Controller = group.Controller;

                                            roleMembership.RoleName = role.RoleName;
                                            roleMembership.RoleID   = role.RoleID;

                                            roleMembership.EntityName = group.GroupName;
                                            roleMembership.EntityID   = group.GroupID;
                                            roleMembership.EntityType = "Group";

                                            roleMembershipsList.Add(roleMembership);
                                        }
                                    }
                                }
                            }
                        }

                        roleMembershipsList = roleMembershipsList.OrderBy(o => o.RoleName).ThenBy(o => o.EntityType).ThenBy(o => o.EntityName).ToList();
                        FileIOHelper.WriteListToCSVFile(roleMembershipsList, new RBACRoleMembershipReportMap(), FilePathMap.RoleMembershipsIndexFilePath(jobTarget));

                        #endregion

                        #region Users in Groups

                        List <RBACGroupMembership> groupMembershipsList = new List <RBACGroupMembership>();

                        if (groupsList != null && usersList != null)
                        {
                            loggerConsole.Info("Index Users in Groups ({0} entities)", groupsList.Count);

                            foreach (RBACGroup group in groupsList)
                            {
                                JArray usersInGroupArray = FileIOHelper.LoadJArrayFromFile(FilePathMap.GroupUsersDataFilePath(jobTarget, group.GroupName, group.GroupID));
                                if (usersInGroupArray != null)
                                {
                                    foreach (JToken userIDToken in usersInGroupArray)
                                    {
                                        long userID = (long)userIDToken;

                                        RBACUser user = usersList.Where(r => r.UserID == userID).FirstOrDefault();
                                        if (user != null)
                                        {
                                            RBACGroupMembership groupMembership = new RBACGroupMembership();
                                            groupMembership.Controller = group.Controller;

                                            groupMembership.GroupName = group.GroupName;
                                            groupMembership.GroupID   = group.GroupID;

                                            groupMembership.UserName = user.UserName;
                                            groupMembership.UserID   = user.UserID;

                                            groupMembershipsList.Add(groupMembership);
                                        }
                                    }
                                }
                            }
                        }

                        groupMembershipsList = groupMembershipsList.OrderBy(o => o.GroupName).ThenBy(o => o.UserName).ToList();
                        FileIOHelper.WriteListToCSVFile(groupMembershipsList, new RBACGroupMembershipReportMap(), FilePathMap.GroupMembershipsIndexFilePath(jobTarget));

                        #endregion

                        #region User Permissions

                        List <RBACUserPermission> userPermissionsList = new List <RBACUserPermission>();

                        if (roleMembershipsList != null && permissionsList != null)
                        {
                            loggerConsole.Info("Index Users Permissions ({0} entities)", roleMembershipsList.Count);

                            // Scroll through the list of Role memberships
                            foreach (RBACRoleMembership roleMembership in roleMembershipsList)
                            {
                                if (roleMembership.EntityType == "User")
                                {
                                    if (usersList != null)
                                    {
                                        // For User, enumerate permissions associated with this role
                                        RBACUser user = usersList.Where(u => u.UserID == roleMembership.EntityID).FirstOrDefault();
                                        if (user != null)
                                        {
                                            List <RBACPermission> permissionsForRoleList = permissionsList.Where(p => p.RoleID == roleMembership.RoleID).ToList();
                                            if (permissionsForRoleList != null)
                                            {
                                                foreach (RBACPermission permission in permissionsForRoleList)
                                                {
                                                    RBACUserPermission userPermission = new RBACUserPermission();
                                                    userPermission.Controller = user.Controller;

                                                    userPermission.UserName             = user.UserName;
                                                    userPermission.UserSecurityProvider = user.SecurityProvider;
                                                    userPermission.UserID = user.UserID;

                                                    userPermission.RoleName = permission.RoleName;
                                                    userPermission.RoleID   = permission.RoleID;

                                                    userPermission.PermissionName = permission.PermissionName;
                                                    userPermission.PermissionID   = permission.PermissionID;

                                                    userPermission.Allowed = permission.Allowed;

                                                    userPermission.EntityName = permission.EntityName;
                                                    userPermission.EntityType = permission.EntityType;
                                                    userPermission.EntityID   = permission.EntityID;

                                                    userPermissionsList.Add(userPermission);
                                                }
                                            }
                                        }
                                    }
                                }
                                else if (roleMembership.EntityType == "Group")
                                {
                                    RBACGroup groupDetail = null;
                                    if (groupsList != null)
                                    {
                                        groupDetail = groupsList.Where(g => g.GroupID == roleMembership.EntityID).FirstOrDefault();
                                    }

                                    if (groupMembershipsList != null)
                                    {
                                        // For Group, find all users in the group and repeat the permission output
                                        List <RBACGroupMembership> usersInGroups = groupMembershipsList.Where(g => g.GroupID == roleMembership.EntityID).ToList();
                                        if (usersInGroups != null)
                                        {
                                            foreach (RBACGroupMembership user in usersInGroups)
                                            {
                                                RBACUser userDetail = null;
                                                if (usersList != null)
                                                {
                                                    userDetail = usersList.Where(u => u.UserID == user.UserID).FirstOrDefault();
                                                }

                                                List <RBACPermission> permissionsForRoleList = permissionsList.Where(p => p.RoleID == roleMembership.RoleID).ToList();
                                                if (permissionsForRoleList != null)
                                                {
                                                    foreach (RBACPermission permission in permissionsForRoleList)
                                                    {
                                                        RBACUserPermission userPermission = new RBACUserPermission();
                                                        userPermission.Controller = user.Controller;

                                                        userPermission.UserName = user.UserName;
                                                        userPermission.UserID   = user.UserID;
                                                        if (userDetail != null)
                                                        {
                                                            userPermission.UserSecurityProvider = userDetail.SecurityProvider;
                                                        }

                                                        if (groupDetail != null)
                                                        {
                                                            userPermission.GroupName             = groupDetail.GroupName;
                                                            userPermission.GroupSecurityProvider = groupDetail.SecurityProvider;
                                                            userPermission.GroupID = groupDetail.GroupID;
                                                        }

                                                        userPermission.RoleName = permission.RoleName;
                                                        userPermission.RoleID   = permission.RoleID;

                                                        userPermission.PermissionName = permission.PermissionName;
                                                        userPermission.PermissionID   = permission.PermissionID;

                                                        userPermission.Allowed = permission.Allowed;

                                                        userPermission.EntityName = permission.EntityName;
                                                        userPermission.EntityType = permission.EntityType;
                                                        userPermission.EntityID   = permission.EntityID;

                                                        userPermissionsList.Add(userPermission);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        userPermissionsList = userPermissionsList.OrderBy(o => o.UserName).ThenBy(o => o.GroupName).ThenBy(o => o.PermissionName).ToList();
                        FileIOHelper.WriteListToCSVFile(userPermissionsList, new RBACUserPermissionReportMap(), FilePathMap.UserPermissionsIndexFilePath(jobTarget));

                        #endregion

                        #region Controller Summary

                        loggerConsole.Info("Index Controller Summary");

                        RBACControllerSummary controller = new RBACControllerSummary();
                        controller.Controller = jobTarget.Controller;

                        string securityProviderType = FileIOHelper.ReadFileFromPath(FilePathMap.SecurityProviderTypeDataFilePath(jobTarget));
                        if (securityProviderType != String.Empty)
                        {
                            controller.SecurityProvider = securityProviderType.Replace("\"", "");
                        }

                        string requireStrongPasswords = FileIOHelper.ReadFileFromPath(FilePathMap.StrongPasswordsDataFilePath(jobTarget));
                        if (requireStrongPasswords != String.Empty)
                        {
                            bool parsedBool = false;
                            Boolean.TryParse(requireStrongPasswords, out parsedBool);
                            controller.IsStrongPasswords = parsedBool;
                        }

                        if (usersList != null)
                        {
                            controller.NumUsers = usersList.Count;
                        }
                        if (groupsList != null)
                        {
                            controller.NumGroups = groupsList.Count;
                        }
                        if (rolesList != null)
                        {
                            controller.NumRoles = rolesList.Count;
                        }

                        List <RBACControllerSummary> controllerList = new List <RBACControllerSummary>(1);
                        controllerList.Add(controller);

                        FileIOHelper.WriteListToCSVFile(controllerList, new RBACControllerSummaryReportMap(), FilePathMap.RBACControllerSummaryIndexFilePath(jobTarget));

                        #endregion

                        #region Combine All for Report CSV

                        // If it is the first one, clear out the combined folder
                        if (reportFolderCleaned == false)
                        {
                            FileIOHelper.DeleteFolder(FilePathMap.UsersGroupsRolesPermissionsReportFolderPath());
                            Thread.Sleep(1000);
                            FileIOHelper.CreateFolder(FilePathMap.UsersGroupsRolesPermissionsReportFolderPath());
                            reportFolderCleaned = true;
                        }

                        // Append all the individual report files into one
                        if (File.Exists(FilePathMap.UsersIndexFilePath(jobTarget)) == true && new FileInfo(FilePathMap.UsersIndexFilePath(jobTarget)).Length > 0)
                        {
                            FileIOHelper.AppendTwoCSVFiles(FilePathMap.UsersReportFilePath(), FilePathMap.UsersIndexFilePath(jobTarget));
                        }
                        if (File.Exists(FilePathMap.GroupsIndexFilePath(jobTarget)) == true && new FileInfo(FilePathMap.GroupsIndexFilePath(jobTarget)).Length > 0)
                        {
                            FileIOHelper.AppendTwoCSVFiles(FilePathMap.GroupsReportFilePath(), FilePathMap.GroupsIndexFilePath(jobTarget));
                        }
                        if (File.Exists(FilePathMap.RolesIndexFilePath(jobTarget)) == true && new FileInfo(FilePathMap.RolesIndexFilePath(jobTarget)).Length > 0)
                        {
                            FileIOHelper.AppendTwoCSVFiles(FilePathMap.RolesReportFilePath(), FilePathMap.RolesIndexFilePath(jobTarget));
                        }
                        if (File.Exists(FilePathMap.PermissionsIndexFilePath(jobTarget)) == true && new FileInfo(FilePathMap.PermissionsIndexFilePath(jobTarget)).Length > 0)
                        {
                            FileIOHelper.AppendTwoCSVFiles(FilePathMap.PermissionsReportFilePath(), FilePathMap.PermissionsIndexFilePath(jobTarget));
                        }
                        if (File.Exists(FilePathMap.GroupMembershipsIndexFilePath(jobTarget)) == true && new FileInfo(FilePathMap.GroupMembershipsIndexFilePath(jobTarget)).Length > 0)
                        {
                            FileIOHelper.AppendTwoCSVFiles(FilePathMap.GroupMembershipsReportFilePath(), FilePathMap.GroupMembershipsIndexFilePath(jobTarget));
                        }
                        if (File.Exists(FilePathMap.RoleMembershipsIndexFilePath(jobTarget)) == true && new FileInfo(FilePathMap.RoleMembershipsIndexFilePath(jobTarget)).Length > 0)
                        {
                            FileIOHelper.AppendTwoCSVFiles(FilePathMap.RoleMembershipsReportFilePath(), FilePathMap.RoleMembershipsIndexFilePath(jobTarget));
                        }
                        if (File.Exists(FilePathMap.UserPermissionsIndexFilePath(jobTarget)) == true && new FileInfo(FilePathMap.UserPermissionsIndexFilePath(jobTarget)).Length > 0)
                        {
                            FileIOHelper.AppendTwoCSVFiles(FilePathMap.UserPermissionsReportFilePath(), FilePathMap.UserPermissionsIndexFilePath(jobTarget));
                        }
                        if (File.Exists(FilePathMap.RBACControllerSummaryIndexFilePath(jobTarget)) == true && new FileInfo(FilePathMap.RBACControllerSummaryIndexFilePath(jobTarget)).Length > 0)
                        {
                            FileIOHelper.AppendTwoCSVFiles(FilePathMap.RBACControllerSummaryReportFilePath(), FilePathMap.RBACControllerSummaryIndexFilePath(jobTarget));
                        }

                        #endregion
                    }
                    catch (Exception ex)
                    {
                        logger.Warn(ex);
                        loggerConsole.Warn(ex);

                        return(false);
                    }
                    finally
                    {
                        stopWatchTarget.Stop();

                        this.DisplayJobTargetEndedStatus(jobConfiguration, jobTarget, i + 1, stopWatchTarget);

                        stepTimingTarget.EndTime    = DateTime.Now;
                        stepTimingTarget.Duration   = stopWatchTarget.Elapsed;
                        stepTimingTarget.DurationMS = stopWatchTarget.ElapsedMilliseconds;

                        List <StepTiming> stepTimings = new List <StepTiming>(1);
                        stepTimings.Add(stepTimingTarget);
                        FileIOHelper.WriteListToCSVFile(stepTimings, new StepTimingReportMap(), FilePathMap.StepTimingReportFilePath(), true);
                    }

                    i++;
                }

                return(true);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                loggerConsole.Error(ex);

                return(false);
            }
            finally
            {
                stopWatch.Stop();

                this.DisplayJobStepEndedStatus(jobConfiguration, stopWatch);

                stepTimingFunction.EndTime    = DateTime.Now;
                stepTimingFunction.Duration   = stopWatch.Elapsed;
                stepTimingFunction.DurationMS = stopWatch.ElapsedMilliseconds;

                List <StepTiming> stepTimings = new List <StepTiming>(1);
                stepTimings.Add(stepTimingFunction);
                FileIOHelper.WriteListToCSVFile(stepTimings, new StepTimingReportMap(), FilePathMap.StepTimingReportFilePath(), true);
            }
        }
        public override bool Execute(ProgramOptions programOptions, JobConfiguration jobConfiguration)
        {
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            StepTiming stepTimingFunction = new StepTiming();

            stepTimingFunction.JobFileName = programOptions.OutputJobFilePath;
            stepTimingFunction.StepName    = jobConfiguration.Status.ToString();
            stepTimingFunction.StepID      = (int)jobConfiguration.Status;
            stepTimingFunction.StartTime   = DateTime.Now;
            stepTimingFunction.NumEntities = jobConfiguration.Target.Count;

            this.DisplayJobStepStartingStatus(jobConfiguration);

            FilePathMap = new FilePathMap(programOptions, jobConfiguration);

            try
            {
                if (this.ShouldExecute(jobConfiguration) == false)
                {
                    return(true);
                }

                if (jobConfiguration.Target.Count(t => t.Type == APPLICATION_TYPE_WEB) == 0)
                {
                    return(true);
                }

                bool reportFolderCleaned = false;

                // Process each target
                for (int i = 0; i < jobConfiguration.Target.Count; i++)
                {
                    Stopwatch stopWatchTarget = new Stopwatch();
                    stopWatchTarget.Start();

                    JobTarget jobTarget = jobConfiguration.Target[i];

                    if (jobTarget.Type != null && jobTarget.Type.Length > 0 && jobTarget.Type != APPLICATION_TYPE_WEB)
                    {
                        continue;
                    }

                    StepTiming stepTimingTarget = new StepTiming();
                    stepTimingTarget.Controller      = jobTarget.Controller;
                    stepTimingTarget.ApplicationName = jobTarget.Application;
                    stepTimingTarget.ApplicationID   = jobTarget.ApplicationID;
                    stepTimingTarget.JobFileName     = programOptions.OutputJobFilePath;
                    stepTimingTarget.StepName        = jobConfiguration.Status.ToString();
                    stepTimingTarget.StepID          = (int)jobConfiguration.Status;
                    stepTimingTarget.StartTime       = DateTime.Now;

                    try
                    {
                        this.DisplayJobTargetStartingStatus(jobConfiguration, jobTarget, i + 1);

                        #region Preload list of detected entities

                        // For later cross-reference
                        List <ControllerApplication> controllerApplicationList = FileIOHelper.ReadListFromCSVFile <ControllerApplication>(FilePathMap.ControllerApplicationsIndexFilePath(jobTarget), new ControllerApplicationReportMap());

                        #endregion

                        #region Application Summary

                        WEBApplicationConfiguration applicationConfiguration = new WEBApplicationConfiguration();

                        loggerConsole.Info("Application Summary");

                        applicationConfiguration.Controller      = jobTarget.Controller;
                        applicationConfiguration.ControllerLink  = String.Format(DEEPLINK_CONTROLLER, applicationConfiguration.Controller, DEEPLINK_TIMERANGE_LAST_15_MINUTES);
                        applicationConfiguration.ApplicationName = jobTarget.Application;
                        applicationConfiguration.ApplicationID   = jobTarget.ApplicationID;
                        applicationConfiguration.ApplicationLink = String.Format(DEEPLINK_APM_APPLICATION, applicationConfiguration.Controller, applicationConfiguration.ApplicationID, DEEPLINK_TIMERANGE_LAST_15_MINUTES);
                        if (controllerApplicationList != null)
                        {
                            ControllerApplication controllerApplication = controllerApplicationList.Where(a => a.Type == APPLICATION_TYPE_WEB && a.ApplicationID == applicationConfiguration.ApplicationID).FirstOrDefault();
                            if (controllerApplication != null)
                            {
                                applicationConfiguration.ApplicationDescription = controllerApplication.Description;
                            }
                        }

                        // Application Key
                        JObject appKeyObject = FileIOHelper.LoadJObjectFromFile(FilePathMap.WEBApplicationKeyDataFilePath(jobTarget));
                        if (appKeyObject != null)
                        {
                            applicationConfiguration.ApplicationKey = getStringValueFromJToken(appKeyObject, "appKey");
                        }

                        // Instrumentation Options
                        JObject instrumentationObject = FileIOHelper.LoadJObjectFromFile(FilePathMap.WEBAgentConfigDataFilePath(jobTarget));
                        if (instrumentationObject != null)
                        {
                            applicationConfiguration.IsXsccEnabled = getBoolValueFromJToken(instrumentationObject, "enableXssc");
                            applicationConfiguration.HostOption    = getIntValueFromJToken(instrumentationObject, "hostOption");

                            applicationConfiguration.AgentHTTP   = getStringValueFromJToken(instrumentationObject, "jsAgentUrlHttp");
                            applicationConfiguration.AgentHTTPS  = getStringValueFromJToken(instrumentationObject, "jsAgentUrlHttps");
                            applicationConfiguration.GeoHTTP     = getStringValueFromJToken(instrumentationObject, "geoUrlHttp");
                            applicationConfiguration.GeoHTTPS    = getStringValueFromJToken(instrumentationObject, "geoUrlHttps");
                            applicationConfiguration.BeaconHTTP  = getStringValueFromJToken(instrumentationObject, "beaconUrlHttp");
                            applicationConfiguration.BeaconHTTPS = getStringValueFromJToken(instrumentationObject, "beaconUrlHttps");

                            applicationConfiguration.AgentCode = getStringValueFromJToken(instrumentationObject, "codeSnippet");
                        }

                        // Monitoring State
                        string monitoringState = FileIOHelper.ReadFileFromPath(FilePathMap.WEBApplicationMonitoringStateDataFilePath(jobTarget));
                        if (monitoringState != String.Empty)
                        {
                            bool parsedBool = false;
                            Boolean.TryParse(monitoringState, out parsedBool);
                            applicationConfiguration.IsEnabled = parsedBool;
                        }

                        // Error Detection
                        JObject errorDetectionRulesObject = FileIOHelper.LoadJObjectFromFile(FilePathMap.WEBAgentErrorRulesDataFilePath(jobTarget));
                        if (errorDetectionRulesObject != null)
                        {
                            applicationConfiguration.IsJSErrorEnabled   = getBoolValueFromJToken(errorDetectionRulesObject, "javaScriptErrorCaptureEnabled");
                            applicationConfiguration.IsAJAXErrorEnabled = getBoolValueFromJToken(errorDetectionRulesObject, "ajaxRequestErrorCaptureEnabled");
                            applicationConfiguration.IgnoreJSErrors     = getStringValueOfObjectFromJToken(errorDetectionRulesObject, "ignoreJavaScriptErrorConfigRules", true);
                            applicationConfiguration.IgnorePageNames    = getStringValueOfObjectFromJToken(errorDetectionRulesObject, "ignorePageNames", true);
                            applicationConfiguration.IgnoreURLs         = getStringValueOfObjectFromJToken(errorDetectionRulesObject, "ignoreUrls", true);
                        }

                        // Page Settings
                        JObject pageSettingsObject = FileIOHelper.LoadJObjectFromFile(FilePathMap.WEBAgentPageSettingsRulesDataFilePath(jobTarget));
                        if (pageSettingsObject != null)
                        {
                            if (isTokenPropertyNull(pageSettingsObject, "thresholds") == false)
                            {
                                applicationConfiguration.SlowThresholdType = getStringValueFromJToken(pageSettingsObject["thresholds"]["slowThreshold"], "type");
                                applicationConfiguration.SlowThreshold     = getIntValueFromJToken(pageSettingsObject["thresholds"]["slowThreshold"], "value");

                                applicationConfiguration.VerySlowThresholdType = getStringValueFromJToken(pageSettingsObject["thresholds"]["verySlowThreshold"], "type");
                                applicationConfiguration.VerySlowThreshold     = getIntValueFromJToken(pageSettingsObject["thresholds"]["verySlowThreshold"], "value");

                                applicationConfiguration.StallThresholdType = getStringValueFromJToken(pageSettingsObject["thresholds"]["stallThreshold"], "type");
                                applicationConfiguration.StallThreshold     = getIntValueFromJToken(pageSettingsObject["thresholds"]["stallThreshold"], "value");
                            }
                            applicationConfiguration.Percentiles    = getStringValueOfObjectFromJToken(pageSettingsObject, "percentileMetrics", true);
                            applicationConfiguration.SessionTimeout = getIntValueFromJToken(pageSettingsObject["sessionsMonitor"], "sessionTimeoutMins");
                            applicationConfiguration.IsIPDisplayed  = getBoolValueFromJToken(pageSettingsObject, "ipAddressDisplayed");

                            applicationConfiguration.EnableSlowSnapshots     = getBoolValueFromJToken(pageSettingsObject["eventPolicy"], "enableSlowSnapshotCollection");
                            applicationConfiguration.EnablePeriodicSnapshots = getBoolValueFromJToken(pageSettingsObject["eventPolicy"], "enablePeriodicSnapshotCollection");
                            applicationConfiguration.EnableErrorSnapshots    = getBoolValueFromJToken(pageSettingsObject["eventPolicy"], "enableErrorSnapshotCollection");
                        }

                        #endregion

                        #region Rules of all kinds

                        loggerConsole.Info("Rules");

                        #region Page Rules

                        List <WEBPageDetectionRule> pageDetectionRulesList = new List <WEBPageDetectionRule>(1024);

                        JObject pageRulesObject = FileIOHelper.LoadJObjectFromFile(FilePathMap.WEBAgentPageRulesDataFilePath(jobTarget));
                        if (pageRulesObject != null)
                        {
                            if (isTokenPropertyNull(pageRulesObject, "customNamingIncludeRules") == false)
                            {
                                JArray includeRulesArray = (JArray)pageRulesObject["customNamingIncludeRules"];
                                foreach (JObject includeRuleObject in includeRulesArray)
                                {
                                    WEBPageDetectionRule webPageDetectionRule = fillWebPageDetectionRule(includeRuleObject, jobTarget);
                                    if (webPageDetectionRule != null)
                                    {
                                        webPageDetectionRule.DetectionType  = "INCLUDE";
                                        webPageDetectionRule.EntityCategory = "Pages&IFrames";

                                        pageDetectionRulesList.Add(webPageDetectionRule);
                                    }
                                }
                            }

                            if (isTokenPropertyNull(pageRulesObject, "customNamingExcludeRules") == false)
                            {
                                JArray excludeRulesArray = (JArray)pageRulesObject["customNamingExcludeRules"];
                                foreach (JObject excludeRuleObject in excludeRulesArray)
                                {
                                    WEBPageDetectionRule webPageDetectionRule = fillWebPageDetectionRule(excludeRuleObject, jobTarget);
                                    if (webPageDetectionRule != null)
                                    {
                                        webPageDetectionRule.DetectionType  = "EXCLUDE";
                                        webPageDetectionRule.EntityCategory = "Pages&IFrames";

                                        pageDetectionRulesList.Add(webPageDetectionRule);
                                    }
                                }
                            }
                        }

                        #endregion

                        #region AJAX Rules

                        JObject ajaxRulesObject = FileIOHelper.LoadJObjectFromFile(FilePathMap.WEBAgentAjaxRulesDataFilePath(jobTarget));
                        if (ajaxRulesObject != null)
                        {
                            if (isTokenPropertyNull(ajaxRulesObject, "customNamingIncludeRules") == false)
                            {
                                JArray includeRulesArray = (JArray)ajaxRulesObject["customNamingIncludeRules"];
                                foreach (JObject includeRuleObject in includeRulesArray)
                                {
                                    WEBPageDetectionRule webPageDetectionRule = fillWebPageDetectionRule(includeRuleObject, jobTarget);
                                    if (webPageDetectionRule != null)
                                    {
                                        webPageDetectionRule.DetectionType  = "INCLUDE";
                                        webPageDetectionRule.EntityCategory = "Ajax";

                                        pageDetectionRulesList.Add(webPageDetectionRule);
                                    }
                                }
                            }

                            if (isTokenPropertyNull(ajaxRulesObject, "customNamingExcludeRules") == false)
                            {
                                JArray excludeRulesArray = (JArray)ajaxRulesObject["customNamingExcludeRules"];
                                foreach (JObject excludeRuleObject in excludeRulesArray)
                                {
                                    WEBPageDetectionRule webPageDetectionRule = fillWebPageDetectionRule(excludeRuleObject, jobTarget);
                                    if (webPageDetectionRule != null)
                                    {
                                        webPageDetectionRule.DetectionType  = "EXCLUDE";
                                        webPageDetectionRule.EntityCategory = "Ajax";

                                        pageDetectionRulesList.Add(webPageDetectionRule);
                                    }
                                }
                            }

                            if (isTokenPropertyNull(ajaxRulesObject, "eventServiceIncludeRules") == false)
                            {
                                JArray includeRulesArray = (JArray)ajaxRulesObject["eventServiceIncludeRules"];
                                foreach (JObject includeRuleObject in includeRulesArray)
                                {
                                    WEBPageDetectionRule webPageDetectionRule = fillWebPageDetectionRule(includeRuleObject, jobTarget);
                                    if (webPageDetectionRule != null)
                                    {
                                        webPageDetectionRule.DetectionType  = "INCLUDE";
                                        webPageDetectionRule.EntityCategory = "AjaxEventsSvc";

                                        pageDetectionRulesList.Add(webPageDetectionRule);
                                    }
                                }
                            }

                            if (isTokenPropertyNull(ajaxRulesObject, "eventServiceExcludeRules") == false)
                            {
                                JArray excludeRulesArray = (JArray)ajaxRulesObject["eventServiceExcludeRules"];
                                foreach (JObject excludeRuleObject in excludeRulesArray)
                                {
                                    WEBPageDetectionRule webPageDetectionRule = fillWebPageDetectionRule(excludeRuleObject, jobTarget);
                                    if (webPageDetectionRule != null)
                                    {
                                        webPageDetectionRule.DetectionType  = "EXCLUDE";
                                        webPageDetectionRule.EntityCategory = "AjaxEventsSvc";

                                        pageDetectionRulesList.Add(webPageDetectionRule);
                                    }
                                }
                            }
                        }

                        #endregion

                        #region Virtual Page Rules

                        JObject virtualPageRulesObject = FileIOHelper.LoadJObjectFromFile(FilePathMap.WEBAgentVirtualPageRulesDataFilePath(jobTarget));
                        if (virtualPageRulesObject != null)
                        {
                            if (isTokenPropertyNull(virtualPageRulesObject, "customNamingIncludeRules") == false)
                            {
                                JArray includeRulesArray = (JArray)virtualPageRulesObject["customNamingIncludeRules"];
                                foreach (JObject includeRuleObject in includeRulesArray)
                                {
                                    WEBPageDetectionRule webPageDetectionRule = fillWebPageDetectionRule(includeRuleObject, jobTarget);
                                    if (webPageDetectionRule != null)
                                    {
                                        webPageDetectionRule.DetectionType  = "INCLUDE";
                                        webPageDetectionRule.EntityCategory = "VirtualPage";

                                        pageDetectionRulesList.Add(webPageDetectionRule);
                                    }
                                }
                            }

                            if (isTokenPropertyNull(virtualPageRulesObject, "customNamingExcludeRules") == false)
                            {
                                JArray excludeRulesArray = (JArray)virtualPageRulesObject["customNamingExcludeRules"];
                                foreach (JObject excludeRuleObject in excludeRulesArray)
                                {
                                    WEBPageDetectionRule webPageDetectionRule = fillWebPageDetectionRule(excludeRuleObject, jobTarget);
                                    if (webPageDetectionRule != null)
                                    {
                                        webPageDetectionRule.DetectionType  = "EXCLUDE";
                                        webPageDetectionRule.EntityCategory = "VirtualPage";

                                        pageDetectionRulesList.Add(webPageDetectionRule);
                                    }
                                }
                            }
                        }

                        #endregion

                        // Sort them
                        pageDetectionRulesList = pageDetectionRulesList.OrderBy(o => o.EntityCategory).ThenBy(o => o.DetectionType).ThenBy(o => o.Priority).ToList();
                        FileIOHelper.WriteListToCSVFile(pageDetectionRulesList, new WEBPageDetectionRuleReportMap(), FilePathMap.WEBAgentPageAjaxVirtualPageRulesIndexFilePath(jobTarget));

                        loggerConsole.Info("Completed {0} Rules", pageDetectionRulesList.Count);

                        #endregion

                        #region Synthetic Jobs

                        loggerConsole.Info("Synthetic Jobs");

                        List <WEBSyntheticJobDefinition> syntheticJobDefinitionsList = null;

                        JObject syntheticJobsObject = FileIOHelper.LoadJObjectFromFile(FilePathMap.WEBSyntheticJobsDataFilePath(jobTarget));
                        if (syntheticJobsObject != null)
                        {
                            if (isTokenPropertyNull(syntheticJobsObject, "jobListDatas") == false)
                            {
                                JArray syntheticJobsArray = (JArray)syntheticJobsObject["jobListDatas"];

                                syntheticJobDefinitionsList = new List <WEBSyntheticJobDefinition>(syntheticJobsArray.Count);

                                foreach (JObject syntheticJobObject in syntheticJobsArray)
                                {
                                    if (isTokenPropertyNull(syntheticJobObject, "config") == false)
                                    {
                                        JObject syntheticJobConfigObject = (JObject)syntheticJobObject["config"];

                                        WEBSyntheticJobDefinition syntheticJobDefinition = new WEBSyntheticJobDefinition();

                                        syntheticJobDefinition.Controller      = jobTarget.Controller;
                                        syntheticJobDefinition.ControllerLink  = String.Format(DEEPLINK_CONTROLLER, jobTarget.Controller, DEEPLINK_TIMERANGE_LAST_15_MINUTES);
                                        syntheticJobDefinition.ApplicationName = jobTarget.Application;
                                        syntheticJobDefinition.ApplicationID   = jobTarget.ApplicationID;
                                        syntheticJobDefinition.ApplicationLink = String.Format(DEEPLINK_APM_APPLICATION, jobTarget.Controller, jobTarget.ApplicationID, DEEPLINK_TIMERANGE_LAST_15_MINUTES);

                                        syntheticJobDefinition.JobName = getStringValueFromJToken(syntheticJobConfigObject, "description");
                                        syntheticJobDefinition.JobID   = getStringValueFromJToken(syntheticJobConfigObject, "id");

                                        syntheticJobDefinition.IsUserEnabled   = getBoolValueFromJToken(syntheticJobConfigObject, "userEnabled");
                                        syntheticJobDefinition.IsSystemEnabled = getBoolValueFromJToken(syntheticJobConfigObject, "systemEnabled");
                                        syntheticJobDefinition.FailOnError     = getBoolValueFromJToken(syntheticJobConfigObject, "failOnPageError");
                                        syntheticJobDefinition.IsPrivateAgent  = getBoolValueFromJToken(syntheticJobObject, "hasPrivateAgent");

                                        syntheticJobDefinition.RateUnit = getStringValueFromJToken(syntheticJobConfigObject["rate"], "unit");
                                        syntheticJobDefinition.Rate     = getIntValueFromJToken(syntheticJobConfigObject["rate"], "value");
                                        syntheticJobDefinition.Timeout  = getIntValueFromJToken(syntheticJobConfigObject, "timeoutSeconds");

                                        syntheticJobDefinition.Days      = getStringValueOfObjectFromJToken(syntheticJobConfigObject, "daysOfWeek", true);
                                        syntheticJobDefinition.Browsers  = getStringValueOfObjectFromJToken(syntheticJobConfigObject, "browserCodes", true);
                                        syntheticJobDefinition.Locations = getStringValueOfObjectFromJToken(syntheticJobConfigObject, "locationCodes", true);
                                        if (syntheticJobDefinition.Locations.Length > 0)
                                        {
                                            syntheticJobDefinition.NumLocations = ((JArray)syntheticJobConfigObject["locationCodes"]).Count();
                                        }
                                        syntheticJobDefinition.ScheduleMode = getStringValueFromJToken(syntheticJobConfigObject, "scheduleMode");

                                        syntheticJobDefinition.URL    = getStringValueFromJToken(syntheticJobConfigObject, "url");
                                        syntheticJobDefinition.Script = getStringValueFromJToken(syntheticJobConfigObject["script"], "script");

                                        if (syntheticJobDefinition.URL.Length > 0)
                                        {
                                            syntheticJobDefinition.JobType = "URL";
                                        }
                                        else
                                        {
                                            syntheticJobDefinition.JobType = "SCRIPT";
                                        }

                                        syntheticJobDefinition.Network      = getStringValueOfObjectFromJToken(syntheticJobConfigObject, "networkProfile", false);
                                        syntheticJobDefinition.Config       = getStringValueOfObjectFromJToken(syntheticJobConfigObject, "composableConfig", false);
                                        syntheticJobDefinition.PerfCriteria = getStringValueOfObjectFromJToken(syntheticJobConfigObject, "performanceCriteria", false);

                                        syntheticJobDefinition.CreatedOnUtc = UnixTimeHelper.ConvertFromUnixTimestamp(getLongValueFromJToken(syntheticJobConfigObject, "created"));
                                        try { syntheticJobDefinition.CreatedOn = syntheticJobDefinition.CreatedOnUtc.ToLocalTime(); } catch { }
                                        syntheticJobDefinition.UpdatedOnUtc = UnixTimeHelper.ConvertFromUnixTimestamp(getLongValueFromJToken(syntheticJobConfigObject, "updated"));
                                        try { syntheticJobDefinition.UpdatedOn = syntheticJobDefinition.UpdatedOnUtc.ToLocalTime(); } catch { }

                                        syntheticJobDefinitionsList.Add(syntheticJobDefinition);
                                    }
                                }

                                // Sort them
                                syntheticJobDefinitionsList = syntheticJobDefinitionsList.OrderBy(o => o.JobName).ToList();
                                FileIOHelper.WriteListToCSVFile(syntheticJobDefinitionsList, new WEBSyntheticJobDefinitionReportMap(), FilePathMap.WEBSyntheticJobsIndexFilePath(jobTarget));

                                loggerConsole.Info("Completed {0} Synthetic Jobs", syntheticJobDefinitionsList.Count);
                            }
                        }

                        #endregion

                        #region Application Settings

                        if (pageDetectionRulesList != null)
                        {
                            applicationConfiguration.NumPageRulesInclude     = pageDetectionRulesList.Count(r => r.EntityCategory == "Pages&IFrames" && r.DetectionType == "INCLUDE");
                            applicationConfiguration.NumPageRulesExclude     = pageDetectionRulesList.Count(r => r.EntityCategory == "Pages&IFrames" && r.DetectionType == "EXCLUDE");
                            applicationConfiguration.NumAJAXRulesInclude     = pageDetectionRulesList.Count(r => r.EntityCategory == "Ajax" && r.DetectionType == "INCLUDE");
                            applicationConfiguration.NumAJAXRulesExclude     = pageDetectionRulesList.Count(r => r.EntityCategory == "Ajax" && r.DetectionType == "EXCLUDE");
                            applicationConfiguration.NumVirtPageRulesInclude = pageDetectionRulesList.Count(r => r.EntityCategory == "VirtualPage" && r.DetectionType == "INCLUDE");
                            applicationConfiguration.NumVirtPageRulesExclude = pageDetectionRulesList.Count(r => r.EntityCategory == "VirtualPage" && r.DetectionType == "EXCLUDE");
                        }

                        if (syntheticJobDefinitionsList != null)
                        {
                            applicationConfiguration.NumSyntheticJobs = syntheticJobDefinitionsList.Count;
                        }

                        List <WEBApplicationConfiguration> applicationConfigurationsList = new List <WEBApplicationConfiguration>(1);
                        applicationConfigurationsList.Add(applicationConfiguration);
                        FileIOHelper.WriteListToCSVFile(applicationConfigurationsList, new WEBApplicationConfigurationReportMap(), FilePathMap.WEBApplicationConfigurationIndexFilePath(jobTarget));

                        stepTimingTarget.NumEntities = stepTimingTarget.NumEntities + applicationConfigurationsList.Count;

                        #endregion

                        #region Combine All for Report CSV

                        // If it is the first one, clear out the combined folder
                        if (reportFolderCleaned == false)
                        {
                            FileIOHelper.DeleteFolder(FilePathMap.WEBConfigurationReportFolderPath());
                            Thread.Sleep(1000);
                            FileIOHelper.CreateFolder(FilePathMap.WEBConfigurationReportFolderPath());
                            reportFolderCleaned = true;
                        }

                        // Append all the individual report files into one
                        if (File.Exists(FilePathMap.WEBApplicationConfigurationIndexFilePath(jobTarget)) == true && new FileInfo(FilePathMap.WEBApplicationConfigurationIndexFilePath(jobTarget)).Length > 0)
                        {
                            FileIOHelper.AppendTwoCSVFiles(FilePathMap.WEBApplicationConfigurationReportFilePath(), FilePathMap.WEBApplicationConfigurationIndexFilePath(jobTarget));
                        }
                        if (File.Exists(FilePathMap.WEBAgentPageAjaxVirtualPageRulesIndexFilePath(jobTarget)) == true && new FileInfo(FilePathMap.WEBAgentPageAjaxVirtualPageRulesIndexFilePath(jobTarget)).Length > 0)
                        {
                            FileIOHelper.AppendTwoCSVFiles(FilePathMap.WEBAgentPageAjaxVirtualPageRulesReportFilePath(), FilePathMap.WEBAgentPageAjaxVirtualPageRulesIndexFilePath(jobTarget));
                        }
                        if (File.Exists(FilePathMap.WEBSyntheticJobsIndexFilePath(jobTarget)) == true && new FileInfo(FilePathMap.WEBSyntheticJobsIndexFilePath(jobTarget)).Length > 0)
                        {
                            FileIOHelper.AppendTwoCSVFiles(FilePathMap.WEBSyntheticJobsReportFilePath(), FilePathMap.WEBSyntheticJobsIndexFilePath(jobTarget));
                        }

                        #endregion
                    }
                    catch (Exception ex)
                    {
                        logger.Warn(ex);
                        loggerConsole.Warn(ex);

                        return(false);
                    }
                    finally
                    {
                        stopWatchTarget.Stop();

                        this.DisplayJobTargetEndedStatus(jobConfiguration, jobTarget, i + 1, stopWatchTarget);

                        stepTimingTarget.EndTime    = DateTime.Now;
                        stepTimingTarget.Duration   = stopWatchTarget.Elapsed;
                        stepTimingTarget.DurationMS = stopWatchTarget.ElapsedMilliseconds;

                        List <StepTiming> stepTimings = new List <StepTiming>(1);
                        stepTimings.Add(stepTimingTarget);
                        FileIOHelper.WriteListToCSVFile(stepTimings, new StepTimingReportMap(), FilePathMap.StepTimingReportFilePath(), true);
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                loggerConsole.Error(ex);

                return(false);
            }
            finally
            {
                stopWatch.Stop();

                this.DisplayJobStepEndedStatus(jobConfiguration, stopWatch);

                stepTimingFunction.EndTime    = DateTime.Now;
                stepTimingFunction.Duration   = stopWatch.Elapsed;
                stepTimingFunction.DurationMS = stopWatch.ElapsedMilliseconds;

                List <StepTiming> stepTimings = new List <StepTiming>(1);
                stepTimings.Add(stepTimingFunction);
                FileIOHelper.WriteListToCSVFile(stepTimings, new StepTimingReportMap(), FilePathMap.StepTimingReportFilePath(), true);
            }
        }