Ejemplo n.º 1
0
 private static bool GetConfigChanged(Guid serverId)
 {
     using (var db = new candykingdomdbEntities())
     {
         try
         {
             return(db.servers.Single(s => s.ServerGUID == serverId).IsConfigChanged);
         }
         catch (Exception ex) when(ex is SocketException || ex is ArgumentNullException)
         {
             (ex.Message + ex.InnerException).WriteLog(EventLogEntryType.Error, 108);
             throw new FaultException(ex.Message);
         }
         catch (InvalidOperationException ex)
         {
             (ex.Message + ex.InnerException).WriteLog(EventLogEntryType.Error, 108);
             foreach (var server in db.servers.Where(s => s.ServerGUID == serverId))
             {
                 db.servers.Remove(server);
             }
             db.SaveChanges();
             throw new FaultException(ex.Message);
         }
     }
 }
Ejemplo n.º 2
0
 private static void UpdateProcmonitor(SrvMonSummary summary)
 {
     try
     {
         using (var db = new candykingdomdbEntities())
         {
             var serverInDb = db.servers.Single(s => s.ServerGUID == summary.Config.ServerId);
             foreach (var proc in db.procmonitorcpu.Where(p => p.ServerID == serverInDb.ID))
             {
                 //remove existing
                 db.procmonitorcpu.Remove(proc);
             }
             foreach (var proc in summary.TopCpuProcesses.Take(serverInDb.TopCpuProcesses))
             {
                 //add new
                 db.procmonitorcpu.Add(new procmonitorcpu
                 {
                     ProcGUID  = Guid.NewGuid(),
                     ServerID  = serverInDb.ID,
                     ProcName  = proc.ProcName,
                     PID       = proc.Pid,
                     ProcCPU   = proc.ProcCpu,
                     TimeStamp = Convert.ToDateTime(proc.TimeStamp)
                 });
             }
             foreach (var proc in db.procmonitorram.Where(p => p.ServerID == serverInDb.ID))
             {
                 //remove existing
                 db.procmonitorram.Remove(proc);
             }
             foreach (var proc in summary.TopRamProcesses.Take(serverInDb.TopRamProcesses))
             {
                 //add new
                 db.procmonitorram.Add(new procmonitorram
                 {
                     ProcGUID   = Guid.NewGuid(),
                     ServerID   = serverInDb.ID,
                     ProcName   = proc.ProcName,
                     PID        = proc.Pid,
                     ProcMemory = proc.ProcMemory,
                     TimeStamp  = Convert.ToDateTime(proc.TimeStamp)
                 });
             }
             db.SaveChanges();
         }
     }
     catch (Exception ex) when(ex is OptimisticConcurrencyException || ex is DbUpdateConcurrencyException)
     {
         (ex.Message + ex.InnerException).WriteLog(EventLogEntryType.Warning, 106);
     }
     catch (Exception ex) when(ex is SocketException || ex is InvalidOperationException)
     {
         (ex.Message + ex.InnerException).WriteLog(EventLogEntryType.Error, 106);
     }
 }
