示例#1
0
        static async Task DoJobAsync()
        {
            if (Application.MQTTCLIENT.IsConnected == false)
            {
                ConsoleLogger.Log($"[mqtt] mqtt server is unreachable");
                return;
            }
            var servicesNames = new List <string>();
            var antdUnits     = Directory.EnumerateFiles("/mnt/cdrom/Units/antd.target.wants").ToArray();

            for (var i = 0; i < antdUnits.Length; i++)
            {
                servicesNames.Add(Path.GetFileName(antdUnits[i]));
            }
            var applicativeUnits = Directory.EnumerateFiles("/mnt/cdrom/Units/applicative.target.wants").ToArray();

            for (var i = 0; i < applicativeUnits.Length; i++)
            {
                servicesNames.Add(Path.GetFileName(applicativeUnits[i]));
            }
            var arrServicesNames = servicesNames.ToArray();
            var services         = new ServiceStatus[arrServicesNames.Length];

            for (var i = 0; i < arrServicesNames.Length; i++)
            {
                services[i] = new ServiceStatus {
                    Name     = arrServicesNames[i],
                    IsActive = cmds.Systemctl.IsActive(arrServicesNames[i])
                };
            }

            var du  = DiskUsage.Get();
            var dus = new DiskUsageStatus[du.Length];

            for (var i = 0; i < du.Length; i++)
            {
                dus[i] = new DiskUsageStatus {
                    Device = du[i].MountedOn,
                    Used   = int.Parse(du[i].UsePercentage.Replace("%", ""))
                };
            }

            var free  = Free.Get();
            var frees = new FreeStatus[free.Length];

            for (var i = 0; i < free.Length; i++)
            {
                frees[i] = new FreeStatus()
                {
                    Name      = free[i].Name,
                    Total     = ParseInt(free[i].Total),
                    Used      = ParseInt(free[i].Used),
                    Free      = ParseInt(free[i].Free),
                    Shared    = ParseInt(free[i].Shared),
                    BuffCache = ParseInt(free[i].BuffCache),
                    Available = ParseInt(free[i].Available)
                };
            }

            var status = new CurrentMachineStatus()
            {
                PublicIp  = PublicIp.Get(),
                MachineId = Application.MACHINE_ID,
                Date      = new DateStatus {
                    Date1 = DateTime.Now,
                    Date2 = Date.Get()
                },
                Free      = frees,
                DiskUsage = dus,
                Services  = services
            };
            var converted = MSG.Serialize(status);
            var message   = new MqttApplicationMessageBuilder()
                            .WithTopic("/status")
                            .WithPayload(converted)
                            .WithExactlyOnceQoS()
                            .WithRetainFlag()
                            .Build();
            await Application.MQTTCLIENT.PublishAsync(message);

            //WriteStatus(status);
        }
示例#2
0
        /// <summary>
        /// Ottiene un Master valorizzato coi valori ottenuti direttamente dal sistema operativo
        /// Potrebbe non corrispondere al Master salvato
        /// Viene utilizzato per ottenere le differenze tra i parametri salvati e quelli effettivamente applicati
        /// </summary>
        /// <returns></returns>
        public static MachineStatus GetRunning()
        {
            //var STOPWATCH = new System.Diagnostics.Stopwatch();
            //STOPWATCH.Start();
            var master = new MachineStatus();

            master.Info.Uptime    = Uptime.Get();
            master.Info.CpuInfo   = CpuInfo.Get();
            master.Info.MemInfo   = MemInfo.Get();
            master.Info.Free      = Free.Get();
            master.Info.Losetup   = Losetup.Get();
            master.Info.DiskUsage = DiskUsage.Get();
            master.Info.Versions  = Versioning.Get();

            master.Host     = Hostnamectl.Get();
            master.TimeDate = new TimeDate()
            {
                Timezone = Timedatectl.Get().Timezone
            };
            master.Boot = new Boot();
            var modules     = Mod.Get();
            var bootModules = new SystemModule[modules.Length];

            for (var i = 0; i < modules.Length; i++)
            {
                bootModules[i] = new SystemModule()
                {
                    Module = modules[i].Module, Active = true
                };
            }
            master.Boot.Modules           = bootModules;
            master.Boot.Services          = cmds.Systemctl.GetAll();
            master.Boot.Parameters        = Sysctl.Get();
            master.Users.SystemUsers      = Passwd.Get();
            master.Users.ApplicativeUsers = new ApplicativeUser[] { new ApplicativeUser()
                                                                    {
                                                                        Active = true, Type = AuthenticationType.simple, Id = "master", Claims = new[] { SHA.Generate("master") }
                                                                    } };

            master.Network.KnownDns          = Dns.GetResolv();
            master.Network.KnownHosts        = Dns.GetHosts();
            master.Network.KnownNetworks     = Dns.GetNetworks();
            master.Network.Bridges           = Brctl.Get();
            master.Network.Bonds             = Bond.Get();
            master.Network.NetworkInterfaces = cmds.Network.Get();
            master.Network.RoutingTables     = Route.GetRoutingTable();
            master.Network.Routing           = Route.Get();

            master.NsSwitch = NS.Switch.Get();

            master.Storage.Mounts       = Mount.Get();
            master.Storage.Zpools       = Zpool.GetPools();
            master.Storage.ZfsDatasets  = Zfs.GetDatasets();
            master.Storage.ZfsSnapshots = Zfs.GetSnapshots();

            master.Services.Ssh.PublicKey     = Ssh.GetRootPublicKey();
            master.Services.Ssh.PrivateKey    = Ssh.GetRootPrivateKey();
            master.Services.Ssh.AuthorizedKey = Ssh.GetAuthorizedKey();

            master.Services.Virsh.Domains = Virsh.GetDomains();

            //ConsoleLogger.Log($"[conf] loaded running conf ({STOPWATCH.ElapsedMilliseconds})");
            return(master);
        }