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.
28 #include "ircd_chattr.h"
29 #include "ircd_events.h"
30 #include "ircd_features.h"
31 #include "ircd_crypt.h"
33 #include "ircd_reply.h"
34 #include "ircd_string.h"
61 * @brief Report configuration lines and other statistics from this
65 * Note: The info is reported in the order the server uses
66 * it--not reversed as in ircd.conf!
69 /** Stats response to use for various ConfItem types. */
70 static unsigned int report_array[][3] = {
71 {CONF_SERVER, RPL_STATSCLINE, 'C'},
72 {CONF_CLIENT, RPL_STATSILINE, 'I'},
73 {CONF_LEAF, RPL_STATSLLINE, 'L'},
74 {CONF_OPERATOR, RPL_STATSOLINE, 'O'},
75 {CONF_HUB, RPL_STATSHLINE, 'H'},
76 {CONF_UWORLD, RPL_STATSULINE, 'U'},
80 /* The statsinfo array should only be used in this file, but just TRY
81 * telling the compiler that you want to forward declare a static
82 * array without specifying a length, and see how it responds. So we
83 * forward declare it "extern".
85 extern struct StatDesc statsinfo[];
87 /** Report items from #GlobalConfList.
88 * Uses sd->sd_funcdata as a filter for ConfItem::status.
89 * @param[in] sptr Client requesting statistics.
90 * @param[in] sd Stats descriptor for request.
91 * @param[in] param Extra parameter from user (ignored).
94 stats_configured_links(struct Client *sptr, const struct StatDesc* sd,
97 static char null[] = "<NULL>";
101 unsigned short int port;
102 char c, *host, *pass, *name;
104 mask = sd->sd_funcdata;
106 for (tmp = GlobalConfList; tmp; tmp = tmp->next)
108 if ((tmp->status & mask))
110 for (p = &report_array[0][0]; *p; p += 3)
111 if (*p == tmp->status)
116 host = BadPtr(tmp->host) ? null : tmp->host;
117 pass = BadPtr(tmp->passwd) ? null : tmp->passwd;
118 name = BadPtr(tmp->name) ? null : tmp->name;
119 port = tmp->address.port;
121 * On K line the passwd contents can be
122 * displayed on STATS reply. -Vesa
124 /* Special-case 'k' or 'K' lines as appropriate... -Kev */
125 if ((tmp->status & CONF_UWORLD))
126 send_reply(sptr, p[1], c, host, pass, name, port, get_conf_class(tmp));
127 else if ((tmp->status & (CONF_SERVER | CONF_HUB)))
128 send_reply(sptr, p[1], c, "*", name, port, get_conf_class(tmp));
129 else if ((tmp->status & CONF_CLIENT))
131 if(tmp->passwd && IsDigit(*tmp->passwd) && (!tmp->passwd[1] ||
132 (IsDigit(tmp->passwd[1]) && !tmp->passwd[2])))
133 send_reply(sptr, p[1], c, host, pass, name, port, get_conf_class(tmp));
135 send_reply(sptr, p[1], c, host, "*", name, port, get_conf_class(tmp));
138 send_reply(sptr, p[1], c, host, name, port, get_conf_class(tmp));
143 /** Report connection rules from conf_get_crule_list().
144 * Uses sd->sd_funcdata as a filter for CRuleConf::type.
145 * @param[in] to Client requesting statistics.
146 * @param[in] sd Stats descriptor for request.
147 * @param[in] param Extra parameter from user (ignored).
150 stats_crule_list(struct Client* to, const struct StatDesc *sd,
153 const struct CRuleConf* p = conf_get_crule_list();
155 for ( ; p; p = p->next)
157 if (p->type & sd->sd_funcdata)
158 send_reply(to, RPL_STATSDLINE, sd->sd_c, p->hostmask, p->rule);
162 /** Report active event engine name.
163 * @param[in] to Client requesting statistics.
164 * @param[in] sd Stats descriptor for request (ignored).
165 * @param[in] param Extra parameter from user (ignored).
168 stats_engine(struct Client *to, const struct StatDesc *sd, char *param)
170 send_reply(to, RPL_STATSENGINE, engine_name());
173 /** Report client access lists.
174 * @param[in] to Client requesting statistics.
175 * @param[in] sd Stats descriptor for request.
176 * @param[in] param Filter for hostname or IP (NULL to show all).
179 stats_access(struct Client *to, const struct StatDesc *sd, char *param)
181 struct ConfItem *aconf;
187 stats_configured_links(to, sd, param);
191 wilds = string_has_wildcards(param);
193 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
195 if (aconf->status != CONF_CLIENT)
197 if ((!wilds && (!match(aconf->host, param) ||
198 !match(aconf->name, param))) ||
199 (wilds && (!mmatch(param, aconf->host) ||
200 !mmatch(param, aconf->name))))
202 send_reply(to, RPL_STATSILINE, 'I', aconf->host, aconf->name,
203 aconf->address.port, get_conf_class(aconf));
211 /** Report DenyConf entries.
212 * @param[in] to Client requesting list.
215 report_deny_list(struct Client* to)
217 const struct DenyConf* p = conf_get_deny_list();
218 for ( ; p; p = p->next)
219 send_reply(to, RPL_STATSKLINE, (p->flags & DENY_FLAGS_IP) ? 'k' : 'K',
220 p->hostmask, p->message, p->usermask);
223 /** Report K/k-lines to a user.
224 * @param[in] sptr Client requesting statistics.
225 * @param[in] sd Stats descriptor for request (ignored).
226 * @param[in] mask Filter for hostmasks to show.
229 stats_klines(struct Client *sptr, const struct StatDesc *sd, char *mask)
236 const struct DenyConf* conf;
244 need_more_params(sptr, "STATS K");
246 report_deny_list(sptr);
252 wilds = string_has_wildcards(mask);
255 if ((host = strchr(mask, '@')))
263 for (conf = conf_get_deny_list(); conf; conf = conf->next)
265 if ((!wilds && ((user || conf->hostmask) &&
266 !match(conf->hostmask, host) &&
267 (!user || !match(conf->usermask, user)))) ||
268 (wilds && !mmatch(host, conf->hostmask) &&
269 (!user || !mmatch(user, conf->usermask))))
271 send_reply(sptr, RPL_STATSKLINE,
272 (conf->flags & DENY_FLAGS_IP) ? 'k' : 'K',
273 conf->hostmask, conf->message, conf->usermask);
280 /** Report on servers and/or clients connected to the network.
281 * @param[in] sptr Client requesting statistics.
282 * @param[in] sd Stats descriptor for request (ignored).
283 * @param[in] name Filter for client names to show.
286 stats_links(struct Client* sptr, const struct StatDesc* sd, char* name)
288 struct Client *acptr;
293 wilds = string_has_wildcards(name);
296 * Send info about connections which match, or all if the
297 * mask matches me.name. Only restrictions are on those who
298 * are invisible not being visible to 'foreigners' who use
299 * a wild card based search to list it.
301 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
302 "SendM SendKBytes RcveM RcveKBytes :Open since");
303 for (i = 0; i <= HighestFd; i++)
305 if (!(acptr = LocalClientArray[i]))
307 /* Don't return clients when this is a request for `all' */
308 if (!name && IsUser(acptr))
310 /* Don't show invisible people to non opers unless they know the nick */
311 if (IsInvisible(acptr) && (!name || wilds) && !IsAnOper(acptr) &&
314 /* Only show the ones that match the given mask - if any */
315 if (name && wilds && match(name, cli_name(acptr)))
317 /* Skip all that do not match the specific query */
318 if (!(!name || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
320 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
321 "%s %u %u %u %u %u :%Tu",
322 (*(cli_name(acptr))) ? cli_name(acptr) : "<unregistered>",
323 (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
324 (int)cli_sendK(acptr), (int)cli_receiveM(acptr),
325 (int)cli_receiveK(acptr), CurrentTime - cli_firsttime(acptr));
329 /** Report on loaded modules.
330 * @param[in] to Client requesting statistics.
331 * @param[in] sd Stats descriptor for request (ignored).
332 * @param[in] param Extra parameter from user (ignored).
335 stats_modules(struct Client* to, const struct StatDesc* sd, char* param)
337 crypt_mechs_t* mechs;
339 send_reply(to, SND_EXPLICIT | RPL_STATSLLINE,
340 "Module Description Entry Point");
342 /* atm the only "modules" we have are the crypto mechanisms,
343 eventualy they'll be part of a global dl module list, for now
344 i'll just output data about them -- hikari */
346 if(crypt_mechs_root == NULL)
349 mechs = crypt_mechs_root->next;
356 send_reply(to, SND_EXPLICIT | RPL_STATSLLINE,
358 mechs->mech->shortname, mechs->mech->description,
359 mechs->mech->crypt_function);
366 /** Report how many times each command has been used.
367 * @param[in] to Client requesting statistics.
368 * @param[in] sd Stats descriptor for request (ignored).
369 * @param[in] param Extra parameter from user (ignored).
372 stats_commands(struct Client* to, const struct StatDesc* sd, char* param)
374 struct Message *mptr;
376 for (mptr = msgtab; mptr->cmd; mptr++)
378 send_reply(to, RPL_STATSCOMMANDS, mptr->cmd, mptr->count, mptr->bytes);
381 /** List channel quarantines.
382 * @param[in] to Client requesting statistics.
383 * @param[in] sd Stats descriptor for request (ignored).
384 * @param[in] param Filter for quarantined channel names.
387 stats_quarantine(struct Client* to, const struct StatDesc* sd, char* param)
391 for (qline = GlobalQuarantineList; qline; qline = qline->next)
393 if (param && match(param, qline->chname)) /* narrow search */
395 send_reply(to, RPL_STATSQLINE, qline->chname, qline->reason);
399 /** List service pseudo-command mappings.
400 * @param[in] to Client requesting statistics.
401 * @param[in] sd Stats descriptor for request (ignored).
402 * @param[in] param Extra parameter from user (ignored).
405 stats_mapping(struct Client *to, const struct StatDesc* sd, char* param)
409 send_reply(to, RPL_STATSRLINE, "Command", "Name", "Prepend", "Target");
410 for (map = GlobalServiceMapList; map; map = map->next) {
411 struct nick_host *nh;
412 for (nh = map->services; nh; nh = nh->next) {
413 send_reply(to, RPL_STATSRLINE, map->command, map->name,
414 (map->prepend ? map->prepend : "*"), nh->nick);
419 /** Report server uptime and maximum connection/client counts.
420 * @param[in] to Client requesting statistics.
421 * @param[in] sd Stats descriptor for request (ignored).
422 * @param[in] param Extra parameter from user (ignored).
425 stats_uptime(struct Client* to, const struct StatDesc* sd, char* param)
429 nowr = CurrentTime - cli_since(&me);
430 send_reply(to, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
431 (nowr / 60) % 60, nowr % 60);
432 send_reply(to, RPL_STATSCONN, max_connection_count, max_client_count);
435 /** Verbosely report on servers connected to the network.
436 * If sd->sd_funcdata != 0, then display in a more human-friendly format.
437 * @param[in] sptr Client requesting statistics.
438 * @param[in] sd Stats descriptor for request.
439 * @param[in] param Filter for server names to display.
442 stats_servers_verbose(struct Client* sptr, const struct StatDesc* sd,
445 struct Client *acptr;
449 * lowercase 'v' is for human-readable,
450 * uppercase 'V' is for machine-readable
452 if (sd->sd_funcdata) {
453 send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE,
454 "%-20s %-20s Flags Hops Numeric Lag RTT Up Down "
455 "Clients/Max Proto %-10s :Info", "Servername", "Uplink",
457 fmt = "%-20s %-20s %c%c%c%c %4i %s %-4i %5i %4i %4i %4i %5i %5i P%-2i %Tu :%s";
459 fmt = "%s %s %c%c%c%c %i %s %i %i %i %i %i %i %i P%i %Tu :%s";
462 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
464 if (!IsServer(acptr) && !IsMe(acptr))
467 if (param && match(param, cli_name(acptr)))
469 send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE, fmt,
471 cli_name(cli_serv(acptr)->up),
472 IsBurst(acptr) ? 'B' : '-',
473 IsBurstAck(acptr) ? 'A' : '-',
474 IsHub(acptr) ? 'H' : '-',
475 IsService(acptr) ? 'S' : '-',
478 base64toint(cli_yxx(acptr)),
479 cli_serv(acptr)->lag,
480 cli_serv(acptr)->asll_rtt,
481 cli_serv(acptr)->asll_to,
482 cli_serv(acptr)->asll_from,
483 cli_serv(acptr)->clients,
484 cli_serv(acptr)->nn_mask,
485 cli_serv(acptr)->prot,
486 cli_serv(acptr)->timestamp,
492 /** Display objects allocated (and total memory used by them) for
493 * several types of structures.
494 * @param[in] to Client requesting statistics.
495 * @param[in] sd Stats descriptor for request (ignored).
496 * @param[in] param Extra parameter from user (ignored).
499 stats_meminfo(struct Client* to, const struct StatDesc* sd, char* param)
501 class_send_meminfo(to);
502 send_listinfo(to, 0);
506 /** Send a list of available statistics.
507 * @param[in] to Client requesting statistics.
508 * @param[in] sd Stats descriptor for request.
509 * @param[in] param Extra parameter from user (ignored).
512 stats_help(struct Client* to, const struct StatDesc* sd, char* param)
514 struct StatDesc *asd;
516 /* only if it's my user */
518 for (asd = statsinfo; asd->sd_name; asd++)
519 if (asd != sd) /* don't send the help for us */
520 sendcmdto_one(&me, CMD_NOTICE, to, "%C :%c (%s) - %s", to, asd->sd_c,
521 asd->sd_name, asd->sd_desc);
524 /** Contains information about all statistics. */
525 struct StatDesc statsinfo[] = {
526 { 'a', "nameservers", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_a,
527 report_dns_servers, 0,
529 { 'c', "connect", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_c,
530 stats_configured_links, CONF_SERVER,
531 "Remote server connection lines." },
532 { 'd', "maskrules", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_d,
533 stats_crule_list, CRULE_MASK,
534 "Dynamic routing configuration." },
535 { 'D', "crules", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_d,
536 stats_crule_list, CRULE_ALL,
537 "Dynamic routing configuration." },
538 { 'e', "engine", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_e,
540 "Report server event loop engine." },
541 { 'f', "features", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_f,
543 "Feature settings." },
544 { 'g', "glines", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_g,
546 "Global bans (G-lines)." },
547 { 'h', "hubs", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_h,
548 stats_configured_links, (CONF_HUB | CONF_LEAF),
549 "Hubs information." },
550 { 'i', "access", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_i,
551 stats_access, CONF_CLIENT,
552 "Connection authorization lines." },
553 { 'j', "histogram", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_j,
555 "Message length histogram." },
556 { 'k', "klines", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_k,
558 "Local bans (K-Lines)." },
559 { 'l', "links", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS),
562 "Current connections information." },
563 { 'L', "modules", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS),
566 "Dynamically loaded modules." },
567 { 'm', "commands", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_m,
569 "Message usage information." },
570 { 'o', "operators", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_o,
571 stats_configured_links, CONF_OPERATOR,
572 "Operator information." },
573 { 'p', "ports", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_p,
575 "Listening ports." },
576 { 'q', "quarantines", (STAT_FLAG_OPERONLY | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_q,
578 "Quarantined channels list." },
579 { 'R', "mappings", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_R,
581 "Service mappings." },
583 { 'r', "usage", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_r,
585 "System resource usage (Debug only)." },
587 { 'T', "motds", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_T,
589 "Configured Message Of The Day files." },
590 { 't', "locals", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_t,
592 "Local connection statistics (Total SND/RCV, etc)." },
593 { 'U', "uworld", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_U,
594 stats_configured_links, CONF_UWORLD,
595 "Service server & nick jupes information." },
596 { 'u', "uptime", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_u,
598 "Current uptime & highest connection count." },
599 { 'v', "vservers", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS), FEAT_HIS_STATS_v,
600 stats_servers_verbose, 1,
601 "Verbose server information." },
602 { 'V', "vserversmach", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS), FEAT_HIS_STATS_v,
603 stats_servers_verbose, 0,
604 "Verbose server information." },
605 { 'w', "userload", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_w,
607 "Userload statistics." },
609 { 'x', "memusage", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_x,
611 "List usage information (Debug only)." },
613 { 'y', "classes", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_y,
615 "Connection classes." },
616 { 'z', "memory", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_z,
618 "Memory/Structure allocation information." },
619 { '*', "help", (STAT_FLAG_CASESENS | STAT_FLAG_VARPARAM), FEAT_LAST_F,
621 "Send help for stats." },
622 { '\0', 0, FEAT_LAST_F, 0, 0, 0 }
625 /** Maps from characters to statistics descriptors.
626 * Statistics descriptors with no single-character alias are not included.
628 static struct StatDesc *statsmap[256];
629 /** Number of statistics descriptors. */
630 static int statscount;
632 /** Compare two StatDesc structures by long name (StatDesc::sd_name).
633 * @param[in] a_ Pointer to a StatDesc.
634 * @param[in] b_ Pointer to a StatDesc.
635 * @return Less than, equal to, or greater than zero if \a a_ is
636 * lexicographically less than, equal to, or greater than \a b_.
639 stats_cmp(const void *a_, const void *b_)
641 const struct StatDesc *a = a_;
642 const struct StatDesc *b = b_;
643 return ircd_strcmp(a->sd_name, b->sd_name);
646 /** Compare a StatDesc's name against a string.
647 * @param[in] key Pointer to a null-terminated string.
648 * @param[in] sd_ Pointer to a StatDesc.
649 * @return Less than,e qual to, or greater than zero if \a key is
650 * lexicographically less than, equal to, or greater than \a
654 stats_search(const void *key, const void *sd_)
656 const struct StatDesc *sd = sd_;
657 return ircd_strcmp(key, sd->sd_name);
660 /** Look up a stats handler. If name_or_char is just one character
661 * long, use that as a character index; otherwise, look it up by name
663 * @param[in] name_or_char Null-terminated string to look up.
664 * @return The statistics descriptor for \a name_or_char (NULL if none).
666 const struct StatDesc *
667 stats_find(const char *name_or_char)
669 if (!name_or_char[1])
670 return statsmap[(int)name_or_char[0]];
672 return bsearch(name_or_char, statsinfo, statscount, sizeof(statsinfo[0]), stats_search);
675 /** Build statsmap from the statsinfo array. */
682 /* Make darn sure the statsmap array is initialized to all zeros */
683 for (i = 0; i < 256; i++)
686 /* Count number of stats entries and sort them. */
687 for (statscount = 0, sd = statsinfo; sd->sd_name; sd++, statscount++) {}
688 qsort(statsinfo, statscount, sizeof(statsinfo[0]), stats_cmp);
690 /* Build the mapping */
691 for (sd = statsinfo; sd->sd_name; sd++)
695 else if (sd->sd_flags & STAT_FLAG_CASESENS)
696 /* case sensitive character... */
697 statsmap[(int)sd->sd_c] = sd;
700 /* case insensitive--make sure to put in two entries */
701 statsmap[(int)ToLower((int)sd->sd_c)] = sd;
702 statsmap[(int)ToUpper((int)sd->sd_c)] = sd;