Пример #1
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);
            }
        }
Пример #2
0
        public static SystemConfiguration Get(bool obfuscate, SQLiteConnection conn)
        {
            SystemConfiguration config = new SystemConfiguration();
            string sql = string.Empty;
            ILog   log = LogManager.GetLogger(typeof(SystemConfigurationStore));

            try
            {
                Dictionary <string, string> monitoredDrives = null;
                SimpleEncryptDecrypt        sed             = new SimpleEncryptDecrypt();

                sql = "SELECT ConfigurationID, Path, Value FROM Configuration WHERE IsValid = 1;";
                using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string path  = reader.GetString(1);
                            string value = reader.IsDBNull(2) ? "" : reader.GetString(2);
                            if (path.EndsWith("connection_string", StringComparison.InvariantCultureIgnoreCase))
                            {
                                if (obfuscate)
                                {
                                    value = "*****";
                                }
                                else
                                {
                                    value = sed.Decrypt(value);
                                }
                            }

                            config.configuration[path] = new ConfigurationData()
                            {
                                configID = reader.GetInt32(0),
                                path     = path,
                                value    = value
                            };

                            if (string.Compare(path, "languages", true) == 0)
                            {
                                Languages lang = new Languages();
                                lang.EnableFromDelimitedString(value);
                                foreach (Language l in lang.All)
                                {
                                    config.languages.Add(new LanguageConfiguration()
                                    {
                                        languageCode = l.GetDescription(), language = l.ToString(), isEnabled = lang.IsEnabled(l)
                                    });
                                }
                            }
                        }
                    }

                Attribute attr = new Attribute();
                config.softwareVersion = attr.Get("software.version", conn);
                if (config.softwareVersion == null)
                {
                    config.softwareVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
                }
                monitoredDrives = attr.GetMultiple("all.drives.descriptions", conn);
                List <string> device_names = monitoredDrives.Keys.ToList();
                foreach (string name in device_names)
                {
                    string[] n = name.Split('.');
                    if (n.Length > 0)
                    {
                        monitoredDrives.ChangeKey(name, n[0]);
                    }
                }

                sql = "SELECT GroupID, Name FROM DeviceGroups";
                using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Group g = new Group()
                            {
                                id = reader.GetInt32(0), name = reader.GetString(1)
                            };
                            config.groups.Add(g);
                        }
                    }

                config.devices = new Database().GetDevices(conn);

                if (monitoredDrives != null)
                {
                    foreach (DeviceInfo dev in config.devices)
                    {
                        string drive_info;
                        if (monitoredDrives.TryGetValue(dev.name, out drive_info))
                        {
                            try
                            {
                                dev.driveNames = JsonConvert.DeserializeObject <Dictionary <string, string> >(drive_info);
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                }

                LoadMonitoredDrives(config.devices, conn);

                // Get the most recent data that has been recorded
                sql = "SELECT Timestamp FROM Data ORDER BY Timestamp DESC LIMIT 1;";
                using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            try
                            {
                                DateTimeOffset ts = DateTimeOffset.Parse(reader.GetString(0));
                                config.mostRecentData = ts;
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
            }
            catch (Exception ex)
            {
                log.Error("GetSystemConfiguration: " + sql);
                log.Error(ex);
            }

            log.Debug("Configuration: \n" + JsonConvert.SerializeObject(config.devices, Formatting.Indented));

            return(config);
        }