Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c8fa483

Browse files
authored
Add custom metrics to defender demo (#507)
Adds custom metrics to the defender demo. The metrics added are a list of the task ids and the stack high water mark.
1 parent e61730c commit c8fa483

File tree

8 files changed

+380
-121
lines changed

8 files changed

+380
-121
lines changed

FreeRTOS-Plus/Demo/AWS/Device_Defender_Windows_Simulator/Device_Defender_Demo/DemoTasks/DefenderDemoExample.c

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,20 @@
3535
* with another MQTT library. This demo requires using the AWS IoT broker as
3636
* Device Defender is an AWS service.
3737
*
38-
* This demo connects to the AWS IoT broker and subscribes to the device
39-
* defender topics. It then collects metrics for the open ports and sockets on
40-
* the device using FreeRTOS+TCP, and generates a device defender report. The
38+
* This demo subscribes to the device defender topics. It then collects metrics
39+
* for the open ports and sockets on the device using FreeRTOS+TCP. Additonally
40+
* the stack high water mark and task IDs are collected for custom metrics.
41+
* These metrics are used to generate a device defender report. The
4142
* report is then published, and the demo waits for a response from the device
42-
* defender service. Upon receiving the response or timing out, the demo
43-
* finishes.
43+
* defender service. Upon receiving an accepted response, the demo finishes.
44+
* If the demo receives a rejected response or times out, the demo repeats up to
45+
* a maximum of DEFENDER_MAX_DEMO_LOOP_COUNT times.
4446
*
4547
* This demo sets the report ID to xTaskGetTickCount(), which may collide if
4648
* the device is reset. Reports for a Thing with a previously used report ID
4749
* will be assumed to be duplicates and discarded by the Device Defender
4850
* service. The report ID needs to be unique per report sent with a given
49-
* Thing. We recommend using an increasing unique id such as the current
51+
* Thing. We recommend using an increasing unique ID such as the current
5052
* timestamp.
5153
*/
5254

@@ -97,7 +99,7 @@
9799
#define DEFENDER_RESPONSE_WAIT_SECONDS ( 2 )
98100

99101
/**
100-
* @brief Name of the report id field in the response from the AWS IoT Device
102+
* @brief Name of the report ID field in the response from the AWS IoT Device
101103
* Defender service.
102104
*/
103105
#define DEFENDER_RESPONSE_REPORT_ID_FIELD "reportId"
@@ -133,8 +135,8 @@ typedef enum
133135
ReportStatusRejected
134136
} ReportStatus_t;
135137

136-
/**
137-
* @brief Each compilation unit that consumes the NetworkContext must define it.
138+
/**
139+
* @brief Each compilation unit that consumes the NetworkContext must define it.
138140
* It should contain a single pointer to the type of your desired transport.
139141
* When using multiple transports in the same compilation unit, define this pointer as void *.
140142
*
@@ -195,6 +197,16 @@ static uint16_t pusOpenUdpPorts[ democonfigOPEN_UDP_PORTS_ARRAY_SIZE ];
195197
*/
196198
static Connection_t pxEstablishedConnections[ democonfigESTABLISHED_CONNECTIONS_ARRAY_SIZE ];
197199

200+
/**
201+
* @brief Array of task statuses, used to generate custom metrics.
202+
*/
203+
static TaskStatus_t pxTaskStatusList[ democonfigCUSTOM_METRICS_TASKS_ARRAY_SIZE ];
204+
205+
/**
206+
* @brief Task numbers custom metric array.
207+
*/
208+
static uint32_t pulCustomMetricsTaskNumbers[ democonfigCUSTOM_METRICS_TASKS_ARRAY_SIZE ];
209+
198210
/**
199211
* @brief All the metrics sent in the device defender report.
200212
*/
@@ -211,9 +223,10 @@ static ReportStatus_t xReportStatus;
211223
static char pcDeviceMetricsJsonReport[ democonfigDEVICE_METRICS_REPORT_BUFFER_SIZE ];
212224

213225
/**
214-
* @brief Report Id sent in the defender report.
226+
* @brief Report ID sent in the defender report.
215227
*/
216228
static uint32_t ulReportId = 0UL;
229+
217230
/*-----------------------------------------------------------*/
218231

