]> jfr.im git - irc/rizon/plexus4.git/commitdiff
Add /stats/ltrace
authorAdam <redacted>
Wed, 1 Apr 2020 00:22:09 +0000 (20:22 -0400)
committerAdam <redacted>
Wed, 1 Apr 2020 00:22:09 +0000 (20:22 -0400)
modules/http_stats.c

index 6a721f7a40b45af3087c4c67298949b5505a4253..6b6ba8725e8feec4bc19afa003486266fcf2cc48 100644 (file)
@@ -421,6 +421,53 @@ http_stats_listeners(struct MHD_Connection *connection, const char *url,
   return ret;
 }
 
+static void
+ltrace_list(json_t *root, dlink_list *list)
+{
+  dlink_node *ptr;
+
+  DLINK_FOREACH(ptr, list->head)
+  {
+    struct Client *target_p = ptr->data;
+
+    json_t *object = json_object();
+    json_object_set_new(object, "name", json_string(get_client_name(target_p, SHOW_IP)));
+    json_object_set_new(object, "sendq_length", json_integer(dbuf_length(&target_p->localClient->buf_sendq)));
+    json_object_set_new(object, "send.messages", json_integer(target_p->localClient->send.messages));
+    json_object_set_new(object, "send.kbytes", json_integer(target_p->localClient->send.bytes>>10));
+    json_object_set_new(object, "recv.messages", json_integer(target_p->localClient->recv.messages));
+    json_object_set_new(object, "recv.kbytes", json_integer(target_p->localClient->recv.bytes>>10));
+    json_object_set_new(object, "firstime", json_integer(target_p->localClient->firsttime));
+    json_object_set_new(object, "since", json_integer(target_p->localClient->since));
+    json_array_append_new(root, object);
+  }
+}
+
+static int
+http_stats_ltrace(struct MHD_Connection *connection, const char *url,
+                     const char *method, const char *version, const char *upload_data,
+                     size_t *upload_data_size)
+{
+  json_t *root = json_array();
+  ltrace_list(root, &unknown_list);
+  ltrace_list(root, &local_client_list);
+  ltrace_list(root, &serv_list);
+
+  const char *dump = json_dumps(root, JSON_INDENT(2));
+
+  json_decref(root);
+  root = NULL;
+
+  if (dump == NULL)
+    return MHD_NO;
+
+  struct MHD_Response *response = MHD_create_response_from_buffer(strlen(dump), (void *) dump, MHD_RESPMEM_MUST_FREE);
+  int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
+  MHD_destroy_response(response);
+
+  return ret;
+}
+
 static struct HttpResource stats_http_memory =
 {
   .method = MHD_HTTP_METHOD_GET,
@@ -449,6 +496,13 @@ static struct HttpResource stats_http_listeners =
   .handler = http_stats_listeners
 };
 
+static struct HttpResource stats_http_ltrace =
+{
+  .method = MHD_HTTP_METHOD_GET,
+  .url = "/stats/ltrace",
+  .handler = http_stats_ltrace
+};
+
 #endif
 #endif
 
@@ -461,6 +515,7 @@ module_init(void)
   httpd_register(&stats_http_links);
   httpd_register(&stats_http_messages);
   httpd_register(&stats_http_listeners);
+  httpd_register(&stats_http_ltrace);
 #endif
 #endif
 }
@@ -474,6 +529,7 @@ module_exit(void)
   httpd_unregister(&stats_http_links);
   httpd_unregister(&stats_http_messages);
   httpd_unregister(&stats_http_listeners);
+  httpd_unregister(&stats_http_ltrace);
 #endif
 #endif
 }