From b617a7539efda6453f599f562c0de754c6f06615 Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Tue, 16 Apr 2019 18:23:05 +0200 Subject: [PATCH] WIP --- Program.cs | 1 + SkyEntities.cs | 30 ++++++++++++++++++++++++++++-- checks/APC.cs | 5 ----- checks/CheckJob.cs | 9 +++++++++ checks/Hostalive.cs | 3 --- checks/PerformanceValue.cs | 2 +- checks/SkyCheck.cs | 4 ++++ checks/Ubiquity.cs | 9 +++++---- http/SkyScannerHttpApplication.cs | 9 +++++++++ 9 files changed, 57 insertions(+), 15 deletions(-) diff --git a/Program.cs b/Program.cs index 1740842..13f34d2 100644 --- a/Program.cs +++ b/Program.cs @@ -16,6 +16,7 @@ using ln.types.threads; using ln.types.net; using ln.types.odb; using ln.types.odb.index; +using ln.types.odb.values; namespace ln.skyscanner { diff --git a/SkyEntities.cs b/SkyEntities.cs index 53eae53..04b09e5 100644 --- a/SkyEntities.cs +++ b/SkyEntities.cs @@ -18,6 +18,7 @@ using ln.types.net; using ln.skyscanner.crawl; using ln.skyscanner.checks; using ln.logging; +using System.Collections.Generic; namespace ln.skyscanner { @@ -52,11 +53,11 @@ namespace ln.skyscanner NodeCollection = ODB.GetCollection(); NodeCollection.EnableStrongCache(true); + NodeCollection.EnsureIndex("uniqueIdentity", true); NodeCollection.EnsureIndeces( "PrimaryIP", "Interfaces[].ConfiguredIPs[].IP", - "Interfaces[].ConfiguredIPs[].Network", - "uniqueIdentity" + "Interfaces[].ConfiguredIPs[].Network" ); Network4 n192 = Network4.Parse("192.168.0.0/16"); @@ -83,6 +84,7 @@ namespace ln.skyscanner SkyCheckStates = ODB.GetCollection(); SkyCheckStates.EnableStrongCache(true); SkyCheckStates.EnsureIndeces("Node"); + SkyCheckStates.EnsureUniqueness("Node", "CheckName"); //foreach (SkyCheckState checkState in SkyCheckStates.ToArray()) //{ @@ -95,6 +97,30 @@ namespace ln.skyscanner GlobalNetwork = new GlobalNetwork(); } + public PerformanceValue LookupPerformanceValue(String perfName) + { + string[] resourcePath = perfName.Split('/'); + + Node node = NodeCollection.Query("uniqueIdentity", resourcePath[0]).FirstOrDefault(); + if (node == null) + throw new KeyNotFoundException(); + + SkyCheckState skyCheckState = SkyCheckStates.Query( + Query.AND( + Query.Equals("Node", node.ID), + Query.Equals("CheckName", resourcePath[1]) + ) + ).FirstOrDefault(); + if (skyCheckState == null) + throw new KeyNotFoundException(); + + foreach (PerformanceValue performanceValue in skyCheckState.PerformanceValues) + if (performanceValue.PerfName.Equals(perfName)) + return performanceValue; + + throw new KeyNotFoundException(); + } + } } diff --git a/checks/APC.cs b/checks/APC.cs index ce09344..f46fa78 100644 --- a/checks/APC.cs +++ b/checks/APC.cs @@ -20,11 +20,6 @@ namespace ln.skyscanner.checks public override void Check(SkyChecker skyChecker,ref SkyCheckState checkState,Node node) { - if ((checkState == null)) - { - checkState = new SkyCheckState(this, node); - } - foreach (URI snmpUri in node.FindURIs("snmp")) { using (SnmpInterface snmp = SnmpInterface.FromURI(snmpUri,skyChecker.SNMPEngine)) diff --git a/checks/CheckJob.cs b/checks/CheckJob.cs index 0e4ed85..440df5f 100644 --- a/checks/CheckJob.cs +++ b/checks/CheckJob.cs @@ -28,13 +28,22 @@ namespace ln.skyscanner.checks { Name = String.Format("Interval check: {0} [{1}]",node.UniqueIdentity,node.PrimaryIP.ToString()); Node = node; + } + public override void Prepare() + { Query stateQuery = Query.Equals("Node", Node.ID); SkyCheckState[] skyCheckStates = SkyScanner.Instance.Entities.SkyCheckStates.Query(stateQuery).ToArray(); foreach (SkyCheckState checkState in skyCheckStates) { checkStates.Add(checkState.CheckName, checkState); } + foreach (SkyCheck check in SkyCheck.SkyChecks) + if (!checkStates.ContainsKey(check.Name) && check.IsValid(Node)) + { + checkStates.Add(check.Name, check.PrepareCheckState(SkyScanner.Instance.Checker, Node)); + SkyScanner.Instance.Entities.SkyCheckStates.Insert(checkStates[check.Name]); + } } public override void RunJob() diff --git a/checks/Hostalive.cs b/checks/Hostalive.cs index e9308cb..b1113d7 100644 --- a/checks/Hostalive.cs +++ b/checks/Hostalive.cs @@ -31,9 +31,6 @@ namespace ln.skyscanner.checks public override void Check(SkyChecker skyChecker,ref SkyCheckState checkState,Node node) { - if (checkState == null) - checkState = new SkyCheckState(this, node); - long roundTripTime = 0; int success = 0; int n; diff --git a/checks/PerformanceValue.cs b/checks/PerformanceValue.cs index d303b04..136c8b9 100644 --- a/checks/PerformanceValue.cs +++ b/checks/PerformanceValue.cs @@ -9,6 +9,7 @@ // **/ using System; using ln.perfdb.storage; +using Newtonsoft.Json; namespace ln.skyscanner.checks { public class PerformanceValue @@ -23,7 +24,6 @@ namespace ln.skyscanner.checks public double CritLower { get; set; } = Double.MinValue; public double CritUpper { get; set; } = Double.MaxValue; - private PerformanceValue() { } diff --git a/checks/SkyCheck.cs b/checks/SkyCheck.cs index 9188c5d..0721275 100644 --- a/checks/SkyCheck.cs +++ b/checks/SkyCheck.cs @@ -33,6 +33,10 @@ namespace ln.skyscanner.checks public abstract bool IsValid(Node node); public abstract void Check(SkyChecker skyChecker,ref SkyCheckState checkState,Node node); + public virtual SkyCheckState PrepareCheckState(SkyChecker skyChecker,Node node) + { + return new SkyCheckState(this, node); + } static SkyCheck() { diff --git a/checks/Ubiquity.cs b/checks/Ubiquity.cs index 23633c4..fc339de 100644 --- a/checks/Ubiquity.cs +++ b/checks/Ubiquity.cs @@ -18,10 +18,6 @@ namespace ln.skyscanner.checks public override void Check(SkyChecker skyChecker,ref SkyCheckState checkState,Node node) { - if ((checkState == null) || !(checkState is UbiquityCheckState)) - { - checkState = new UbiquityCheckState(this, node); - } UbiquityCheckState ubiquityCheckState = checkState as UbiquityCheckState; foreach (URI snmpUri in node.FindURIs("snmp")) @@ -63,6 +59,11 @@ namespace ln.skyscanner.checks { return (node.Vendor != null) && node.Vendor.Equals("Ubiquity"); } + + public override SkyCheckState PrepareCheckState(SkyChecker skyChecker, Node node) + { + return new UbiquityCheckState(this, node); + } } class UbiquityCheckState : SkyCheckState diff --git a/http/SkyScannerHttpApplication.cs b/http/SkyScannerHttpApplication.cs index 9abed48..a62dd7a 100644 --- a/http/SkyScannerHttpApplication.cs +++ b/http/SkyScannerHttpApplication.cs @@ -6,6 +6,9 @@ using ln.skyscanner.entities; using ln.skyscanner.checks; using ln.skyscanner.crawl; using ln.types.net; +using System.Linq; +using System.Collections.Generic; +using ln.types.odb; namespace ln.skyscanner.http { public class SkyScannerHttpApplication : ResourceApplication @@ -48,6 +51,12 @@ namespace ln.skyscanner.http new CollectionResource(collections, skyScanner.Entities.SkyCheckStates); new CollectionResource(collections, skyScanner.Entities.BlockedNetworks); + + ObjectContainerResource resPerformanceValues = new ObjectContainerResource(collections); + resPerformanceValues.GetResourceName = (pv) => pv.PerfName; + resPerformanceValues.Enumeration = () => skyScanner.Entities.SkyCheckStates.SelectMany((checkState) => checkState.PerformanceValues); + resPerformanceValues.Lookup = skyScanner.Entities.LookupPerformanceValue; + } private Resource ResourceTypeHook(DirectoryResource directoryResource, FileInfo fileInfo)