219232
/**
@@ -463,7 +476,9 @@ static bool prvCollectDeviceMetrics( void )
463476
{
464477
bool xStatus = false;
465478
eMetricsCollectorStatus eMetricsCollectorStatus;
466-
uint32_t ulNumOpenTcpPorts = 0UL, ulNumOpenUdpPorts = 0UL, ulNumEstablishedConnections = 0UL;
479+
uint32_t ulNumOpenTcpPorts = 0UL, ulNumOpenUdpPorts = 0UL, ulNumEstablishedConnections = 0UL, i;
480+
UBaseType_t uxTasksWritten = { 0 };
481+
TaskStatus_t pxTaskStatus = { 0 };
467482

468483
/* Collect bytes and packets sent and received. */
469484
eMetricsCollectorStatus = eGetNetworkStats( &( xNetworkStats ) );
@@ -516,6 +531,36 @@ static bool prvCollectDeviceMetrics( void )
516531
}
517532
}
518533

534+
/* Collect custom metrics. This demo sends this task's stack high water mark
535+
* as a number type custom metric and the current task IDs as a list of
536+
* numbers type custom metric. */
537+
if( eMetricsCollectorStatus == eMetricsCollectorSuccess )
538+
{
539+
vTaskGetInfo(
540+
/* Query this task. */
541+
NULL,
542+
&pxTaskStatus,
543+
/* Include the stack high water mark value. */
544+
pdTRUE,
545+
/* Don't include the task state in the TaskStatus_t structure. */
546+
0 );
547+
uxTasksWritten = uxTaskGetSystemState( pxTaskStatusList, democonfigCUSTOM_METRICS_TASKS_ARRAY_SIZE, NULL );
548+
549+
if( uxTasksWritten == 0 )
550+
{
551+
eMetricsCollectorStatus = eMetricsCollectorCollectionFailed;
552+
LogError( ( "Failed to collect system state. uxTaskGetSystemState() failed due to insufficient buffer space.",
553+
eMetricsCollectorStatus ) );
554+
}
555+
else
556+
{
557+
for( i = 0; i < uxTasksWritten; i++ )
558+
{
559+
pulCustomMetricsTaskNumbers[ i ] = pxTaskStatusList[ i ].xTaskNumber;
560+
}
561+
}
562+
}
563+
519564
/* Populate device metrics. */
520565
if( eMetricsCollectorStatus == eMetricsCollectorSuccess )
521566
{
@@ -527,6 +572,9 @@ static bool prvCollectDeviceMetrics( void )
527572
xDeviceMetrics.ulOpenUdpPortsArrayLength = ulNumOpenUdpPorts;
528573
xDeviceMetrics.pxEstablishedConnectionsArray = &( pxEstablishedConnections[ 0 ] );
529574
xDeviceMetrics.ulEstablishedConnectionsArrayLength = ulNumEstablishedConnections;
575+
xDeviceMetrics.ulStackHighWaterMark = pxTaskStatus.usStackHighWaterMark;
576+
xDeviceMetrics.pulTaskIdArray = pulCustomMetricsTaskNumbers;
577+
xDeviceMetrics.ulTaskIdArrayLength = uxTasksWritten;
530578
}
531579

532580
return xStatus;
@@ -671,14 +719,14 @@ void prvDefenderDemoTask( void * pvParameters )
671719
* DEFENDER_MAX_DEMO_LOOP_COUNT times. */
672720
do
673721
{
674-
/* Set a report Id to be used.
722+
/* Set a report ID to be used.
675723
*
676724
* !!!NOTE!!!
677725
* This demo sets the report ID to xTaskGetTickCount(), which may collide
678726
* if the device is reset. Reports for a Thing with a previously used
679727
* report ID will be assumed to be duplicates and discarded by the Device
680728
* Defender service. The report ID needs to be unique per report sent with
681-
* a given Thing. We recommend using an increasing unique id such as the
729+
* a given Thing. We recommend using an increasing unique ID such as the
682730
* current timestamp. */
683731
ulReportId = ( uint32_t ) xTaskGetTickCount();
684732

FreeRTOS-Plus/Demo/AWS/Device_Defender_Windows_Simulator/Device_Defender_Demo/FreeRTOSConfig.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 60 ) /* In this simulated case, the stack only has to hold one small structure as the real stack is part of the Win32 thread. */
4949
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 2048U * 1024U ) )
5050
#define configMAX_TASK_NAME_LEN ( 15 )
51-
#define configUSE_TRACE_FACILITY 0
51+
#define configUSE_TRACE_FACILITY 1
52+
#define configUSE_STATS_FORMATTING_FUNCTIONS 0
5253
#define configUSE_16_BIT_TICKS 0
5354
#define configIDLE_SHOULD_YIELD 1
5455
#define configUSE_CO_ROUTINES 0
@@ -103,15 +104,6 @@
103104
#define INCLUDE_xTimerPendFunctionCall 1
104105
#define INCLUDE_pcTaskGetTaskName 1
105106

