예제 #1
0
파일: Models.cs 프로젝트: gaybro8777/common
 public DeviceInfo(DeviceType type)
 {
     m_device_id     = new DeviceID(-1, string.Empty);
     this.type       = type;
     ipAddress       = "0.0.0.0";
     username        = password = string.Empty;
     deleted         = false;
     collectors      = CollectorInfo.FromCollectorTypes(type.GetCollectors());
     driveNames      = new Dictionary <string, string>();
     monitoredDrives = new MonitoredDriveManager();
     groupID         = -1;
 }
예제 #2
0
        public CollectorInfo CollectNow(long collector_id)
        {
            CollectorInfo collector_info = null;

            try
            {
                Updater updater = new Updater("Collectors", $"CollectorID = {collector_id}", Conn);

                // Set the time to null so it will rise to the top of the to-do list.
                updater.SetNull("NextCollectionTime");
                updater.Execute();

                collector_info = GetCollectorInfo(collector_id);
            }
            catch (Exception e)
            {
                logging.EventLog log = new ApplicationEventLog();
                log.LogInformation($"Error in CollectNow({collector_id})");
                log.Log(e);
            }

            return(collector_info);
        }
예제 #3
0
        private CollectorInfo GetCollectorInfo(long collector_id)
        {
            CollectorInfo collector_info = null;
            string        sql            = $"SELECT Name, DeviceID, CollectorType, IsEnabled, FrequencyInMinutes, LastCollectionAttempt, LastCollectedAt, NextCollectionTime, SuccessfullyCollected, CurrentlyBeingCollected FROM Collectors WHERE CollectorID = {collector_id};";

            using (SQLiteCommand command = new SQLiteCommand(sql, Conn))
                using (SQLiteDataReader reader = command.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        CollectorInfo c = new CollectorInfo((ECollectorType)reader.GetInt32(2));
                        c.CID                = new data.CollectorID(collector_id, reader.GetString(0));
                        c.isEnabled          = reader.GetInt32(3) != 0;
                        c.frequencyInMinutes = reader.GetInt32(4);

                        if (reader.IsDBNull(5) == false)
                        {
                            c.lastCollectionAttempt = DateTimeOffset.Parse(reader.GetString(5));
                        }
                        if (reader.IsDBNull(6) == false)
                        {
                            c.lastCollectedAt = DateTimeOffset.Parse(reader.GetString(6));
                        }
                        if (reader.IsDBNull(7) == false)
                        {
                            c.nextCollectionTime = DateTimeOffset.Parse(reader.GetString(7));
                        }
                        c.successfullyCollected = reader.GetInt32(8) != 0;
                        c.isBeingCollected      = reader.GetInt32(9) == 1;

                        collector_info = c;
                    }
                }

            return(collector_info);
        }
예제 #4
0
파일: Models.cs 프로젝트: gaybro8777/common
        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;
                }
            }
        }
예제 #5
0
파일: Models.cs 프로젝트: gaybro8777/common
 public void Merge(CollectorInfo other)
 {
     frequencyInMinutes = other.frequencyInMinutes;
     isEnabled          = other.isEnabled;
 }
예제 #6
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);
            }
        }