annotate ServerMonitor/Helpers.cs @ 14:2db36ab759de

Add comments.
author Brad Greco <brad@bgreco.net>
date Mon, 22 Apr 2019 21:10:42 -0400
parents 052aa62cb42a
children 68d7834dc28e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
1 using Renci.SshNet;
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
2 using Renci.SshNet.Common;
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
3 using ServerMonitorApp.Properties;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
4 using System;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
5 using System.Collections.Generic;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
6 using System.ComponentModel;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
7 using System.Drawing;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
8 using System.Linq;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
9 using System.Net.NetworkInformation;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
10 using System.Text;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
11 using System.Windows.Forms;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
12
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
13 namespace ServerMonitorApp
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
14 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
15 static class Helpers
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
16 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
17 public static void FormatImageButton(Button button)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
18 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
19 button.Image = new Bitmap(button.Image, button.Height - 8, button.Height - 8);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
20 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
21
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
22 public static string GetAllMessages(this Exception ex)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
23 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
24 return "\t" + (ex.InnerException == null ? ex.Message : ex.Message + Environment.NewLine + ex.InnerException.GetAllMessages());
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
25 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
26
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
27 public static string GetDisplayName(Type type)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
28 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
29 return type?.GetAttribute<DisplayNameAttribute>()?.DisplayName ?? type?.Name ?? "null";
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
30 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
31
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
32 public static bool IsNullOrEmpty(this string aString)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
33 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
34 return aString == null || aString.Trim() == string.Empty;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
35 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
36
2
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
37 public static string ConvertNewlines(this string aString)
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
38 {
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
39 return aString.Replace("\r\n", "\n").Replace('\r', '\n');
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
40 }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
41
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
42 public static T GetAttribute<T>(this Type type) where T : Attribute
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
43 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
44 return type.GetCustomAttributes(typeof(T), false).SingleOrDefault() as T;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
45 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
46
8
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
47 public static Image GetImage(this CheckStatus checkStatus)
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
48 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
49 switch (checkStatus)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
50 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
51 case CheckStatus.Error: return Resources.error;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
52 case CheckStatus.Warning: return Resources.warning;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
53 case CheckStatus.Information: return Resources.info;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
54 case CheckStatus.Success: return Resources.pass;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
55 case CheckStatus.Running: return Resources.run;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
56 case CheckStatus.Disabled: return Resources.disable;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
57 default: return null;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
58 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
59 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
60
8
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
61 public static Icon GetIcon(this CheckStatus checkStatus)
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
62 {
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
63 switch (checkStatus)
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
64 {
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
65 case CheckStatus.Error: return Resources.icon_error;
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
66 case CheckStatus.Warning: return Resources.icon_warning;
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
67 case CheckStatus.Information: return Resources.icon_info;
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
68 default: return Resources.icon;
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
69 }
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
70 }
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
71
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
72 public static bool In(this Enum value, params Enum[] values) {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
73 return values.Contains(value);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
74 }
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
75
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
76 // https://stackoverflow.com/a/8345173
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
77 /// <summary>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
78 /// Indicates whether any network connection is available.
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
79 /// Filter connections below a specified speed, as well as virtual network cards.
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
80 /// </summary>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
81 /// <returns>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
82 /// <c>true</c> if a network connection is available; otherwise, <c>false</c>.
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
83 /// </returns>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
84 public static bool IsNetworkAvailable()
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
85 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
86 if (!NetworkInterface.GetIsNetworkAvailable())
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
87 return false;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
88
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
89 foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
90 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
91 // discard because of standard reasons
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
92 if ((ni.OperationalStatus != OperationalStatus.Up) ||
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
93 (ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) ||
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
94 (ni.NetworkInterfaceType == NetworkInterfaceType.Tunnel))
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
95 continue;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
96
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
97 // discard virtual cards (virtual box, virtual pc, etc.)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
98 if ((ni.Description.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0) ||
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
99 (ni.Name.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0))
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
100 continue;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
101
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
102 // discard "Microsoft Loopback Adapter", it will not show as NetworkInterfaceType.Loopback but as Ethernet Card.
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
103 if (ni.Description.Equals("Microsoft Loopback Adapter", StringComparison.OrdinalIgnoreCase))
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
104 continue;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
105
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
106 return true;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
107 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
108 return false;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
109 }
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
110 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
111 }