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.
29 #include "ircd_chattr.h"
30 #include "ircd_events.h"
31 #include "ircd_features.h"
32 #include "ircd_crypt.h"
34 #include "ircd_reply.h"
35 #include "ircd_string.h"
44 #include "querycmds.h"
63 * @brief Report configuration lines and other statistics from this
67 * Note: The info is reported in the order the server uses
68 * it--not reversed as in ircd.conf!
71 /* The statsinfo array should only be used in this file, but just TRY
72 * telling the compiler that you want to forward declare a static
73 * array without specifying a length, and see how it responds. So we
74 * forward declare it "extern".
76 extern struct StatDesc statsinfo[];
78 /** Report items from #GlobalConfList.
79 * Uses sd->sd_funcdata as a filter for ConfItem::status.
80 * @param[in] sptr Client requesting statistics.
81 * @param[in] sd Stats descriptor for request.
82 * @param[in] param Extra parameter from user (ignored).
85 stats_configured_links(struct Client *sptr, const struct StatDesc* sd,
88 static char null[] = "<NULL>";
90 unsigned short int port;
92 char *host, *pass, *name, *username, *hub_limit;
94 for (tmp = GlobalConfList; tmp; tmp = tmp->next)
96 if ((tmp->status & sd->sd_funcdata))
98 host = BadPtr(tmp->host) ? null : tmp->host;
99 pass = BadPtr(tmp->passwd) ? null : tmp->passwd;
100 name = BadPtr(tmp->name) ? null : tmp->name;
101 username = BadPtr(tmp->username) ? null : tmp->username;
102 hub_limit = BadPtr(tmp->hub_limit) ? null : tmp->hub_limit;
103 maximum = tmp->maximum;
104 port = tmp->address.port;
106 if (tmp->status & CONF_UWORLD)
107 send_reply(sptr, RPL_STATSULINE, host);
108 else if (tmp->status & CONF_SERVER)
109 send_reply(sptr, RPL_STATSCLINE, name, "", "*", port, maximum, hub_limit, get_conf_class(tmp));
110 else if (tmp->status & CONF_CLIENT)
111 send_reply(sptr, RPL_STATSILINE,
112 (tmp->host ? tmp->host : "*"), maximum,
113 (name[0] == ':' ? "0" : ""), (tmp->name ? tmp->name : "*"),
114 port, get_conf_class(tmp));
115 else if (tmp->status & CONF_OPERATOR)
116 send_reply(sptr, RPL_STATSOLINE, username, host, name, get_conf_class(tmp));
121 /** Report connection rules from conf_get_crule_list().
122 * Uses sd->sd_funcdata as a filter for CRuleConf::type.
123 * @param[in] to Client requesting statistics.
124 * @param[in] sd Stats descriptor for request.
125 * @param[in] param Extra parameter from user (ignored).
128 stats_crule_list(struct Client* to, const struct StatDesc *sd,
131 const struct CRuleConf* p = conf_get_crule_list();
133 for ( ; p; p = p->next)
135 if (p->type & sd->sd_funcdata)
136 send_reply(to, RPL_STATSDLINE, (p->type & CRULE_ALL ? 'D' : 'd'), p->hostmask, p->rule);
140 /** Report active event engine name.
141 * @param[in] to Client requesting statistics.
142 * @param[in] sd Stats descriptor for request (ignored).
143 * @param[in] param Extra parameter from user (ignored).
146 stats_engine(struct Client *to, const struct StatDesc *sd, char *param)
148 send_reply(to, RPL_STATSENGINE, engine_name());
151 /** Report client access lists.
152 * @param[in] to Client requesting statistics.
153 * @param[in] sd Stats descriptor for request.
154 * @param[in] param Filter for hostname or IP (NULL to show all).
157 stats_access(struct Client *to, const struct StatDesc *sd, char *param)
159 struct ConfItem *aconf;
165 stats_configured_links(to, sd, param);
169 wilds = string_has_wildcards(param);
171 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
173 if (aconf->status != CONF_CLIENT)
175 if (wilds ? ((aconf->host && !mmatch(aconf->host, param))
176 || (aconf->name && !mmatch(aconf->name, param)))
177 : ((aconf->host && !match(param, aconf->host))
178 || (aconf->name && !match(param, aconf->name))))
180 send_reply(to, RPL_STATSILINE,
181 (aconf->host ? aconf->host : "*"), aconf->maximum,
182 (aconf->name && aconf->name[0] == ':' ? "0":""),
183 aconf->name ? aconf->name : "*",
184 aconf->address.port, get_conf_class(aconf));
192 /** Report DenyConf entries.
193 * @param[in] to Client requesting list.
196 report_deny_list(struct Client* to)
198 const struct DenyConf* p = conf_get_deny_list();
199 for ( ; p; p = p->next)
200 send_reply(to, RPL_STATSKLINE, p->bits > 0 ? 'k' : 'K',
201 p->usermask ? p->usermask : "*",
202 p->hostmask ? p->hostmask : "*",
203 p->message ? p->message : "(none)",
204 p->realmask ? p->realmask : "*");
207 /** Report K/k-lines to a user.
208 * @param[in] sptr Client requesting statistics.
209 * @param[in] sd Stats descriptor for request (ignored).
210 * @param[in] mask Filter for hostmasks to show.
213 stats_klines(struct Client *sptr, const struct StatDesc *sd, char *mask)
220 const struct DenyConf* conf;
228 need_more_params(sptr, "STATS K");
230 report_deny_list(sptr);
236 wilds = string_has_wildcards(mask);
239 if ((host = strchr(mask, '@')))
247 for (conf = conf_get_deny_list(); conf; conf = conf->next)
249 /* Skip this block if the user is searching for a user-matching
250 * mask but the current Kill doesn't have a usermask, or if user
251 * is searching for a host-matching mask but the Kill has no
252 * hostmask, or if the user mask is specified and doesn't match,
253 * or if the host mask is specified and doesn't match.
255 if ((user && !conf->usermask)
256 || (host && !conf->hostmask)
257 || (user && conf->usermask
259 ? mmatch(user, conf->usermask)
260 : match(conf->usermask, user)))
261 || (host && conf->hostmask
263 ? mmatch(host, conf->hostmask)
264 : match(conf->hostmask, host))))
266 send_reply(sptr, RPL_STATSKLINE, conf->bits > 0 ? 'k' : 'K',
267 conf->usermask ? conf->usermask : "*",
268 conf->hostmask ? conf->hostmask : "*",
269 conf->message ? conf->message : "(none)",
270 conf->realmask ? conf->realmask : "*");
276 /** Report on servers and/or clients connected to the network.
277 * @param[in] sptr Client requesting statistics.
278 * @param[in] sd Stats descriptor for request (ignored).
279 * @param[in] name Filter for client names to show.
282 stats_links(struct Client* sptr, const struct StatDesc* sd, char* name)
284 struct Client *acptr;
289 wilds = string_has_wildcards(name);
292 * Send info about connections which match, or all if the
293 * mask matches me.name. Only restrictions are on those who
294 * are invisible not being visible to 'foreigners' who use
295 * a wild card based search to list it.
297 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
298 "SendM SendKBytes RcveM RcveKBytes :Open since");
299 for (i = 0; i <= HighestFd; i++)
301 if (!(acptr = LocalClientArray[i]))
303 /* Don't return clients when this is a request for `all' */
304 if (!name && IsUser(acptr))
306 /* Don't show invisible people to non opers unless they know the nick */
307 if (IsInvisible(acptr) && (!name || wilds) && !IsAnOper(acptr) &&
310 /* Only show the ones that match the given mask - if any */
311 if (name && wilds && match(name, cli_name(acptr)))
313 /* Skip all that do not match the specific query */
314 if (!(!name || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
316 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
317 "%s %u %u %Lu %u %Lu :%Tu",
318 (*(cli_name(acptr))) ? cli_name(acptr) : "<unregistered>",
319 (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
320 (cli_sendB(acptr) >> 10), (int)cli_receiveM(acptr),
321 (cli_receiveB(acptr) >> 10), CurrentTime - cli_firsttime(acptr));
325 /** Report on loaded modules.
326 * @param[in] to Client requesting statistics.
327 * @param[in] sd Stats descriptor for request (ignored).
328 * @param[in] param Extra parameter from user (ignored).
331 stats_modules(struct Client* to, const struct StatDesc* sd, char* param)
333 crypt_mechs_t* mechs;
335 send_reply(to, SND_EXPLICIT | RPL_STATSLLINE,
336 "Module Description Entry Point");
338 /* atm the only "modules" we have are the crypto mechanisms,
339 eventualy they'll be part of a global dl module list, for now
340 i'll just output data about them -- hikari */
342 if(crypt_mechs_root == NULL)
345 mechs = crypt_mechs_root->next;
352 send_reply(to, SND_EXPLICIT | RPL_STATSLLINE,
354 mechs->mech->shortname, mechs->mech->description,
355 mechs->mech->crypt_function);
362 /** Report how many times each command has been used.
363 * @param[in] to Client requesting statistics.
364 * @param[in] sd Stats descriptor for request (ignored).
365 * @param[in] param Extra parameter from user (ignored).
368 stats_commands(struct Client* to, const struct StatDesc* sd, char* param)
370 struct Message *mptr;
372 for (mptr = msgtab; mptr->cmd; mptr++)
374 send_reply(to, RPL_STATSCOMMANDS, mptr->cmd, mptr->count, mptr->bytes);
377 /** List channel quarantines.
378 * @param[in] to Client requesting statistics.
379 * @param[in] sd Stats descriptor for request (ignored).
380 * @param[in] param Filter for quarantined channel names.
383 stats_quarantine(struct Client* to, const struct StatDesc* sd, char* param)
387 for (qline = GlobalQuarantineList; qline; qline = qline->next)
389 if (param && match(param, qline->chname)) /* narrow search */
391 send_reply(to, RPL_STATSQLINE, qline->chname, qline->reason);
395 /** List service pseudo-command mappings.
396 * @param[in] to Client requesting statistics.
397 * @param[in] sd Stats descriptor for request (ignored).
398 * @param[in] param Extra parameter from user (ignored).
401 stats_mapping(struct Client *to, const struct StatDesc* sd, char* param)
405 send_reply(to, RPL_STATSRLINE, "Command", "Name", "Prepend", "Target");
406 for (map = GlobalServiceMapList; map; map = map->next) {
407 struct nick_host *nh;
408 for (nh = map->services; nh; nh = nh->next) {
409 send_reply(to, RPL_STATSRLINE, map->command, map->name,
410 (map->prepend ? map->prepend : "*"), nh->nick);
415 /** Report server uptime and maximum connection/client counts.
416 * @param[in] to Client requesting statistics.
417 * @param[in] sd Stats descriptor for request (ignored).
418 * @param[in] param Extra parameter from user (ignored).
421 stats_uptime(struct Client* to, const struct StatDesc* sd, char* param)
425 nowr = CurrentTime - cli_since(&me);
426 send_reply(to, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
427 (nowr / 60) % 60, nowr % 60);
428 send_reply(to, RPL_STATSCONN, max_connection_count, max_client_count);
431 /** Verbosely report on servers connected to the network.
432 * If sd->sd_funcdata != 0, then display in a more human-friendly format.
433 * @param[in] sptr Client requesting statistics.
434 * @param[in] sd Stats descriptor for request.
435 * @param[in] param Filter for server names to display.
438 stats_servers_verbose(struct Client* sptr, const struct StatDesc* sd,
441 struct Client *acptr;
445 * lowercase 'v' is for human-readable,
446 * uppercase 'V' is for machine-readable
448 if (sd->sd_funcdata) {
449 send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE,
450 "%-20s %-20s Flags Hops Numeric Lag RTT Up Down "
451 "Clients/Max Proto %-10s :Info", "Servername", "Uplink",
453 fmt = "%-20s %-20s %c%c%c%c %4i %s %-4i %5i %4i %4i %4i %5i %5i P%-2i %Tu :%s";
455 fmt = "%s %s %c%c%c%c %i %s %i %i %i %i %i %i %i P%i %Tu :%s";
458 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
460 if (!IsServer(acptr) && !IsMe(acptr))
463 if (param && match(param, cli_name(acptr)))
465 send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE, fmt,
467 cli_name(cli_serv(acptr)->up),
468 IsBurst(acptr) ? 'B' : '-',
469 IsBurstAck(acptr) ? 'A' : '-',
470 IsHub(acptr) ? 'H' : '-',
471 IsService(acptr) ? 'S' : '-',
474 base64toint(cli_yxx(acptr)),
475 cli_serv(acptr)->lag,
476 cli_serv(acptr)->asll_rtt,
477 cli_serv(acptr)->asll_to,
478 cli_serv(acptr)->asll_from,
479 (acptr == &me ? UserStats.local_clients : cli_serv(acptr)->clients),
480 cli_serv(acptr)->nn_mask,
481 cli_serv(acptr)->prot,
482 cli_serv(acptr)->timestamp,
488 /** Display objects allocated (and total memory used by them) for
489 * several types of structures.
490 * @param[in] to Client requesting statistics.
491 * @param[in] sd Stats descriptor for request (ignored).
492 * @param[in] param Extra parameter from user (ignored).
495 stats_meminfo(struct Client* to, const struct StatDesc* sd, char* param)
497 class_send_meminfo(to);
498 send_listinfo(to, 0);
502 /** Send a list of available statistics.
503 * @param[in] to Client requesting statistics.
504 * @param[in] sd Stats descriptor for request.
505 * @param[in] param Extra parameter from user (ignored).
508 stats_help(struct Client* to, const struct StatDesc* sd, char* param)
510 struct StatDesc *asd;
512 /* only if it's my user */
514 for (asd = statsinfo; asd->sd_name; asd++)
515 if (asd != sd) /* don't send the help for us */
516 sendcmdto_one(&me, CMD_NOTICE, to, "%C :%c (%s) - %s", to, asd->sd_c,
517 asd->sd_name, asd->sd_desc);
520 /** Contains information about all statistics. */
521 struct StatDesc statsinfo[] = {
522 { 'a', "nameservers", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_a,
523 report_dns_servers, 0,
525 { 'c', "connect", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_c,
526 stats_configured_links, CONF_SERVER,
527 "Remote server connection lines." },
528 { 'd', "maskrules", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_d,
529 stats_crule_list, CRULE_MASK,
530 "Dynamic routing configuration." },
531 { 'D', "crules", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_d,
532 stats_crule_list, CRULE_ALL,
533 "Dynamic routing configuration." },
534 { 'e', "engine", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_e,
536 "Report server event loop engine." },
537 { 'f', "features", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_f,
539 "Feature settings." },
540 { 'g', "glines", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_g,
542 "Global bans (G-lines)." },
543 { 'i', "access", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_i,
544 stats_access, CONF_CLIENT,
545 "Connection authorization lines." },
546 { 'j', "histogram", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_j,
548 "Message length histogram." },
549 { 'J', "jupes", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_J,
552 { 'k', "klines", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_k,
554 "Local bans (K-Lines)." },
555 { 'l', "links", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS),
558 "Current connections information." },
559 { 'L', "modules", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS),
562 "Dynamically loaded modules." },
563 { 'm', "commands", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_m,
565 "Message usage information." },
566 { 'o', "operators", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_o,
567 stats_configured_links, CONF_OPERATOR,
568 "Operator information." },
569 { 'p', "ports", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_p,
571 "Listening ports." },
572 { 'q', "quarantines", (STAT_FLAG_OPERONLY | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_q,
574 "Quarantined channels list." },
575 { 'R', "mappings", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_R,
577 "Service mappings." },
579 { 'r', "usage", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_r,
581 "System resource usage (Debug only)." },
583 { 'T', "motds", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_T,
585 "Configured Message Of The Day files." },
586 { 't', "locals", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_t,
588 "Local connection statistics (Total SND/RCV, etc)." },
589 { 'U', "uworld", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_U,
590 stats_configured_links, CONF_UWORLD,
591 "Service server information." },
592 { 'u', "uptime", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_u,
594 "Current uptime & highest connection count." },
595 { 'v', "vservers", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS), FEAT_HIS_STATS_v,
596 stats_servers_verbose, 1,
597 "Verbose server information." },
598 { 'V', "vserversmach", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS), FEAT_HIS_STATS_v,
599 stats_servers_verbose, 0,
600 "Verbose server information." },
601 { 'w', "userload", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_w,
603 "Userload statistics." },
605 { 'x', "memusage", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_x,
607 "List usage information (Debug only)." },
609 { 'y', "classes", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_y,
611 "Connection classes." },
612 { 'z', "memory", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_z,
614 "Memory/Structure allocation information." },
615 { '*', "help", STAT_FLAG_CASESENS, FEAT_LAST_F,
617 "Send help for stats." },
618 { '\0', 0, FEAT_LAST_F, 0, 0, 0 }
621 /** Maps from characters to statistics descriptors.
622 * Statistics descriptors with no single-character alias are not included.
624 static struct StatDesc *statsmap[256];
625 /** Number of statistics descriptors. */
626 static int statscount;
628 /** Compare two StatDesc structures by long name (StatDesc::sd_name).
629 * @param[in] a_ Pointer to a StatDesc.
630 * @param[in] b_ Pointer to a StatDesc.
631 * @return Less than, equal to, or greater than zero if \a a_ is
632 * lexicographically less than, equal to, or greater than \a b_.
635 stats_cmp(const void *a_, const void *b_)
637 const struct StatDesc *a = a_;
638 const struct StatDesc *b = b_;
639 return ircd_strcmp(a->sd_name, b->sd_name);
642 /** Compare a StatDesc's name against a string.
643 * @param[in] key Pointer to a null-terminated string.
644 * @param[in] sd_ Pointer to a StatDesc.
645 * @return Less than, equal to, or greater than zero if \a key is
646 * lexicographically less than, equal to, or greater than \a
650 stats_search(const void *key, const void *sd_)
652 const struct StatDesc *sd = sd_;
653 return ircd_strcmp(key, sd->sd_name);
656 /** Look up a stats handler. If name_or_char is just one character
657 * long, use that as a character index; otherwise, look it up by name
659 * @param[in] name_or_char Null-terminated string to look up.
660 * @return The statistics descriptor for \a name_or_char (NULL if none).
662 const struct StatDesc *
663 stats_find(const char *name_or_char)
665 if (!name_or_char[1])
666 return statsmap[name_or_char[0] - CHAR_MIN];
668 return bsearch(name_or_char, statsinfo, statscount, sizeof(statsinfo[0]), stats_search);
671 /** Build statsmap from the statsinfo array. */
677 /* Count number of stats entries and sort them. */
678 for (statscount = 0, sd = statsinfo; sd->sd_name; sd++, statscount++) {}
679 qsort(statsinfo, statscount, sizeof(statsinfo[0]), stats_cmp);
681 /* Build the mapping */
682 for (sd = statsinfo; sd->sd_name; sd++)
686 else if (sd->sd_flags & STAT_FLAG_CASESENS)
687 /* case sensitive character... */
688 statsmap[sd->sd_c - CHAR_MIN] = sd;
691 /* case insensitive--make sure to put in two entries */
692 statsmap[ToLower(sd->sd_c) - CHAR_MIN] = sd;
693 statsmap[ToUpper(sd->sd_c) - CHAR_MIN] = sd;