Пример #1
0
        /// <summary>
        /// Stop monitoring the server(s)
        /// </summary>
        public void StopWatching()
        {
            if (activeSessions == null || activeSessions.Count == 0)
            {
                return;
            }

            inputActivityTimer.Stop();
            sessionStateUpdateTimer.Stop();
            checkRepositoryTimer.Stop();
            for (int i = activeSessions.Count - 1; i > -1; i--)
            {
                ActiveSession activeSession = activeSessions[i];
                if (LogonTracerConfig.Instance.LogSessionHistory)
                {
                    repoProvider.UpdateSessionHistory(activeSession.DbId.Value, SessionUpdateDetails.StateUpdated, new Dictionary <string, string>
                    {
                        { "Old value", SessionState.Active.ToString() },
                        { "New value", SessionState.ServiceDown.ToString() }
                    });
                }
                activeSession.SessionState = SessionState.ServiceDown;
                repoProvider.SaveSession(activeSession);
            }
        }
Пример #2
0
 /// <summary>
 /// Register all active sessions on server(s)
 /// </summary>
 public void RegisterSessions()
 {
     if (!checkProgramActivityTimer.Enabled)
     {
         checkProgramActivityTimer.Start();
     }
     if (!troubleShootingTimer.Enabled)
     {
         troubleShootingTimer.Start();
     }
     activeSessions = new List <ActiveSession>();
     for (int i = LogonTracerConfig.Instance.ServersToTrace.Count() - 1; i > -1; i--)
     {
         string serverName = LogonTracerConfig.Instance.ServersToTrace[i];
         try
         {
             using (ITerminalServer server = manager.GetRemoteServer(serverName))
             {
                 server.Open();
                 foreach (ITerminalServicesSession session in server.GetSessions())
                 {
                     ActiveSession activeSession = null;
                     if (session.UserAccount == null || session.ConnectionState != Cassia.ConnectionState.Active)
                     {
                         continue;
                     }
                     activeSession = repoProvider.BuildSession(session);
                     activeSessions.Add(activeSession);
                     if (activeSession.DbId.HasValue && LogonTracerConfig.Instance.LogSessionHistory)
                     {
                         repoProvider.UpdateSessionHistory(activeSession.DbId.Value, SessionUpdateDetails.StateUpdated, new Dictionary <string, string>
                         {
                             { "Old value", SessionState.ServiceDown.ToString() },
                             { "New value", SessionState.Active.ToString() }
                         });
                     }
                     repoProvider.SaveSession(activeSession);
                 }
             }
         }
         catch (Win32Exception ex)
         {
             LoggingUtils.DefaultLogger.AddLogMessage(this, MessageType.Error, "Error while trying access the server. Check Remote Desktop Services Permissions for this server and try again. Server:{0}. Message: {1}", serverName, ex.Message);
             LogonTracerConfig.Instance.ServersToTrace = LogonTracerConfig.Instance.ServersToTrace.Where(stt => stt != serverName).ToArray();
         }
         catch (Exception ex)
         {
             LoggingUtils.DefaultLogger.AddLogMessage(this, MessageType.Error, "Type: {0}. Server:{1}. Message: {2}", ex.GetType(), serverName, ex.Message);
         }
     }
     inputActivityTimer.Start();
     sessionStateUpdateTimer.Start();
     checkRepositoryTimer.Start();
 }