106-
/* This demo makes use of one or more example stats formatting functions. These
107-
* format the raw data provided by the uxTaskGetSystemState() function in to human
108-
* readable ASCII form. See the notes in the implementation of vTaskList() within
109-
* FreeRTOS/Source/tasks.c for limitations. configUSE_STATS_FORMATTING_FUNCTIONS
110-
* is set to 2 so the formatting functions are included without the stdio.h being
111-
* included in tasks.c. That is because this project defines its own sprintf()
112-
* functions. */
113-
#define configUSE_STATS_FORMATTING_FUNCTIONS 1
114-
115107
/* Assert call defined for debug builds. */
116108
#ifdef _DEBUG
117109
extern void vAssertCalled( const char * pcFile,

FreeRTOS-Plus/Demo/AWS/Device_Defender_Windows_Simulator/Device_Defender_Demo/defender_config.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,44 @@
5252

5353
/************ End of logging configuration ****************/
5454

55+
/**
56+
* AWS IoT Device Defender Service supports both long and short names for keys
57+
* in the report sent by a device. For example,
58+
*
59+
* A device defender report using long key names:
60+
* {
61+
* "header": {
62+
* "report_id": 1530304554,
63+
* "version": "1.0"
64+
* },
65+
* "metrics": {
66+
* "network_stats": {
67+
* "bytes_in": 29358693495,
68+
* "bytes_out": 26485035,
69+
* "packets_in": 10013573555,
70+
* "packets_out": 11382615
71+
* }
72+
* }
73+
* }
74+
*
75+
* An equivalent report using short key names:
76+
* {
77+
* "hed": {
78+
* "rid": 1530304554,
79+
* "v": "1.0"
80+
* },
81+
* "met": {
82+
* "ns": {
83+
* "bi": 29358693495,
84+
* "bo": 26485035,
85+
* "pi": 10013573555,
86+
* "po": 11382615
87+
* }
88+
* }
89+
* }
90+
*
91+
* Set to 1 to enable use of long key names in the defender report.
92+
*/
93+
#define DEFENDER_USE_LONG_KEYS 0
94+
5595
#endif /* ifndef DEFENDER_CONFIG_H_ */

FreeRTOS-Plus/Demo/AWS/Device_Defender_Windows_Simulator/Device_Defender_Demo/demo_config.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,13 @@ extern void vLoggingPrintf( const char * pcFormatString,
261261
*/
262262
#define democonfigESTABLISHED_CONNECTIONS_ARRAY_SIZE 10
263263

264+
/**
265+
* @brief Size of the task numbers array.
266+
*
267+
* This must be at least the number of tasks used.
268+
*/
269+
#define democonfigCUSTOM_METRICS_TASKS_ARRAY_SIZE 10
270+
264271
/**
265272
* @brief Size of the buffer which contains the generated device defender report.
266273
*

FreeRTOS-Plus/Demo/AWS/Device_Defender_Windows_Simulator/Device_Defender_Demo/metrics_collector.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@
4343
#include "FreeRTOS.h"
4444
#include "FreeRTOS_IP.h"
4545

46-
/* FreeRTOS+TCP tcp_netstat utility include. */
47-
#include "tcp_netstat.h"
48-
4946
/* Demo config. */
5047
#include "demo_config.h"
5148

0 commit comments

Comments
 (0)