private void cmdRemoteAgentTest_Click(object sender, EventArgs e)
        {
            CollectorEntry testCollector = new CollectorEntry();

            testCollector.UniqueId = lblId.Text;
            testCollector.Name     = txtName.Text.Trim();
            testCollector.Enabled  = chkEnabled.Checked;
            testCollector.IsFolder = currentEditingEntry.IsFolder;

            if (cboParentCollector.SelectedIndex > 0)
            {
                CollectorEntryDisplay ced = (CollectorEntryDisplay)cboParentCollector.SelectedItem;
                SelectedEntry.ParentCollectorId = ced.CE.UniqueId;
            }
            else
            {
                SelectedEntry.ParentCollectorId = "";
            }

            if (currentEditingEntry.IsFolder)
            {
                testCollector.CollectorRegistrationName = "Folder";
            }
            else
            {
                //Collector type
                testCollector.CollectorRegistrationName        = currentEditingEntry.CollectorRegistrationName;
                testCollector.CollectorRegistrationDisplayName = currentEditingEntry.CollectorRegistrationDisplayName;

                testCollector.InitialConfiguration = currentEditingEntry.InitialConfiguration;
            }
            testCollector.CollectOnParentWarning        = chkCollectOnParentWarning.Checked && !currentEditingEntry.IsFolder;
            testCollector.RepeatAlertInXMin             = (int)numericUpDownRepeatAlertInXMin.Value;
            testCollector.AlertOnceInXMin               = (int)AlertOnceInXMinNumericUpDown.Value;
            testCollector.DelayErrWarnAlertForXSec      = (int)delayAlertSecNumericUpDown.Value;
            testCollector.CorrectiveScriptDisabled      = chkCorrectiveScriptDisabled.Checked;
            testCollector.CorrectiveScriptOnWarningPath = txtCorrectiveScriptOnWarning.Text;
            testCollector.CorrectiveScriptOnErrorPath   = txtCorrectiveScriptOnError.Text;
            testCollector.EnableRemoteExecute           = chkRemoteAgentEnabled.Checked;
            testCollector.RemoteAgentHostAddress        = txtRemoteAgentServer.Text;
            testCollector.RemoteAgentHostPort           = (int)remoteportNumericUpDown.Value;
            testCollector.EnabledPollingOverride        = false;

            try
            {
                MonitorState testState = CollectorEntryRelay.GetRemoteAgentState(testCollector);
                if (testState.State == CollectorState.Good)
                {
                    MessageBox.Show("Success", "Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show(string.Format("State: {0}\r\nDetails: {1}", testState.State, testState.RawDetails), "Test", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void LoadParentCollectorList(CollectorEntry parentEntry = null, int indent = 0)
        {
            if (cboParentCollector.Items.Count == 0)
            {
                cboParentCollector.Items.Add("<None>");
                cboParentCollector.SelectedIndex = 0;
            }
            if (monitorPack != null)
            {
                foreach (CollectorEntry ce in (from c in monitorPack.Collectors
                                               where (parentEntry == null && (c.ParentCollectorId == null || c.ParentCollectorId == "")) ||
                                               (parentEntry != null && parentEntry.UniqueId == c.ParentCollectorId)
                                               select c))
                {
                    CollectorEntryDisplay ceDisplay = new CollectorEntryDisplay()
                    {
                        Ident = indent, CE = ce
                    };
                    if (IsNotInCurrentDependantTree(currentEditingEntry.UniqueId, ce))
                    {
                        cboParentCollector.Items.Add(ceDisplay);
                    }
                    if (ce.UniqueId == currentEditingEntry.ParentCollectorId)
                    {
                        cboParentCollector.SelectedItem = ceDisplay;
                    }

                    LoadParentCollectorList(ce, indent + 1);
                }
            }
        }
        private void cmdOK_Click(object sender, EventArgs e)
        {
            if (SelectedEntry == null)
            {
                SelectedEntry = new CollectorEntry();
            }

            SelectedEntry.Name          = txtName.Text;
            SelectedEntry.Enabled       = chkEnabled.Checked;
            SelectedEntry.ExpandOnStart = chkExpandOnStart.Checked;
            SelectedEntry.IsFolder      = currentEditingEntry.IsFolder;
            if (cboParentCollector.SelectedIndex > 0)
            {
                CollectorEntryDisplay ced = (CollectorEntryDisplay)cboParentCollector.SelectedItem;
                SelectedEntry.ParentCollectorId = ced.CE.UniqueId;
            }
            else
            {
                SelectedEntry.ParentCollectorId = "";
            }
            SelectedEntry.CollectOnParentWarning = chkCollectOnParentWarning.Checked && !SelectedEntry.IsFolder;

            //Collector type
            SelectedEntry.CollectorRegistrationName        = currentEditingEntry.CollectorRegistrationName;
            SelectedEntry.CollectorRegistrationDisplayName = currentEditingEntry.CollectorRegistrationDisplayName;

            //Remote agents
            SelectedEntry.EnableRemoteExecute = chkRemoteAgentEnabled.Checked;
            SelectedEntry.ForceRemoteExcuteOnChildCollectors = chkForceRemoteExcuteOnChildCollectors.Checked;
            SelectedEntry.RemoteAgentHostAddress             = txtRemoteAgentServer.Text;
            SelectedEntry.RemoteAgentHostPort = (int)remoteportNumericUpDown.Value;
            SelectedEntry.BlockParentOverrideRemoteAgentHostSettings = chkBlockParentRHOverride.Checked && !chkRemoteAgentEnabled.Checked;
            SelectedEntry.RunLocalOnRemoteHostConnectionFailure      = chkRunLocalOnRemoteHostConnectionFailure.Checked;
            if (chkRemoteAgentEnabled.Checked && SelectedEntry.RemoteAgentHostAddress.Length > 0)
            {
                if (KnownRemoteHosts == null)
                {
                    KnownRemoteHosts = new List <string>();
                }
                if ((from string rh in KnownRemoteHosts
                     where rh.ToLower() == SelectedEntry.RemoteAgentHostAddress.ToLower() + ":" + SelectedEntry.RemoteAgentHostPort.ToString()
                     select rh).Count() == 0
                    )
                {
                    KnownRemoteHosts.Add(SelectedEntry.RemoteAgentHostAddress + ":" + SelectedEntry.RemoteAgentHostPort.ToString());
                }
            }

            //Polling overrides
            if (onlyAllowUpdateOncePerXSecNumericUpDown.Value >= pollSlideFrequencyAfterFirstRepeatSecNumericUpDown.Value)
            {
                pollSlideFrequencyAfterFirstRepeatSecNumericUpDown.Value = onlyAllowUpdateOncePerXSecNumericUpDown.Value + 1;
            }
            if (pollSlideFrequencyAfterFirstRepeatSecNumericUpDown.Value >= pollSlideFrequencyAfterSecondRepeatSecNumericUpDown.Value)
            {
                pollSlideFrequencyAfterSecondRepeatSecNumericUpDown.Value = pollSlideFrequencyAfterFirstRepeatSecNumericUpDown.Value + 1;
            }
            if (pollSlideFrequencyAfterSecondRepeatSecNumericUpDown.Value >= pollSlideFrequencyAfterThirdRepeatSecNumericUpDown.Value)
            {
                pollSlideFrequencyAfterThirdRepeatSecNumericUpDown.Value = pollSlideFrequencyAfterSecondRepeatSecNumericUpDown.Value + 1;
            }

            SelectedEntry.EnabledPollingOverride                 = chkEnablePollingOverride.Checked;
            SelectedEntry.OnlyAllowUpdateOncePerXSec             = (int)onlyAllowUpdateOncePerXSecNumericUpDown.Value;
            SelectedEntry.EnablePollFrequencySliding             = chkEnablePollingFrequencySliding.Checked;
            SelectedEntry.PollSlideFrequencyAfterFirstRepeatSec  = (int)pollSlideFrequencyAfterFirstRepeatSecNumericUpDown.Value;
            SelectedEntry.PollSlideFrequencyAfterSecondRepeatSec = (int)pollSlideFrequencyAfterSecondRepeatSecNumericUpDown.Value;
            SelectedEntry.PollSlideFrequencyAfterThirdRepeatSec  = (int)pollSlideFrequencyAfterThirdRepeatSecNumericUpDown.Value;

            //Alert suppresion
            SelectedEntry.AlertsPaused               = chkAlertsPaused.Checked;
            SelectedEntry.RepeatAlertInXMin          = (int)numericUpDownRepeatAlertInXMin.Value;
            SelectedEntry.RepeatAlertInXPolls        = (int)numericUpDownRepeatAlertInXPolls.Value;
            SelectedEntry.AlertOnceInXMin            = (int)AlertOnceInXMinNumericUpDown.Value;
            SelectedEntry.AlertOnceInXPolls          = (int)AlertOnceInXPollsNumericUpDown.Value;
            SelectedEntry.DelayErrWarnAlertForXSec   = (int)delayAlertSecNumericUpDown.Value;
            SelectedEntry.DelayErrWarnAlertForXPolls = (int)delayAlertPollsNumericUpDown.Value;
            //Corrective scripts
            SelectedEntry.CorrectiveScriptDisabled           = chkCorrectiveScriptDisabled.Checked;
            SelectedEntry.CorrectiveScriptOnWarningPath      = txtCorrectiveScriptOnWarning.Text;
            SelectedEntry.CorrectiveScriptOnErrorPath        = txtCorrectiveScriptOnError.Text;
            SelectedEntry.RestorationScriptPath              = txtRestorationScript.Text;
            SelectedEntry.CorrectiveScriptsOnlyOnStateChange = chkOnlyRunCorrectiveScriptsOnStateChange.Checked;
            //Service windows
            SelectedEntry.ServiceWindows.CreateFromConfig(currentEditingEntry.ServiceWindows.ToConfig());
            SelectedEntry.ConfigVariables = new List <ConfigVariable>();
            SelectedEntry.ConfigVariables.AddRange((from ConfigVariable cv in currentEditingEntry.ConfigVariables
                                                    select cv.Clone()).ToArray());

            if (SelectedEntry.IsFolder)
            {
                SelectedEntry.Collector = null;
            }
            else
            {
                SelectedEntry.InitialConfiguration = currentEditingEntry.InitialConfiguration;
                RegisteredAgent currentRA = null;
                currentRA = RegisteredAgentCache.GetRegisteredAgentByClassName("." + SelectedEntry.CollectorRegistrationName);
                if (currentRA != null)
                {
                    try
                    {
                        SelectedEntry.CreateAndConfigureEntry(currentRA, monitorPack.ConfigVariables);
                    }
                    catch (Exception ex)
                    {
                        SelectedEntry.LastMonitorState.State = CollectorState.ConfigurationError;
                        SelectedEntry.Enabled = false;
                        SelectedEntry.LastMonitorState.RawDetails = ex.Message;
                    }
                }
                else
                {
                    SelectedEntry.LastMonitorState.State = CollectorState.ConfigurationError;
                    SelectedEntry.Enabled = false;
                    SelectedEntry.LastMonitorState.RawDetails = string.Format("Collector '{0}' cannot be loaded as the type '{1}' is not registered!", SelectedEntry.Name, SelectedEntry.CollectorRegistrationName);
                }
            }

            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }