Exemple #1
0
        public void Startup()
        {
            if (m_thread != null)
            {
                throw new Exception("Startup: Starting while already running");
            }

            logging.EventLog elog = new ApplicationEventLog();

            elog.LogInformation("Starting");
            GlobalIsRunning.IsRunning = true;
            m_shutdown.Reset();

            elog.LogInformation("Initializing database");
            Stopwatch watch = Stopwatch.StartNew();
            Database  db    = new Database();

            new Initializer(null).Initialize(db);
            elog.LogInformation($"Database initialization took {watch.ElapsedMilliseconds} ms");

            using (SQLiteConnection conn = db.Connection)
            {
                conn.Open();

                db.Attribute attr = new db.Attribute();
                attr.Set("service.startup_time", DateTimeOffset.Now.ToString("o"), conn);
                string assembly_ver = Assembly.GetExecutingAssembly().GetName().Version.ToString();
                attr.Set("software.version", assembly_ver, conn);
            }

            elog.LogInformation("Setting up responders");
            m_responders.ForEach(r => RequestBus.Instance.Subscribe(r));

            elog.LogInformation("Starting web server");
            watch.Restart();
            m_host.Start();
            elog.LogInformation($"Web server startup took {watch.ElapsedMilliseconds} ms");

            elog.LogInformation("Starting work thread");
            watch.Restart();
            m_thread = new Thread(new ThreadStart(ThreadFunc));
            m_thread.Start();
            // Wait for the thread to start
            while (!m_thread.IsAlive)
            {
                Thread.Sleep(25);
            }
            elog.LogInformation($"Work thread startup took {watch.ElapsedMilliseconds} ms");

            elog.LogInformation("Completed startup");
        }
Exemple #2
0
        public static void Set(SystemConfiguration config, DateTimeOffset timestamp, Database db)
        {
            try
            {
                using (SQLiteConnection conn = db.Connection)
                {
                    conn.Open();

                    HandleConfiguration(config.configuration, timestamp, conn);
                    HandleDevices(config.devices, timestamp, conn);
                    HandleGroups(config.groups, conn);
                    StoreMonitoredDrives(config.devices, conn);

                    Attribute attr = new Attribute();
                    attr.Set("configuration.last_update", timestamp.ToString("o"), conn);
                }

                Obfuscate(config);
                SaveConfiguration(config, db);

                // When the configuration changes, we want to do an immediate ping so we can determine if any
                // new devices are online/offline
                CollectPingNow(db);
            }
            catch (Exception ex)
            {
                ILog log = LogManager.GetLogger(typeof(SystemConfigurationStore));
                log.Error("SetSystemConfiguration");
                log.Error(ex);
            }
        }
Exemple #3
0
        public void Shutdown()
        {
            logging.EventLog elog = new ApplicationEventLog();

            elog.LogInformation("Stopping");
            Stopwatch watch = Stopwatch.StartNew();

            GlobalIsRunning.IsRunning = false;

            m_shutdown.Set();
            while (m_thread != null && m_thread.ThreadState == System.Threading.ThreadState.Running)
            {
                Thread.Sleep(100);
            }

            elog.LogInformation($"Stopping worker thread took {watch.ElapsedMilliseconds} ms");

            Database db = new Database();

            using (SQLiteConnection conn = db.Connection)
            {
                conn.Open();
                db.Attribute attr = new db.Attribute();
                attr.Set("service.stop_time", DateTimeOffset.Now.ToString("o"), conn);
            }

            elog.LogInformation("Stopping web server");
            m_host.Stop();
            m_host.Dispose();

            elog.LogInformation("Clearing responders");
            m_responders.ForEach(r => RequestBus.Instance.Unsubscribe(r));

            m_thread = null;
            elog.LogInformation("Completed stopping");
        }
Exemple #4
0
        public void Initialize(Database db)
        {
            ILog log = LogManager.GetLogger(typeof(Initializer));

            log.Debug("Initializing DB");

            try
            {
                using (SQLiteConnection conn = db.Connection)
                {
                    conn.Open();

                    CreateTables(conn);
                    InitializeTypeTables(conn);
                    AddMissingColumns(conn);

                    // Don't create the indices until after the missing columns have been added since
                    // some of those new columns want indices.
                    CreateIndices(conn);

                    if (Options.Contains(EOptions.SkipSystemCreation) == false)
                    {
                        // Make sure there's always a System device with a Configuration collector. This is so we can
                        // always collect the configuration data when it comes in.
                        DeviceInfo system = db.GetDevice("System", conn);
                        if (system == null)
                        {
                            // No System device exists, so we want to add it and add a Configuration collector
                            system = new DeviceInfo(EDeviceType.System)
                            {
                                name = "System"
                            };
                            db.AddDevice(system, DateTimeOffset.Now, conn);
                        }
                        else
                        {
                            // One exists...see if the Configuration collector is there
                            CollectorInfo config_info = system.collectors.FirstOrDefault(c => c.collectorType == ECollectorType.Configuration);
                            if (config_info == null)
                            {
                                // Nope. Add it.
                                config_info = new CollectorInfo(ECollectorType.Configuration);
                                system.collectors.Add(config_info);
                                Database.UpdateCollectors(system.id, "System", string.Empty, system.collectors, DateTimeOffset.Now, conn);
                            }
                        }

                        // And make sure there's a server device is there as well
                        List <DeviceInfo> servers = db.GetDevicesFromType(EDeviceType.Server, conn);
                        if (servers.Count == 0)
                        {
                            DeviceInfo server = new DeviceInfo(EDeviceType.Server)
                            {
                                name = "Server", ipAddress = "localhost"
                            };
                            db.AddDevice(server, DateTimeOffset.Now, conn);
                        }
                    }

                    // We changed the "monitored.drives.descriptions" attributes so they are now "all.drives.descriptions" since
                    // all of the drives descriptions are kept there instead of just the few that are being monitored. So, let's rename
                    // these attributes.
                    Attribute attr = new Attribute();
                    Dictionary <string, string> d = attr.GetMultiple("monitored.drives.descriptions", conn);
                    foreach (string path in d.Keys)
                    {
                        attr.Clear(path, conn);
                        string path2 = path.Replace("monitored.", "all.");
                        attr.Set(path2, d[path], conn);
                    }

                    // Initialize the last configuration update time if it doesn't exist
                    DateTimeOffset?first_config = db.GetLastConfigurationUpdateAttribute(conn);
                    if (first_config == null)
                    {
                        first_config = DateTimeOffset.Now;

                        attr = new Attribute();
                        attr.Set("configuration.last_update", first_config.Value, conn);
                    }

                    // Doing this will ensure that the default configurations will be put in the Configuration table
                    foreach (Configuration c in Configuration.Configs)
                    {
                        string v = c.GetValue(conn);
                    }
                }
            }
            catch (Exception e)
            {
                log.Error("Error in Database Initializer");
                log.Error(e);
            }
        }