added GPL header to all files and added INSTALL AUTHORS COPYING files.
[NeonServV5.git] / src / cmd_neonserv_netinfo.c
1 /* cmd_neonserv_netinfo.c - NeonServ v5.0
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     
65     content[0] = get_language_string(user, "NS_NETINFO_CACHE");
66     sprintf(tmp, "%.2f kB (%.2f MB)", total_memory / 1024, total_memory / 1024 / 1024);
67     content[1] = tmp;
68     table_add(table, content);
69     
70     content[0] = get_language_string(user, "NS_NETINFO_CHANNEL");
71     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);
72     content[1] = tmp;
73     table_add(table, content);
74     
75     content[0] = get_language_string(user, "NS_NETINFO_CHANNEL_BAN");
76     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);
77     content[1] = tmp;
78     table_add(table, content);
79     
80     content[0] = get_language_string(user, "NS_NETINFO_USER");
81     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);
82     content[1] = tmp;
83     table_add(table, content);
84     
85     content[0] = get_language_string(user, "NS_NETINFO_CHANUSER");
86     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);
87     content[1] = tmp;
88     table_add(table, content);
89     
90     MYSQL_RES *res;
91     MYSQL_ROW row;
92     printf_mysql_query("SHOW TABLE STATUS");
93     res = mysql_use();
94     int mysql_entrys[4];
95     float mysql_length[5];
96     total_memory = 0;
97     mysql_entrys[0] = 0; mysql_entrys[1] = 0; mysql_entrys[2] = 0; mysql_entrys[3] = 0;
98     mysql_length[0] = 0; mysql_length[1] = 0; mysql_length[2] = 0; mysql_length[3] = 0; mysql_length[4] = 0;
99     while ((row = mysql_fetch_row(res)) != NULL) {
100         if(!stricmp(row[0], "channels")) {
101             mysql_entrys[0] = atoi(row[4]);
102             mysql_length[0] = atof(row[6]);
103             total_memory += atof(row[6]);
104         } else if(!stricmp(row[0], "bans")) {
105             mysql_entrys[1] = atoi(row[4]);
106             mysql_length[1] = atof(row[6]);
107             total_memory += atof(row[6]);
108         } else if(!stricmp(row[0], "users")) {
109             mysql_entrys[2] = atoi(row[4]);
110             mysql_length[2] = atof(row[6]);
111             total_memory += atof(row[6]);
112         } else if(!stricmp(row[0], "chanusers")) {
113             mysql_entrys[3] = atoi(row[4]);
114             mysql_length[3] = atof(row[6]);
115             total_memory += atof(row[6]);
116         } else {
117             mysql_length[4] += atof(row[6]);
118             total_memory += atof(row[6]);
119         }
120     }
121     
122     content[0] = get_language_string(user, "NS_NETINFO_DATABASE");
123     sprintf(tmp, "%.2f kB (%.2f MB)", total_memory / 1024, total_memory / 1024 / 1024);
124     content[1] = tmp;
125     table_add(table, content);
126     
127     content[0] = get_language_string(user, "NS_NETINFO_CHANNEL");
128     sprintf(tmp, "%d    %.2f kB", mysql_entrys[0], mysql_length[0] / 1024);
129     content[1] = tmp;
130     table_add(table, content);
131     
132     content[0] = get_language_string(user, "NS_NETINFO_CHANNEL_BAN");
133     sprintf(tmp, "%d    %.2f kB", mysql_entrys[1], mysql_length[1] / 1024);
134     content[1] = tmp;
135     table_add(table, content);
136     
137     content[0] = get_language_string(user, "NS_NETINFO_USER");
138     sprintf(tmp, "%d    %.2f kB", mysql_entrys[2], mysql_length[2] / 1024);
139     content[1] = tmp;
140     table_add(table, content);
141     
142     content[0] = get_language_string(user, "NS_NETINFO_CHANUSER");
143     sprintf(tmp, "%d    %.2f kB", mysql_entrys[3], mysql_length[3] / 1024);
144     content[1] = tmp;
145     table_add(table, content);
146     
147     content[0] = get_language_string(user, "NS_NETINFO_OTHER");
148     sprintf(tmp, "*     %.2f kB", mysql_length[4] / 1024);
149     content[1] = tmp;
150     table_add(table, content);
151     
152     if(strcmp(revision, ""))
153         sprintf(tmp, "%s  (%s)", NEONSERV_VERSION, revision);
154     else 
155         strcpy(tmp, NEONSERV_VERSION);
156     content[0] = get_language_string(user, "NS_NETINFO_VERSION");
157     content[1] = tmp;
158     table_add(table, content);
159     
160     content[0] = get_language_string(user, "NS_NETINFO_COMPILER");
161     content[1] = build_language_string(user, tmp, "NS_NETINFO_COMPILER_VALUE", COMPILER, creation);
162     table_add(table, content);
163     
164     content[0] = get_language_string(user, "NS_NETINFO_CODE");
165     content[1] = build_language_string(user, tmp, "NS_NETINFO_CODE_VALUE", codelines);
166     table_add(table, content);
167     
168     char **table_lines = table_end(table);
169     int i;
170     for(i = 0; i < table->entrys; i++) {
171         reply(getTextBot(), user, table_lines[i]);
172     }
173     table_free(table);
174 }
175