From 04ef5326dbd92e964bbfacb4408960f6fd839d43 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Wed, 28 Jun 2023 22:03:14 +0100 Subject: [PATCH] Extend metrics to all packets. Not published in $SYS, may be made available for plugins. --- lib/send_connect.c | 2 ++ lib/send_disconnect.c | 3 +++ lib/send_mosq.c | 8 ++++++++ lib/send_subscribe.c | 4 ++++ lib/send_unsubscribe.c | 2 ++ src/read_handle.c | 13 +++++++++++++ src/send_auth.c | 1 + src/send_connack.c | 1 + src/send_suback.c | 1 + src/send_unsuback.c | 1 + src/sys_tree.c | 27 +++++++++++++++++++++++++++ src/sys_tree.h | 33 ++++++++++++++++++++++++++++++--- 12 files changed, 93 insertions(+), 3 deletions(-) diff --git a/lib/send_connect.c b/lib/send_connect.c index c60a13c9..29349fec 100644 --- a/lib/send_connect.c +++ b/lib/send_connect.c @@ -23,6 +23,7 @@ Contributors: #ifdef WITH_BROKER # include "mosquitto_broker_internal.h" +# include "sys_tree.h" #endif #include "logging_mosq.h" @@ -200,6 +201,7 @@ int send__connect(struct mosquitto *mosq, uint16_t keepalive, bool clean_session # ifdef WITH_BRIDGE log__printf(mosq, MOSQ_LOG_DEBUG, "Bridge %s sending CONNECT", SAFE_PRINT(clientid)); # endif + metrics__int_inc(mosq_counter_mqtt_connect_sent, 1); #else log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending CONNECT", SAFE_PRINT(clientid)); #endif diff --git a/lib/send_disconnect.c b/lib/send_disconnect.c index 6f291316..4c0d1283 100644 --- a/lib/send_disconnect.c +++ b/lib/send_disconnect.c @@ -78,6 +78,9 @@ int send__disconnect(struct mosquitto *mosq, uint8_t reason_code, const mosquitt } } +#ifdef WITH_BROKER + metrics__int_inc(mosq_counter_mqtt_disconnect_sent, 1); +#endif return packet__queue(mosq, packet); } diff --git a/lib/send_mosq.c b/lib/send_mosq.c index 9eeac172..c4c6ff7d 100644 --- a/lib/send_mosq.c +++ b/lib/send_mosq.c @@ -51,6 +51,9 @@ int send__pingreq(struct mosquitto *mosq) rc = send__simple_command(mosq, CMD_PINGREQ); if(rc == MOSQ_ERR_SUCCESS){ mosq->ping_t = mosquitto_time(); +#ifdef WITH_BROKER + metrics__int_inc(mosq_counter_mqtt_pingreq_sent, 1); +#endif } return rc; } @@ -59,6 +62,7 @@ int send__pingresp(struct mosquitto *mosq) { #ifdef WITH_BROKER log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PINGRESP to %s", SAFE_PRINT(mosq->id)); + metrics__int_inc(mosq_counter_mqtt_pingresp_sent, 1); #else log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PINGRESP", SAFE_PRINT(mosq->id)); #endif @@ -69,6 +73,7 @@ int send__puback(struct mosquitto *mosq, uint16_t mid, uint8_t reason_code, cons { #ifdef WITH_BROKER log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBACK to %s (m%d, rc%d)", SAFE_PRINT(mosq->id), mid, reason_code); + metrics__int_inc(mosq_counter_mqtt_puback_sent, 1); #else log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PUBACK (m%d, rc%d)", SAFE_PRINT(mosq->id), mid, reason_code); #endif @@ -81,6 +86,7 @@ int send__pubcomp(struct mosquitto *mosq, uint16_t mid, const mosquitto_property { #ifdef WITH_BROKER log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBCOMP to %s (m%d)", SAFE_PRINT(mosq->id), mid); + metrics__int_inc(mosq_counter_mqtt_pubcomp_sent, 1); #else log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PUBCOMP (m%d)", SAFE_PRINT(mosq->id), mid); #endif @@ -94,6 +100,7 @@ int send__pubrec(struct mosquitto *mosq, uint16_t mid, uint8_t reason_code, cons { #ifdef WITH_BROKER log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBREC to %s (m%d, rc%d)", SAFE_PRINT(mosq->id), mid, reason_code); + metrics__int_inc(mosq_counter_mqtt_pubrec_sent, 1); #else log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PUBREC (m%d, rc%d)", SAFE_PRINT(mosq->id), mid, reason_code); #endif @@ -108,6 +115,7 @@ int send__pubrel(struct mosquitto *mosq, uint16_t mid, const mosquitto_property { #ifdef WITH_BROKER log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBREL to %s (m%d)", SAFE_PRINT(mosq->id), mid); + metrics__int_inc(mosq_counter_mqtt_pubrel_sent, 1); #else log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PUBREL (m%d)", SAFE_PRINT(mosq->id), mid); #endif diff --git a/lib/send_subscribe.c b/lib/send_subscribe.c index 004efd9b..98a69663 100644 --- a/lib/send_subscribe.c +++ b/lib/send_subscribe.c @@ -23,6 +23,7 @@ Contributors: #ifdef WITH_BROKER # include "mosquitto_broker_internal.h" +# include "sys_tree.h" #endif #include "mosquitto.h" @@ -91,5 +92,8 @@ int send__subscribe(struct mosquitto *mosq, int *mid, int topic_count, char *con } #endif +#ifdef WITH_BROKER + metrics__int_inc(mosq_counter_mqtt_subscribe_sent, 1); +#endif return packet__queue(mosq, packet); } diff --git a/lib/send_unsubscribe.c b/lib/send_unsubscribe.c index b8037d21..0bc3a843 100644 --- a/lib/send_unsubscribe.c +++ b/lib/send_unsubscribe.c @@ -23,6 +23,7 @@ Contributors: #ifdef WITH_BROKER # include "mosquitto_broker_internal.h" +# include "sys_tree.h" #endif #include "mosquitto.h" @@ -86,6 +87,7 @@ int send__unsubscribe(struct mosquitto *mosq, int *mid, int topic_count, char *c log__printf(mosq, MOSQ_LOG_DEBUG, "Bridge %s sending UNSUBSCRIBE (Mid: %d, Topic: %s)", SAFE_PRINT(mosq->id), local_mid, topic[i]); } # endif + metrics__int_inc(mosq_counter_mqtt_unsubscribe_sent, 1); #else for(i=0; iid), local_mid, topic[i]); diff --git a/src/read_handle.c b/src/read_handle.c index 82280c57..16143179 100644 --- a/src/read_handle.c +++ b/src/read_handle.c @@ -40,15 +40,19 @@ int handle__packet(struct mosquitto *context) switch((context->in_packet.command)&0xF0){ case CMD_PINGREQ: + metrics__int_inc(mosq_counter_mqtt_pingreq_received, 1); rc = handle__pingreq(context); break; case CMD_PINGRESP: + metrics__int_inc(mosq_counter_mqtt_pingresp_received, 1); rc = handle__pingresp(context); break; case CMD_PUBACK: + metrics__int_inc(mosq_counter_mqtt_puback_received, 1); rc = handle__pubackcomp(context, "PUBACK"); break; case CMD_PUBCOMP: + metrics__int_inc(mosq_counter_mqtt_pubcomp_received, 1); rc = handle__pubackcomp(context, "PUBCOMP"); break; case CMD_PUBLISH: @@ -56,35 +60,44 @@ int handle__packet(struct mosquitto *context) rc = handle__publish(context); break; case CMD_PUBREC: + metrics__int_inc(mosq_counter_mqtt_pubrec_received, 1); rc = handle__pubrec(context); break; case CMD_PUBREL: + metrics__int_inc(mosq_counter_mqtt_pubrel_received, 1); rc = handle__pubrel(context); break; case CMD_CONNECT: metrics__int_inc(mosq_counter_mqtt_connect_received, 1); return handle__connect(context); case CMD_DISCONNECT: + metrics__int_inc(mosq_counter_mqtt_disconnect_received, 1); rc = handle__disconnect(context); break; case CMD_SUBSCRIBE: + metrics__int_inc(mosq_counter_mqtt_subscribe_received, 1); rc = handle__subscribe(context); break; case CMD_UNSUBSCRIBE: + metrics__int_inc(mosq_counter_mqtt_unsubscribe_received, 1); rc = handle__unsubscribe(context); break; #ifdef WITH_BRIDGE case CMD_CONNACK: + metrics__int_inc(mosq_counter_mqtt_connack_received, 1); rc = handle__connack(context); break; case CMD_SUBACK: + metrics__int_inc(mosq_counter_mqtt_suback_received, 1); rc = handle__suback(context); break; case CMD_UNSUBACK: + metrics__int_inc(mosq_counter_mqtt_unsuback_received, 1); rc = handle__unsuback(context); break; #endif case CMD_AUTH: + metrics__int_inc(mosq_counter_mqtt_auth_received, 1); rc = handle__auth(context); break; default: diff --git a/src/send_auth.c b/src/send_auth.c index bec5814a..87b462e4 100644 --- a/src/send_auth.c +++ b/src/send_auth.c @@ -60,6 +60,7 @@ int send__auth(struct mosquitto *context, uint8_t reason_code, const void *auth_ property__write_all(packet, properties, true); mosquitto_property_free_all(&properties); + metrics__int_inc(mosq_counter_mqtt_auth_sent, 1); return packet__queue(context, packet); error: mosquitto_property_free_all(&properties); diff --git a/src/send_connack.c b/src/send_connack.c index d3a48a16..e41a0743 100644 --- a/src/send_connack.c +++ b/src/send_connack.c @@ -96,6 +96,7 @@ int send__connack(struct mosquitto *context, uint8_t ack, uint8_t reason_code, c } mosquitto_property_free_all(&connack_props); + metrics__int_inc(mosq_counter_mqtt_connack_sent, 1); return packet__queue(context, packet); } diff --git a/src/send_suback.c b/src/send_suback.c index 213a901d..dd3cf312 100644 --- a/src/send_suback.c +++ b/src/send_suback.c @@ -55,5 +55,6 @@ int send__suback(struct mosquitto *context, uint16_t mid, uint32_t payloadlen, c packet__write_bytes(packet, payload, payloadlen); } + metrics__int_inc(mosq_counter_mqtt_suback_sent, 1); return packet__queue(context, packet); } diff --git a/src/send_unsuback.c b/src/send_unsuback.c index c8365eca..2cd67eaf 100644 --- a/src/send_unsuback.c +++ b/src/send_unsuback.c @@ -54,5 +54,6 @@ int send__unsuback(struct mosquitto *mosq, uint16_t mid, int reason_code_count, packet__write_bytes(packet, reason_codes, (uint32_t)reason_code_count); } + metrics__int_inc(mosq_counter_mqtt_unsuback_sent, 1); return packet__queue(mosq, packet); } diff --git a/src/sys_tree.c b/src/sys_tree.c index ea21d16f..63e217aa 100644 --- a/src/sys_tree.c +++ b/src/sys_tree.c @@ -80,9 +80,36 @@ struct metric metrics[mosq_metric_max] = { { 1, 0, "$SYS/broker/packet/out/bytes", NULL, false }, /* mosq_gauge_out_packet_bytes */ { 1, 0, "$SYS/broker/connections/socket/count", NULL, false }, /* mosq_counter_socket_connections */ { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_connect_received */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_connect_sent */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_connack_received */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_connack_sent */ { 1, 0, "$SYS/broker/publish/messages/dropped", NULL, false }, /* mosq_counter_mqtt_publish_dropped */ { 1, 0, "$SYS/broker/publish/messages/received", NULL, false }, /* mosq_counter_mqtt_publish_received */ { 1, 0, "$SYS/broker/publish/messages/sent", NULL, false }, /* mosq_counter_mqtt_publish_sent */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_puback_received */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_puback_sent */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_pubrec_received */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_pubrec_sent */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_pubrel_received */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_pubrel_sent */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_pubcomp_received */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_pubcomp_sent */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_subscribe_received */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_subscribe_sent */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_suback_received */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_suback_sent */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_unsubscribe_received */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_unsubscribe_sent */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_unsuback_received */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_unsuback_sent */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_pingreq_received */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_pingreq_sent */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_pingresp_received */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_pingresp_sent */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_disconnect_received */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_disconnect_sent */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_auth_received */ + { 1, 0, NULL, NULL, false }, /* mosq_counter_mqtt_auth_sent */ }; struct metric_load metric_loads[mosq_metric_load_max] = { diff --git a/src/sys_tree.h b/src/sys_tree.h index de5ec5f2..45540cb8 100644 --- a/src/sys_tree.h +++ b/src/sys_tree.h @@ -45,9 +45,36 @@ enum mosq_metric_type{ mosq_gauge_out_packet_bytes = 19, mosq_counter_socket_connections = 20, mosq_counter_mqtt_connect_received = 21, - mosq_counter_mqtt_publish_dropped = 22, - mosq_counter_mqtt_publish_received = 23, - mosq_counter_mqtt_publish_sent = 24, + mosq_counter_mqtt_connect_sent = 22, + mosq_counter_mqtt_connack_received = 23, + mosq_counter_mqtt_connack_sent = 24, + mosq_counter_mqtt_publish_dropped = 25, + mosq_counter_mqtt_publish_received = 26, + mosq_counter_mqtt_publish_sent = 27, + mosq_counter_mqtt_puback_received = 28, + mosq_counter_mqtt_puback_sent = 29, + mosq_counter_mqtt_pubrec_received = 30, + mosq_counter_mqtt_pubrec_sent = 31, + mosq_counter_mqtt_pubrel_received = 32, + mosq_counter_mqtt_pubrel_sent = 33, + mosq_counter_mqtt_pubcomp_received = 34, + mosq_counter_mqtt_pubcomp_sent = 35, + mosq_counter_mqtt_subscribe_received = 36, + mosq_counter_mqtt_subscribe_sent = 37, + mosq_counter_mqtt_suback_received = 38, + mosq_counter_mqtt_suback_sent = 39, + mosq_counter_mqtt_unsubscribe_received = 40, + mosq_counter_mqtt_unsubscribe_sent = 41, + mosq_counter_mqtt_unsuback_received = 42, + mosq_counter_mqtt_unsuback_sent = 43, + mosq_counter_mqtt_pingreq_received = 44, + mosq_counter_mqtt_pingreq_sent = 45, + mosq_counter_mqtt_pingresp_received = 46, + mosq_counter_mqtt_pingresp_sent = 47, + mosq_counter_mqtt_disconnect_received = 48, + mosq_counter_mqtt_disconnect_sent = 49, + mosq_counter_mqtt_auth_received = 50, + mosq_counter_mqtt_auth_sent = 51, mosq_metric_max, };