]> jfr.im git - irc/rizon/plexus4.git/commitdiff
Add /stats/links from /stats ?
authorAdam <redacted>
Mon, 23 May 2016 14:31:49 +0000 (10:31 -0400)
committerAdam <redacted>
Mon, 23 May 2016 21:24:30 +0000 (17:24 -0400)
modules/http_stats.c

index 521091ec0fd6d926deccf1d548b033f23c637ab8..42f4552736c2eea0a102048947cb43b50ce87d35 100644 (file)
@@ -26,6 +26,8 @@
 #include "whowas.h"
 #include "watch.h"
 #include "httpd.h"
+#include "ircd.h"
+#include "s_serv.h"
 
 #ifdef HAVE_LIBJANSSON
 #ifdef HAVE_LIBMICROHTTPD
@@ -282,6 +284,46 @@ http_stats_memory(struct MHD_Connection *connection, const char *url,
   return ret;
 }
 
+static int
+http_stats_links(struct MHD_Connection *connection, const char *url,
+                  const char *method, const char *version, const char *upload_data,
+                  size_t *upload_data_size)
+{
+  dlink_node *ptr = NULL;
+
+  json_t *root = json_array();
+
+  DLINK_FOREACH(ptr, serv_list.head)
+  {
+    struct Client *target_p = ptr->data;
+
+    json_t *link = json_object();
+    json_object_set_new(link, "name", json_string(target_p->name));
+    json_object_set_new(link, "sendq length", json_integer(dbuf_length(&target_p->localClient->buf_sendq)));
+    json_object_set_new(link, "sent messages", json_integer(target_p->localClient->send.messages));
+    json_object_set_new(link, "sent bytes", json_integer(target_p->localClient->send.messages));
+    json_object_set_new(link, "recv messages", json_integer(target_p->localClient->recv.messages));
+    json_object_set_new(link, "recv bytes", json_integer(target_p->localClient->recv.messages));
+    json_object_set_new(link, "connected for", json_integer(CurrentTime - target_p->localClient->firsttime));
+    json_object_set_new(link, "since", json_integer(CurrentTime - target_p->localClient->since));
+    json_object_set_new(link, "capabilities", json_string(show_capabilities(target_p)));
+    json_array_append(root, link);
+  }
+
+  const char *dump = json_dumps(root, JSON_INDENT(2));
+  if (dump == NULL)
+    return MHD_NO;
+
+  json_decref(root);
+  root = NULL;
+
+  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,
@@ -289,6 +331,13 @@ static struct HttpResource stats_http_memory =
   .handler = http_stats_memory
 };
 
+static struct HttpResource stats_http_links =
+{
+  .method = MHD_HTTP_METHOD_GET,
+  .url = "/stats/links",
+  .handler = http_stats_links
+};
+
 #endif
 #endif
 
@@ -298,6 +347,7 @@ module_init(void)
 #ifdef HAVE_LIBJANSSON
 #ifdef HAVE_LIBMICROHTTPD
   httpd_register(&stats_http_memory);
+  httpd_register(&stats_http_links);
 #endif
 #endif
 }
@@ -308,6 +358,7 @@ module_exit(void)
 #ifdef HAVE_LIBJANSSON
 #ifdef HAVE_LIBMICROHTTPD
   httpd_unregister(&stats_http_memory);
+  httpd_unregister(&stats_http_links);
 #endif
 #endif
 }