-
Notifications
You must be signed in to change notification settings - Fork 826
Expand file tree
/
Copy pathNode.cs
More file actions
336 lines (291 loc) · 13.9 KB
/
Node.cs
File metadata and controls
336 lines (291 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
using System;
using System.Collections.Generic;
using System.Linq;
using Opserver.Data.Dashboard.Providers;
using System.Globalization;
using System.Text.RegularExpressions;
namespace Opserver.Data.Dashboard
{
public partial class Node : IMonitorStatus, ISearchableNode
{
string ISearchableNode.DisplayName => PrettyName;
string ISearchableNode.Name => PrettyName;
string ISearchableNode.CategoryName => Category?.Name.Replace(" Servers", "") ?? "Unknown";
public DashboardDataProvider DataProvider { get; set; }
public bool IsRealTimePollable => MachineType?.Contains("Windows") == true;
public List<Issue<Node>> Issues { get; set; }
public string Id { get; internal set; }
public string Name { get; internal set; }
public DateTime? LastSync { get; internal set; }
public string MachineType { get; internal set; }
public string MachineOSVersion { get; internal set; }
private string _machineTypePretty;
public string MachineTypePretty => _machineTypePretty ??= GetPrettyMachineType();
public string Ip { get; internal set; }
public short? PollIntervalSeconds { get; internal set; }
public DateTime? LastBoot { get; internal set; }
public NodeStatus Status { get; internal set; }
public NodeStatus? ChildStatus { get; internal set; }
public string StatusDescription { get; internal set; }
private HardwareType? _hardwareType;
public HardwareType HardwareType => _hardwareType ??= GetHardwareType();
public short? CPULoad { get; internal set; }
public float? TotalMemory { get; internal set; }
public float? MemoryUsed { get; internal set; }
public string VMHostID { get; internal set; }
public bool IsVMHost { get; internal set; }
public bool IsUnwatched { get; internal set; }
public string Manufacturer { get; internal set; }
public string Model { get; internal set; }
public string ServiceTag { get; internal set; }
public Version KernelVersion { get; internal set; }
public string PrettyName => (Name ?? "").ToUpper();
public TimeSpan? UpTime => DateTime.UtcNow - LastBoot;
public MonitorStatus MonitorStatus => Status.ToMonitorStatus();
// TODO: Implement
public string MonitorStatusReason => null;
public bool IsVM => VMHostID.HasValue() || (Manufacturer?.Contains("VMware") ?? false);
public bool HasValidMemoryReading => MemoryUsed >= 0;
public Node VMHost { get; internal set; }
public List<Node> VMs { get; internal set; }
private DashboardCategory _category;
public DashboardCategory Category =>
_category ??= DataProvider.Module.AllCategories.Find(c => c.PatternRegex.IsMatch(Name)) ?? DashboardCategory.Unknown;
private string GetPrettyMachineType()
{
if (MachineType?.StartsWith("Linux") ?? false) return MachineOSVersion.IsNullOrEmptyReturn("Linux");
return MachineType?.Replace("Microsoft Windows ", "");
}
private HardwareType GetHardwareType()
{
if (IsVM) return HardwareType.VirtualMachine;
return HardwareType.Physical;
// TODO: Detect network gear in a reliable way
//return HardwareType.Unknown;
}
public string ManagementUrl { get; internal set; }
private string _searchString, _networkTextSummary, _applicationCPUTextSummary, _applicationMemoryTextSummary;
public void ClearSummaries()
{
_searchString = _networkTextSummary = _applicationCPUTextSummary = _applicationMemoryTextSummary = null;
}
public string SearchString
{
get
{
if (_searchString == null)
{
var result = StringBuilderCache.Get();
result.Append(MachineType)
.Pipend(PrettyName)
.Pipend(Status.ToString())
.Pipend(Manufacturer)
.Pipend(Model)
.Pipend(ServiceTag);
if (Hardware?.Processors != null)
{
foreach (var p in Hardware.Processors)
{
result.Pipend(p.Name)
.Pipend(p.Description);
}
}
if (Hardware?.Storage?.Controllers != null)
{
foreach (var c in Hardware.Storage.Controllers)
{
result.Pipend(c.Name)
.Pipend(c.FirmwareVersion)
.Pipend(c.DriverVersion);
}
}
if (Hardware?.Storage?.PhysicalDisks != null)
{
foreach (var d in Hardware.Storage.PhysicalDisks)
{
result.Pipend(d.Media)
.Pipend(d.ProductId)
.Pipend(d.Serial)
.Pipend(d.Part);
}
}
if (Interfaces != null)
{
foreach (var i in Interfaces)
{
result.Pipend(i.Name)
.Pipend(i.Caption)
.Pipend(i.PhysicalAddress)
.Pipend(i.TypeDescription);
foreach (var ip in i.IPs)
{
result.Pipend(ip.ToString());
}
}
}
if (Apps != null)
{
foreach (var app in Apps)
{
result.Pipend(app.NiceName);
}
}
if (IsVM && VMHost != null)
{
result.Pipend(VMHost.PrettyName);
}
if (IsVMHost && VMs != null)
{
foreach (var vm in VMs)
{
result.Pipend(vm?.Name);
}
}
_searchString = result.ToStringRecycle();
}
return _searchString;
}
}
public string NetworkTextSummary
{
get
{
if (_networkTextSummary != null) return _networkTextSummary;
var sb = StringBuilderCache.Get();
sb.Append("Total Traffic: ").Append(TotalPrimaryNetworkBitsPerSecond.ToSize("b")).AppendLine("/s");
sb.AppendFormat("Interfaces ({0} total):", Interfaces.Count.ToString()).AppendLine();
foreach (var i in PrimaryInterfaces.Take(5).OrderByDescending(i => i.InBitsPerSecond + i.OutBitsPerSecond))
{
sb.AppendFormat("{0}: {1}/s\n(In: {2}/s, Out: {3}/s)\n", i.PrettyName,
(i.InBitsPerSecond.GetValueOrDefault(0) + i.OutBitsPerSecond.GetValueOrDefault(0)).ToSize("b"),
i.InBitsPerSecond.GetValueOrDefault(0).ToSize("b"), i.OutBitsPerSecond.GetValueOrDefault(0).ToSize("b"));
}
return _networkTextSummary = sb.ToStringRecycle();
}
}
public string ApplicationCPUTextSummary
{
get
{
if (_applicationCPUTextSummary != null) return _applicationCPUTextSummary;
if (Apps?.Any() != true) return _applicationCPUTextSummary = "";
var sb = StringBuilderCache.Get();
sb.AppendFormat("Total App Pool CPU: {0:0.##} %\n", Apps.Sum(a => a.PercentCPU.GetValueOrDefault(0)).ToString(CultureInfo.CurrentCulture));
sb.AppendLine("App Pools:");
foreach (var a in Apps.OrderBy(a => a.NiceName))
{
sb.AppendFormat(" {0}: {1:0.##} %\n", a.NiceName, a.PercentCPU?.ToString(CultureInfo.CurrentCulture));
}
return _applicationCPUTextSummary = sb.ToStringRecycle();
}
}
public string ApplicationMemoryTextSummary
{
get
{
if (_applicationMemoryTextSummary != null) return _applicationMemoryTextSummary;
if (Apps?.Any() != true) return _applicationMemoryTextSummary = "";
var sb = StringBuilderCache.Get();
sb.AppendFormat("Total App Pool Memory: {0}\n", Apps.Sum(a => a.MemoryUsed.GetValueOrDefault(0)).ToSize());
sb.AppendLine("App Pools:");
foreach (var a in Apps.OrderBy(a => a.NiceName))
{
sb.AppendFormat(" {0}: {1}\n", a.NiceName, a.MemoryUsed.GetValueOrDefault(0).ToSize());
}
return _applicationMemoryTextSummary = sb.ToStringRecycle();
}
}
public TimeSpan? PollInterval => PollIntervalSeconds.HasValue ? TimeSpan.FromSeconds(PollIntervalSeconds.Value) : (TimeSpan?) null;
// Interfaces, Volumes, Applications, and Services are set by the provider
public List<Interface> Interfaces { get; internal set; }
public List<Volume> Volumes { get; internal set; }
public List<Application> Apps { get; internal set; }
public List<NodeService> Services { get; internal set; }
public Interface GetInterface(string id)
{
foreach (var i in Interfaces)
{
if (i.Id == id) return i;
}
return null;
}
public Volume GetVolume(string id)
{
foreach (var v in Volumes)
{
if (v.Id == id) return v;
}
return null;
}
public Application GetApp(string id)
{
foreach (var a in Apps)
{
if (a.Id == id) return a;
}
return null;
}
public NodeService GetService(string id)
{
foreach (var s in Services)
{
if (s.Id == id) return s;
}
return null;
}
private static readonly List<IPNet> EmptyIPs = new();
public List<IPNet> IPs => Interfaces?.SelectMany(i => i.IPs).ToList() ?? EmptyIPs;
public float? PercentMemoryUsed => MemoryUsed * 100 / TotalMemory;
public float TotalNetworkBitsPerSecond => Interfaces?.Sum(i => i.InBitsPerSecond.GetValueOrDefault(0) + i.OutBitsPerSecond.GetValueOrDefault(0)) ?? 0;
public float TotalPrimaryNetworkBitsPerSecond => PrimaryInterfaces.Sum(i => i.InBitsPerSecond.GetValueOrDefault(0) + i.OutBitsPerSecond.GetValueOrDefault(0));
public float TotalVolumePerformanceBytesPerSecond => Volumes?.Sum(i => i.ReadBytesPerSecond.GetValueOrDefault(0) + i.WriteBytesPerSecond.GetValueOrDefault(0)) ?? 0;
private DashboardSettings.NodeSettings _settings;
public DashboardSettings.NodeSettings Settings => _settings ??= DataProvider.Module.Settings.GetNodeSettings(PrettyName);
private decimal? GetSetting(Func<INodeSettings, decimal?> func) => func(Settings) ?? func(Category?.Settings) ?? func(DataProvider.Module.Settings);
private Regex GetSetting(Func<INodeSettings, Regex> func) => func(Settings) ?? func(Category?.Settings) ?? func(DataProvider.Module.Settings);
public decimal? CPUWarningPercent => GetSetting(i => i.CPUWarningPercent);
public decimal? CPUCriticalPercent => GetSetting(i => i.CPUCriticalPercent);
public decimal? MemoryWarningPercent => GetSetting(i => i.MemoryCriticalPercent);
public decimal? MemoryCriticalPercent => GetSetting(i => i.MemoryCriticalPercent);
public decimal? DiskWarningPercent => GetSetting(i => i.DiskWarningPercent);
public decimal? DiskCriticalPercent => GetSetting(i => i.DiskCriticalPercent);
public Regex ServicesPatternRegEx => GetSetting(i => i.ServicesPatternRegEx);
private List<Interface> _primaryInterfaces;
public List<Interface> PrimaryInterfaces
{
get
{
if (_primaryInterfaces == null || (_primaryInterfaces.Count == 0 && Interfaces?.Count > 0))
{
var pattern = Settings?.PrimaryInterfacePatternRegex ?? Category?.Settings?.PrimaryInterfacePatternRegex;
var dbInterfaces = Interfaces.Where(i => i.IsLikelyPrimary(pattern)).ToList();
_primaryInterfaces = (dbInterfaces.Count > 0
? dbInterfaces.OrderBy(i => i.Name)
: Interfaces.OrderByDescending(i => i.InBitsPerSecond + i.OutBitsPerSecond)).ToList();
}
return _primaryInterfaces;
}
}
/// <summary>
/// Should be called after a node is created to set parent referneces
/// and removed ignored interfaces, volumes, etc.
///
/// This allows interfaces, volumes, etc. to poll through the provider
/// </summary>
public void AfterInitialize()
{
if (Interfaces != null)
{
var ignoredInterfaceRegex = GetSetting(s => s.IgnoredInterfaceRegEx);
if (ignoredInterfaceRegex != null)
{
Interfaces.RemoveAll(i => ignoredInterfaceRegex.IsMatch(i.Id));
}
Interfaces.ForEach(i => i.Node = this);
}
Volumes?.ForEach(v => v.Node = this);
Apps?.ForEach(a => a.Node = this);
Services?.ForEach(s => s.Node = this);
}
}
}