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); } }
private static void Obfuscate(SystemConfiguration config) { SimpleEncryptDecrypt sed = new SimpleEncryptDecrypt(); // Before we write the configuration data to the database, let's make sure we obfuscate // the appropriate things. That would be the database connection strings, and the // device's passwords. foreach (ConfigurationData cd in config.configuration.Values) { try { if (cd.path.EndsWith("connection_string", StringComparison.InvariantCultureIgnoreCase)) { cd.value = sed.Encrypt(cd.value); } } catch (Exception) { } } foreach (DeviceInfo di in config.devices) { try { di.password = sed.Encrypt(di.password); } catch (Exception) { } } }
private static void SaveConfiguration(SystemConfiguration config, Database db) { int system_configuration_id = -1; string sql = "SELECT CollectorID FROM Collectors WHERE Name = 'System.Configuration' AND IsEnabled = 1;"; try { using (SQLiteConnection conn = db.Connection) { conn.Open(); using (SQLiteCommand command = new SQLiteCommand(sql, conn)) using (SQLiteDataReader reader = command.ExecuteReader()) { if (reader.Read()) { system_configuration_id = reader.GetInt32(0); } } } } catch (Exception ex) { ILog log = LogManager.GetLogger(typeof(SystemConfigurationStore)); log.Error("Initialize: " + sql); log.Error(ex); } if (system_configuration_id > 0) { DataCollectorContext dcc = new DataCollectorContext(new CollectorID(system_configuration_id, "System.Configuration"), ECollectorType.Configuration); Data d = new Data(dcc) { Value = JsonConvert.SerializeObject(config), }; DataStorage ds = new DataStorage(); ds.SaveData(new CollectedData(dcc, true, d), db); } else { ILog log = LogManager.GetLogger(typeof(SystemConfigurationStore)); log.Error("Error obtaining ID for System.Configuration collector"); } }
public void Merge(SystemConfiguration config) { HashSet <string> doomed = new HashSet <string>(configuration.Keys, StringComparer.OrdinalIgnoreCase); foreach (KeyValuePair <string, ConfigurationData> configpair in config.configuration) { doomed.Remove(configpair.Key); configuration[configpair.Key] = configpair.Value; } doomed.ToList().ForEach(c => configuration[c].deleted = true); doomed.Clear(); doomed.Concat(devices.ConvertAll <string>(d => d.name)); // Never remove the System device, or the device that is a Server. DeviceInfo system = devices.FirstOrDefault(d => d.type == DeviceType.System); doomed.Remove("System"); DeviceInfo server = devices.FirstOrDefault(d => d.type == DeviceType.Server); if (server != null) { doomed.Remove(server.name); } foreach (DeviceInfo new_device in config.devices) { // Don't allow the System to be imported if (new_device.type == DeviceType.System) { continue; } doomed.Remove(new_device.name); DeviceInfo old_device = devices.FirstOrDefault(d => string.Compare(d.name, new_device.name, true) == 0); DeviceInfo insert_device = new DeviceInfo(new_device.type) { DID = new DeviceID(-1, new_device.name) }; if (old_device != null) { devices.Remove(old_device); insert_device.DID = new DeviceID(old_device.DID.ID, new_device.name); } switch (new_device.type) { case DeviceType.Server: case DeviceType.System: insert_device.ipAddress = "0.0.0.0"; break; case DeviceType.Workstation: insert_device.ipAddress = new_device.ipAddress; insert_device.username = new_device.username; insert_device.password = new_device.password; break; case DeviceType.Camera: case DeviceType.RPM: case DeviceType.Generic: case DeviceType.Unknown: default: insert_device.ipAddress = new_device.ipAddress; insert_device.username = insert_device.password = string.Empty; break; } foreach (CollectorInfo insert_ci in insert_device.collectors) { CollectorInfo new_ci = new_device.collectors.FirstOrDefault(nc => nc.collectorType == insert_ci.collectorType); if (new_ci != null) { insert_ci.Merge(new_ci); } } devices.Add(insert_device); } foreach (string name in doomed) { DeviceInfo doomed_device = devices.FirstOrDefault(d => d.name == name); if (doomed_device != null) { doomed_device.deleted = true; } } }
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); }