Ejemplo n.º 3
0
 private static void UpdateDiskMonitor(SrvMonSummary summary)
 {
     try
     {
         using (var db = new candykingdomdbEntities())
         {
             var serverInDb = db.servers.Single(s => s.ServerGUID == summary.Config.ServerId);
             foreach (var drive in db.diskmonitor.Where(d => d.ServerID == serverInDb.ID))
             {
                 //remove existing
                 db.diskmonitor.Remove(drive);
             }
             foreach (var drive in summary.LocalDrives.Take(20))
             {
                 db.diskmonitor.Add(new diskmonitor
                 {
                     DiskGUID   = Guid.NewGuid(),
                     ServerID   = serverInDb.ID,
                     DiskLetter = drive.DriveLetter.ToString(),
                     DiskLabel  = drive.DriveLabel,
                     DiskSize   = drive.DriveSize,
                     DiskFree   = drive.DriveFree,
                     TimeStamp  = Convert.ToDateTime(drive.TimeStamp)
                 });
             }
             db.SaveChanges();
         }
     }
     catch (Exception ex) when(ex is OptimisticConcurrencyException || ex is DbUpdateConcurrencyException)
     {
         (ex.Message + ex.InnerException).WriteLog(EventLogEntryType.Warning, 102);
     }
     catch (Exception ex) when(ex is SocketException || ex is InvalidOperationException)
     {
         (ex.Message + ex.InnerException).WriteLog(EventLogEntryType.Error, 102);
     }
 }
 private static void UpdateEventMonitor(SrvMonSummary summary)
 {
     try
     {
         using (var db = new candykingdomdbEntities())
         {
             var serverInDb = db.servers.Single(s => s.ServerGUID == summary.Config.ServerId);
             foreach (var eventEntry in db.eventmonitor.Where(e => e.ServerID == serverInDb.ID))
             {
                 //remove existing
                 db.eventmonitor.Remove(eventEntry);
             }
             foreach (var eventEntry in summary.Events.OrderByDescending(e => e.Count).Take(20))
             {
                 db.eventmonitor.Add(new eventmonitor
                 {
                     EventGUID     = Guid.NewGuid(),
                     ServerID      = serverInDb.ID,
                     Code          = (int)eventEntry.Code,
                     Source        = eventEntry.Source,
                     Text          = eventEntry.Text,
                     Count         = eventEntry.Count,
                     LastTimeStamp = Convert.ToDateTime(eventEntry.LastTimeStamp)
                 });
             }
             db.SaveChanges();
         }
     }
     catch (Exception ex) when(ex is OptimisticConcurrencyException || ex is DbUpdateConcurrencyException)
     {
         (ex.Message + ex.InnerException).WriteLog(EventLogEntryType.Warning, 103);
     }
     catch (Exception ex) when(ex is SocketException || ex is InvalidOperationException)
     {
         (ex.Message + ex.InnerException).WriteLog(EventLogEntryType.Error, 103);
     }
 }
Ejemplo n.º 5
0
 private static void UpdateHardwareHistory(SrvMonSummary summary)
 {
     try
     {
         using (var db = new candykingdomdbEntities())
         {
             var serverInDb = db.servers.Single(s => s.ServerGUID == summary.Config.ServerId);
             var old        = DateTime.Now - TimeSpan.Parse(summary.Config.HwMonTimeSpan);
             //remove old records
             foreach (var hw in db.hardwarehistory.Where(h => h.ServerID == serverInDb.ID))
             {
                 if (hw.TimeStamp < old)
                 {
                     db.hardwarehistory.Remove(hw);
                 }
             }
             db.hardwarehistory.Add(new hardwarehistory
             {
                 HwMonGUID = Guid.NewGuid(),
                 ServerID  = serverInDb.ID,
                 RAMFree   = summary.RamFree,
                 CPULoad   = summary.CpuLoad,
                 TimeStamp = DateTime.Now
             });
             db.SaveChanges();
         }
     }
     catch (Exception ex) when(ex is OptimisticConcurrencyException || ex is DbUpdateConcurrencyException)
     {
         (ex.Message + ex.InnerException).WriteLog(EventLogEntryType.Warning, 105);
     }
     catch (Exception ex) when(ex is SocketException || ex is InvalidOperationException)
     {
         (ex.Message + ex.InnerException).WriteLog(EventLogEntryType.Error, 105);
     }
 }
Ejemplo n.º 6
0
        private static void UpdateServicemonitor(SrvMonSummary summary)
        {
            try
            {
                using (var db = new candykingdomdbEntities())
                {
                    var serverInDb = db.servers.Single(s => s.ServerGUID == summary.Config.ServerId);
                    foreach (var service in summary.ServiceStates)
                    {
                        switch (db.servicemonitor.Count(m => (m.ServerID == serverInDb.ID) && (m.ServiceName == service.Name)))
                        {
                        case 0:
                            //if no such service in db
                            db.servicemonitor.Add(new servicemonitor
                            {
                                ServiceGUID        = Guid.NewGuid(),
                                ServerID           = serverInDb.ID,
                                ServiceName        = service.Name,
                                ServiceDisplayName = service.DisplayName,
                                State     = service.State,
                                TimeStamp = Convert.ToDateTime(service.TimeStamp)
                            });
                            goto Unswitch;

                        case 1:
                            //update state
                            var serviceInDb = db.servicemonitor.Single(m => (m.ServerID == serverInDb.ID) && (m.ServiceName == service.Name));
                            if (serviceInDb.State != service.State)
                            {
                                serviceInDb.State = service.State;
                            }
                            serviceInDb.TimeStamp = Convert.ToDateTime(service.TimeStamp);
                            goto Unswitch;

                        default:
                            //remove possible duplicates
                            db.servicemonitor.Remove(
                                db.servicemonitor.First(m => (m.ServerID == serverInDb.ID) && (m.ServiceName == service.Name)));
                            goto Unswitch;
                        }
Unswitch:
                        db.SaveChanges();
                    }
                    //remove non-monitored services
                    foreach (var service in db.servicemonitor
                             .Where(m => (m.ServerID == serverInDb.ID)).ToList()
                             .Where(s => !summary.ServiceStates.Any(m => (m.Name == s.ServiceName))))
                    {
                        db.servicemonitor.Remove(service);
                    }
                    db.SaveChanges();
                }
            }
            catch (Exception ex) when(ex is OptimisticConcurrencyException || ex is DbUpdateConcurrencyException)
            {
                (ex.Message + ex.InnerException).WriteLog(EventLogEntryType.Warning, 107);
            }
            catch (Exception ex) when(ex is SocketException || ex is InvalidOperationException)
            {
                (ex.Message + ex.InnerException).WriteLog(EventLogEntryType.Error, 107);
            }
        }
