comparison ServerMonitor/Objects/Server.cs @ 4:3142e52cbe69

Lots more progress
author Brad Greco <brad@bgreco.net>
date Sun, 10 Feb 2019 20:51:26 -0500
parents 3e1a2131f897
children b6fe203af9d5
comparison
equal deleted inserted replaced
3:96f0b028176d 4:3142e52cbe69
17 private int _port; 17 private int _port;
18 private string _username; 18 private string _username;
19 private LoginType _loginType; 19 private LoginType _loginType;
20 private string _keyFile; 20 private string _keyFile;
21 private SshClient _sshClient; 21 private SshClient _sshClient;
22 private bool _enabled = true;
22 private byte[] passwordHash; 23 private byte[] passwordHash;
23 24
24 public event EventHandler CheckModified; 25 public event EventHandler CheckModified;
26 public event EventHandler EnabledChanged;
25 27
26 public readonly BindingList<Check> Checks = new BindingList<Check>(); 28 public readonly BindingList<Check> Checks = new BindingList<Check>();
27 29
28 public int Id { get; set; } 30 public int Id { get; set; }
29 31
71 Encoding.UTF8.GetBytes("Server".Reverse().ToString()), // Minor obfuscation of additional entropy 73 Encoding.UTF8.GetBytes("Server".Reverse().ToString()), // Minor obfuscation of additional entropy
72 DataProtectionScope.CurrentUser); 74 DataProtectionScope.CurrentUser);
73 } 75 }
74 } 76 }
75 77
76 public bool Enabled { get; set; } = true; 78 public bool Enabled
79 {
80 get { return _enabled; }
81 set { _enabled = value; EnabledChanged?.Invoke(this, new EventArgs()); }
82 }
83
84 public CheckStatus Status => !Enabled ? CheckStatus.Disabled : Checks
85 .Where(c => c.Enabled)
86 .Select(c => c.LastRunStatus)
87 .DefaultIfEmpty(CheckStatus.Success)
88 .Max();
77 89
78 public SshClient SshClient 90 public SshClient SshClient
79 { 91 {
80 get 92 get
81 { 93 {
83 { 95 {
84 AuthenticationMethod auth = null; 96 AuthenticationMethod auth = null;
85 if (LoginType == LoginType.Password) 97 if (LoginType == LoginType.Password)
86 auth = new PasswordAuthenticationMethod(Username, Password); 98 auth = new PasswordAuthenticationMethod(Username, Password);
87 else 99 else
88 new PrivateKeyAuthenticationMethod(Username, new PrivateKeyFile(KeyFile)); 100 auth = new PrivateKeyAuthenticationMethod(Username, new PrivateKeyFile(KeyFile));
89 ConnectionInfo info = new ConnectionInfo(Host, Port, Username, auth); 101 ConnectionInfo info = new ConnectionInfo(Host, Port, Username, auth);
90 _sshClient = new SshClient(info); 102 _sshClient = new SshClient(info);
91 } 103 }
92 return _sshClient; 104 return _sshClient;
93 } 105 }
135 Checks[index] = check; 147 Checks[index] = check;
136 } 148 }
137 CheckModified?.Invoke(check, new EventArgs()); 149 CheckModified?.Invoke(check, new EventArgs());
138 } 150 }
139 151
152 public bool IsEmpty()
153 {
154 return Name.IsNullOrEmpty()
155 && Host.IsNullOrEmpty()
156 && Checks.Count == 0;
157 }
158
140 private void InvalidateSshConnection() 159 private void InvalidateSshConnection()
141 { 160 {
142 _sshClient?.Dispose(); 161 _sshClient?.Dispose();
143 _sshClient = null; 162 _sshClient = null;
144 } 163 }