Add code
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<system.net>
|
||||
<settings>
|
||||
<httpWebRequest useUnsafeHeaderParsing="true" />
|
||||
</settings>
|
||||
<defaultProxy>
|
||||
<proxy bypassonlocal="True" usesystemdefault="False" />
|
||||
</defaultProxy>
|
||||
</system.net>
|
||||
</configuration>
|
||||
@@ -0,0 +1,105 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProductVersion>9.0.21022</ProductVersion>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>{7686BAA6-A0C5-4FA5-BE6E-FA044C2FFDD4}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>Endpoint</RootNamespace>
|
||||
<AssemblyName>Endpoint</AssemblyName>
|
||||
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<FileUpgradeFlags>
|
||||
</FileUpgradeFlags>
|
||||
<OldToolsVersion>3.5</OldToolsVersion>
|
||||
<UpgradeBackupLocation />
|
||||
<PublishUrl>publish\</PublishUrl>
|
||||
<Install>true</Install>
|
||||
<InstallFrom>Disk</InstallFrom>
|
||||
<UpdateEnabled>false</UpdateEnabled>
|
||||
<UpdateMode>Foreground</UpdateMode>
|
||||
<UpdateInterval>7</UpdateInterval>
|
||||
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
|
||||
<UpdatePeriodically>false</UpdatePeriodically>
|
||||
<UpdateRequired>false</UpdateRequired>
|
||||
<MapFileExtensions>true</MapFileExtensions>
|
||||
<ApplicationRevision>0</ApplicationRevision>
|
||||
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
|
||||
<IsWebBootstrapper>false</IsWebBootstrapper>
|
||||
<UseApplicationTrust>false</UseApplicationTrust>
|
||||
<BootstrapperEnabled>true</BootstrapperEnabled>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="HtmlAgilityPack, Version=1.3.0.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>References\HtmlAgilityPack.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.configuration" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Management" />
|
||||
<Reference Include="System.ServiceProcess" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="HtmlEndpoint.cs" />
|
||||
<Compile Include="IEndpoint.cs" />
|
||||
<Compile Include="HttpEndpoint.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="SoapEndpoint.cs" />
|
||||
<Compile Include="Status.cs" />
|
||||
<Compile Include="StatusComparer.cs" />
|
||||
<Compile Include="WindowsServiceEndpoint.cs" />
|
||||
<Compile Include="WmiEndpoint.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
|
||||
<Visible>False</Visible>
|
||||
<ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
|
||||
<Install>false</Install>
|
||||
</BootstrapperPackage>
|
||||
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
|
||||
<Visible>False</Visible>
|
||||
<ProductName>.NET Framework 3.5 SP1</ProductName>
|
||||
<Install>true</Install>
|
||||
</BootstrapperPackage>
|
||||
<BootstrapperPackage Include="Microsoft.Windows.Installer.3.1">
|
||||
<Visible>False</Visible>
|
||||
<ProductName>Windows Installer 3.1</ProductName>
|
||||
<Install>true</Install>
|
||||
</BootstrapperPackage>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
||||
@@ -0,0 +1,112 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using HtmlAgilityPack;
|
||||
|
||||
namespace Endpoint
|
||||
{
|
||||
/// <summary>
|
||||
/// A website endpoint
|
||||
/// </summary>
|
||||
public class HtmlEndpoint : HttpEndpoint
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the xpath query for the html document
|
||||
/// </summary>
|
||||
/// <value>The xpath query.</value>
|
||||
public string XpathQuery { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the expected query results.
|
||||
/// </summary>
|
||||
/// <value>The expected query results.</value>
|
||||
public string ExpectedXpathResult { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the post.
|
||||
/// </summary>
|
||||
/// <value>The post.</value>
|
||||
public string RequestContent { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the HTML parsed into a standard <see cref="XmlDocument"/>.
|
||||
/// Uses the <see cref="HtmlAgilityPack"/> library for "out of the web" (poorly formatted) html file support.
|
||||
/// </summary>
|
||||
/// <param name="html">The HTML</param>
|
||||
/// <returns>An <see cref="XmlDocument"/> from the HTML</returns>
|
||||
private static XmlDocument getHtmlXml(string html)
|
||||
{
|
||||
var htmlDocument = new HtmlDocument();
|
||||
htmlDocument.LoadHtml(html);
|
||||
|
||||
var xmlDocument = new XmlDocument();
|
||||
using (Stream stream = new MemoryStream())
|
||||
{
|
||||
XmlWriter xmlTextWriter = new XmlTextWriter(stream, Encoding.UTF8);
|
||||
|
||||
htmlDocument.Save(xmlTextWriter);
|
||||
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
using (var sr = new StreamReader(stream))
|
||||
xmlDocument.Load(sr);
|
||||
|
||||
xmlTextWriter.Close();
|
||||
}
|
||||
return xmlDocument;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the HTML grabbed from the passed in URL parsed into a standard <see cref="XmlDocument"/>.
|
||||
/// Uses the <see cref="HtmlAgilityPack"/> library for "out of the web" html file support.
|
||||
/// </summary>
|
||||
/// <param name="url">The URL.</param>
|
||||
/// <param name="requestContent">The post data.</param>
|
||||
/// <returns>An <see cref="XmlDocument"/> from the HTML</returns>
|
||||
private static XmlDocument getHtmlXml(Uri url, string requestContent)
|
||||
{
|
||||
return getHtmlXml(GetUrlContent(url, "application/x-www-form-urlencoded", requestContent, null));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current status result of the endpoint
|
||||
/// </summary>
|
||||
/// <returns>Status</returns>
|
||||
public override Status GetStatus()
|
||||
{
|
||||
var baseStatus = base.GetStatus();
|
||||
if (baseStatus != Status.Up)
|
||||
return baseStatus;
|
||||
|
||||
try
|
||||
{
|
||||
var xml = getHtmlXml(Uri, RequestContent);
|
||||
|
||||
// xml.Save(@"c:\test.xml");
|
||||
|
||||
var nodes = xml.SelectNodes(XpathQuery);
|
||||
|
||||
// verify html has expected value
|
||||
if (nodes == null || nodes.Count == 0)
|
||||
{
|
||||
StatusDescription = "Couldn't find expected value in html";
|
||||
return Status.Error;
|
||||
}
|
||||
var value = nodes[0].Value.Trim();
|
||||
if (value != ExpectedXpathResult)
|
||||
{
|
||||
StatusDescription = String.Format("Result was: '{0}', was expecting '{1}'", value,
|
||||
ExpectedXpathResult);
|
||||
return Status.Error;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
StatusDescription = ex.Message;
|
||||
return Status.Error;
|
||||
}
|
||||
return Status.Up;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
|
||||
namespace Endpoint
|
||||
{
|
||||
/// <summary>
|
||||
/// Http Endpoint - such as a webserver, a webservice, etc
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class HttpEndpoint : IEndpoint
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the name.
|
||||
/// </summary>
|
||||
/// <value>The name.</value>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// URI of the service
|
||||
/// </summary>
|
||||
public Uri Uri { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the URI string.
|
||||
/// </summary>
|
||||
/// <value>The URI string.</value>
|
||||
public string UriString
|
||||
{
|
||||
get { return Uri.ToString(); }
|
||||
set { Uri = new Uri(value); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the response string grabbed from the passed in URL.
|
||||
/// </summary>
|
||||
/// <param name="url">The URL.</param>
|
||||
/// <param name="contentType">The HTTP content type.</param>
|
||||
/// <param name="requestContent">The request content.</param>
|
||||
/// <param name="webHeaders">The web headers.</param>
|
||||
/// <returns>A string containing response</returns>
|
||||
protected static string GetUrlContent(Uri url, string contentType, string requestContent,
|
||||
Dictionary<string, string> webHeaders)
|
||||
{
|
||||
// POST request
|
||||
if (!string.IsNullOrEmpty(contentType) && !string.IsNullOrEmpty(requestContent))
|
||||
{
|
||||
var webRequest = (HttpWebRequest) WebRequest.Create(url);
|
||||
|
||||
// NOTE: need a proxy? Here's where it would go
|
||||
// webRequest.Proxy = new WebProxy();
|
||||
|
||||
// SOAP
|
||||
webRequest.ContentType = contentType;
|
||||
if (webHeaders != null && webHeaders.Count > 0)
|
||||
{
|
||||
foreach (var webHeader in webHeaders)
|
||||
webRequest.Headers.Add(webHeader.Key, webHeader.Value);
|
||||
}
|
||||
|
||||
webRequest.Method = "POST";
|
||||
|
||||
//We need to count how many bytes we're sending. Post'ed Faked Forms should be name=value&
|
||||
var bytes = Encoding.ASCII.GetBytes(requestContent);
|
||||
webRequest.ContentLength = bytes.Length;
|
||||
using (var requestStream = webRequest.GetRequestStream())
|
||||
{
|
||||
requestStream.Write(bytes, 0, bytes.Length); //write it to the stream
|
||||
}
|
||||
var webResponse = webRequest.GetResponse();
|
||||
if (webResponse == null)
|
||||
return null;
|
||||
using (var streamReader = new StreamReader(webResponse.GetResponseStream()))
|
||||
{
|
||||
return streamReader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
|
||||
// GET Request
|
||||
var client = new WebClient();
|
||||
using (var data = client.OpenRead(url))
|
||||
using (var reader = new StreamReader(data))
|
||||
{
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a string explaining details on the current status
|
||||
/// </summary>
|
||||
/// <returns>string with status message</returns>
|
||||
public virtual string StatusDescription { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current status result of the endpoint
|
||||
/// </summary>
|
||||
/// <returns>Status</returns>
|
||||
public virtual Status GetStatus()
|
||||
{
|
||||
try
|
||||
{
|
||||
var request = WebRequest.Create(Uri);
|
||||
|
||||
using (var response = (HttpWebResponse) request.GetResponse())
|
||||
{
|
||||
StatusDescription = response.StatusDescription;
|
||||
return response.StatusCode == HttpStatusCode.OK
|
||||
? Status.Up
|
||||
: Status.Unreachable;
|
||||
}
|
||||
}
|
||||
catch (WebException ex)
|
||||
{
|
||||
StatusDescription = ex.Message + " (" + ex.Status + ")";
|
||||
|
||||
return ex.Status == WebExceptionStatus.Timeout
|
||||
? Status.Timeout
|
||||
: Status.Unreachable;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
|
||||
namespace Endpoint
|
||||
{
|
||||
/// <summary>
|
||||
/// Describes a service endpoint, and the methods to find its current status
|
||||
/// </summary>
|
||||
public interface IEndpoint
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the service name
|
||||
/// </summary>
|
||||
string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current status result of the endpoint
|
||||
/// </summary>
|
||||
/// <returns>Status</returns>
|
||||
Status GetStatus();
|
||||
|
||||
/// <summary>
|
||||
/// Returns a string explaining details on the current status
|
||||
/// </summary>
|
||||
/// <returns>string with status message</returns>
|
||||
string StatusDescription { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("Endpoint")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("Endpoint")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2008")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("aef466bc-73b7-4f86-a374-8db991adb437")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,116 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Xml;
|
||||
|
||||
namespace Endpoint
|
||||
{
|
||||
/// <summary>
|
||||
/// A SOAP webservice endpoint.
|
||||
/// </summary>
|
||||
public class SoapEndpoint : HttpEndpoint
|
||||
{
|
||||
private string _xpathNamespaces = string.Empty;
|
||||
private Dictionary<string, string> _namespaceToUri;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the xpath query for the html document
|
||||
/// </summary>
|
||||
/// <value>The xpath query.</value>
|
||||
public string XpathQuery { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the namespaces used in the xpath query
|
||||
/// </summary>
|
||||
/// <value>The xpath namespaces.</value>
|
||||
public string XpathNamespaces
|
||||
{
|
||||
get { return _xpathNamespaces; }
|
||||
set
|
||||
{
|
||||
_xpathNamespaces = value;
|
||||
if (string.IsNullOrEmpty(_xpathNamespaces)) return;
|
||||
|
||||
var split = _xpathNamespaces.Split(',');
|
||||
if (split.Length != 2)
|
||||
throw new ArgumentException("Need a comma separated pair in XpathNamespaces.");
|
||||
|
||||
_namespaceToUri = new Dictionary<string, string>();
|
||||
_namespaceToUri.Add(split[0], split[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the expected query results.
|
||||
/// </summary>
|
||||
/// <value>The expected query results.</value>
|
||||
public string ExpectedXpathResult { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the SOAP action.
|
||||
/// </summary>
|
||||
/// <value>The post.</value>
|
||||
public string SoapAction { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the SOAP request.
|
||||
/// </summary>
|
||||
/// <value>The post.</value>
|
||||
public string SoapRequest { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current status result of the endpoint
|
||||
/// </summary>
|
||||
/// <returns>Status</returns>
|
||||
public override Status GetStatus()
|
||||
{
|
||||
var baseStatus = base.GetStatus();
|
||||
if (baseStatus != Status.Up) // if we can't even get to the webserver, no need to try and call a WS.
|
||||
return baseStatus;
|
||||
|
||||
try
|
||||
{
|
||||
var headers = new Dictionary<string, string> {{"SOAPAction", SoapAction}};
|
||||
|
||||
var xml = new XmlDocument();
|
||||
|
||||
xml.LoadXml(GetUrlContent(Uri, "text/xml; charset=utf-8", SoapRequest, headers));
|
||||
|
||||
XmlNodeList nodes;
|
||||
if (_namespaceToUri != null)
|
||||
{
|
||||
var nsMgr = new XmlNamespaceManager(xml.NameTable);
|
||||
foreach (var namespaceToUri in _namespaceToUri)
|
||||
{
|
||||
nsMgr.AddNamespace(namespaceToUri.Key, namespaceToUri.Value);
|
||||
}
|
||||
|
||||
nodes = xml.SelectNodes(XpathQuery, nsMgr);
|
||||
}
|
||||
else
|
||||
nodes = xml.SelectNodes(XpathQuery);
|
||||
|
||||
//xml.Save(@"c:\soap.xml");
|
||||
|
||||
// verify response has expected value
|
||||
if (nodes == null || nodes.Count == 0)
|
||||
{
|
||||
StatusDescription = "Couldn't find expected value in SOAP response";
|
||||
return Status.Error;
|
||||
}
|
||||
var value = nodes[0].Value.Trim();
|
||||
if (value != ExpectedXpathResult)
|
||||
{
|
||||
StatusDescription = String.Format("Result was: '{0}', was expecting '{1}'", value,
|
||||
ExpectedXpathResult);
|
||||
return Status.Error;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
StatusDescription = ex.Message;
|
||||
return Status.Error;
|
||||
}
|
||||
return Status.Up;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
namespace Endpoint
|
||||
{
|
||||
/// <summary>
|
||||
/// Status of an endpoint
|
||||
/// </summary>
|
||||
/// <remarks>The <see cref="StatusComparer"/> expects these to be in order of best-to-worst descending.</remarks>
|
||||
public enum Status
|
||||
{
|
||||
/// <summary>
|
||||
/// Service state is unknown - this will occur only before the first poll.
|
||||
/// </summary>
|
||||
Unknown,
|
||||
/// <summary>
|
||||
/// Service is working
|
||||
/// </summary>
|
||||
Up,
|
||||
/// <summary>
|
||||
/// Service can be resolved, but does not respond
|
||||
/// </summary>
|
||||
Timeout,
|
||||
/// <summary>
|
||||
/// Service can not be resolved
|
||||
/// </summary>
|
||||
Unreachable,
|
||||
/// <summary>
|
||||
/// Service is available, but returns an error message
|
||||
/// </summary>
|
||||
Error
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Endpoint
|
||||
{
|
||||
/// <summary>
|
||||
/// Compares a Status for which is the worst status.
|
||||
/// </summary>
|
||||
public class StatusComparer : IComparer<Status>
|
||||
{
|
||||
/// <summary>
|
||||
/// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
|
||||
/// </summary>
|
||||
/// <param name="x">The first object to compare.</param>
|
||||
/// <param name="y">The second object to compare.</param>
|
||||
/// <returns>
|
||||
/// Value
|
||||
/// Condition
|
||||
/// Less than zero
|
||||
/// <paramref name="x"/> is less than <paramref name="y"/>.
|
||||
/// Zero
|
||||
/// <paramref name="x"/> equals <paramref name="y"/>.
|
||||
/// Greater than zero
|
||||
/// <paramref name="x"/> is greater than <paramref name="y"/>.
|
||||
/// </returns>
|
||||
public int Compare(Status x, Status y)
|
||||
{
|
||||
return y.CompareTo(x);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.ServiceProcess;
|
||||
|
||||
namespace Endpoint
|
||||
{
|
||||
/// <summary>
|
||||
/// Endpoint for a Windows Service
|
||||
/// </summary>
|
||||
public class WindowsServiceEndpoint : IEndpoint
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the service name
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a string explaining details on the current status
|
||||
/// </summary>
|
||||
/// <returns>string with status message</returns>
|
||||
public string StatusDescription { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the name of the windows service.
|
||||
/// </summary>
|
||||
/// <value>The name of the service.</value>
|
||||
public string ServiceName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the name of the machine the service runs on.
|
||||
/// </summary>
|
||||
/// <value>The name of the machine.</value>
|
||||
public string MachineName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current status result of the endpoint
|
||||
/// </summary>
|
||||
/// <returns>Status</returns>
|
||||
public Status GetStatus()
|
||||
{
|
||||
ServiceController myservice;
|
||||
try
|
||||
{
|
||||
if (!string.IsNullOrEmpty(MachineName))
|
||||
myservice = new ServiceController(ServiceName, MachineName);
|
||||
else
|
||||
myservice = new ServiceController(ServiceName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
StatusDescription = ex.Message;
|
||||
return Status.Unreachable;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
switch (myservice.Status)
|
||||
{
|
||||
case ServiceControllerStatus.Running:
|
||||
StatusDescription = "Running";
|
||||
return Status.Up;
|
||||
default:
|
||||
StatusDescription = myservice.Status.ToString();
|
||||
return Status.Error;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
StatusDescription = ex.Message;
|
||||
return Status.Error;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
using System;
|
||||
using System.Management;
|
||||
|
||||
namespace Endpoint
|
||||
{
|
||||
/// <summary>
|
||||
/// Endpoint for Windows Management Instrumentation (WMI)
|
||||
/// </summary>
|
||||
public class WmiEndpoint : IEndpoint
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the service name
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a string explaining details on the current status
|
||||
/// </summary>
|
||||
/// <returns>string with status message</returns>
|
||||
public string StatusDescription { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the name of the machine to query with WMI.
|
||||
/// </summary>
|
||||
/// <value>The name of the machine.</value>
|
||||
public string MachineName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the object query string.
|
||||
/// </summary>
|
||||
/// <value>The object query string.</value>
|
||||
public string ObjectQueryString { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the connection username.
|
||||
/// </summary>
|
||||
/// <value>The connection username.</value>
|
||||
public string ConnectionUsername { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the connection password.
|
||||
/// </summary>
|
||||
/// <value>The connection password.</value>
|
||||
public string ConnectionPassword { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the name of the result property - this needs to be returned by the query.
|
||||
/// </summary>
|
||||
/// <value>The name of the result property.</value>
|
||||
public string ResultPropertyName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Report error when result is this value.
|
||||
/// </summary>
|
||||
/// <value>A result indicating an error.</value>
|
||||
public string ErrorResult { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Report up when result is this value.
|
||||
/// </summary>
|
||||
/// <value>A result indicating everything is ok.</value>
|
||||
public string UpResult { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Report error when result is below this number
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public double MinimumThreshold { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Report error when result is above this number
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public double MaximumThreshold { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="WmiEndpoint"/> class.
|
||||
/// </summary>
|
||||
public WmiEndpoint()
|
||||
{
|
||||
MinimumThreshold = double.MinValue;
|
||||
MaximumThreshold = double.MaxValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current status result of the endpoint
|
||||
/// </summary>
|
||||
/// <returns>Status</returns>
|
||||
public Status GetStatus()
|
||||
{
|
||||
var connectionOptions = new ConnectionOptions();
|
||||
if (!string.IsNullOrEmpty(ConnectionUsername) && !string.IsNullOrEmpty(ConnectionPassword))
|
||||
{
|
||||
connectionOptions.Username = ConnectionUsername;
|
||||
connectionOptions.Password = ConnectionPassword;
|
||||
}
|
||||
|
||||
ManagementScope managementScope;
|
||||
try
|
||||
{
|
||||
managementScope = new ManagementScope(MachineName, connectionOptions);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
StatusDescription = string.Format("Management Scope for MachineName \"{1}\" Failed : \"{0}\"", e.Message, MachineName);
|
||||
return Status.Error;
|
||||
}
|
||||
|
||||
ObjectQuery objectQuery;
|
||||
try
|
||||
{
|
||||
objectQuery = new ObjectQuery(ObjectQueryString);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
StatusDescription = string.Format("ObjectQuery initialization \"{1}\" Failed : \"{0}\"", e.Message, ObjectQueryString);
|
||||
return Status.Error;
|
||||
}
|
||||
|
||||
ManagementObjectSearcher results;
|
||||
try
|
||||
{
|
||||
//Execute the query
|
||||
results = new ManagementObjectSearcher(managementScope, objectQuery);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
StatusDescription = string.Format("Building Query failed : \"{0}\"", e.Message);
|
||||
return Status.Error;
|
||||
}
|
||||
|
||||
ManagementObjectCollection managementObjectCollection;
|
||||
try
|
||||
{
|
||||
//Get the results
|
||||
managementObjectCollection = results.Get();
|
||||
|
||||
if (managementObjectCollection.Count == 0)
|
||||
{
|
||||
StatusDescription = string.Format("Query returned 0 results : \"{0}\"", ObjectQueryString);
|
||||
return Status.Error;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
StatusDescription = "Error retrieving results. Exception: " + ex.Message;
|
||||
return Status.Unreachable;
|
||||
}
|
||||
|
||||
ManagementObject firstResult = null;
|
||||
|
||||
//take only the first result if there are more than one
|
||||
foreach (ManagementObject result in managementObjectCollection)
|
||||
{
|
||||
firstResult = result;
|
||||
break;
|
||||
}
|
||||
|
||||
if (firstResult == null)
|
||||
{
|
||||
StatusDescription = "Problem accessing first result object";
|
||||
return Status.Error;
|
||||
}
|
||||
|
||||
object resultValue;
|
||||
try
|
||||
{
|
||||
resultValue = firstResult[ResultPropertyName];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
StatusDescription = string.Format("Error retrieving result property \"{0}\". Exception: {1}",
|
||||
ResultPropertyName, ex.Message);
|
||||
return Status.Error;
|
||||
}
|
||||
|
||||
if (resultValue == null)
|
||||
{
|
||||
StatusDescription = string.Format("Result value was null for property \"{0}\".", ResultPropertyName);
|
||||
return Status.Error;
|
||||
}
|
||||
|
||||
return getStatus(resultValue.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the status.
|
||||
/// </summary>
|
||||
/// <param name="resultValueString">The result value string.</param>
|
||||
/// <returns></returns>
|
||||
private Status getStatus(string resultValueString)
|
||||
{
|
||||
// Up Value
|
||||
if (!string.IsNullOrEmpty(UpResult) && UpResult != resultValueString)
|
||||
{
|
||||
StatusDescription =
|
||||
string.Format("Result for property \"{0}\" was not expected value. Expected \"{1}\" but was \"{2}\"",
|
||||
ResultPropertyName, UpResult ?? "(NULL)", resultValueString);
|
||||
return Status.Error;
|
||||
}
|
||||
|
||||
// Error Value
|
||||
if (!string.IsNullOrEmpty(ErrorResult) && ErrorResult == resultValueString)
|
||||
{
|
||||
StatusDescription =
|
||||
string.Format("Result for property \"{0}\" was error value - \"{1}\"",
|
||||
ResultPropertyName, resultValueString);
|
||||
return Status.Error;
|
||||
}
|
||||
|
||||
if (MinimumThreshold != double.MinValue || MaximumThreshold != double.MaxValue)
|
||||
{
|
||||
double resultValueDouble;
|
||||
if (Double.TryParse(resultValueString, out resultValueDouble))
|
||||
{
|
||||
if (resultValueDouble < MinimumThreshold)
|
||||
{
|
||||
StatusDescription =
|
||||
string.Format("Result for property \"{0}\" was less then threshold - {1} < {2}",
|
||||
ResultPropertyName, resultValueDouble, MinimumThreshold);
|
||||
return Status.Error;
|
||||
}
|
||||
if (resultValueDouble > MaximumThreshold)
|
||||
{
|
||||
StatusDescription =
|
||||
string.Format("Result for property \"{0}\" was more then threshold - {1} > {2}",
|
||||
ResultPropertyName, resultValueDouble, MaximumThreshold);
|
||||
return Status.Error;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
StatusDescription =
|
||||
string.Format("Result for property \"{0}\" was not a number - \"{1}\"",
|
||||
ResultPropertyName, resultValueString);
|
||||
return Status.Error;
|
||||
}
|
||||
}
|
||||
|
||||
StatusDescription = resultValueString;
|
||||
return Status.Up;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user