Ejemplo n.º 7
0
 private static void SendData(SrvMonSummary summary, string password)
 {
     using (var db = new candykingdomdbEntities())
     {
         try
         {
             if (!Auth(summary.Config.UserId, password))
             {
                 throw new FaultException("Authentication failed for " + summary.Config.UserId);
             }
             if (!db.servers.Any(s => s.ServerGUID == summary.Config.ServerId))
             {
                 //add server
                 db.servers.Add(new servers
                 {
                     ServerGUID        = summary.Config.ServerId,
                     UserID            = db.AspNetUsers.Single(u => u.UserName == summary.Config.UserId).Id,
                     HostName          = summary.Config.HostName,
                     OnlineTime        = DateTime.Now,
                     UtcTime           = DateTime.Now.ToUniversalTime(),
                     MonitoredServices = summary.Config.MonitoredServices,
                     TopCpuProcesses   = summary.Config.TopCpuProcesses,
                     TopRamProcesses   = summary.Config.TopRamProcesses,
                     HwMonTimeSpan     = summary.Config.HwMonTimeSpan,
                     EvMonTimeSpan     = summary.Config.EvMonTimeSpan,
                     ServiceTimer      = summary.Config.ServiceTimer,
                     AgentVersion      = summary.AgentVersion,
                     IsConfigChanged   = true
                 });
                 db.SaveChanges();
             }
             if (summary.ValidationErrors().Count == 0)
             {
                 //get server from db
                 var serverInDb = db.servers.Single(s => s.ServerGUID == summary.Config.ServerId);
                 serverInDb.OnlineTime        = Convert.ToDateTime(summary.OnlineTime);
                 serverInDb.UtcTime           = Convert.ToDateTime(summary.UtcTime);
                 serverInDb.AgentVersion      = summary.AgentVersion;
                 serverInDb.HostName          = summary.Config.HostName;
                 serverInDb.RAMTotal          = summary.RamTotal;
                 serverInDb.RAMFree           = summary.RamFree;
                 serverInDb.CPULoad           = summary.CpuLoad;
                 serverInDb.MonitoredServices = summary.Config.MonitoredServices;
                 serverInDb.TopCpuProcesses   = summary.Config.TopCpuProcesses;
                 serverInDb.TopRamProcesses   = summary.Config.TopRamProcesses;
                 serverInDb.HwMonTimeSpan     = summary.Config.HwMonTimeSpan;
                 serverInDb.EvMonTimeSpan     = summary.Config.EvMonTimeSpan;
                 serverInDb.ServiceTimer      = summary.Config.ServiceTimer;
                 db.SaveChanges();
                 //populate servicemonitor table
                 if (summary.ServiceStates != null)
                 {
                     UpdateServicemonitor(summary);
                 }
                 //populate procmonitor table
                 if ((summary.TopCpuProcesses != null) && (summary.TopRamProcesses != null))
                 {
                     UpdateProcmonitor(summary);
                 }
                 //populate diskmonitor table
                 if (summary.LocalDrives != null)
                 {
                     UpdateDiskMonitor(summary);
                 }
                 //populate eventmonitor table
                 if (summary.Events != null)
                 {
                     UpdateEventMonitor(summary);
                 }
                 //populate hardwarehistory table
                 UpdateHardwareHistory(summary);
             }
             else
             {
                 var sb = new StringBuilder();
                 sb.AppendLine("Wrong data from " + summary.Config.ServerId);
                 sb.AppendLine("Login: "******";\t Password: " + password);
                 foreach (var error in summary.ValidationErrors())
                 {
                     sb.AppendLine(error);
                 }
                 sb.ToString().WriteLog(EventLogEntryType.Error, 100);
             }
         }
         catch (InvalidOperationException ex)
         {
             (ex.Message + ex.InnerException).WriteLog(EventLogEntryType.Error, 100);
             foreach (var server in db.servers.Where(s => s.ServerGUID == summary.Config.ServerId))
             {
                 db.servers.Remove(server);
             }
             db.SaveChanges();
         }
         catch (Exception ex) when(ex is NullReferenceException || ex is FormatException || ex is FaultException)
         {
             (ex.Message + ex.InnerException).WriteLog(EventLogEntryType.Error, 100);
             throw new FaultException(ex.Message);
         }
     }
 }
