25113adc2c210c732480ea0ef746fae11953af65
[NeonServV5.git] / src / cmd_neonserv_netinfo.c
1 /* cmd_neonserv_netinfo.c - NeonServ v5.1
2  * Copyright (C) 2011  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include "cmd_neonserv.h"
19
20 /*
21 * no args
22 */
23
24 CMD_BIND(neonserv_cmd_netinfo) {
25     reply(getTextBot(), user, "NS_NETINFO_HEADER");
26     char tmp[MAXLEN];
27     struct Table *table;
28     table = table_init(2, 18, 0);
29     char *content[2];
30     
31     content[0] = get_language_string(user, "NS_NETINFO_UPTIME");
32     content[1] = timeToStr(user, (time(0) - start_time), 3, tmp);
33     table_add(table, content);
34     
35     content[0] = get_language_string(user, "NS_NETINFO_BOTS");
36     struct ClientSocket *cclient;
37     int bot_count = 0, connected_bot_count = 0;
38     float traffic_in = 0, traffic_out = 0;
39     for(cclient = getBots(0, NULL); cclient; cclient = getBots(0, cclient)) {
40         bot_count++;
41         if(cclient->flags & SOCKET_FLAG_READY)
42             connected_bot_count++;
43         traffic_in += cclient->traffic_in;
44         traffic_out += cclient->traffic_out;
45     }
46     sprintf(tmp, "%d (%d connected)", bot_count, connected_bot_count);
47     content[1] = tmp;
48     table_add(table, content);
49     
50     content[0] = get_language_string(user, "NS_NETINFO_TRAFFIC");
51     sprintf(tmp, "in: %.2f kb  out: %.2f kb", traffic_in / 1024, traffic_out / 1024);
52     content[1] = tmp;
53     table_add(table, content);
54     
55     int channel_count = getChannelCount();
56     float channel_memory = channel_count * sizeof(struct ChanNode);
57     int channel_ban_count = getChanBanCount();
58     float channel_ban_memory = channel_ban_count * sizeof(struct BanNode);
59     int user_count = getUserCount();
60     float user_memory = user_count * sizeof(struct UserNode);
61     int chanuser_count = getChanUserCount();
62     float chanuser_memory = chanuser_count * sizeof(struct ChanUser);
63     float total_memory = channel_memory + channel_ban_memory + user_memory + chanuser_memory;
64     content[0] = get_language_string(user, "NS_NETINFO_CACHE");
65     sprintf(tmp, "%.2f kB (%.2f MB)", total_memory / 1024, total_memory / 1024 / 1024);
66     content[1] = tmp;
67     table_add(table, content);
68     content[0] = get_language_string(user, "NS_NETINFO_CHANNEL");
69     sprintf(tmp, "%d    %.2f kB (%d * %u B = %.2f kB)", channel_count, channel_memory / 1024, channel_count, (unsigned int) sizeof(struct ChanNode), channel_memory / 1024);
70     content[1] = tmp;
71     table_add(table, content);
72     content[0] = get_language_string(user, "NS_NETINFO_CHANNEL_BAN");
73     sprintf(tmp, "%d    %.2f kB (%d * %u B = %.2f kB)", channel_ban_count, channel_ban_memory / 1024, channel_ban_count, (unsigned int) sizeof(struct BanNode), channel_ban_memory / 1024);
74     content[1] = tmp;
75     table_add(table, content);
76     content[0] = get_language_string(user, "NS_NETINFO_USER");
77     sprintf(tmp, "%d    %.2f kB (%d * %u B = %.2f kB)", user_count, user_memory / 1024, user_count, (unsigned int) sizeof(struct UserNode), user_memory / 1024);
78     content[1] = tmp;
79     table_add(table, content);
80     content[0] = get_language_string(user, "NS_NETINFO_CHANUSER");
81     sprintf(tmp, "%d    %.2f kB (%d * %u B = %.2f kB)", chanuser_count, chanuser_memory / 1024, chanuser_count, (unsigned int) sizeof(struct ChanUser), chanuser_memory / 1024);
82     content[1] = tmp;
83     table_add(table, content);
84     
85     MYSQL_RES *res;
86     MYSQL_ROW row;
87     printf_mysql_query("SHOW TABLE STATUS");
88     res = mysql_use();
89     int mysql_entrys[4];
90     float mysql_length[5];
91     total_memory = 0;
92     mysql_entrys[0] = 0; mysql_entrys[1] = 0; mysql_entrys[2] = 0; mysql_entrys[3] = 0;
93     mysql_length[0] = 0; mysql_length[1] = 0; mysql_length[2] = 0; mysql_length[3] = 0; mysql_length[4] = 0;
94     while ((row = mysql_fetch_row(res)) != NULL) {
95         if(!stricmp(row[0], "channels")) {
96             mysql_entrys[0] = atoi(row[4]);
97             mysql_length[0] = atof(row[6]);
98             total_memory += atof(row[6]);
99         } else if(!stricmp(row[0], "bans")) {
100             mysql_entrys[1] = atoi(row[4]);
101             mysql_length[1] = atof(row[6]);
102             total_memory += atof(row[6]);
103         } else if(!stricmp(row[0], "users")) {
104             mysql_entrys[2] = atoi(row[4]);
105             mysql_length[2] = atof(row[6]);
106             total_memory += atof(row[6]);
107         } else if(!stricmp(row[0], "chanusers")) {
108             mysql_entrys[3] = atoi(row[4]);
109             mysql_length[3] = atof(row[6]);
110             total_memory += atof(row[6]);
111         } else {
112             mysql_length[4] += atof(row[6]);
113             total_memory += atof(row[6]);
114         }
115     }
116     
117     content[0] = get_language_string(user, "NS_NETINFO_DATABASE");
118     sprintf(tmp, "%.2f kB (%.2f MB)", total_memory / 1024, total_memory / 1024 / 1024);
119     content[1] = tmp;
120     table_add(table, content);
121     
122     content[0] = get_language_string(user, "NS_NETINFO_CHANNEL");
123     sprintf(tmp, "%d    %.2f kB", mysql_entrys[0], mysql_length[0] / 1024);
124     content[1] = tmp;
125     table_add(table, content);
126     
127     content[0] = get_language_string(user, "NS_NETINFO_CHANNEL_BAN");
128     sprintf(tmp, "%d    %.2f kB", mysql_entrys[1], mysql_length[1] / 1024);
129     content[1] = tmp;
130     table_add(table, content);
131     
132     content[0] = get_language_string(user, "NS_NETINFO_USER");
133     sprintf(tmp, "%d    %.2f kB", mysql_entrys[2], mysql_length[2] / 1024);
134     content[1] = tmp;
135     table_add(table, content);
136     
137     content[0] = get_language_string(user, "NS_NETINFO_CHANUSER");
138     sprintf(tmp, "%d    %.2f kB", mysql_entrys[3], mysql_length[3] / 1024);
139     content[1] = tmp;
140     table_add(table, content);
141     
142     content[0] = get_language_string(user, "NS_NETINFO_OTHER");
143     sprintf(tmp, "*     %.2f kB", mysql_length[4] / 1024);
144     content[1] = tmp;
145     table_add(table, content);
146     
147     if(strcmp(revision, ""))
148         sprintf(tmp, "%s.%d  (%s)", NEONSERV_VERSION, patchlevel, revision);
149     else 
150         sprintf(tmp, "%s.%d", NEONSERV_VERSION, patchlevel);
151     content[0] = get_language_string(user, "NS_NETINFO_VERSION");
152     content[1] = tmp;
153     table_add(table, content);
154     
155     content[0] = get_language_string(user, "NS_NETINFO_COMPILER");
156     content[1] = build_language_string(user, tmp, "NS_NETINFO_COMPILER_VALUE", COMPILER, creation);
157     table_add(table, content);
158     
159     content[0] = get_language_string(user, "NS_NETINFO_CODE");
160     content[1] = build_language_string(user, tmp, "NS_NETINFO_CODE_VALUE", codelines);
161     table_add(table, content);
162     
163     char **table_lines = table_end(table);
164     int i;
165     for(i = 0; i < table->entrys; i++) {
166         reply(getTextBot(), user, table_lines[i]);
167     }
168     table_free(table);
169 }
170