Skip to content

Commit

Permalink
add ibmmq healthchecks
Browse files Browse the repository at this point in the history
  • Loading branch information
fglaeser committed Mar 3, 2020
1 parent 2a66bc9 commit c71ed12
Show file tree
Hide file tree
Showing 10 changed files with 376 additions and 1 deletion.
7 changes: 7 additions & 0 deletions AspNetCore.Diagnostics.HealthChecks.sln
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HealthChecks.UI.Branding",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HealthChecks.Azure.IoTHub", "src\HealthChecks.Azure.IoTHub\HealthChecks.Azure.IoTHub.csproj", "{252BB504-B7CB-4581-8CD8-D7398CAA16F5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HealthChecks.IbmMQ", "src\HealthChecks.IbmMQ\HealthChecks.IbmMQ.csproj", "{E780FB0C-19B8-432B-A749-B9959F2CCA4A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -282,6 +284,10 @@ Global
{252BB504-B7CB-4581-8CD8-D7398CAA16F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{252BB504-B7CB-4581-8CD8-D7398CAA16F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{252BB504-B7CB-4581-8CD8-D7398CAA16F5}.Release|Any CPU.Build.0 = Release|Any CPU
{E780FB0C-19B8-432B-A749-B9959F2CCA4A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E780FB0C-19B8-432B-A749-B9959F2CCA4A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E780FB0C-19B8-432B-A749-B9959F2CCA4A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E780FB0C-19B8-432B-A749-B9959F2CCA4A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -329,6 +335,7 @@ Global
{18F9E412-646D-4751-9751-30AA7A0233DF} = {2A3FD988-2BB8-43CF-B3A2-B70E648259D4}
{B526834E-9392-4749-BAB2-7DF579F8F418} = {092533AB-7505-4EDC-8932-D40BF575D0D2}
{252BB504-B7CB-4581-8CD8-D7398CAA16F5} = {2A3FD988-2BB8-43CF-B3A2-B70E648259D4}
{E780FB0C-19B8-432B-A749-B9959F2CCA4A} = {2A3FD988-2BB8-43CF-B3A2-B70E648259D4}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2B8C62A1-11B6-469F-874C-A02443256568}
Expand Down
1 change: 1 addition & 0 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,5 @@ else {
exec { & dotnet pack .\src\HealthChecks.Kubernetes\HealthChecks.Kubernetes.csproj -c Release -o .\artifacts --include-symbols --no-build --version-suffix=$suffix }
exec { & dotnet pack .\src\HealthChecks.SignalR\HealthChecks.SignalR.csproj -c Release -o .\artifacts --include-symbols --no-build --version-suffix=$suffix }
exec { & dotnet pack .\src\HealthChecks.Gcp.CloudFirestore\HealthChecks.Gcp.CloudFirestore.csproj -c Release -o .\artifacts --include-symbols --no-build --version-suffix=$suffix }
exec { & dotnet pack .\src\HealthChecks.IbmMQ\HealthChecks.IbmMQ.csproj -c Release -o .\artifacts --include-symbols --no-build }
}
1 change: 1 addition & 0 deletions build/dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,6 @@
<HealthCheckSignalR>3.0.0</HealthCheckSignalR>
<HealthCheckCloudFirestore>3.0.0</HealthCheckCloudFirestore>
<HealthCheckIoTHub>3.0.0</HealthCheckIoTHub>
<HealthCheckIbmMQ>3.0.0</HealthCheckIbmMQ>
</PropertyGroup>
</Project>
13 changes: 12 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,15 @@ services:
image: consul:latest
ports:
- "8500:8500"
- "8600:8600"
- "8600:8600"
ibmmq:
image: ibmcom/mq
ports:
- "1414:1414"
- "9157:9157"
environment:
- LICENSE=accept
- MQ_QMGR_NAME=QM.TEST.01
- MQ_APP_PASSWORD=12345678
- MQ_ENABLE_METRICS=true

Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using HealthChecks.IbmMQ;
using IBM.WMQ;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System;
using System.Collections;
using System.Collections.Generic;


namespace Microsoft.Extensions.DependencyInjection
{
public static class IbmMQHealthCheckBuilderExtensions
{
const string NAME = "ibmmq";

/// <summary>
/// Add a health check for IbmMQ services using connection properties.
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="queueManager">The name of the queue manager to use.</param>
/// <param name="connectionProperties">The list of properties that will be used for connection.</param>
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'ibmmq' will be used for the name.</param>
/// <param name="failureStatus">
/// The <see cref="HealthStatus"/> that should be reported when the health check fails. Optional. If <c>null</c> then
/// the default status of <see cref="HealthStatus.Unhealthy"/> will be reported.
/// </param>
/// <param name="tags">A list of tags that can be used to filter sets of health checks. Optional.</param>
/// <param name="timeout">An optional System.TimeSpan representing the timeout of the check.</param>
/// <returns>The <see cref="IHealthChecksBuilder"/>.</returns>
public static IHealthChecksBuilder AddIbmMQ(this IHealthChecksBuilder builder, string queueManager, Hashtable connectionProperties, string name = default, HealthStatus? failureStatus = default, IEnumerable<string> tags = default, TimeSpan? timeout = default)
{
return builder.Add(new HealthCheckRegistration(
name ?? NAME,
new IbmMQHealthCheck(queueManager, connectionProperties),
failureStatus,
tags,
timeout));
}

/// <summary>
/// Add a health check for IbmMQ services using a managed connection.
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="queueManager">The name of the queue manager to use.</param>
/// <param name="channel">The name of the channel.</param>
/// <param name="connectionInfo">The connection information in the following format HOSTNAME(PORT).</param>
/// <param name="userName">The user name. Optional.</param>
/// <param name="password">The password. Optional</param>
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'ibmmq' will be used for the name.</param>
/// <param name="failureStatus">
/// The <see cref="HealthStatus"/> that should be reported when the health check fails. Optional. If <c>null</c> then
/// the default status of <see cref="HealthStatus.Unhealthy"/> will be reported.
/// </param>
/// <param name="tags">A list of tags that can be used to filter sets of health checks. Optional.</param>
/// <param name="timeout">An optional System.TimeSpan representing the timeout of the check.</param>
/// <returns>The <see cref="IHealthChecksBuilder"/>.</returns>
public static IHealthChecksBuilder AddIbmMQManagedConnection(this IHealthChecksBuilder builder, string queueManager, string channel, string connectionInfo, string userName = null, string password = null, string name = default, HealthStatus? failureStatus = default, IEnumerable<string> tags = default, TimeSpan? timeout = default)
{
return builder.Add(new HealthCheckRegistration(
name ?? NAME,
new IbmMQHealthCheck(queueManager, BuildProperties(channel, connectionInfo, userName, password)),
failureStatus,
tags,
timeout));
}
private static Hashtable BuildProperties(string channel, string connectionInfo, string userName = null, string password = null)
{
Hashtable properties = new Hashtable {
{MQC.CHANNEL_PROPERTY, channel},
{MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED},
{MQC.CONNECTION_NAME_PROPERTY, connectionInfo}
};

if (!string.IsNullOrEmpty(userName))
properties.Add(MQC.USER_ID_PROPERTY, userName);
if (!string.IsNullOrEmpty(password))
properties.Add(MQC.PASSWORD_PROPERTY, password);

return properties;
}
}
}
24 changes: 24 additions & 0 deletions src/HealthChecks.IbmMQ/HealthChecks.IbmMQ.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(NetStandardTargetVersion)</TargetFramework>
<PackageLicenseUrl>$(PackageLicenseUrl)</PackageLicenseUrl>
<PackageProjectUrl>$(PackageProjectUrl)</PackageProjectUrl>
<PackageTags>HealthCheck;IbmMQ</PackageTags>
<Description>HealthChecks.IbmMQ is the health check package for IbmMQ.</Description>
<Version>$(HealthCheckIbmMQ)</Version>
<RepositoryUrl>$(RepositoryUrl)</RepositoryUrl>
<Company>$(Company)</Company>
<Authors>$(Authors)</Authors>
<LangVersion>latest</LangVersion>
<PackageId>AspNetCore.HealthChecks.IbmMQ</PackageId>
<PublishRepositoryUrl>$(PublishRepositoryUrl)</PublishRepositoryUrl>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder)</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="IBMMQDotnetClient" Version="9.1.4" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="$(MicrosoftExtensionsDiagnosticsHealthChecks)" />
</ItemGroup>

</Project>
38 changes: 38 additions & 0 deletions src/HealthChecks.IbmMQ/IbmMQHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using IBM.WMQ;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System;
using System.Collections;
using System.Threading;
using System.Threading.Tasks;

namespace HealthChecks.IbmMQ
{
public class IbmMQHealthCheck : IHealthCheck
{
private readonly Hashtable _connectionProperties;
private readonly string _queueManager;

public IbmMQHealthCheck(string queueManager, Hashtable connectionProperties)
{
_queueManager = queueManager;
_connectionProperties = connectionProperties;
}

public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
using (var connection = new MQQueueManager(_queueManager, _connectionProperties))
{
return Task.FromResult(
HealthCheckResult.Healthy());
}
}
catch (Exception ex)
{
return Task.FromResult(
new HealthCheckResult(context.Registration.FailureStatus, exception: ex));
}
}
}
}
41 changes: 41 additions & 0 deletions src/HealthChecks.IbmMQ/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# IbmMQ Health Check