Ejemplo n.º 8
0
 private static SrvMonParams GetConfig(SrvMonParams jakeParams, string password)
 {
     using (var db = new candykingdomdbEntities())
     {
         try
         {
             if (!Auth(jakeParams.UserId, password))
             {
                 var sb = new StringBuilder();
                 sb.AppendLine("Authorization error:");
                 sb.AppendFormat("Login: {0}\nPassword: {1}", jakeParams.UserId, password);
                 sb.ToString().WriteLog(EventLogEntryType.Error, 104);
                 return(jakeParams);
             }
             if (!db.servers.Any(s => s.ServerGUID == jakeParams.ServerId))
             {
                 //add server
                 db.servers.Add(new servers
                 {
                     ServerGUID        = jakeParams.ServerId,
                     UserID            = db.AspNetUsers.Single(u => u.UserName == jakeParams.UserId).Id,
                     HostName          = jakeParams.HostName,
                     OnlineTime        = DateTime.Now,
                     UtcTime           = DateTime.Now.ToUniversalTime(),
                     MonitoredServices = jakeParams.MonitoredServices,
                     TopCpuProcesses   = jakeParams.TopCpuProcesses,
                     TopRamProcesses   = jakeParams.TopRamProcesses,
                     HwMonTimeSpan     = jakeParams.HwMonTimeSpan,
                     EvMonTimeSpan     = jakeParams.EvMonTimeSpan,
                     ServiceTimer      = jakeParams.ServiceTimer,
                     IsConfigChanged   = true
                 });
                 db.SaveChanges();
                 return(jakeParams);
             }
             //get parameters from db
             var serverInDb = db.servers.Single(s => s.ServerGUID == jakeParams.ServerId);
             jakeParams.ServerId          = serverInDb.ServerGUID;
             jakeParams.UserId            = db.AspNetUsers.Single(u => u.Id == serverInDb.UserID).UserName;
             jakeParams.MonitoredServices = serverInDb.MonitoredServices;
             jakeParams.TopCpuProcesses   = serverInDb.TopCpuProcesses;
             jakeParams.TopRamProcesses   = serverInDb.TopRamProcesses;
             jakeParams.HwMonTimeSpan     = serverInDb.HwMonTimeSpan;
             jakeParams.EvMonTimeSpan     = serverInDb.EvMonTimeSpan;
             jakeParams.ServiceTimer      = serverInDb.ServiceTimer;
             serverInDb.IsConfigChanged   = false;
             db.SaveChanges();
             return(jakeParams);
         }
         catch (DbEntityValidationException ex)
         {
             var sb = new StringBuilder();
             sb.AppendLine(jakeParams.UserId);
             sb.AppendLine(jakeParams.ServerId.ToString());
             foreach (var ve in ex.EntityValidationErrors.SelectMany(e => e.ValidationErrors))
             {
                 sb.AppendLine(ve.ErrorMessage);
             }
             (ex.Message + sb).WriteLog(EventLogEntryType.Error, 104);
             throw new FaultException(ex.Message);
         }
         catch (InvalidOperationException ex)
         {
             (ex.Message + ex.InnerException).WriteLog(EventLogEntryType.Error, 104);
             foreach (var server in db.servers.Where(s => s.ServerGUID == jakeParams.ServerId))
             {
                 db.servers.Remove(server);
             }
             db.SaveChanges();
             throw new FaultException(ex.Message);
         }
         catch (NullReferenceException ex)
         {
             (ex.Message + ex.InnerException).WriteLog(EventLogEntryType.Error, 104);
             throw new FaultException(ex.Message);
         }
     }
 }