added cmd_netinfo
[NeonServV5.git] / cmd_neonserv_netinfo.c
diff --git a/cmd_neonserv_netinfo.c b/cmd_neonserv_netinfo.c
new file mode 100644 (file)
index 0000000..baf926e
--- /dev/null
@@ -0,0 +1,70 @@
+
+/*
+* no args
+*/
+static CMD_BIND(neonserv_cmd_netinfo) {
+    reply(getTextBot(), user, "NS_NETINFO_HEADER");
+    char tmp[MAXLEN];
+    struct Table *table;
+    table = table_init(2, 5, 0);
+    char *content[2];
+    
+    content[0] = get_language_string(user, "NS_NETINFO_UPTIME");
+    content[1] = timeToStr(user, (time(0) - start_time), 3, tmp);
+    table_add(table, content);
+    
+    content[0] = get_language_string(user, "NS_NETINFO_BOTS");
+    struct ClientSocket *cclient;
+    int bot_count = 0, connected_bot_count = 0;
+    unsigned long traffic_in = 0, traffic_out = 0;
+    for(cclient = getBots(0, NULL); cclient; cclient = getBots(0, cclient)) {
+        bot_count++;
+        if(cclient->flags & SOCKET_FLAG_READY)
+            connected_bot_count++;
+        traffic_in += cclient->traffic_in;
+        traffic_out += cclient->traffic_out;
+    }
+    sprintf(tmp, "%d (%d connected)", bot_count, connected_bot_count);
+    content[1] = tmp;
+    table_add(table, content);
+    
+    content[0] = get_language_string(user, "NS_NETINFO_TRAFFIC");
+    sprintf(tmp, "in: %lu kb  out: %lu kb", traffic_in / 1024, traffic_out / 1024);
+    content[1] = tmp;
+    table_add(table, content);
+    
+    int channel_count = getChannelCount();
+    float channel_memory = channel_count * sizeof(struct ChanNode);
+    int user_count = getUserCount();
+    float user_memory = user_count * sizeof(struct UserNode);
+    int chanuser_count = getChanUserCount();
+    float chanuser_memory = chanuser_count * sizeof(struct ChanUser);
+    float total_memory = channel_memory + user_memory + chanuser_memory;
+    
+    content[0] = get_language_string(user, "NS_NETINFO_CACHE");
+    sprintf(tmp, "%f kB (%f MB)", total_memory / 1024, total_memory / 1024 / 1024);
+    content[1] = tmp;
+    table_add(table, content);
+    
+    content[0] = get_language_string(user, "NS_NETINFO_CHANNEL");
+    sprintf(tmp, "%d    %f kB (%d * %lu B = %f kB)", channel_count, channel_memory / 1024, channel_count, sizeof(struct ChanNode), channel_memory / 1024);
+    content[1] = tmp;
+    table_add(table, content);
+    
+    content[0] = get_language_string(user, "NS_NETINFO_USER");
+    sprintf(tmp, "%d    %f kB (%d * %lu B = %f kB)", user_count, user_memory / 1024, user_count, sizeof(struct UserNode), user_memory / 1024);
+    content[1] = tmp;
+    table_add(table, content);
+    
+    content[0] = get_language_string(user, "NS_NETINFO_CHANUSER");
+    sprintf(tmp, "%d    %f kB (%d * %lu B = %f kB)", chanuser_count, chanuser_memory / 1024, chanuser_count, sizeof(struct ChanUser), chanuser_memory / 1024);
+    content[1] = tmp;
+    table_add(table, content);
+    
+    char **table_lines = table_end(table);
+    int i;
+    for(i = 0; i < table->entrys; i++) {
+        reply(getTextBot(), user, table_lines[i]);
+    }
+    table_free(table);
+}