This health check verifies the ability to communicate with a IbmMQ 9.0.+ server

## Example Usage

With all of the following examples, you can additionally add the following parameters:

- `name`: The health check name. Default if not specified is `ibmmq`.
- `failureStatus`: The `HealthStatus` that should be reported when the health check fails. Default is `HealthStatus.Unhealthy`.
- `tags`: A list of tags that can be used to filter sets of health checks.
- `timeout`: A `System.TimeSpan` representing the timeout of the check.

### Basic

Use the extension method where you provide the queue manager name and the connection properties.

```cs
public void ConfigureServices(IServiceCollection services)
{
Hashtable connectionProperties = new Hashtable {
{MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_BINDINGS},
};
services
.AddHealthChecks()
.AddIbmMQ("QM.TEST.01", connectionProperties);
}
```

### Using Managed Client Connection

For `MQC.TRANSPORT_MQSERIES_MANAGED` connection you can use the following conveniece extension method where you need to specified the channel and the host(port) information. User and password are optional parameters.

```cs
public void ConfigureServices(IServiceCollection services)
{
services
.AddHealthChecks()
.AddIbmMQManagedConnection("QM.TEST.01", "DEV.APP.SVRCONN", "localhost(1417)", userName: "app", password: "xxx");
}
```
1 change: 1 addition & 0 deletions test/FunctionalTests/FunctionalTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<ProjectReference Include="..\..\src\HealthChecks.DynamoDb\HealthChecks.DynamoDb.csproj" />
<ProjectReference Include="..\..\src\HealthChecks.EventStore\HealthChecks.EventStore.csproj" />
<ProjectReference Include="..\..\src\HealthChecks.Elasticsearch\HealthChecks.Elasticsearch.csproj" />
<ProjectReference Include="..\..\src\HealthChecks.IbmMQ\HealthChecks.IbmMQ.csproj" />
<ProjectReference Include="..\..\src\HealthChecks.IdSvr\HealthChecks.IdSvr.csproj" />
<ProjectReference Include="..\..\src\HealthChecks.Kafka\HealthChecks.Kafka.csproj" />
<ProjectReference Include="..\..\src\HealthChecks.MongoDb\HealthChecks.MongoDb.csproj" />
Expand Down
Loading

0 comments on commit c71ed12

Please sign in to comment.