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"
62 * @brief Report configuration lines and other statistics from this
66 * Note: The info is reported in the order the server uses
67 * it--not reversed as in ircd.conf!
70 /* The statsinfo array should only be used in this file, but just TRY
71 * telling the compiler that you want to forward declare a static
72 * array without specifying a length, and see how it responds. So we
73 * forward declare it "extern".
75 extern struct StatDesc statsinfo[];
77 /** Report items from #GlobalConfList.
78 * Uses sd->sd_funcdata as a filter for ConfItem::status.
79 * @param[in] sptr Client requesting statistics.
80 * @param[in] sd Stats descriptor for request.
81 * @param[in] param Extra parameter from user (ignored).
84 stats_configured_links(struct Client *sptr, const struct StatDesc* sd,
87 static char null[] = "<NULL>";
89 unsigned short int port;
91 char *host, *pass, *name, *username, *hub_limit;
93 for (tmp = GlobalConfList; tmp; tmp = tmp->next)
95 if ((tmp->status & sd->sd_funcdata))
97 host = BadPtr(tmp->host) ? null : tmp->host;
98 pass = BadPtr(tmp->passwd) ? null : tmp->passwd;
99 name = BadPtr(tmp->name) ? null : tmp->name;
100 username = BadPtr(tmp->username) ? null : tmp->username;
101 hub_limit = BadPtr(tmp->hub_limit) ? null : tmp->hub_limit;
102 maximum = tmp->maximum;
103 port = tmp->address.port;
105 if (tmp->status & CONF_UWORLD)
106 send_reply(sptr, RPL_STATSULINE, host);
107 else if (tmp->status & CONF_SERVER)
108 send_reply(sptr, RPL_STATSCLINE, name, "", "*", port, maximum, hub_limit, get_conf_class(tmp));
109 else if (tmp->status & CONF_CLIENT)
110 send_reply(sptr, RPL_STATSILINE,
111 (tmp->host ? tmp->host : "*"), maximum,
112 (name[0] == ':' ? "0" : ""), (tmp->name ? tmp->name : "*"),
113 port, get_conf_class(tmp));
114 else if (tmp->status & CONF_OPERATOR)
115 send_reply(sptr, RPL_STATSOLINE, username, host, name, get_conf_class(tmp));
120 /** Report connection rules from conf_get_crule_list().
121 * Uses sd->sd_funcdata as a filter for CRuleConf::type.
122 * @param[in] to Client requesting statistics.
123 * @param[in] sd Stats descriptor for request.
124 * @param[in] param Extra parameter from user (ignored).
127 stats_crule_list(struct Client* to, const struct StatDesc *sd,
130 const struct CRuleConf* p = conf_get_crule_list();
132 for ( ; p; p = p->next)
134 if (p->type & sd->sd_funcdata)
135 send_reply(to, RPL_STATSDLINE, sd->sd_c, p->hostmask, p->rule);
139 /** Report active event engine name.
140 * @param[in] to Client requesting statistics.
141 * @param[in] sd Stats descriptor for request (ignored).
142 * @param[in] param Extra parameter from user (ignored).
145 stats_engine(struct Client *to, const struct StatDesc *sd, char *param)
147 send_reply(to, RPL_STATSENGINE, engine_name());
150 /** Report client access lists.
151 * @param[in] to Client requesting statistics.
152 * @param[in] sd Stats descriptor for request.
153 * @param[in] param Filter for hostname or IP (NULL to show all).
156 stats_access(struct Client *to, const struct StatDesc *sd, char *param)
158 struct ConfItem *aconf;
164 stats_configured_links(to, sd, param);
168 wilds = string_has_wildcards(param);
170 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
172 if (aconf->status != CONF_CLIENT)
174 if ((!wilds && (!match(aconf->host, param) ||
175 !match(aconf->name, param))) ||
176 (wilds && (!mmatch(param, aconf->host) ||
177 !mmatch(param, aconf->name))))
179 send_reply(to, RPL_STATSILINE, 'I', aconf->host, aconf->name,
180 aconf->address.port, get_conf_class(aconf));
188 /** Report DenyConf entries.
189 * @param[in] to Client requesting list.
192 report_deny_list(struct Client* to)
194 const struct DenyConf* p = conf_get_deny_list();
195 for ( ; p; p = p->next)
196 send_reply(to, RPL_STATSKLINE, (p->flags & DENY_FLAGS_IP) ? 'k' : 'K',
197 p->hostmask, p->message, p->usermask);
200 /** Report K/k-lines to a user.
201 * @param[in] sptr Client requesting statistics.
202 * @param[in] sd Stats descriptor for request (ignored).
203 * @param[in] mask Filter for hostmasks to show.
206 stats_klines(struct Client *sptr, const struct StatDesc *sd, char *mask)
213 const struct DenyConf* conf;
221 need_more_params(sptr, "STATS K");
223 report_deny_list(sptr);
229 wilds = string_has_wildcards(mask);
232 if ((host = strchr(mask, '@')))
240 for (conf = conf_get_deny_list(); conf; conf = conf->next)
242 if ((!wilds && ((user || conf->hostmask) &&
243 !match(conf->hostmask, host) &&
244 (!user || !match(conf->usermask, user)))) ||
245 (wilds && !mmatch(host, conf->hostmask) &&
246 (!user || !mmatch(user, conf->usermask))))
248 send_reply(sptr, RPL_STATSKLINE,
249 (conf->flags & DENY_FLAGS_IP) ? 'k' : 'K',
250 conf->hostmask, conf->message, conf->usermask);
257 /** Report on servers and/or clients connected to the network.
258 * @param[in] sptr Client requesting statistics.
259 * @param[in] sd Stats descriptor for request (ignored).
260 * @param[in] name Filter for client names to show.
263 stats_links(struct Client* sptr, const struct StatDesc* sd, char* name)
265 struct Client *acptr;
270 wilds = string_has_wildcards(name);
273 * Send info about connections which match, or all if the
274 * mask matches me.name. Only restrictions are on those who
275 * are invisible not being visible to 'foreigners' who use
276 * a wild card based search to list it.
278 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
279 "SendM SendKBytes RcveM RcveKBytes :Open since");
280 for (i = 0; i <= HighestFd; i++)
282 if (!(acptr = LocalClientArray[i]))
284 /* Don't return clients when this is a request for `all' */
285 if (!name && IsUser(acptr))
287 /* Don't show invisible people to non opers unless they know the nick */
288 if (IsInvisible(acptr) && (!name || wilds) && !IsAnOper(acptr) &&
291 /* Only show the ones that match the given mask - if any */
292 if (name && wilds && match(name, cli_name(acptr)))
294 /* Skip all that do not match the specific query */
295 if (!(!name || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
297 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
298 "%s %u %u %Lu %u %Lu :%Tu",
299 (*(cli_name(acptr))) ? cli_name(acptr) : "<unregistered>",
300 (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
301 (cli_sendB(acptr) >> 10), (int)cli_receiveM(acptr),
302 (cli_receiveB(acptr) >> 10), CurrentTime - cli_firsttime(acptr));
306 /** Report on loaded modules.
307 * @param[in] to Client requesting statistics.
308 * @param[in] sd Stats descriptor for request (ignored).
309 * @param[in] param Extra parameter from user (ignored).
312 stats_modules(struct Client* to, const struct StatDesc* sd, char* param)
314 crypt_mechs_t* mechs;
316 send_reply(to, SND_EXPLICIT | RPL_STATSLLINE,
317 "Module Description Entry Point");
319 /* atm the only "modules" we have are the crypto mechanisms,
320 eventualy they'll be part of a global dl module list, for now
321 i'll just output data about them -- hikari */
323 if(crypt_mechs_root == NULL)
326 mechs = crypt_mechs_root->next;
333 send_reply(to, SND_EXPLICIT | RPL_STATSLLINE,
335 mechs->mech->shortname, mechs->mech->description,
336 mechs->mech->crypt_function);
343 /** Report how many times each command has been used.
344 * @param[in] to Client requesting statistics.
345 * @param[in] sd Stats descriptor for request (ignored).
346 * @param[in] param Extra parameter from user (ignored).
349 stats_commands(struct Client* to, const struct StatDesc* sd, char* param)
351 struct Message *mptr;
353 for (mptr = msgtab; mptr->cmd; mptr++)
355 send_reply(to, RPL_STATSCOMMANDS, mptr->cmd, mptr->count, mptr->bytes);
358 /** List channel quarantines.
359 * @param[in] to Client requesting statistics.
360 * @param[in] sd Stats descriptor for request (ignored).
361 * @param[in] param Filter for quarantined channel names.
364 stats_quarantine(struct Client* to, const struct StatDesc* sd, char* param)
368 for (qline = GlobalQuarantineList; qline; qline = qline->next)
370 if (param && match(param, qline->chname)) /* narrow search */
372 send_reply(to, RPL_STATSQLINE, qline->chname, qline->reason);
376 /** List service pseudo-command mappings.
377 * @param[in] to Client requesting statistics.
378 * @param[in] sd Stats descriptor for request (ignored).
379 * @param[in] param Extra parameter from user (ignored).
382 stats_mapping(struct Client *to, const struct StatDesc* sd, char* param)
386 send_reply(to, RPL_STATSRLINE, "Command", "Name", "Prepend", "Target");
387 for (map = GlobalServiceMapList; map; map = map->next) {
388 struct nick_host *nh;
389 for (nh = map->services; nh; nh = nh->next) {
390 send_reply(to, RPL_STATSRLINE, map->command, map->name,
391 (map->prepend ? map->prepend : "*"), nh->nick);
396 /** Report server uptime and maximum connection/client counts.
397 * @param[in] to Client requesting statistics.
398 * @param[in] sd Stats descriptor for request (ignored).
399 * @param[in] param Extra parameter from user (ignored).
402 stats_uptime(struct Client* to, const struct StatDesc* sd, char* param)
406 nowr = CurrentTime - cli_since(&me);
407 send_reply(to, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
408 (nowr / 60) % 60, nowr % 60);
409 send_reply(to, RPL_STATSCONN, max_connection_count, max_client_count);
412 /** Verbosely report on servers connected to the network.
413 * If sd->sd_funcdata != 0, then display in a more human-friendly format.
414 * @param[in] sptr Client requesting statistics.
415 * @param[in] sd Stats descriptor for request.
416 * @param[in] param Filter for server names to display.
419 stats_servers_verbose(struct Client* sptr, const struct StatDesc* sd,
422 struct Client *acptr;
426 * lowercase 'v' is for human-readable,
427 * uppercase 'V' is for machine-readable
429 if (sd->sd_funcdata) {
430 send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE,
431 "%-20s %-20s Flags Hops Numeric Lag RTT Up Down "
432 "Clients/Max Proto %-10s :Info", "Servername", "Uplink",
434 fmt = "%-20s %-20s %c%c%c%c %4i %s %-4i %5i %4i %4i %4i %5i %5i P%-2i %Tu :%s";
436 fmt = "%s %s %c%c%c%c %i %s %i %i %i %i %i %i %i P%i %Tu :%s";
439 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
441 if (!IsServer(acptr) && !IsMe(acptr))
444 if (param && match(param, cli_name(acptr)))
446 send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE, fmt,
448 cli_name(cli_serv(acptr)->up),
449 IsBurst(acptr) ? 'B' : '-',
450 IsBurstAck(acptr) ? 'A' : '-',
451 IsHub(acptr) ? 'H' : '-',
452 IsService(acptr) ? 'S' : '-',
455 base64toint(cli_yxx(acptr)),
456 cli_serv(acptr)->lag,
457 cli_serv(acptr)->asll_rtt,
458 cli_serv(acptr)->asll_to,
459 cli_serv(acptr)->asll_from,
460 cli_serv(acptr)->clients,
461 cli_serv(acptr)->nn_mask,
462 cli_serv(acptr)->prot,
463 cli_serv(acptr)->timestamp,
469 /** Display objects allocated (and total memory used by them) for
470 * several types of structures.
471 * @param[in] to Client requesting statistics.
472 * @param[in] sd Stats descriptor for request (ignored).
473 * @param[in] param Extra parameter from user (ignored).
476 stats_meminfo(struct Client* to, const struct StatDesc* sd, char* param)
478 class_send_meminfo(to);
479 send_listinfo(to, 0);
483 /** Send a list of available statistics.
484 * @param[in] to Client requesting statistics.
485 * @param[in] sd Stats descriptor for request.
486 * @param[in] param Extra parameter from user (ignored).
489 stats_help(struct Client* to, const struct StatDesc* sd, char* param)
491 struct StatDesc *asd;
493 /* only if it's my user */
495 for (asd = statsinfo; asd->sd_name; asd++)
496 if (asd != sd) /* don't send the help for us */
497 sendcmdto_one(&me, CMD_NOTICE, to, "%C :%c (%s) - %s", to, asd->sd_c,
498 asd->sd_name, asd->sd_desc);
501 /** Contains information about all statistics. */
502 struct StatDesc statsinfo[] = {
503 { 'a', "nameservers", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_a,
504 report_dns_servers, 0,
506 { 'c', "connect", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_c,
507 stats_configured_links, CONF_SERVER,
508 "Remote server connection lines." },
509 { 'd', "maskrules", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_d,
510 stats_crule_list, CRULE_MASK,
511 "Dynamic routing configuration." },
512 { 'D', "crules", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_d,
513 stats_crule_list, CRULE_ALL,
514 "Dynamic routing configuration." },
515 { 'e', "engine", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_e,
517 "Report server event loop engine." },
518 { 'f', "features", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_f,
520 "Feature settings." },
521 { 'g', "glines", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_g,
523 "Global bans (G-lines)." },
524 { 'i', "access", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_i,
525 stats_access, CONF_CLIENT,
526 "Connection authorization lines." },
527 { 'j', "histogram", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_j,
529 "Message length histogram." },
530 { 'J', "jupes", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_J,
533 { 'k', "klines", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_k,
535 "Local bans (K-Lines)." },
536 { 'l', "links", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS),
539 "Current connections information." },
540 { 'L', "modules", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS),
543 "Dynamically loaded modules." },
544 { 'm', "commands", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_m,
546 "Message usage information." },
547 { 'o', "operators", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_o,
548 stats_configured_links, CONF_OPERATOR,
549 "Operator information." },
550 { 'p', "ports", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_p,
552 "Listening ports." },
553 { 'q', "quarantines", (STAT_FLAG_OPERONLY | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_q,
555 "Quarantined channels list." },
556 { 'R', "mappings", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_R,
558 "Service mappings." },
560 { 'r', "usage", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_r,
562 "System resource usage (Debug only)." },
564 { 'T', "motds", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_T,
566 "Configured Message Of The Day files." },
567 { 't', "locals", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_t,
569 "Local connection statistics (Total SND/RCV, etc)." },
570 { 'U', "uworld", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_U,
571 stats_configured_links, CONF_UWORLD,
572 "Service server information." },
573 { 'u', "uptime", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_u,
575 "Current uptime & highest connection count." },
576 { 'v', "vservers", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS), FEAT_HIS_STATS_v,
577 stats_servers_verbose, 1,
578 "Verbose server information." },
579 { 'V', "vserversmach", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS), FEAT_HIS_STATS_v,
580 stats_servers_verbose, 0,
581 "Verbose server information." },
582 { 'w', "userload", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_w,
584 "Userload statistics." },
586 { 'x', "memusage", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_x,
588 "List usage information (Debug only)." },
590 { 'y', "classes", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_y,
592 "Connection classes." },
593 { 'z', "memory", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_z,
595 "Memory/Structure allocation information." },
596 { '*', "help", (STAT_FLAG_CASESENS | STAT_FLAG_VARPARAM), FEAT_LAST_F,
598 "Send help for stats." },
599 { '\0', 0, FEAT_LAST_F, 0, 0, 0 }
602 /** Maps from characters to statistics descriptors.
603 * Statistics descriptors with no single-character alias are not included.
605 static struct StatDesc *statsmap[256];
606 /** Number of statistics descriptors. */
607 static int statscount;
609 /** Compare two StatDesc structures by long name (StatDesc::sd_name).
610 * @param[in] a_ Pointer to a StatDesc.
611 * @param[in] b_ Pointer to a StatDesc.
612 * @return Less than, equal to, or greater than zero if \a a_ is
613 * lexicographically less than, equal to, or greater than \a b_.
616 stats_cmp(const void *a_, const void *b_)
618 const struct StatDesc *a = a_;
619 const struct StatDesc *b = b_;
620 return ircd_strcmp(a->sd_name, b->sd_name);
623 /** Compare a StatDesc's name against a string.
624 * @param[in] key Pointer to a null-terminated string.
625 * @param[in] sd_ Pointer to a StatDesc.
626 * @return Less than, equal to, or greater than zero if \a key is
627 * lexicographically less than, equal to, or greater than \a
631 stats_search(const void *key, const void *sd_)
633 const struct StatDesc *sd = sd_;
634 return ircd_strcmp(key, sd->sd_name);
637 /** Look up a stats handler. If name_or_char is just one character
638 * long, use that as a character index; otherwise, look it up by name
640 * @param[in] name_or_char Null-terminated string to look up.
641 * @return The statistics descriptor for \a name_or_char (NULL if none).
643 const struct StatDesc *
644 stats_find(const char *name_or_char)
646 if (!name_or_char[1])
647 return statsmap[(int)name_or_char[0]];
649 return bsearch(name_or_char, statsinfo, statscount, sizeof(statsinfo[0]), stats_search);
652 /** Build statsmap from the statsinfo array. */
659 /* Make darn sure the statsmap array is initialized to all zeros */
660 for (i = 0; i < 256; i++)
663 /* Count number of stats entries and sort them. */
664 for (statscount = 0, sd = statsinfo; sd->sd_name; sd++, statscount++) {}
665 qsort(statsinfo, statscount, sizeof(statsinfo[0]), stats_cmp);
667 /* Build the mapping */
668 for (sd = statsinfo; sd->sd_name; sd++)
672 else if (sd->sd_flags & STAT_FLAG_CASESENS)
673 /* case sensitive character... */
674 statsmap[(int)sd->sd_c] = sd;
677 /* case insensitive--make sure to put in two entries */
678 statsmap[(int)ToLower((int)sd->sd_c)] = sd;
679 statsmap[(int)ToUpper((int)sd->sd_c)] = sd;