public CurrentQueriesPanel(ServerManager server)
 {
     InitializeComponent();
     this.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
     | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)));
     myServer = server;
 }
        /// <summary>
        /// Creates a new Server Control tab from an existing ServerManager
        /// </summary>
        /// <param name="sc">the server manager for this server control</param>
        public ServerControl(ServerManager sc)
        {
            serverMan = sc;

            InitializeComponent();
            this.Text = this.Name = sc.Name;

            foreach (Rule rule in serverMan.RuleMan.GetRules())
                rulesPanel.GuiExistingRule(rule);

            panelArray = new UserControl[11] { alertsPanel, activeSessionsPanel,
                                              currentQueriesPanel, snapshotDormantSessionsPanel,
                                              SnapshotBlockedSessionsPanel,
                                              snapshotCPUPanel, snapshotIOPanel, snapshotObjectsPanel,
                                              locksPanel, rulesPanel, usersPanel};

            this.Controls.AddRange(panelArray);
            hideAllPanels();
            panelArray[0].Show();

            toolStripMenuItem = new ToolStripMenuItem[11] { AlertsToolStripMenuItem, ActiveSessionsToolStripMenuItem,
                                                            CurrentQueriesToolStripMenuItem, DormantSessionsToolStripMenuItem,
                                                            BlockedSessionsToolStripMenuItem,
                                                            CPUToolStripMenuItem, IOToolStripMenuItem,
                                                            ObjectsToolStripMenuItem, LocksToolStripMenuItem,
                                                            RulesToolStripMenuItem, UsersToolStripMenuItem};
        }
        /// <summary>
        /// Installs an existing Servermanager into the UI
        /// </summary>
        /// <param name="sm">The ServerManager to install</param>
        public void InstallExistingServer(ServerManager sm)
        {
            ServerControl newTab = new ServerControl(sm);

            //add tab and list box items
            ConnectionDataGrid.Rows.Add(GetServerRow(sm));
            MainTabControl.TabPages.Add(newTab);
        }
        public SnapshotCPUPanel(ServerManager server)
        {
            InitializeComponent();
            this.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));

            myServer = server;
            refreshCPUTable();
        }
 public CurrentQueriesPanel(ServerManager server)
 {
     InitializeComponent();
     this.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
     | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)));
     myServer = server;
     QueriesDataGrid.CellFormatting +=
     new System.Windows.Forms.DataGridViewCellFormattingEventHandler(
     this.QueriesDataGrid_CellFormatting);
 }
 public SnapshotDormantSessionsPanel(ServerManager server)
 {
     InitializeComponent();
     myServer = server;
     this.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
     | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)));
     DormantDataGrid.CellFormatting +=
     new System.Windows.Forms.DataGridViewCellFormattingEventHandler(
     this.DormantDataGrid_CellFormatting);
 }
        public CurrentStateOverviewPanel(ServerManager server)
        {
            myServer = server;
            InitializeComponent();
            this.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
             | System.Windows.Forms.AnchorStyles.Left)| System.Windows.Forms.AnchorStyles.Right)));

            this.DetailsButton.Enabled = false;
            this.RemoveAlertButton.Enabled = false;
        }
        public SnapshotCPUPanel(ServerManager server)
        {
            InitializeComponent();
            this.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            CPUDataGrid.CellFormatting +=
            new System.Windows.Forms.DataGridViewCellFormattingEventHandler(
            this.CPUDataGrid_CellFormatting);

            myServer = server;
            refreshCPUTable();
        }
        /// <summary>
        /// Creates a new ServerControl Tab from a connection string
        /// </summary>
        /// <param name="connection">The connection string for this server tab</param>
        public ServerControl(string connection)
        {
            serverMan = new ServerManager(connection);

            InitializeComponent();
            this.Text = this.Name = serverMan.Name;

            panelArray = new UserControl[11] { alertsPanel, activeSessionsPanel,
                                              currentQueriesPanel, snapshotDormantSessionsPanel,SnapshotBlockedSessionsPanel,
                                              snapshotCPUPanel, snapshotIOPanel, snapshotObjectsPanel,
                                              locksPanel, rulesPanel, usersPanel};
            this.Controls.AddRange(panelArray);
            hideAllPanels();
            panelArray[0].Show();

            toolStripMenuItem = new ToolStripMenuItem[11] { AlertsToolStripMenuItem, ActiveSessionsToolStripMenuItem,
                                                            CurrentQueriesToolStripMenuItem, DormantSessionsToolStripMenuItem,
                                                            BlockedSessionsToolStripMenuItem,
                                                            CPUToolStripMenuItem, IOToolStripMenuItem,
                                                            ObjectsToolStripMenuItem, LocksToolStripMenuItem,
                                                            RulesToolStripMenuItem, UsersToolStripMenuItem};
        }
        /// <summary>
        /// Load a previous session from a predefined configuration file
        /// </summary>
        private void LoadSession()
        {
            FileStream fs=null;
            try
            {   //try to open the file and return if the open fails
                fs = new FileStream("config.xml", FileMode.Open);
            }
            catch{
                return;
            }
            try
            {
                XmlTextReader reader = new XmlTextReader(fs);

                reader.ReadToFollowing("Servers");
                reader.ReadToDescendant("Server");
                do
                {   //read connection string for each server and try to add it to the program
                    reader.ReadToDescendant("String");
                    ServerManager newman = new ServerManager(reader.ReadElementString("String"));
                    try
                    {
                        AddServer(newman);
                    }
                    catch { }

                    //add saved rules to the server
                    while (reader.Name == "Rule")
                    {
                        Rule newRule = newman.RuleMan.AddRule(reader.ReadElementString("Rule"));
                        newRule.SetPriority(int.Parse(reader.ReadElementString("Priority")));
                    }

                } while (reader.ReadToFollowing("Server"));
            }
            catch
            {//unfortunately an error in the configuration file may cancel loading any data after that point
             //will figure this out when more familiar with parsing XML files, probably should use DOM, oh well
            }
            finally
            {
                fs.Close();
            }
        }
 /// <summary>
 /// Removes a server from the program
 /// </summary>
 /// <param name="server">The server to remove</param>
 public void RemoveServer(ServerManager server)
 {
     server.Disconnect();
     servers.Remove(server);
 }
 /// <summary>
 /// Attempts to add a server to the program, does not allow duplicates or faulty connections
 /// </summary>
 /// <param name="server">The server to attempt to add to the program</param>
 public void AddServer(ServerManager server)
 {
     foreach (ServerManager existing in servers)//do not allow duplicates
         if (existing.ASMan.ConnectionString.Equals(server.ASMan.ConnectionString))
             throw new ArgumentException("Server Already Exists");
     //test for a valid connection, delegate exception up if connection is bad
     server.TestConnection();
     servers.Add(server);
 }
 /// <summary>
 /// Sets the server this alert was raised on
 /// </summary>
 /// <param name="Server">The server this alert was raised on</param>
 public void SetServer(ServerManager server)
 {
     this.server = server;
 }
        /// <summary>
        /// Installs a new server into the program with the given connection string
        /// </summary>
        /// <param name="connectionString">The connection string to connect to server with</param>
        public void InstallNewServer(string connectionString)
        {
            ServerManager sm = new ServerManager(connectionString);

            ServerControl newTab = new ServerControl(sm);
            pm.AddServer(newTab.serverMan);
            newTab.serverMan.Connect();

            //Add the new tab and connect to the server
            ConnectionDataGrid.Rows.Add(GetServerRow(sm));
            MainTabControl.TabPages.Add(newTab);
        }
 /// <summary>
 /// Installs some default rules on a new connection
 /// </summary>
 /// <param name="sm">the server to install rules on</param>
 private static void InstallDefaultRules(ServerManager sm)
 {
     sm.RuleMan.AddRule("IF SESSION_CPU_TIME_MS > 10000 THEN KILL AND ALERT");
     sm.RuleMan.AddRule("IF SESSION_COMMAND_COUNT >= 100 THEN ALERT");
 }
        private static object[] GetServerRow(ServerManager sm)
        {
            DataTable theData = sm.ASMan.DataTableFromQuery("SELECT SESSION_CPU_TIME_MS, SESSION_USED_MEMORY, SESSION_READ_KB, SESSION_WRITE_KB from $System.DISCOVER_SESSIONS");
            object[] row = new object[5];
            row[0] = sm.Name;
            row[1] = (int)0;
            row[2] = (decimal)0;
            row[3] = (decimal)0;
            row[4] = (decimal)0;

            foreach (DataRow urow in theData.Rows)
                {
                    row[1] = (int)row[1] + (int)urow["SESSION_USED_MEMORY"];
                    row[2] = (decimal)row[2] + (decimal)urow["SESSION_CPU_TIME_MS"];
                    row[3] = (decimal)row[3] + (decimal)urow["SESSION_READ_KB"];
                    row[4] = (decimal)row[4] + (decimal)urow["SESSION_WRITE_KB"];
                }

            return row;
        }