【问题标题】:Azure IoT Hub Operations MonitoringAzure IoT 中心操作监控
【发布时间】:2017-09-15 09:10:36
【问题描述】:

我有一个将设备连接到的 Azure IoT 中心。我想启用监控以监控与集线器连接和断开连接的设备。

我已在我的 IoT 集线器的监控类别中启用 Verbose on Connections:

我的设备连接到我的 Hub 并显示在设备资源管理器中:

然后我设置了一个 Azure 函数来将我的数据从操作监控记录到 Azure SQL 数据库:

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; }
}

如果我在操作监控中启用设备身份操作,我会记录创建事件。所以我相信函数的输入是正确的。但是,没有为 Connections 创建任何内容???

我也可以很好地向我连​​接的设备发送消息。我只是没有看到任何连接/断开连接的事件。

我还尝试创建流分析并对输入进行采样,一段时间内我知道我有连接/断开连接,但没有找到任何东西。

【问题讨论】:

标签: c# azure iot azure-iot-hub


【解决方案1】:

我通过在开发环境中强制我的设备通过 MQTT 连接解决了这个问题,现在我看到它正在连接和断开连接。

很遗憾,我无法在生产环境中使用 MQTT。

有谁知道连接/断开事件是否只能在 MQTT 中而不是 AMQP 中发生?

【讨论】:

  • HTTP 怎么样?
  • HTTP 根本不显示连接状态,如果我通过 HTTP 连接设备,设备资源管理器总是将其显示为已断开连接。
  • HTTP 完全不显示连接/连接状态。但是 AMQP 和 MQTT 对我有用……您的 IoT 中心定价层是什么样的?您在创建中心时选择了哪个位置?
  • Hub 是西欧,层是免费的,因为我们仍处于开发阶段。
  • 我的是 S1 层。根据您的评论,我也尝试过免费套餐,但我什至无法成功保存监控类别设置。我会将此问题报告给内部相关团队。
【解决方案2】:

如果您只想获取设备连接状态,请改用 REST。

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

另外,还有一个在线工具可以监控设备的连接状态。

https://iotdevtool.com/registry/

【讨论】:

    【解决方案3】:

    我们发明了一种数据流来确定“设备状态”。我们创建了一个流分析作业,连接到 Operations monitoring(即 IOT 输入定义中的 Endpoint 类型)。我们有流分析查询 SELECT INTO 一个 ServiceBus queue 。我们有一个 WebJob 处理队列并更新持久存储(SQL 表、Azure 表、您的选择),我们在其中记下状态。然后,UI(或 WebAPI 控制器)可以检查该持久存储。

    到目前为止,我们已将所有 Monitoring categories(在 IOT Hub 门户刀片中)设置为 Verbose,但以后可能会拨出。

    我们确实得到了数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多