2 * IRC - Internet Relay Chat, ircd/s_stats.c
3 * Copyright (C) 2000 Joseph Bongaarts
5 * See file AUTHORS in IRC package for additional names of
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 1, or (at your option)
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include "ircd_chattr.h"
31 #include "ircd_events.h"
32 #include "ircd_features.h"
33 #include "ircd_crypt.h"
35 #include "ircd_reply.h"
36 #include "ircd_string.h"
66 * Report configuration lines and other statistics from this
69 * Note: The info is reported in the order the server uses
70 * it--not reversed as in ircd.conf!
73 static unsigned int report_array[17][3] = {
74 {CONF_SERVER, RPL_STATSCLINE, 'C'},
75 {CONF_CLIENT, RPL_STATSILINE, 'I'},
76 {CONF_LEAF, RPL_STATSLLINE, 'L'},
77 {CONF_OPERATOR, RPL_STATSOLINE, 'O'},
78 {CONF_HUB, RPL_STATSHLINE, 'H'},
79 {CONF_UWORLD, RPL_STATSULINE, 'U'},
83 /* The statsinfo array should only be used in this file, but just TRY
84 * telling the compiler that you want to forward declare a static array,
85 * and see how it responds. So we forward declare it "extern".
87 extern struct StatDesc statsinfo[];
90 stats_configured_links(struct Client *sptr, const struct StatDesc* sd,
93 static char null[] = "<NULL>";
97 unsigned short int port;
98 char c, *host, *pass, *name;
100 mask = sd->sd_funcdata;
102 for (tmp = GlobalConfList; tmp; tmp = tmp->next)
104 if ((tmp->status & mask))
106 for (p = &report_array[0][0]; *p; p += 3)
107 if (*p == tmp->status)
112 host = BadPtr(tmp->host) ? null : tmp->host;
113 pass = BadPtr(tmp->passwd) ? null : tmp->passwd;
114 name = BadPtr(tmp->name) ? null : tmp->name;
115 port = tmp->address.port;
117 * On K line the passwd contents can be
118 * displayed on STATS reply. -Vesa
120 /* Special-case 'k' or 'K' lines as appropriate... -Kev */
121 if ((tmp->status & CONF_UWORLD))
122 send_reply(sptr, p[1], c, host, pass, name, port, get_conf_class(tmp));
123 else if ((tmp->status & (CONF_SERVER | CONF_HUB)))
124 send_reply(sptr, p[1], c, "*", name, port, get_conf_class(tmp));
125 else if ((tmp->status & CONF_CLIENT))
127 if(tmp->passwd && IsDigit(*tmp->passwd) && (!tmp->passwd[1] ||
128 (IsDigit(tmp->passwd[1]) && !tmp->passwd[2])))
129 send_reply(sptr, p[1], c, host, pass, name, port, get_conf_class(tmp));
131 send_reply(sptr, p[1], c, host, "*", name, port, get_conf_class(tmp));
134 send_reply(sptr, p[1], c, host, name, port, get_conf_class(tmp));
140 * {CONF_CRULEALL, RPL_STATSDLINE, 'D'},
141 * {CONF_CRULEAUTO, RPL_STATSDLINE, 'd'},
144 stats_crule_list(struct Client* to, const struct StatDesc *sd,
147 const struct CRuleConf* p = conf_get_crule_list();
149 for ( ; p; p = p->next)
151 if (p->type & sd->sd_funcdata)
152 send_reply(to, RPL_STATSDLINE, sd->sd_c, p->hostmask, p->rule);
157 stats_engine(struct Client *to, const struct StatDesc *sd, char *param)
159 send_reply(to, RPL_STATSENGINE, engine_name());
163 stats_access(struct Client *to, const struct StatDesc *sd, char *param)
165 struct ConfItem *aconf;
171 stats_configured_links(to, sd, param);
175 wilds = string_has_wildcards(param);
177 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
179 if (aconf->status != CONF_CLIENT)
181 if ((!wilds && (!match(aconf->host, param) ||
182 !match(aconf->name, param))) ||
183 (wilds && (!mmatch(param, aconf->host) ||
184 !mmatch(param, aconf->name))))
186 send_reply(to, RPL_STATSILINE, 'I', aconf->host, aconf->name,
187 aconf->address.port, get_conf_class(aconf));
197 * {CONF_KILL, RPL_STATSKLINE, 'K'},
198 * {CONF_IPKILL, RPL_STATSKLINE, 'k'},
201 report_deny_list(struct Client* to)
203 const struct DenyConf* p = conf_get_deny_list();
204 for ( ; p; p = p->next)
205 send_reply(to, RPL_STATSKLINE, (p->flags & DENY_FLAGS_IP) ? 'k' : 'K',
206 p->hostmask, p->message, p->usermask);
210 stats_klines(struct Client *sptr, const struct StatDesc *sd, char *mask)
217 const struct DenyConf* conf;
225 need_more_params(sptr, "STATS K");
227 report_deny_list(sptr);
233 wilds = string_has_wildcards(mask);
236 if ((host = strchr(mask, '@')))
244 for (conf = conf_get_deny_list(); conf; conf = conf->next)
246 if ((!wilds && ((user || conf->hostmask) &&
247 !match(conf->hostmask, host) &&
248 (!user || !match(conf->usermask, user)))) ||
249 (wilds && !mmatch(host, conf->hostmask) &&
250 (!user || !mmatch(user, conf->usermask))))
252 send_reply(sptr, RPL_STATSKLINE,
253 (conf->flags & DENY_FLAGS_IP) ? 'k' : 'K',
254 conf->hostmask, conf->message, conf->usermask);
262 stats_links(struct Client* sptr, const struct StatDesc* sd, char* name)
264 struct Client *acptr;
269 wilds = string_has_wildcards(name);
272 * Send info about connections which match, or all if the
273 * mask matches me.name. Only restrictions are on those who
274 * are invisible not being visible to 'foreigners' who use
275 * a wild card based search to list it.
277 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
278 "SendM SendKBytes RcveM RcveKBytes :Open since");
279 for (i = 0; i <= HighestFd; i++)
281 if (!(acptr = LocalClientArray[i]))
283 /* Don't return clients when this is a request for `all' */
284 if (!name && IsUser(acptr))
286 /* Don't show invisible people to non opers unless they know the nick */
287 if (IsInvisible(acptr) && (!name || wilds) && !IsAnOper(acptr) &&
290 /* Only show the ones that match the given mask - if any */
291 if (name && wilds && match(name, cli_name(acptr)))
293 /* Skip all that do not match the specific query */
294 if (!(!name || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
296 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
297 "%s %u %u %u %u %u :%Tu",
298 (*(cli_name(acptr))) ? cli_name(acptr) : "<unregistered>",
299 (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
300 (int)cli_sendK(acptr), (int)cli_receiveM(acptr),
301 (int)cli_receiveK(acptr), CurrentTime - cli_firsttime(acptr));
305 /* hopefuly this will be where we'll spit out info about loaded modules */
307 stats_modules(struct Client* to, const struct StatDesc* sd, char* param)
309 crypt_mechs_t* mechs;
311 send_reply(to, SND_EXPLICIT | RPL_STATSLLINE,
312 "Module Description Entry Point");
314 /* atm the only "modules" we have are the crypto mechanisms,
315 eventualy they'll be part of a global dl module list, for now
316 i'll just output data about them -- hikari */
318 if(crypt_mechs_root == NULL)
321 mechs = crypt_mechs_root->next;
328 send_reply(to, SND_EXPLICIT | RPL_STATSLLINE,
330 mechs->mech->shortname, mechs->mech->description,
331 mechs->mech->crypt_function);
339 stats_commands(struct Client* to, const struct StatDesc* sd, char* param)
341 struct Message *mptr;
343 for (mptr = msgtab; mptr->cmd; mptr++)
345 send_reply(to, RPL_STATSCOMMANDS, mptr->cmd, mptr->count, mptr->bytes);
349 stats_quarantine(struct Client* to, const struct StatDesc* sd, char* param)
353 for (qline = GlobalQuarantineList; qline; qline = qline->next)
355 if (param && match(param, qline->chname)) /* narrow search */
357 send_reply(to, RPL_STATSQLINE, qline->chname, qline->reason);
362 stats_mapping(struct Client *to, const struct StatDesc* sd, char* param)
366 send_reply(to, RPL_STATSRLINE, "Command", "Name", "Prepend", "Target");
367 for (map = GlobalServiceMapList; map; map = map->next) {
368 struct nick_host *nh;
369 for (nh = map->services; nh; nh = nh->next) {
370 send_reply(to, RPL_STATSRLINE, map->command, map->name,
371 (map->prepend ? map->prepend : "*"), nh->nick);
377 stats_uptime(struct Client* to, const struct StatDesc* sd, char* param)
381 nowr = CurrentTime - cli_since(&me);
382 send_reply(to, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
383 (nowr / 60) % 60, nowr % 60);
384 send_reply(to, RPL_STATSCONN, max_connection_count, max_client_count);
388 stats_servers_verbose(struct Client* sptr, const struct StatDesc* sd,
391 struct Client *acptr;
395 * lowercase 'v' is for human-readable,
396 * uppercase 'V' is for machine-readable
398 if (sd->sd_funcdata) {
399 send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE,
400 "%-20s %-20s Flags Hops Numeric Lag RTT Up Down "
401 "Clients/Max Proto %-10s :Info", "Servername", "Uplink",
403 fmt = "%-20s %-20s %c%c%c%c %4i %s %-4i %5i %4i %4i %4i %5i %5i P%-2i %Tu :%s";
405 fmt = "%s %s %c%c%c%c %i %s %i %i %i %i %i %i %i P%i %Tu :%s";
408 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
410 if (!IsServer(acptr) && !IsMe(acptr))
413 if (param && match(param, cli_name(acptr)))
415 send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE, fmt,
417 cli_name(cli_serv(acptr)->up),
418 IsBurst(acptr) ? 'B' : '-',
419 IsBurstAck(acptr) ? 'A' : '-',
420 IsHub(acptr) ? 'H' : '-',
421 IsService(acptr) ? 'S' : '-',
424 base64toint(cli_yxx(acptr)),
425 cli_serv(acptr)->lag,
426 cli_serv(acptr)->asll_rtt,
427 cli_serv(acptr)->asll_to,
428 cli_serv(acptr)->asll_from,
429 cli_serv(acptr)->clients,
430 cli_serv(acptr)->nn_mask,
431 cli_serv(acptr)->prot,
432 cli_serv(acptr)->timestamp,
439 stats_meminfo(struct Client* to, const struct StatDesc* sd, char* param)
441 class_send_meminfo(to);
442 send_listinfo(to, 0);
447 stats_help(struct Client* to, const struct StatDesc* sd, char* param)
449 struct StatDesc *asd;
451 /* only if it's my user */
453 for (asd = statsinfo; asd->sd_name; asd++)
454 if (asd != sd) /* don't send the help for us */
455 sendcmdto_one(&me, CMD_NOTICE, to, "%C :%c (%s) - %s", to, asd->sd_c,
456 asd->sd_name, asd->sd_desc);
459 /* This array of structures contains information about all single-character
460 * stats. Struct StatDesc is defined in s_stats.h.
462 struct StatDesc statsinfo[] = {
463 { 'a', "nameservers", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_a,
464 report_dns_servers, 0,
466 { 'c', "connect", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_c,
467 stats_configured_links, CONF_SERVER,
468 "Remote server connection lines." },
469 { 'd', "maskrules", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_d,
470 stats_crule_list, CRULE_MASK,
471 "Dynamic routing configuration." },
472 { 'D', "crules", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_d,
473 stats_crule_list, CRULE_ALL,
474 "Dynamic routing configuration." },
475 { 'e', "engine", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_e,
477 "Report server event loop engine." },
478 { 'f', "features", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_f,
480 "Feature settings." },
481 { 'g', "glines", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_g,
483 "Global bans (G-lines)." },
484 { 'h', "hubs", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_h,
485 stats_configured_links, (CONF_HUB | CONF_LEAF),
486 "Hubs information." },
487 { 'i', "access", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_i,
488 stats_access, CONF_CLIENT,
489 "Connection authorization lines." },
490 { 'j', "histogram", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_j,
492 "Message length histogram." },
493 { 'k', "klines", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_k,
495 "Local bans (K-Lines)." },
496 { 'l', "links", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS),
499 "Current connections information." },
500 { 'L', "modules", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS),
503 "Dynamically loaded modules." },
504 { 'm', "commands", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_m,
506 "Message usage information." },
507 { 'o', "operators", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_o,
508 stats_configured_links, CONF_OPERATOR,
509 "Operator information." },
510 { 'p', "ports", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_p,
512 "Listening ports." },
513 { 'q', "quarantines", (STAT_FLAG_OPERONLY | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_q,
515 "Quarantined channels list." },
516 { 'R', "mappings", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_R,
518 "Service mappings." },
520 { 'r', "usage", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_r,
522 "System resource usage (Debug only)." },
524 { 'T', "motds", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_T,
526 "Configured Message Of The Day files." },
527 { 't', "locals", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_t,
529 "Local connection statistics (Total SND/RCV, etc)." },
530 { 'U', "uworld", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_U,
531 stats_configured_links, CONF_UWORLD,
532 "Service server & nick jupes information." },
533 { 'u', "uptime", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_u,
535 "Current uptime & highest connection count." },
536 { 'v', "vservers", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS), FEAT_HIS_STATS_v,
537 stats_servers_verbose, 1,
538 "Verbose server information." },
539 { 'V', "vserversmach", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS), FEAT_HIS_STATS_v,
540 stats_servers_verbose, 0,
541 "Verbose server information." },
542 { 'w', "userload", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_w,
544 "Userload statistics." },
546 { 'x', "memusage", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_x,
548 "List usage information (Debug only)." },
550 { 'y', "classes", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_y,
552 "Connection classes." },
553 { 'z', "memory", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_z,
555 "Memory/Structure allocation information." },
556 { '*', "help", (STAT_FLAG_CASESENS | STAT_FLAG_VARPARAM), FEAT_LAST_F,
558 "Send help for stats." },
559 { '\0', 0, FEAT_LAST_F, 0, 0, 0 }
562 /* This array is for mapping from characters to statistics descriptors */
563 static struct StatDesc *statsmap[256];
564 static int statscount;
567 stats_cmp(const void *a_, const void *b_)
569 const struct StatDesc *a = a_;
570 const struct StatDesc *b = b_;
571 return ircd_strcmp(a->sd_name, b->sd_name);
575 stats_search(const void *key, const void *sd_)
577 const struct StatDesc *sd = sd_;
578 return ircd_strcmp(key, sd->sd_name);
581 /* Look up a stats handler. If name_or_char is just one character
582 * long, use that as a character index; otherwise, look it up by
585 const struct StatDesc *
586 stats_find(const char *name_or_char)
588 if (!name_or_char[1])
589 return statsmap[(int)name_or_char[0]];
591 return bsearch(name_or_char, statsinfo, statscount, sizeof(statsinfo[0]), stats_search);
594 /* Function to build the statsmap from the statsinfo array */
601 /* Make darn sure the statsmap array is initialized to all zeros */
602 for (i = 0; i < 256; i++)
605 /* Count number of stats entries and sort them. */
606 for (statscount = 0, sd = statsinfo; sd->sd_name; sd++, statscount++) {}
607 qsort(statsinfo, statscount, sizeof(statsinfo[0]), stats_cmp);
609 /* Build the mapping */
610 for (sd = statsinfo; sd->sd_name; sd++)
614 else if (sd->sd_flags & STAT_FLAG_CASESENS)
615 /* case sensitive character... */
616 statsmap[(int)sd->sd_c] = sd;
619 /* case insensitive--make sure to put in two entries */
620 statsmap[(int)ToLower((int)sd->sd_c)] = sd;
621 statsmap[(int)ToUpper((int)sd->sd_c)] = sd;