Пример #3
0
        private void OnInputActivityTimer(object sender, System.Timers.ElapsedEventArgs args)
        {
#if DEBUG
            if (sessionLocked)
            {
                return;
            }
#endif
            sessionLocked = true;
            if (activeSessions.Count == 0)
            {
                RegisterSessions();
                sessionLocked = false;
                return;
            }
            for (int i = 0; i < activeSessions.Count; i++)
            {
                bool          activityRegistered = false;
                ActiveSession activeSession      = activeSessions[i];

                ITerminalServicesSession session = FindSession(activeSession);

                if (session != null)
                {
                    if (AppData.DateTimeConvertUtils.Compare(activeSession.LastInputTime, session.LastInputTime) != 1 &&
                        (AppData.DateTimeConvertUtils.ConvertTimeByUtcOffset(DateTime.Now, LogonTracerConfig.Instance.UtcOffset).Value -
                         AppData.DateTimeConvertUtils.ConvertTimeByUtcOffset(session.ConnectTime, LogonTracerConfig.Instance.UtcOffset).Value).TotalMinutes > LogonTracerConfig.Instance.InputActivityTimePeriod)
                    {
                        Dictionary <string, string> activeHoursDict = new Dictionary <string, string> {
                            { "Old value", activeSession.ActivityHours.ToString("0.0000", System.Globalization.CultureInfo.InvariantCulture) }
                        };
                        Dictionary <string, string> lastInputTimeDict = new Dictionary <string, string> {
                            { "Old value", activeSession.LastInputTime.ToString() }
                        };
                        activeSession.ActivityHours += Convert.ToDouble(LogonTracerConfig.Instance.InputActivityTimePeriod) / 60;
                        activeSession.LastInputTime  = session.LastInputTime;
                        activeHoursDict.Add("New value", activeSession.ActivityHours.ToString("0.0000", System.Globalization.CultureInfo.InvariantCulture));
                        lastInputTimeDict.Add("New value", activeSession.LastInputTime.ToString());
                        activityRegistered = true;
                        if (LogonTracerConfig.Instance.LogSessionHistory)
                        {
                            repoProvider.UpdateSessionHistory(activeSession.DbId.Value, SessionUpdateDetails.ActivityHoursIncreased, activeHoursDict);
                            repoProvider.UpdateSessionHistory(activeSession.DbId.Value, SessionUpdateDetails.LastInputTimeUpdate, lastInputTimeDict);
                        }
                        repoProvider.SaveSession(activeSession);
                    }
                    repoProvider.SaveSessionActivityProfile(activeSession.DbId, activityRegistered);
                }
            }
            UpdateListenersStatus();
            sessionLocked = false;
        }
Пример #4
0
        private void OnSessionStateUpdateTimer(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (sessionLocked)
            {
                return;
            }
            sessionLocked = true;

            for (int i = activeSessions.Count - 1; i > -1; i--)
            {
                bool          sessionExist  = false;
                ActiveSession activeSession = activeSessions[i];

                ITerminalServicesSession session = FindSession(activeSession);

                if (session != null)
                {
                    if (/*AppData.DateTimeConvertUtils.Compare(activeSession.SessionBegin.Value, session.ConnectTime.Value) != 1*/
                        activeSession.SessionBegin.Value != session.ConnectTime.Value)
                    {
                        activeSession.SessionState = SessionState.Disconnected;
                    }

                    if (session.ConnectionState == Cassia.ConnectionState.Disconnected)
                    {
                        activeSession.SessionEnd   = session.DisconnectTime;
                        activeSession.SessionState = SessionState.Disconnected;
                    }
                    sessionExist = true;
                }
                if (!sessionExist)
                {
                    activeSession.SessionState = SessionState.Disconnected;
                }
                if (activeSession.SessionState != SessionState.Active)
                {
                    repoProvider.SaveSession(activeSession);
                    if (LogonTracerConfig.Instance.LogSessionHistory)
                    {
                        repoProvider.UpdateSessionHistory(activeSession.DbId.Value, SessionUpdateDetails.StateUpdated, new Dictionary <string, string> {
                            { "Old value", SessionState.Active.ToString() },
                            { "New value", SessionState.Disconnected.ToString() }
                        });
                    }
                    activeSessions.Remove(activeSession);
                }
            }

            sessionLocked = false;
        }
Пример #5
0
        /// <summary>
        /// Find session among all servers and sessions
        /// </summary>
        /// <param name="activeSession">Registered session</param>
        /// <returns>Session</returns>
        private ITerminalServicesSession FindSession(ActiveSession activeSession)
        {
            ITerminalServicesSession foundedSession = null;

            foreach (string serverName in LogonTracerConfig.Instance.ServersToTrace)
            {
                try
                {
                    using (ITerminalServer server = manager.GetRemoteServer(serverName))
                    {
                        server.Open();
                        foreach (ITerminalServicesSession session in server.GetSessions())
                        {
                            if (session.UserAccount == null)
                            {
                                continue;
                            }
                            if (activeSessions.Where(acs => acs.MachineName == session.Server.ServerName &&
                                                     acs.UserName == session.UserName &&
                                                     /*AppData.DateTimeConvertUtils.Compare(acs.SessionBegin.Value, session.ConnectTime.Value) == 1*/ acs.SessionBegin.Value == session.ConnectTime.Value).Count() == 0 &&
                                session.ConnectionState != Cassia.ConnectionState.Disconnected)
                            {
                                ActiveSession newSession = repoProvider.BuildSession(session);
                                activeSessions.Add(newSession);
                                repoProvider.SaveSession(newSession);
                                continue;
                            }
                            if (session.UserName == activeSession.UserName && session.Server.ServerName == activeSession.MachineName)
                            {
                                foundedSession = session;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    LoggingUtils.DefaultLogger.AddLogMessage(this, MessageType.Error, "Type: {0}. Server{1}. Message: {2}", ex.GetType(), serverName, ex.Message);
                }
            }
            return(foundedSession);
        }