Extend aclk-state information (#12458)
Timotej S committed
Mar 22, 2022 at 15:14 UTC
8dbb8d29239e4b5e2ba553d7fc19a5d0a4a870fb
1 file changed
+60
-2
aclk/aclk.c
+60
-2
@@ -28,6 +28,12 @@ int aclk_rcvd_cloud_msgs = 0;
28
int aclk_connection_counter = 0;
29
int disconnect_req = 0;
30
31
+time_t last_conn_time_mqtt = 0;
32
+time_t last_conn_time_appl = 0;
33
+time_t last_disconnect_time = 0;
34
+time_t next_connection_attempt = 0;
35
+float last_backoff_value = 0;
36
+
37
int aclk_alert_reloaded = 1; //1 on startup, and again on health_reload
38
39
time_t aclk_block_until = 0;
@@ -276,8 +282,10 @@ static inline void msg_callback(const char *topic, const void *msg, size_t msgle
282
283
static void puback_callback(uint16_t packet_id)
284
{
279
- if (++aclk_pubacks_per_conn == ACLK_PUBACKS_CONN_STABLE)
285
+ if (++aclk_pubacks_per_conn == ACLK_PUBACKS_CONN_STABLE) {
286
+ last_conn_time_appl = now_realtime_sec();
287
aclk_tbeb_reset();
288
+ }
289
290
#ifdef NETDATA_INTERNAL_CHECKS
291
aclk_stats_msg_puback(packet_id);
@@ -483,6 +491,7 @@ void aclk_graceful_disconnect(mqtt_wss_client client)
491
info("ACLK link is down");
492
log_access("ACLK DISCONNECTED");
493
aclk_stats_upd_online(0);
494
+ last_disconnect_time = now_realtime_sec();
495
aclk_connected = 0;
496
497
info("Attempting to gracefully shutdown the MQTT/WSS connection");
@@ -524,6 +533,9 @@ static unsigned long aclk_reconnect_delay() {
533
static int aclk_block_till_recon_allowed() {
534
unsigned long recon_delay = aclk_reconnect_delay();
535
536
+ next_connection_attempt = now_realtime_sec() + (recon_delay / MSEC_PER_SEC);
537
+ last_backoff_value = (float)recon_delay / MSEC_PER_SEC;
538
+
539
info("Wait before attempting to reconnect in %.3f seconds\n", recon_delay / (float)MSEC_PER_SEC);
540
// we want to wake up from time to time to check netdata_exit
541
while (recon_delay)
@@ -725,6 +737,7 @@ static int aclk_attempt_to_connect(mqtt_wss_client client)
737
json_object_put(lwt);
738
739
if (!ret) {
740
+ last_conn_time_mqtt = now_realtime_sec();
741
info("ACLK connection successfully established");
742
log_access("ACLK CONNECTED");
743
mqtt_connected_actions(client);
@@ -842,6 +855,7 @@ void *aclk_main(void *ptr)
855
856
if (handle_connection(mqttwss_client)) {
857
aclk_stats_upd_online(0);
858
+ last_disconnect_time = now_realtime_sec();
859
aclk_connected = 0;
860
log_access("ACLK DISCONNECTED");
861
}
@@ -1181,6 +1195,7 @@ static void fill_chart_status_for_host(BUFFER *wb, RRDHOST *host)
1195
char *ng_aclk_state(void)
1196
{
1197
BUFFER *wb = buffer_create(1024);
1198
+ struct tm *tmptr, tmbuf;
1199
char *ret;
1200
1201
buffer_strcat(wb,
@@ -1203,7 +1218,27 @@ char *ng_aclk_state(void)
1218
freez(agent_id);
1219
}
1220
1206
- buffer_sprintf(wb, "Online: %s\nReconnect count: %d\n", aclk_connected ? "Yes" : "No", aclk_connection_counter > 0 ? (aclk_connection_counter - 1) : 0);
1221
+ buffer_sprintf(wb, "Online: %s\nReconnect count: %d\nBanned By Cloud: %s\n", aclk_connected ? "Yes" : "No", aclk_connection_counter > 0 ? (aclk_connection_counter - 1) : 0, aclk_disable_runtime ? "Yes" : "No");
1222
+ if (last_conn_time_mqtt && (tmptr = localtime_r(&last_conn_time_mqtt, &tmbuf)) ) {
1223
+ char timebuf[26];
1224
+ strftime(timebuf, 26, "%Y-%m-%d %H:%M:%S", tmptr);
1225
+ buffer_sprintf(wb, "Last Connection Time: %s\n", timebuf);
1226
+ }
1227
+ if (last_conn_time_appl && (tmptr = localtime_r(&last_conn_time_appl, &tmbuf)) ) {
1228
+ char timebuf[26];
1229
+ strftime(timebuf, 26, "%Y-%m-%d %H:%M:%S", tmptr);
1230
+ buffer_sprintf(wb, "Last Connection Time + %d PUBACKs received: %s\n", ACLK_PUBACKS_CONN_STABLE, timebuf);
1231
+ }
1232
+ if (last_disconnect_time && (tmptr = localtime_r(&last_disconnect_time, &tmbuf)) ) {
1233
+ char timebuf[26];
1234
+ strftime(timebuf, 26, "%Y-%m-%d %H:%M:%S", tmptr);
1235
+ buffer_sprintf(wb, "Last Disconnect Time: %s\n", timebuf);
1236
+ }
1237
+ if (!aclk_connected && next_connection_attempt && (tmptr = localtime_r(&next_connection_attempt, &tmbuf)) ) {
1238
+ char timebuf[26];
1239
+ strftime(timebuf, 26, "%Y-%m-%d %H:%M:%S", tmptr);
1240
+ buffer_sprintf(wb, "Next Connection Attempt At: %s\nLast Backoff: %.3f", timebuf, last_backoff_value);
1241
+ }
1242
1243
if (aclk_connected) {
1244
buffer_sprintf(wb, "Received Cloud MQTT Messages: %d\nMQTT Messages Confirmed by Remote Broker (PUBACKs): %d", aclk_rcvd_cloud_msgs, aclk_pubacks_per_conn);
@@ -1318,6 +1353,17 @@ static void fill_chart_status_for_host_json(json_object *obj, RRDHOST *host)
1353
}
1354
#endif
1355
1356
+static json_object *timestamp_to_json(const time_t *t)
1357
+{
1358
+ struct tm *tmptr, tmbuf;
1359
+ if (*t && (tmptr = gmtime_r(t, &tmbuf)) ) {
1360
+ char timebuf[26];
1361
+ strftime(timebuf, 26, "%Y-%m-%d %H:%M:%S", tmptr);
1362
+ return json_object_new_string(timebuf);
1363
+ }
1364
+ return NULL;
1365
+}
1366
+
1367
char *ng_aclk_state_json(void)
1368
{
1369
json_object *tmp, *grp, *msg = json_object_new_object();
@@ -1370,6 +1416,18 @@ char *ng_aclk_state_json(void)
1416
tmp = json_object_new_int(aclk_connection_counter > 0 ? (aclk_connection_counter - 1) : 0);
1417
json_object_object_add(msg, "reconnect-count", tmp);
1418
1419
+ json_object_object_add(msg, "last-connect-time-utc", timestamp_to_json(&last_conn_time_mqtt));
1420
+ json_object_object_add(msg, "last-connect-time-puback-utc", timestamp_to_json(&last_conn_time_appl));
1421
+ json_object_object_add(msg, "last-disconnect-time-utc", timestamp_to_json(&last_disconnect_time));
1422
+ json_object_object_add(msg, "next-connection-attempt-utc", !aclk_connected ? timestamp_to_json(&next_connection_attempt) : NULL);
1423
+ tmp = NULL;
1424
+ if (!aclk_connected && last_backoff_value)
1425
+ tmp = json_object_new_double(last_backoff_value);
1426
+ json_object_object_add(msg, "last-backoff-value", tmp);
1427
+
1428
+ tmp = json_object_new_boolean(aclk_disable_runtime);
1429
+ json_object_object_add(msg, "banned-by-cloud", tmp);
1430
+
1431
#ifdef ENABLE_NEW_CLOUD_PROTOCOL
1432
grp = json_object_new_array();
1433