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