Restore stats_spy hook that was removed in commit 401f2454671ca233e35b0e6e4f3fa4c43cd...
[seven-1.x.git] / src / s_stats.c
blob772951a09c7aba180545062ef8d446890ba61d9c
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * s_stats.c: Statistics related functions
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
25 #include "stdinc.h"
26 #include "s_stats.h"
27 #include "client.h"
28 #include "irc_string.h"
29 #include "ircd.h"
30 #include "numeric.h"
31 #include "commio.h"
32 #include "send.h"
33 #include "memory.h"
34 #include "s_conf.h"
35 #include "s_newconf.h"
36 #include "whowas.h"
37 #include "hash.h"
38 #include "scache.h"
39 #include "reject.h"
42 * stats stuff
44 static struct ServerStatistics ircst;
45 struct ServerStatistics *ServerStats = &ircst;
47 void
48 init_stats()
50 memset(&ircst, 0, sizeof(ircst));
54 * tstats
56 * inputs - client to report to
57 * output - NONE
58 * side effects -
60 void
61 tstats(struct Client *source_p)
63 struct Client *target_p;
64 struct ServerStatistics *sp;
65 struct ServerStatistics tmp;
66 dlink_node *ptr;
68 sp = &tmp;
69 memcpy(sp, ServerStats, sizeof(struct ServerStatistics));
71 DLINK_FOREACH(ptr, serv_list.head)
73 target_p = ptr->data;
75 sp->is_sbs += target_p->localClient->sendB;
76 sp->is_sbr += target_p->localClient->receiveB;
77 sp->is_sks += target_p->localClient->sendK;
78 sp->is_skr += target_p->localClient->receiveK;
79 sp->is_sti += CurrentTime - target_p->localClient->firsttime;
80 sp->is_sv++;
81 if(sp->is_sbs > 1023)
83 sp->is_sks += (sp->is_sbs >> 10);
84 sp->is_sbs &= 0x3ff;
86 if(sp->is_sbr > 1023)
88 sp->is_skr += (sp->is_sbr >> 10);
89 sp->is_sbr &= 0x3ff;
93 DLINK_FOREACH(ptr, lclient_list.head)
95 target_p = ptr->data;
97 sp->is_cbs += target_p->localClient->sendB;
98 sp->is_cbr += target_p->localClient->receiveB;
99 sp->is_cks += target_p->localClient->sendK;
100 sp->is_ckr += target_p->localClient->receiveK;
101 sp->is_cti += CurrentTime - target_p->localClient->firsttime;
102 sp->is_cl++;
103 if(sp->is_cbs > 1023)
105 sp->is_cks += (sp->is_cbs >> 10);
106 sp->is_cbs &= 0x3ff;
108 if(sp->is_cbr > 1023)
110 sp->is_ckr += (sp->is_cbr >> 10);
111 sp->is_cbr &= 0x3ff;
116 DLINK_FOREACH(ptr, unknown_list.head)
118 sp->is_ni++;
121 sendto_one_numeric(source_p, RPL_STATSDEBUG,
122 "T :accepts %u refused %u", sp->is_ac, sp->is_ref);
123 sendto_one_numeric(source_p, RPL_STATSDEBUG,
124 "T :rejected %u delaying %lu",
125 sp->is_rej, dlink_list_length(&delay_exit));
126 sendto_one_numeric(source_p, RPL_STATSDEBUG,
127 "T :nicks being delayed %lu",
128 get_nd_count());
129 sendto_one_numeric(source_p, RPL_STATSDEBUG,
130 "T :unknown commands %u prefixes %u",
131 sp->is_unco, sp->is_unpf);
132 sendto_one_numeric(source_p, RPL_STATSDEBUG,
133 "T :nick collisions %u saves %u unknown closes %u",
134 sp->is_kill, sp->is_save, sp->is_ni);
135 sendto_one_numeric(source_p, RPL_STATSDEBUG,
136 "T :wrong direction %u empty %u",
137 sp->is_wrdi, sp->is_empt);
138 sendto_one_numeric(source_p, RPL_STATSDEBUG,
139 "T :numerics seen %u", sp->is_num);
140 sendto_one_numeric(source_p, RPL_STATSDEBUG,
141 "T :auth successes %u fails %u",
142 sp->is_asuc, sp->is_abad);
143 sendto_one_numeric(source_p, RPL_STATSDEBUG,
144 "T :sasl successes %u fails %u",
145 sp->is_ssuc, sp->is_sbad);
146 sendto_one_numeric(source_p, RPL_STATSDEBUG, "T :Client Server");
147 sendto_one_numeric(source_p, RPL_STATSDEBUG,
148 "T :connected %u %u", sp->is_cl, sp->is_sv);
149 sendto_one_numeric(source_p, RPL_STATSDEBUG,
150 "T :bytes sent %d.%uK %d.%uK",
151 (int) sp->is_cks, sp->is_cbs,
152 (int) sp->is_sks, sp->is_sbs);
153 sendto_one_numeric(source_p, RPL_STATSDEBUG,
154 "T :bytes recv %d.%uK %d.%uK",
155 (int) sp->is_ckr, sp->is_cbr,
156 (int) sp->is_skr, sp->is_sbr);
157 sendto_one_numeric(source_p, RPL_STATSDEBUG,
158 "T :time connected %d %d",
159 (int) sp->is_cti, (int) sp->is_sti);
162 void
163 count_memory(struct Client *source_p)
165 struct Client *target_p;
166 struct Channel *chptr;
167 struct Ban *actualBan;
168 dlink_node *dlink;
169 dlink_node *ptr;
170 int channel_count = 0;
171 int local_client_conf_count = 0; /* local client conf links */
172 int users_counted = 0; /* user structs */
174 int channel_users = 0;
175 int channel_invites = 0;
176 int channel_bans = 0;
177 int channel_except = 0;
178 int channel_invex = 0;
179 int channel_quiets = 0;
181 size_t wwu = 0; /* whowas users */
182 int class_count = 0; /* classes */
183 int conf_count = 0; /* conf lines */
184 int users_invited_count = 0; /* users invited */
185 int user_channels = 0; /* users in channels */
186 int aways_counted = 0;
187 size_t number_servers_cached; /* number of servers cached by scache */
189 size_t channel_memory = 0;
190 size_t channel_ban_memory = 0;
191 size_t channel_except_memory = 0;
192 size_t channel_invex_memory = 0;
193 size_t channel_quiet_memory = 0;
195 size_t away_memory = 0; /* memory used by aways */
196 size_t wwm = 0; /* whowas array memory used */
197 size_t conf_memory = 0; /* memory used by conf lines */
198 size_t mem_servers_cached; /* memory used by scache */
200 size_t linebuf_count = 0;
201 size_t linebuf_memory_used = 0;
203 size_t total_channel_memory = 0;
204 size_t totww = 0;
206 size_t local_client_count = 0;
207 size_t local_client_memory_used = 0;
209 size_t remote_client_count = 0;
210 size_t remote_client_memory_used = 0;
212 size_t total_memory = 0;
214 count_whowas_memory(&wwu, &wwm);
216 DLINK_FOREACH(ptr, global_client_list.head)
218 target_p = ptr->data;
219 if(MyConnect(target_p))
221 local_client_conf_count++;
224 if(target_p->user)
226 users_counted++;
227 users_invited_count += dlink_list_length(&target_p->user->invited);
228 user_channels += dlink_list_length(&target_p->user->channel);
229 if(target_p->user->away)
231 aways_counted++;
232 away_memory += (strlen(target_p->user->away) + 1);
237 /* Count up all channels, ban lists, except lists, Invex lists */
238 DLINK_FOREACH(ptr, global_channel_list.head)
240 chptr = ptr->data;
241 channel_count++;
242 channel_memory += (strlen(chptr->chname) + sizeof(struct Channel));
244 channel_users += dlink_list_length(&chptr->members);
245 channel_invites += dlink_list_length(&chptr->invites);
247 DLINK_FOREACH(dlink, chptr->banlist.head)
249 actualBan = dlink->data;
250 channel_bans++;
252 channel_ban_memory += sizeof(dlink_node) + sizeof(struct Ban);
255 DLINK_FOREACH(dlink, chptr->exceptlist.head)
257 actualBan = dlink->data;
258 channel_except++;
260 channel_except_memory += (sizeof(dlink_node) + sizeof(struct Ban));
263 DLINK_FOREACH(dlink, chptr->invexlist.head)
265 actualBan = dlink->data;
266 channel_invex++;
268 channel_invex_memory += (sizeof(dlink_node) + sizeof(struct Ban));
271 DLINK_FOREACH(dlink, chptr->quietlist.head)
273 actualBan = dlink->data;
274 channel_quiets++;
276 channel_quiet_memory += (sizeof(dlink_node) + sizeof(struct Ban));
280 /* count up all classes */
282 class_count = dlink_list_length(&class_list) + 1;
284 count_linebuf_memory(&linebuf_count, &linebuf_memory_used);
286 sendto_one_numeric(source_p, RPL_STATSDEBUG,
287 "z :Users %u(%lu) Invites %u(%lu)",
288 users_counted,
289 (unsigned long) users_counted * sizeof(struct User),
290 users_invited_count,
291 (unsigned long) users_invited_count * sizeof(dlink_node));
293 sendto_one_numeric(source_p, RPL_STATSDEBUG,
294 "z :User channels %u(%lu) Aways %u(%d)",
295 user_channels,
296 (unsigned long) user_channels * sizeof(dlink_node),
297 aways_counted, (int) away_memory);
299 sendto_one_numeric(source_p, RPL_STATSDEBUG,
300 "z :Attached confs %u(%lu)",
301 local_client_conf_count,
302 (unsigned long) local_client_conf_count * sizeof(dlink_node));
304 sendto_one_numeric(source_p, RPL_STATSDEBUG,
305 "z :Conflines %u(%d)", conf_count, (int) conf_memory);
307 sendto_one_numeric(source_p, RPL_STATSDEBUG,
308 "z :Classes %u(%lu)",
309 class_count,
310 (unsigned long) class_count * sizeof(struct Class));
312 sendto_one_numeric(source_p, RPL_STATSDEBUG,
313 "z :Channels %u(%d)",
314 channel_count, (int) channel_memory);
316 sendto_one_numeric(source_p, RPL_STATSDEBUG,
317 "z :Bans %u(%d) Exceptions %u(%d) Invex %u(%d) Quiets %u(%d)",
318 channel_bans, (int) channel_ban_memory,
319 channel_except, (int) channel_except_memory,
320 channel_invex, (int) channel_invex_memory,
321 channel_quiets, (int) channel_quiet_memory);
323 sendto_one_numeric(source_p, RPL_STATSDEBUG,
324 "z :Channel members %u(%lu) invite %u(%lu)",
325 channel_users,
326 (unsigned long) channel_users * sizeof(dlink_node),
327 channel_invites,
328 (unsigned long) channel_invites * sizeof(dlink_node));
330 total_channel_memory = channel_memory +
331 channel_ban_memory +
332 channel_users * sizeof(dlink_node) + channel_invites * sizeof(dlink_node);
334 sendto_one_numeric(source_p, RPL_STATSDEBUG,
335 "z :Whowas users %ld(%ld)",
336 (long)wwu, (long) (wwu * sizeof(struct User)));
338 sendto_one_numeric(source_p, RPL_STATSDEBUG,
339 "z :Whowas array %u(%d)",
340 NICKNAMEHISTORYLENGTH, (int) wwm);
342 totww = wwu * sizeof(struct User) + wwm;
344 sendto_one_numeric(source_p, RPL_STATSDEBUG,
345 "z :Hash: client %u(%ld) chan %u(%ld)",
346 U_MAX, (long)(U_MAX * sizeof(dlink_list)),
347 CH_MAX, (long)(CH_MAX * sizeof(dlink_list)));
349 sendto_one_numeric(source_p, RPL_STATSDEBUG,
350 "z :linebuf %ld(%ld)",
351 (long)linebuf_count, (long)linebuf_memory_used);
353 count_scache(&number_servers_cached, &mem_servers_cached);
355 sendto_one_numeric(source_p, RPL_STATSDEBUG,
356 "z :scache %ld(%ld)",
357 (long)number_servers_cached, (long)mem_servers_cached);
359 sendto_one_numeric(source_p, RPL_STATSDEBUG,
360 "z :hostname hash %d(%ld)",
361 HOST_MAX, (long)HOST_MAX * sizeof(dlink_list));
363 total_memory = totww + total_channel_memory + conf_memory +
364 class_count * sizeof(struct Class);
366 total_memory += mem_servers_cached;
367 sendto_one_numeric(source_p, RPL_STATSDEBUG,
368 "z :Total: whowas %d channel %d conf %d",
369 (int) totww, (int) total_channel_memory,
370 (int) conf_memory);
372 count_local_client_memory(&local_client_count, &local_client_memory_used);
373 total_memory += local_client_memory_used;
375 sendto_one_numeric(source_p, RPL_STATSDEBUG,
376 "z :Local client Memory in use: %ld(%ld)",
377 (long)local_client_count, (long)local_client_memory_used);
380 count_remote_client_memory(&remote_client_count, &remote_client_memory_used);
381 total_memory += remote_client_memory_used;
383 sendto_one_numeric(source_p, RPL_STATSDEBUG,
384 "z :Remote client Memory in use: %ld(%ld)",
385 (long)remote_client_count,
386 (long)remote_client_memory_used);
388 sendto_one_numeric(source_p, RPL_STATSDEBUG,
389 "z :TOTAL: %d Available: Current max RSS: %lu",
390 (int) total_memory, get_maxrss());