public HealthData CheckHealth() { HealthData healthData = new HealthData(_appName); try { ConcurrentBag <HealthData> healthDataCollection = new ConcurrentBag <HealthData>(); _healthDependencyCollection.AsParallel().ForAll(dep => { healthDataCollection.Add(CheckDependency(dep)); }); healthData.DependenciesStatus.AddRange(healthDataCollection); healthData.IsOK = true; } catch (Exception ex) { OnDependencyException?.Invoke(this, new DependencyExceptionEvent(ex)); healthData.IsOK = false; healthData.ErrorMessage = "HealthChecker crashed."; } return(healthData); }
public HealthData CheckHealth() { HealthData directoryHealthData = new HealthData(_windowsPrincipal.Identity.Name); directoryHealthData.Type = "DirectoryPermission"; try { foreach (string directoryPath in _fileSystemRigths.Keys) { HealthData directoryHealth = CheckFileRigthPermissions(directoryPath, _fileSystemRigths[directoryPath]); directoryHealthData.DependenciesStatus.Add(directoryHealth); } directoryHealthData.IsOK = true; } catch (Exception ex) { OnDependencyException?.Invoke(this, new DependencyExceptionEvent(ex)); directoryHealthData.IsOK = false; directoryHealthData.ErrorMessage = ex.Message; } return(directoryHealthData); }
private HealthData CheckFileRigthPermissions(string directoryPath, ICollection <FileSystemRights> fileSystemRights) { HealthData tableHealth = new HealthData(directoryPath); try { foreach (FileSystemRights fileSystemRight in fileSystemRights) { HealthData tablePermissionHealth = new HealthData(fileSystemRight.ToString()); tablePermissionHealth.IsOK = CheckUserPermission(directoryPath, fileSystemRight); if (tablePermissionHealth.IsOK == false) { tablePermissionHealth.ErrorMessage = "Does not have permission."; } tableHealth.DependenciesStatus.Add(tablePermissionHealth); } tableHealth.IsOK = true; } catch (Exception ex) { OnDependencyException?.Invoke(this, new DependencyExceptionEvent(ex)); tableHealth.ErrorMessage = ex.Message; tableHealth.IsOK = false; } return(tableHealth); }
public HealthData CheckHealth() { HealthData healthData = new HealthData($"{_hostname}:{_port} [{_serviceName ?? ""}]"); healthData.Type = "Telnet"; try { using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp)) { socket.Connect(_hostname, _port); if (socket.Connected) { healthData.IsOK = true; } } } catch (Exception ex) { OnDependencyException?.Invoke(this, new DependencyExceptionEvent(ex)); healthData.IsOK = false; healthData.ErrorMessage = ex.Message; } return(healthData); }
public HealthData CheckTablePermissions(string tableName, ICollection <TablePermission> permissions) { HealthData tableHealth = new HealthData(tableName); try { foreach (TablePermission permission in permissions) { HealthData tablePermissionHealth = new HealthData(permission.Permission.ToString()); tablePermissionHealth.IsOK = this.CheckPermission(permission); if (tablePermissionHealth.IsOK == false) { tablePermissionHealth.ErrorMessage = "Does not have permission."; } tableHealth.DependenciesStatus.Add(tablePermissionHealth); } tableHealth.IsOK = true; } catch (Exception ex) { OnDependencyException?.Invoke(this, new DependencyExceptionEvent(ex)); tableHealth.ErrorMessage = ex.Message; tableHealth.IsOK = false; } return(tableHealth); }
private bool CheckUserPermission(string directoryPath, FileSystemRights accessRights) { var isInRoleWithAccess = false; try { var di = new DirectoryInfo(directoryPath); if (di.Exists == false) { throw new DirectoryNotFoundException($"Directory {directoryPath} not found."); } var acl = di.GetAccessControl(); var rules = acl.GetAccessRules(true, true, typeof(NTAccount)); var principal = _windowsPrincipal; foreach (AuthorizationRule rule in rules) { var fsAccessRule = rule as FileSystemAccessRule; if (fsAccessRule == null) { continue; } if ((fsAccessRule.FileSystemRights & accessRights) > 0) { var ntAccount = rule.IdentityReference as NTAccount; if (ntAccount == null) { continue; } if (principal.IsInRole(ntAccount.Value)) { if (fsAccessRule.AccessControlType == AccessControlType.Deny) { return(false); } isInRoleWithAccess = true; } } } } catch (UnauthorizedAccessException ex) { OnDependencyException?.Invoke(this, new DependencyExceptionEvent(ex)); return(false); } return(isInRoleWithAccess); }
private HealthData CheckDependency(IHealthDependency dependency) { try { dependency.OnDependencyException += (o, e) => { OnDependencyException?.Invoke(this, e); }; return(dependency.CheckHealth()); } catch (Exception ex) { OnDependencyException?.Invoke(this, new DependencyExceptionEvent(ex)); throw; } }
public HealthData CheckHealth() { _dbConnection = this.GetConnection(); HealthData sqlHealthData = new HealthData(_databaseName); sqlHealthData.Type = _type; try { using (_dbConnection) { if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); } sqlHealthData = new HealthData(_dbConnection.Database); sqlHealthData.Type = _type; foreach (string tableName in _permissions.Keys) { HealthData tableHealth = CheckTablePermissions(tableName, _permissions[tableName]); sqlHealthData.DependenciesStatus.Add(tableHealth); } foreach (Index ix in _indexes) { HealthData indexHealth = this.CheckIndex(ix); sqlHealthData.DependenciesStatus.Add(indexHealth); } sqlHealthData.IsOK = true; } } catch (Exception ex) { OnDependencyException?.Invoke(this, new DependencyExceptionEvent(ex)); sqlHealthData.IsOK = false; sqlHealthData.ErrorMessage = ex.Message; } return(sqlHealthData); }
public void InvokeDependencyException(Exception ex) { OnDependencyException?.Invoke(this, new DependencyExceptionEvent(ex)); }