How to Monitor a particular windows service and send the metrics to Azure IOThub

Singh, Prabhu 6 Reputation points
2024-02-12T16:21:26.07+00:00

Hi Team, I have a requirement to monitor a Windows service (to check its status of being up and running) that is operating within a private network on Windows Server 2019. Currently, we are already utilizing Azure IoT Hub to send messages from IoT devices within the private network. My question is: Can we leverage the same route (IoT Hub) to send the status of the Windows services status to IoT Hub? If yes, what options are available to achieve this?

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,133 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Sander van de Velde 29,456 Reputation points MVP
    2024-02-12T19:07:19+00:00

    Hello @Singh, Prabhu,

    welcome to this moderated Azure community forum.

    Using the Azure IoT Hub, you can send whatever information you want to the cloud.

    If you are using an application via the Azure IoT Device SDK, you need to program the data collection yourself.

    If you are using Azure IoT Edge for running logic on your device, you can check the Metrics collector module. Next to metrics about individual (Docker) modules, it also shows information about the host (disk space available, memory usage).

    Because you are not running your logic on Linux, you cannot make use of the OS Config agent:

    IoT/Edge solution builders and operators often need to configure and observe device settings. Common examples include network settings, hostnames, time zones, security benchmarks, firewall rules, ssh users, and so on.

    As an alternative, check out Azure Arc:

    Azure Arc simplifies governance and management by delivering a consistent multicloud and on-premises management platform... Manage Windows and Linux physical servers and virtual machines hosted outside of Azure...

    I recommend reading a bit more about the different topics and then try it out if these solutions are offering what you need.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.

    1 person found this answer helpful.
    0 comments No comments

  2. LeelaRajeshSayana-MSFT 13,781 Reputation points
    2024-02-21T01:23:16.0366667+00:00

    Hi @Singh, Prabhu Thank you for posting the question here.

    @Sander van de Velde response provides correct guidance on different approaches we can take in tracking the status of a windows service and sending it to an IoT Hub. I am adding to the response by providing a sample implementation using .Net SDK. Please find the sample below.

    using Microsoft.Azure.Devices.Client;
    using Newtonsoft.Json;
    using System;
    using System.Text;
    using System.Threading.Tasks;
    using System.ServiceProcess;
    
    
    namespace ServiceMonitor
    {
        internal class Program
        {
            static async Task Main(string[] args)
            {
    
                try
                {
                    string serviceName = "Mosquitto Broker";//Replace with the service you like to monitor
                    ServiceController sc = new ServiceController(serviceName);
                    // Get the current status of the service
                    ServiceControllerStatus status = sc.Status;
                    // Log the service status
                    Console.WriteLine($"Service Status: {status}");
                    // Connection string for the IoT Hub device
                    string connectionString = "<Provide your device connection string>";
                    // Create an instance of the DeviceClient class
                    DeviceClient deviceClient = DeviceClient.CreateFromConnectionString(connectionString, TransportType.Mqtt);
                    // Create a telemetry message
                    var telemetryDataPoint = new
                    {
                        service = serviceName,
                        servicestatus = status.ToString()
                    };
                    var messageString = JsonConvert.SerializeObject(telemetryDataPoint);
                    Message message = new Message(Encoding.ASCII.GetBytes(messageString));
                    message.ContentEncoding = "utf-8";
                    message.ContentType = "application/json";
                    // Send the telemetry message
                    await deviceClient.SendEventAsync(message);
                    Console.WriteLine("Telemetry sent");
    
                }
                catch (Exception ex)
                {
                    // Log any exceptions that may occur
                    Console.WriteLine($"Error: {ex.Message}");
                }
            }
        }
    }
    

    I have my messages routed to a Blob storage container and I could see the following message at the routed endpoint once the code is executed.

    {"EnqueuedTimeUtc":"2024-02-21T01:16:58.7770000Z","Properties":{},"SystemProperties":{"connectionDeviceId":"TestDevice1","connectionAuthMethod":"{\"scope\":\"device\",\"type\":\"sas\",\"issuer\":\"iothub\",\"acceptingIpFilterRule\":null}","connectionDeviceGenerationId":"638012746782284609","contentType":"application/json","contentEncoding":"utf-8","enqueuedTime":"2024-02-21T01:16:58.7770000Z"},"Body":{"service":"Mosquitto Broker","servicestatus":"Stopped"}}
    

    Hope this helps! If you are using a different approach to send telemetry data from your system or using a different SDK, kindly share more details with us so we can assist you further on this request.


    If the response helped, please do click Accept Answer and Yes for the answer provided. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.

    0 comments No comments