annotate ServerMonitor/Helpers.cs @ 33:69e8ecdbbdd3

Fix schedule time not being saved on daily schedules.
author Brad Greco <brad@bgreco.net>
date Sun, 16 Jun 2019 16:55:37 -0400
parents 68d7834dc28e
children 7645122aa7a9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
1 using ServerMonitorApp.Properties;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
2 using System;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
3 using System.ComponentModel;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
4 using System.Drawing;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
5 using System.Linq;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
6 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
7 using System.Windows.Forms;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
8
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
9 namespace ServerMonitorApp
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
10 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
11 static class Helpers
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
12 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
13 /// <summary>Resizes the image on a button to fit inside the button's dimensions.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
14 /// <param name="button">A button containing an image.</param>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
15 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
16 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
17 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
18 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
19
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
20 /// <summary>Recursively collects the messages for an exception and its inner exceptions.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
21 /// <param name="ex">The parent exception.</param>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
22 /// <returns>A string containing the messages for the exception and its inner exceptions.</returns>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
23 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
24 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
25 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
26 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
27
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
28 /// <summary>Gets the value of the DisplayNameAttribute for a type.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
29 /// <param name="type">The type to get the display name of.</param>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
30 /// <returns>The type's display name, or the type name if no display name is defined.</returns>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
31 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
32 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
33 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
34 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
35
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
36 /// <summary>Checks whether a string is null, an empty string, or only contains white space.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
37 /// <param name="aString">The string to test.</param>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
38 /// <returns>True if the string is null, an empty string, or only contains white space. False otherwise.</returns>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
39 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
40 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
41 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
42 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
43
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
44 /// <summary>Converts all newlines in a string to unix format.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
45 /// <param name="aString">The string to convert.</param>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
46 /// <returns>The string with all newlines converted to unix format.</returns>
2
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
47 public static string ConvertNewlines(this string aString)
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
48 {
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
49 return aString.Replace("\r\n", "\n").Replace('\r', '\n');
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
50 }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
51
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
52 /// <summary>Gets an attribute on a class.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
53 /// <typeparam name="T">The type of the attribute to return.</typeparam>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
54 /// <param name="type">The type of the class the attribute is on.</param>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
55 /// <returns>The attribute, or null if the attribute does not exist on the class.</returns>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
56 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
57 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
58 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
59 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
60
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
61 /// <summary>Gets an image associated with a check status for use in the UI.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
62 /// <param name="checkStatus">The check status.</param>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
63 /// <returns>The image associated with the check status.</returns>
8
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
64 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
65 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
66 switch (checkStatus)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
67 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
68 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
69 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
70 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
71 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
72 case CheckStatus.Running: return Resources.run;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
73 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
74 default: return null;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
75 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
76 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
77
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
78 /// <summary>Gets a program icon associated with a check status.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
79 /// <param name="checkStatus">The check status.</param>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
80 /// <returns>The program icon associated with the check status.</returns>
8
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
81 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
82 {
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
83 switch (checkStatus)
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
84 {
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
85 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
86 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
87 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
88 default: return Resources.icon;
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
89 }
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
90 }
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
91
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
92 /// <summary>Returns whether an enum is in a list of values.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
93 /// <param name="value">The value to check.</param>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
94 /// <param name="values">The list of possible values.</param>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
95 /// <returns>True if the value was in the list, false otherwise.</returns>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
96 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
97 return values.Contains(value);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
98 }
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
99
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
100 // https://stackoverflow.com/a/8345173
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
101 /// <summary>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
102 /// Indicates whether any network connection is available.
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
103 /// 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
104 /// </summary>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
105 /// <returns>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
106 /// <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
107 /// </returns>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
108 public static bool IsNetworkAvailable()
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
109 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
110 if (!NetworkInterface.GetIsNetworkAvailable())
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
111 return false;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
112
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
113 foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
114 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
115 // discard because of standard reasons
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
116 if ((ni.OperationalStatus != OperationalStatus.Up) ||
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
117 (ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) ||
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
118 (ni.NetworkInterfaceType == NetworkInterfaceType.Tunnel))
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
119 continue;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
120
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
121 // discard virtual cards (virtual box, virtual pc, etc.)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
122 if ((ni.Description.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0) ||
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
123 (ni.Name.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0))
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
124 continue;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
125
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
126 // 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
127 if (ni.Description.Equals("Microsoft Loopback Adapter", StringComparison.OrdinalIgnoreCase))
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
128 continue;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
129
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
130 return true;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
131 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
132 return false;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
133 }
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
134 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
135 }