c8fd1b643d87ecaa4fea62678e2e0ca9912c8765
[NeonServV5.git] / cmd_neonserv_netinfo.c
1
2 #include "cmd_neonserv.h"
3
4 /*
5 * no args
6 */
7
8 CMD_BIND(neonserv_cmd_netinfo) {
9     reply(getTextBot(), user, "NS_NETINFO_HEADER");
10     char tmp[MAXLEN];
11     struct Table *table;
12     table = table_init(2, 18, 0);
13     char *content[2];
14     
15     content[0] = get_language_string(user, "NS_NETINFO_UPTIME");
16     content[1] = timeToStr(user, (time(0) - start_time), 3, tmp);
17     table_add(table, content);
18     
19     content[0] = get_language_string(user, "NS_NETINFO_BOTS");
20     struct ClientSocket *cclient;
21     int bot_count = 0, connected_bot_count = 0;
22     float traffic_in = 0, traffic_out = 0;
23     for(cclient = getBots(0, NULL); cclient; cclient = getBots(0, cclient)) {
24         bot_count++;
25         if(cclient->flags & SOCKET_FLAG_READY)
26             connected_bot_count++;
27         traffic_in += cclient->traffic_in;
28         traffic_out += cclient->traffic_out;
29     }
30     sprintf(tmp, "%d (%d connected)", bot_count, connected_bot_count);
31     content[1] = tmp;
32     table_add(table, content);
33     
34     content[0] = get_language_string(user, "NS_NETINFO_TRAFFIC");
35     sprintf(tmp, "in: %.2f kb  out: %.2f kb", traffic_in / 1024, traffic_out / 1024);
36     content[1] = tmp;
37     table_add(table, content);
38     
39     int channel_count = getChannelCount();
40     float channel_memory = channel_count * sizeof(struct ChanNode);
41     int channel_ban_count = getChanBanCount();
42     float channel_ban_memory = channel_ban_count * sizeof(struct BanNode);
43     int user_count = getUserCount();
44     float user_memory = user_count * sizeof(struct UserNode);
45     int chanuser_count = getChanUserCount();
46     float chanuser_memory = chanuser_count * sizeof(struct ChanUser);
47     float total_memory = channel_memory + channel_ban_memory + user_memory + chanuser_memory;
48     
49     content[0] = get_language_string(user, "NS_NETINFO_CACHE");
50     sprintf(tmp, "%.2f kB (%.2f MB)", total_memory / 1024, total_memory / 1024 / 1024);
51     content[1] = tmp;
52     table_add(table, content);
53     
54     content[0] = get_language_string(user, "NS_NETINFO_CHANNEL");
55     sprintf(tmp, "%d    %.2f kB (%d * %lu B = %.2f kB)", channel_count, channel_memory / 1024, channel_count, sizeof(struct ChanNode), channel_memory / 1024);
56     content[1] = tmp;
57     table_add(table, content);
58     
59     content[0] = get_language_string(user, "NS_NETINFO_CHANNEL_BAN");
60     sprintf(tmp, "%d    %.2f kB (%d * %lu B = %.2f kB)", channel_ban_count, channel_ban_memory / 1024, channel_ban_count, sizeof(struct BanNode), channel_ban_memory / 1024);
61     content[1] = tmp;
62     table_add(table, content);
63     
64     content[0] = get_language_string(user, "NS_NETINFO_USER");
65     sprintf(tmp, "%d    %.2f kB (%d * %lu B = %.2f kB)", user_count, user_memory / 1024, user_count, sizeof(struct UserNode), user_memory / 1024);
66     content[1] = tmp;
67     table_add(table, content);
68     
69     content[0] = get_language_string(user, "NS_NETINFO_CHANUSER");
70     sprintf(tmp, "%d    %.2f kB (%d * %lu B = %.2f kB)", chanuser_count, chanuser_memory / 1024, chanuser_count, sizeof(struct ChanUser), chanuser_memory / 1024);
71     content[1] = tmp;
72     table_add(table, content);
73     
74     MYSQL_RES *res;
75     MYSQL_ROW row;
76     printf_mysql_query("SHOW TABLE STATUS");
77     res = mysql_use();
78     int mysql_entrys[4];
79     float mysql_length[5];
80     total_memory = 0;
81     mysql_entrys[0] = 0; mysql_entrys[1] = 0; mysql_entrys[2] = 0; mysql_entrys[3] = 0;
82     mysql_length[0] = 0; mysql_length[1] = 0; mysql_length[2] = 0; mysql_length[3] = 0; mysql_length[4] = 0;
83     while ((row = mysql_fetch_row(res)) != NULL) {
84         if(!stricmp(row[0], "channels")) {
85             mysql_entrys[0] = atoi(row[4]);
86             mysql_length[0] = atof(row[6]);
87             total_memory += atof(row[6]);
88         } else if(!stricmp(row[0], "bans")) {
89             mysql_entrys[1] = atoi(row[4]);
90             mysql_length[1] = atof(row[6]);
91             total_memory += atof(row[6]);
92         } else if(!stricmp(row[0], "users")) {
93             mysql_entrys[2] = atoi(row[4]);
94             mysql_length[2] = atof(row[6]);
95             total_memory += atof(row[6]);
96         } else if(!stricmp(row[0], "chanusers")) {
97             mysql_entrys[3] = atoi(row[4]);
98             mysql_length[3] = atof(row[6]);
99             total_memory += atof(row[6]);
100         } else {
101             mysql_length[4] += atof(row[6]);
102             total_memory += atof(row[6]);
103         }
104     }
105     
106     content[0] = get_language_string(user, "NS_NETINFO_DATABASE");
107     sprintf(tmp, "%.2f kB (%.2f MB)", total_memory / 1024, total_memory / 1024 / 1024);
108     content[1] = tmp;
109     table_add(table, content);
110     
111     content[0] = get_language_string(user, "NS_NETINFO_CHANNEL");
112     sprintf(tmp, "%d    %.2f kB", mysql_entrys[0], mysql_length[0] / 1024);
113     content[1] = tmp;
114     table_add(table, content);
115     
116     content[0] = get_language_string(user, "NS_NETINFO_CHANNEL_BAN");
117     sprintf(tmp, "%d    %.2f kB", mysql_entrys[1], mysql_length[1] / 1024);
118     content[1] = tmp;
119     table_add(table, content);
120     
121     content[0] = get_language_string(user, "NS_NETINFO_USER");
122     sprintf(tmp, "%d    %.2f kB", mysql_entrys[2], mysql_length[2] / 1024);
123     content[1] = tmp;
124     table_add(table, content);
125     
126     content[0] = get_language_string(user, "NS_NETINFO_CHANUSER");
127     sprintf(tmp, "%d    %.2f kB", mysql_entrys[3], mysql_length[3] / 1024);
128     content[1] = tmp;
129     table_add(table, content);
130     
131     content[0] = get_language_string(user, "NS_NETINFO_OTHER");
132     sprintf(tmp, "*     %.2f kB", mysql_length[4] / 1024);
133     content[1] = tmp;
134     table_add(table, content);
135     
136     if(strcmp(revision, ""))
137         sprintf(tmp, "%s  (%s)", NEONSERV_VERSION, revision);
138     else 
139         strcpy(tmp, NEONSERV_VERSION);
140     content[0] = get_language_string(user, "NS_NETINFO_VERSION");
141     content[1] = tmp;
142     table_add(table, content);
143     
144     content[0] = get_language_string(user, "NS_NETINFO_COMPILER");
145     content[1] = build_language_string(user, tmp, "NS_NETINFO_COMPILER_VALUE", COMPILER, creation);
146     table_add(table, content);
147     
148     content[0] = get_language_string(user, "NS_NETINFO_CODE");
149     content[1] = build_language_string(user, tmp, "NS_NETINFO_CODE_VALUE", codelines);
150     table_add(table, content);
151     
152     char **table_lines = table_end(table);
153     int i;
154     for(i = 0; i < table->entrys; i++) {
155         reply(getTextBot(), user, table_lines[i]);
156     }
157     table_free(table);
158 }
159