Search code examples
azureazure-iot-hubazure-iot-edge

How to get the running status of IOT Edge Modules via Azure REST API? (not the connectionState or status from the 'Get Modules on Device' API Call)


Context:

Azure REST - Modules - Get Modules On Device

By using this API call, I can get information about connectionState(connected/disconnected) & status(enalbled/disabled) of modules. We can check the runtime status of the modules deployed on the device by visiting the Azure Iot Hub web portal portal.azure.com -> iot hub -> iot edge section -> select the device you wish to find the details for

Question: enter image description here How can I get this RUNTIME STATUS via the Azure API?(please refer to the picture).


Solution

  • If you check the module twin of edgeAgent it is like below:-

    {
      "deviceId": "edgeDevice",
      "moduleId": "$edgeAgent",
      "etag": "AAAAAAAAAEA=",
      "deviceEtag": "NDU1OTY3MjA=",
      "status": "enabled",
      "statusUpdateTime": "0001-01-01T00:00:00Z",
      "connectionState": "Disconnected",
      "lastActivityTime": "0001-01-01T00:00:00Z",
      "cloudToDeviceMessageCount": 0,
      "authenticationType": "sas",
      "x509Thumbprint": {
        "primaryThumbprint": null,
        "secondaryThumbprint": null
      },
      "version": 501,
      "properties": {
        "desired": {
          "schemaVersion": "1.0",
          "runtime": {
            "type": "docker",
            "settings": {
              "minDockerVersion": "v1.25",
              "loggingOptions": "",
              "registryCredentials": {
              }
            }
          },
          "systemModules": {
            "edgeAgent": {
              "type": "docker",
              "settings": {
                "image": "mcr.microsoft.com/azureiotedge-agent:1.0",
                "createOptions": "{}"
              }
            },
            "edgeHub": {
              "type": "docker",
              "status": "running",
              "restartPolicy": "always",
              "settings": {
                "image": "mcr.microsoft.com/azureiotedge-hub:1.0",
                "createOptions": "{\"HostConfig\":{\"PortBindings\":{\"5671/tcp\":[{\"HostPort\":\"5671\"}],\"8883/tcp\":[{\"HostPort\":\"8883\"}],\"443/tcp\":[{\"HostPort\":\"443\"}]}}}"
              }
            }
          },
          "modules": {
            "CustomModuleName": {
              "version": "1.0",
              "type": "docker",
              "status": "running",
              "restartPolicy": "always",
              "settings": {
                "image": "iotregdev300.azurecr.io/customModuleName:0.0.2-amd64",
                "createOptions": "{\"HostConfig\":{\"PortBindings\":{\"8080/tcp\":[{\"HostPort\":\"8080\"}]}}}"
              }
            }
          }
        },
        "reported": {
          "schemaVersion": "1.0",
          "version": {
            "version": "1.0.9.4",
            "build": "32971639",
            "commit": "12d55e582cc7ce95c8abfe11eddfbbc938ed6001"
          },
          "lastDesiredStatus": {
            "code": 200,
            "description": ""
          },
          "runtime": {
            "platform": {
              "os": "linux",
              "architecture": "x86_64",
              "version": "1.0.9.4"
            },
            "type": "docker",
            "settings": {
              "minDockerVersion": "v1.25",
              "loggingOptions": "",
              "registryCredentials": {
              }
            }
          },
          "systemModules": {
            "edgeAgent": {
              "type": "docker",
              "exitCode": 0,
              "statusDescription": "running",
              "lastStartTimeUtc": "2020-09-09T07:34:34.4585643Z",
              "lastExitTimeUtc": "2020-09-09T07:34:26.9869915Z",
              "runtimeStatus": "running",
              "imagePullPolicy": "on-create",
              "settings": {
                "image": "mcr.microsoft.com/azureiotedge-agent:1.0",
                "imageHash": "sha256:1a2fffc3c74a2b2510a3149bb2295b68a553e4c9aca90698879902f36fd6d163",
                "createOptions": "{}"
              }
            },
            "edgeHub": {
              "type": "docker",
              "status": "running",
              "restartPolicy": "always",
              "imagePullPolicy": "on-create",
              "env": {},
              "exitCode": 0,
              "statusDescription": "running",
              "lastStartTimeUtc": "2020-09-09T07:34:50.8012461Z",
              "lastExitTimeUtc": "2020-09-09T07:34:26.9845717Z",
              "restartCount": 0,
              "lastRestartTimeUtc": "2020-09-09T07:34:26.9845717Z",
              "runtimeStatus": "running",
              "settings": {
                "image": "mcr.microsoft.com/azureiotedge-hub:1.0",
                "imageHash": "sha256:f531eb6c23f347c37ea8c90204e9cb12024aec77d8b2e68e93b14c38ec066520",
                "createOptions": "{\"HostConfig\":{\"PortBindings\":{\"5671/tcp\":[{\"HostPort\":\"5671\"}],\"8883/tcp\":[{\"HostPort\":\"8883\"}],\"443/tcp\":[{\"HostPort\":\"443\"}]}}}"
              }
            }
          },
          "lastDesiredVersion": 64,
          "modules": {
            "CustomModuleName": {
              "exitCode": 0,
              "statusDescription": "running",
              "lastStartTimeUtc": "2020-09-09T07:34:49.3923079Z",
              "lastExitTimeUtc": "2020-09-09T07:34:26.9606688Z",
              "restartCount": 0,
              "lastRestartTimeUtc": "2020-09-09T07:34:26.9606688Z",
              "runtimeStatus": "running",
              "version": "1.0",
              "status": "running",
              "restartPolicy": "always",
              "imagePullPolicy": "on-create",
              "type": "docker",
              "settings": {
                "image": "iotregdev300.azurecr.io/custommodulename:0.0.2-amd64",
                "imageHash": "sha256:e728d4b8804d2114beab7c1903f706d8152e404be3f5601ee5e7371e8ac32ecf",
                "createOptions": "{\"HostConfig\":{\"PortBindings\":{\"8080/tcp\":[{\"HostPort\":\"8080\"}]}}}"
              },
              "env": {}
            }
          }
        }
      }
    }
    

    In the above json, CustomModuleName is the custom module and it has the field called runtimeStatus: "running". Same field exists in the edgeHub and edgeAgent modules too. So you need to just fetch the edgeAgentTwin through REST API or Azure Device/Service sdk.