Ejemplo n.º 1
0
        public void CollectFromApi()
        {
            try
            {
                var collectionStatus = MessageLevels.Ok;
                _notify("Starting Collecting from API", MessageLevels.Animate, "ApiCollector");
                using (var qmsApiService = new QMS_API.AgentsQmsApiService(_settings.QvSettings.QmsAddress))
                {
                    if (!qmsApiService.TestConnection())
                    {
                        _logger.Add("Could not connect to QMS API (" + _settings.QvSettings.QmsAddress + ")!");
                        _notify("Failed Collecting from API", MessageLevels.Error, "ApiCollector");
                        return;
                    }

                    var         services   = qmsApiService.GetServices();
                    List <Guid> serviceIDs = services.Select(t => t.ID).ToList();


                    //List<ServiceInfo> qvsServices = qmsApiService.GetServices(ServiceTypes.QlikViewServer);
                    _collectorHelper.WriteContentToFile(qmsApiService.GetServiceStatuses(serviceIDs), "QvServiceStatuses");
                    _collectorHelper.WriteContentToFile(services, "QvServices");

                    var qvServers = services.Where(p => p.Type == ServiceTypes.QlikViewServer | p.Type == ServiceTypes.QlikViewDistributionService).ToList();

                    _notify("Collecting Service info from API", MessageLevels.Animate, "ApiCollector");
                    qvServers.ForEach(p =>
                    {
                        if (p.Type == ServiceTypes.QlikViewServer)
                        {
                            QVSSettings settings = null;
                            try
                            {
                                settings = qmsApiService.GetQvsSettings(p.ID, QVSSettingsScope.All);

                                _collectorHelper.WriteContentToFile(settings, $"QvsSettings_{p.Name}");
                                _collectorHelper.WriteContentToFile(qmsApiService.GetCalConfiguration(p.ID, CALConfigurationScope.All), $"QvsCals_{p.Name}");
                                _collectorHelper.WriteContentToFile(qmsApiService.GetUserDocuments(p.ID), $"QvsUserDocuments_{p.Name}");
                                _collectorHelper.WriteContentToFile(qmsApiService.GetQvsDocumentsAndUsers(p.ID, QueryTarget.Resource), $"QvsDocumentsAndUsers_{p.Name}");
                            }
                            catch (Exception e)
                            {
                                _logger.Add($"Failed collecting API details from {p.Name ?? "UndefinedService"}", e);
                                _notify($"Failed collecting details from {p.Name}", MessageLevels.Warning, "ApiCollector");
                                collectionStatus = MessageLevels.Warning;
                            }

                            _settings.QvSettings.QvLogLocations.Add(new QvLogLocation
                            {
                                Name = $"Qvs settings - {p.Name}",
                                Type = QvLogLocationSource.QvsSetting,
                                Path = settings?.Logging?.Folder ?? "Failed retrival"
                            });

                            _settings.QvSettings.QvLogLocations.Add(new QvLogLocation
                            {
                                Name = $"Qvs Root Folder - {p.Name}",
                                Type = QvLogLocationSource.QvsSetting,
                                Path = settings?.Folders?.UserDocumentRootFolder ?? "Failed retrival"
                            });
                        }

                        if (p.Type == ServiceTypes.QlikViewDistributionService)
                        {
                            var docs = GetAllDocumentsAndFolders(qmsApiService, p);
                            _collectorHelper.WriteContentToFile(docs, $"QvsDocumentsAndFolders_{p.Name}");

                            var qdsSettings = qmsApiService.GetQdsSettings(p.ID, QDSSettingsScope.All);
                            qdsSettings.General.ClusterInfo.ForEach(clusterInfo =>
                            {
                                var hostName = clusterInfo.Url.Host;
                                _settings.QvSettings.QvLogLocations.Add(new QvLogLocation
                                {
                                    Name = $"Programdata - QlikTech - {hostName}",
                                    Type = QvLogLocationSource.QdsClusterInfoUrl,
                                    Path = $"\\\\{hostName}\\c$\\ProgramData\\QlikTech"
                                });

                                _settings.QvSettings.QvLogLocations.Add(new QvLogLocation
                                {
                                    Name = $"ProgramFiles - QlikView - {hostName}",
                                    Type = QvLogLocationSource.QdsClusterInfoUrl,
                                    Path = $"\\\\{hostName}\\c$\\Program Files\\QlikView",
                                    LogCollectionType = QvLogCollectionType.SettingsOnly
                                });

                                _settings.QvSettings.QvLogLocations.Add(new QvLogLocation
                                {
                                    Name = $"CommonFiles - QlikTech - {hostName}",
                                    Type = QvLogLocationSource.QdsClusterInfoUrl,
                                    Path = $"\\\\{hostName}\\c$\\Program Files\\Common Files\\QlikTech",
                                    LogCollectionType = QvLogCollectionType.SettingsOnly
                                });
                            });
                            _settings.QvSettings.QvLogLocations.Add(new QvLogLocation {
                                Name              = $"QDS Application DataFolder - {p.Name}",
                                Type              = QvLogLocationSource.QdsSettingsApplicationDataFolder,
                                Path              = qdsSettings.General.ApplicationDataFolder,
                                IgnorePaths       = IgnoreQdsApplicationDataFolder,
                                LogCollectionType = QvLogCollectionType.SettingsOnly
                            });
                            _collectorHelper.WriteContentToFile(qdsSettings, $"QdsSettings_{p.Name}");
                        }
                        _collectorHelper.WriteContentToFile(qmsApiService.GetLicense(p.Type == ServiceTypes.QlikViewServer ? LicenseType.QlikViewServer : LicenseType.Publisher, p.ID), $"License_{p.Name}");
                    });
                    if (collectionStatus == MessageLevels.Ok)
                    {
                        _notify("Finished collectinging from API", MessageLevels.Ok, "ApiCollector");
                    }
                    else
                    {
                        _notify("Finished collectinging from API but some errors where found.", MessageLevels.Warning, "ApiCollector");
                    }

                    //Analyze(qmsApiService);
                }
            }
            catch (Exception e)
            {
                _logger.Add("Failed collecting from API", e);
                _notify("Failed collecting from API", MessageLevels.Error, "ApiCollector");
            }
        }
        public void Execute()
        {
            try
            {
                var data = new StatisticsDto();
                var cals = new List <QvCalAgentDto>();

                var qmsAddress = Settings.GetSetting($"{MonitorName}.QmsAddress", "(undefined)");
                if (qmsAddress.Equals("(undefined)", StringComparison.InvariantCultureIgnoreCase))
                {
                    qmsAddress = $"http://{(Dns.GetHostEntry(Dns.GetHostName()).HostName).ToLower()}:4799/QMS/Service";
                }

                WmiSystemInfoDto wmiData = new WmiSystemInfoDto();
                var exceptionList        = new List <Exception>();
                try
                {
                    wmiData = new WmiSystemInfo().GetValuesFromWin32Os();
                }
                catch (Exception e)
                {
                    data.Exceptions.Add(e);
                }

                using (var qmsApiService = new QMS_API.AgentsQmsApiService(qmsAddress))
                {
                    if (!qmsApiService.TestConnection())
                    {
                        Log.To.Main.Add($"Could not connect to QMS API ({qmsAddress})!");
                        return;
                    }

                    var services       = qmsApiService.GetServices(ServiceTypes.QlikViewServer | ServiceTypes.QlikViewDistributionService);
                    var qvServers      = services.Where(p => p.Type == ServiceTypes.QlikViewServer).ToList();
                    var installationId = qvServers.OrderBy(p => p.ID).First().ID.ToString();
                    qvServers.ForEach(p =>
                    {
                        if (p.Type == ServiceTypes.QlikViewServer)
                        {
                            var calConfigurations = new[]
                            {
                                qmsApiService.GetCalConfiguration(p.ID, CALConfigurationScope.DocumentCALs),
                                qmsApiService.GetCalConfiguration(p.ID, CALConfigurationScope.NamedCALs),
                                qmsApiService.GetCalConfiguration(p.ID, CALConfigurationScope.SessionCALs),
                                qmsApiService.GetCalConfiguration(p.ID, CALConfigurationScope.UsageCALs)
                            };
                            cals.Add(_licenceHelper.ComputeCals(p, calConfigurations));
                        }

                        var license          = qmsApiService.GetLicense(p.Type == ServiceTypes.QlikViewServer ? LicenseType.QlikViewServer : LicenseType.Publisher, p.ID);
                        data.InstallationId  = $"{license?.Serial ?? "(unknown)"}_{installationId} ";
                        data.WmiSystemInfo   = wmiData;
                        data.QlikViewLicence = _licenceHelper.AnalyzeLicense(license);
                        data.QlikViewCals    = cals;
                        data.Exceptions      = exceptionList;

                        Notify($"{MonitorName} has analyzed the following system", new List <string> {
                            JsonConvert.SerializeObject(data, Formatting.Indented)
                        }, "-1");
                    });
                }
            }
            catch (Exception ex)
            {
                Log.To.Main.AddException($"Failed executing {MonitorName}", ex);
            }
        }