0
votes

I have an Azure IoT hub that I connect devices to. I want to enable monitoring to monitor devices connecting and disconnecting from the hub.

I've enabled Verbose on Connections in the monitoring categories for my Iot hub:

enter image description here

My devices connect to my Hub and show in Device Explorer:

enter image description here

I then have an Azure Function set to log my data from the Operations Monitoring to an Azure SQL db:

using System;
using System.Configuration;
using System.Data.SqlClient;
using Newtonsoft.Json;

public static void Run(string myEventHubMessage, TraceWriter log)
{
    log.Info(myEventHubMessage);

    try
    {
        var connectionString = ConfigurationManager.ConnectionStrings["iotAzureSQLDb"].ConnectionString;

        log.Info(connectionString);
        var message = JsonConvert.DeserializeObject<MonitoringMessage>(myEventHubMessage);

        using (var connection = new SqlConnection(connectionString))
        {
            var sqlStatement = "insert into [dbo].[DeviceStatuses] " +
                                "(DeviceId, ConnectionStatus, ConnectionUpdateTime)" +
                                "values " +
                                "(@DeviceId, @ConnectionStatus, @ConnectionUpdateTime)";
            using (var dataCommand = new SqlCommand(sqlStatement, connection))
            {
                dataCommand.Parameters.AddWithValue("@ConnectionStatus", message.operationName);
                dataCommand.Parameters.AddWithValue("@DeviceId", message.deviceId);
                dataCommand.Parameters.AddWithValue("@ConnectionUpdateTime", message.time);

                connection.Open();
                dataCommand.ExecuteNonQuery();
                connection.Close();

                log.Info($"Device {message.deviceId} changed state: {message.operationName}");
            }
        }
    }
    catch (Exception ex)
    {
        log.Info(ex.Message);
    }
}

public class MonitoringMessage
{
    public string deviceId { get; set; }
    public string operationName { get; set; }
    public int? durationMs { get; set; }
    public string authType { get; set; }
    public string protocol { get; set; }
    public DateTimeOffset? time { get; set; }
    public string category { get; set; }
    public string level { get; set; }
    public int? statusCode { get; set; }
    public int? statusType { get; set; }
    public string statusDescription { get; set; }
}

If I enable Device Identity Operations in Operations Monitoring, I get create events being logged. So I'm confident the inputs to the function is correct. However, nothing is ever created for Connections???

I can also send messages to my connected devices fine. I'm just seeing no events for connections / disconnections.

I've also tried creating a Stream Analytics and sampling the input for a period where I know I have connections / disconnections and nothing is being found.

3
That's what I'm using an alternative, the problem is you have to poll it every 'x' minutes to get what's connected / disconnected. Whereas if the IoT Monitoring worked properly, I would receive events when a device disconnects.Stuart

3 Answers

0
votes

I have resolved this by forcing my device to connect over MQTT in the dev environment and now I see it connecting and disconnecting.

Unfortunately I can't use MQTT in production.

Does anyone know if Connection / Disconnection events are only possible in MQTT as opposed to AMQP?

0
votes

If you just would like to get devices connection states, use REST instead.

https://docs.microsoft.com/en-us/rest/api/iothub/deviceapi#DeviceApi_GetDevices

Also, here's an online tool to monitor devices connection state.

https://iotdevtool.com/registry/ enter image description here

0
votes

We invented a data flow to determine "device state". We created a Stream Analytics Job, wired to the Operations monitoring (that's the Endpoint type in the IOT Input definition). We have the Stream Analytics query SELECT INTO a ServiceBus queue . We have a WebJob processing that queue and updating a persistent store (SQL Table, Azure Table, your pick) where we write down the status. A UI (or WebAPI Controller) can then inspect that persistent store.

So far we have all the Monitoring categories (in the IOT Hub Portal blade) set to Verbose but may dial it down later.

We do get data.