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 /* The statsinfo array should only be used in this file, but just TRY
70 * telling the compiler that you want to forward declare a static
71 * array without specifying a length, and see how it responds. So we
72 * forward declare it "extern".
74 extern struct StatDesc statsinfo[];
76 /** Report items from #GlobalConfList.
77 * Uses sd->sd_funcdata as a filter for ConfItem::status.
78 * @param[in] sptr Client requesting statistics.
79 * @param[in] sd Stats descriptor for request.
80 * @param[in] param Extra parameter from user (ignored).
83 stats_configured_links(struct Client *sptr, const struct StatDesc* sd,
86 static char null[] = "<NULL>";
88 unsigned short int port;
90 char *host, *pass, *name, *username, *hub_limit;
92 for (tmp = GlobalConfList; tmp; tmp = tmp->next)
94 if ((tmp->status & sd->sd_funcdata))
96 host = BadPtr(tmp->host) ? null : tmp->host;
97 pass = BadPtr(tmp->passwd) ? null : tmp->passwd;
98 name = BadPtr(tmp->name) ? null : tmp->name;
99 username = BadPtr(tmp->username) ? null : tmp->username;
100 hub_limit = BadPtr(tmp->hub_limit) ? null : tmp->hub_limit;
101 maximum = tmp->maximum;
102 port = tmp->address.port;
104 if (tmp->status & CONF_UWORLD)
105 send_reply(sptr, RPL_STATSULINE, host);
106 else if (tmp->status & CONF_SERVER)
107 send_reply(sptr, RPL_STATSCLINE, name, "", "*", port, maximum, hub_limit, get_conf_class(tmp));
108 else if (tmp->status & CONF_CLIENT)
109 send_reply(sptr, RPL_STATSILINE,
110 (tmp->host ? tmp->host : "*"), maximum,
111 (name[0] == ':' ? "0" : ""), (tmp->name ? tmp->name : "*"),
112 port, get_conf_class(tmp));
113 else if (tmp->status & CONF_OPERATOR)
114 send_reply(sptr, RPL_STATSOLINE, username, host, name, get_conf_class(tmp));
119 /** Report connection rules from conf_get_crule_list().
120 * Uses sd->sd_funcdata as a filter for CRuleConf::type.
121 * @param[in] to Client requesting statistics.
122 * @param[in] sd Stats descriptor for request.
123 * @param[in] param Extra parameter from user (ignored).
126 stats_crule_list(struct Client* to, const struct StatDesc *sd,
129 const struct CRuleConf* p = conf_get_crule_list();
131 for ( ; p; p = p->next)
133 if (p->type & sd->sd_funcdata)
134 send_reply(to, RPL_STATSDLINE, sd->sd_c, p->hostmask, p->rule);
138 /** Report active event engine name.
139 * @param[in] to Client requesting statistics.
140 * @param[in] sd Stats descriptor for request (ignored).
141 * @param[in] param Extra parameter from user (ignored).
144 stats_engine(struct Client *to, const struct StatDesc *sd, char *param)
146 send_reply(to, RPL_STATSENGINE, engine_name());
149 /** Report client access lists.
150 * @param[in] to Client requesting statistics.
151 * @param[in] sd Stats descriptor for request.
152 * @param[in] param Filter for hostname or IP (NULL to show all).
155 stats_access(struct Client *to, const struct StatDesc *sd, char *param)
157 struct ConfItem *aconf;
163 stats_configured_links(to, sd, param);
167 wilds = string_has_wildcards(param);
169 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
171 if (aconf->status != CONF_CLIENT)
173 if ((!wilds && (!match(aconf->host, param) ||
174 !match(aconf->name, param))) ||
175 (wilds && (!mmatch(param, aconf->host) ||
176 !mmatch(param, aconf->name))))
178 send_reply(to, RPL_STATSILINE, 'I', aconf->host, aconf->name,
179 aconf->address.port, get_conf_class(aconf));
187 /** Report DenyConf entries.
188 * @param[in] to Client requesting list.
191 report_deny_list(struct Client* to)
193 const struct DenyConf* p = conf_get_deny_list();
194 for ( ; p; p = p->next)
195 send_reply(to, RPL_STATSKLINE, (p->flags & DENY_FLAGS_IP) ? 'k' : 'K',
196 p->hostmask, p->message, p->usermask);
199 /** Report K/k-lines to a user.
200 * @param[in] sptr Client requesting statistics.
201 * @param[in] sd Stats descriptor for request (ignored).
202 * @param[in] mask Filter for hostmasks to show.
205 stats_klines(struct Client *sptr, const struct StatDesc *sd, char *mask)
212 const struct DenyConf* conf;
220 need_more_params(sptr, "STATS K");
222 report_deny_list(sptr);
228 wilds = string_has_wildcards(mask);
231 if ((host = strchr(mask, '@')))
239 for (conf = conf_get_deny_list(); conf; conf = conf->next)
241 if ((!wilds && ((user || conf->hostmask) &&
242 !match(conf->hostmask, host) &&
243 (!user || !match(conf->usermask, user)))) ||
244 (wilds && !mmatch(host, conf->hostmask) &&
245 (!user || !mmatch(user, conf->usermask))))
247 send_reply(sptr, RPL_STATSKLINE,
248 (conf->flags & DENY_FLAGS_IP) ? 'k' : 'K',
249 conf->hostmask, conf->message, conf->usermask);
256 /** Report on servers and/or clients connected to the network.
257 * @param[in] sptr Client requesting statistics.
258 * @param[in] sd Stats descriptor for request (ignored).
259 * @param[in] name Filter for client names to show.
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 %Lu %u %Lu :%Tu",
298 (*(cli_name(acptr))) ? cli_name(acptr) : "<unregistered>",
299 (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
300 (cli_sendB(acptr) >> 10), (int)cli_receiveM(acptr),
301 (cli_receiveB(acptr) >> 10), CurrentTime - cli_firsttime(acptr));
305 /** Report on loaded modules.
306 * @param[in] to Client requesting statistics.
307 * @param[in] sd Stats descriptor for request (ignored).
308 * @param[in] param Extra parameter from user (ignored).
311 stats_modules(struct Client* to, const struct StatDesc* sd, char* param)
313 crypt_mechs_t* mechs;
315 send_reply(to, SND_EXPLICIT | RPL_STATSLLINE,
316 "Module Description Entry Point");
318 /* atm the only "modules" we have are the crypto mechanisms,
319 eventualy they'll be part of a global dl module list, for now
320 i'll just output data about them -- hikari */
322 if(crypt_mechs_root == NULL)
325 mechs = crypt_mechs_root->next;
332 send_reply(to, SND_EXPLICIT | RPL_STATSLLINE,
334 mechs->mech->shortname, mechs->mech->description,
335 mechs->mech->crypt_function);
342 /** Report how many times each command has been used.
343 * @param[in] to Client requesting statistics.
344 * @param[in] sd Stats descriptor for request (ignored).
345 * @param[in] param Extra parameter from user (ignored).
348 stats_commands(struct Client* to, const struct StatDesc* sd, char* param)
350 struct Message *mptr;
352 for (mptr = msgtab; mptr->cmd; mptr++)
354 send_reply(to, RPL_STATSCOMMANDS, mptr->cmd, mptr->count, mptr->bytes);
357 /** List channel quarantines.
358 * @param[in] to Client requesting statistics.
359 * @param[in] sd Stats descriptor for request (ignored).
360 * @param[in] param Filter for quarantined channel names.
363 stats_quarantine(struct Client* to, const struct StatDesc* sd, char* param)
367 for (qline = GlobalQuarantineList; qline; qline = qline->next)
369 if (param && match(param, qline->chname)) /* narrow search */
371 send_reply(to, RPL_STATSQLINE, qline->chname, qline->reason);
375 /** List service pseudo-command mappings.
376 * @param[in] to Client requesting statistics.
377 * @param[in] sd Stats descriptor for request (ignored).
378 * @param[in] param Extra parameter from user (ignored).
381 stats_mapping(struct Client *to, const struct StatDesc* sd, char* param)
385 send_reply(to, RPL_STATSRLINE, "Command", "Name", "Prepend", "Target");
386 for (map = GlobalServiceMapList; map; map = map->next) {
387 struct nick_host *nh;
388 for (nh = map->services; nh; nh = nh->next) {
389 send_reply(to, RPL_STATSRLINE, map->command, map->name,
390 (map->prepend ? map->prepend : "*"), nh->nick);
395 /** Report server uptime and maximum connection/client counts.
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_uptime(struct Client* to, const struct StatDesc* sd, char* param)
405 nowr = CurrentTime - cli_since(&me);
406 send_reply(to, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
407 (nowr / 60) % 60, nowr % 60);
408 send_reply(to, RPL_STATSCONN, max_connection_count, max_client_count);
411 /** Verbosely report on servers connected to the network.
412 * If sd->sd_funcdata != 0, then display in a more human-friendly format.
413 * @param[in] sptr Client requesting statistics.
414 * @param[in] sd Stats descriptor for request.
415 * @param[in] param Filter for server names to display.
418 stats_servers_verbose(struct Client* sptr, const struct StatDesc* sd,
421 struct Client *acptr;
425 * lowercase 'v' is for human-readable,
426 * uppercase 'V' is for machine-readable
428 if (sd->sd_funcdata) {
429 send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE,
430 "%-20s %-20s Flags Hops Numeric Lag RTT Up Down "
431 "Clients/Max Proto %-10s :Info", "Servername", "Uplink",
433 fmt = "%-20s %-20s %c%c%c%c %4i %s %-4i %5i %4i %4i %4i %5i %5i P%-2i %Tu :%s";
435 fmt = "%s %s %c%c%c%c %i %s %i %i %i %i %i %i %i P%i %Tu :%s";
438 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
440 if (!IsServer(acptr) && !IsMe(acptr))
443 if (param && match(param, cli_name(acptr)))
445 send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE, fmt,
447 cli_name(cli_serv(acptr)->up),
448 IsBurst(acptr) ? 'B' : '-',
449 IsBurstAck(acptr) ? 'A' : '-',
450 IsHub(acptr) ? 'H' : '-',
451 IsService(acptr) ? 'S' : '-',
454 base64toint(cli_yxx(acptr)),
455 cli_serv(acptr)->lag,
456 cli_serv(acptr)->asll_rtt,
457 cli_serv(acptr)->asll_to,
458 cli_serv(acptr)->asll_from,
459 cli_serv(acptr)->clients,
460 cli_serv(acptr)->nn_mask,
461 cli_serv(acptr)->prot,
462 cli_serv(acptr)->timestamp,
468 /** Display objects allocated (and total memory used by them) for
469 * several types of structures.
470 * @param[in] to Client requesting statistics.
471 * @param[in] sd Stats descriptor for request (ignored).
472 * @param[in] param Extra parameter from user (ignored).
475 stats_meminfo(struct Client* to, const struct StatDesc* sd, char* param)
477 class_send_meminfo(to);
478 send_listinfo(to, 0);
482 /** Send a list of available statistics.
483 * @param[in] to Client requesting statistics.
484 * @param[in] sd Stats descriptor for request.
485 * @param[in] param Extra parameter from user (ignored).
488 stats_help(struct Client* to, const struct StatDesc* sd, char* param)
490 struct StatDesc *asd;
492 /* only if it's my user */
494 for (asd = statsinfo; asd->sd_name; asd++)
495 if (asd != sd) /* don't send the help for us */
496 sendcmdto_one(&me, CMD_NOTICE, to, "%C :%c (%s) - %s", to, asd->sd_c,
497 asd->sd_name, asd->sd_desc);
500 /** Contains information about all statistics. */
501 struct StatDesc statsinfo[] = {
502 { 'a', "nameservers", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_a,
503 report_dns_servers, 0,
505 { 'c', "connect", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_c,
506 stats_configured_links, CONF_SERVER,
507 "Remote server connection lines." },
508 { 'd', "maskrules", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_d,
509 stats_crule_list, CRULE_MASK,
510 "Dynamic routing configuration." },
511 { 'D', "crules", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_d,
512 stats_crule_list, CRULE_ALL,
513 "Dynamic routing configuration." },
514 { 'e', "engine", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_e,
516 "Report server event loop engine." },
517 { 'f', "features", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_f,
519 "Feature settings." },
520 { 'g', "glines", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_g,
522 "Global bans (G-lines)." },
523 { 'i', "access", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_i,
524 stats_access, CONF_CLIENT,
525 "Connection authorization lines." },
526 { 'j', "histogram", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_j,
528 "Message length histogram." },
529 { 'k', "klines", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_k,
531 "Local bans (K-Lines)." },
532 { 'l', "links", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS),
535 "Current connections information." },
536 { 'L', "modules", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS),
539 "Dynamically loaded modules." },
540 { 'm', "commands", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_m,
542 "Message usage information." },
543 { 'o', "operators", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_o,
544 stats_configured_links, CONF_OPERATOR,
545 "Operator information." },
546 { 'p', "ports", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_p,
548 "Listening ports." },
549 { 'q', "quarantines", (STAT_FLAG_OPERONLY | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_q,
551 "Quarantined channels list." },
552 { 'R', "mappings", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_R,
554 "Service mappings." },
556 { 'r', "usage", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_r,
558 "System resource usage (Debug only)." },
560 { 'T', "motds", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_T,
562 "Configured Message Of The Day files." },
563 { 't', "locals", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_t,
565 "Local connection statistics (Total SND/RCV, etc)." },
566 { 'U', "uworld", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_U,
567 stats_configured_links, CONF_UWORLD,
568 "Service server & nick jupes information." },
569 { 'u', "uptime", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_u,
571 "Current uptime & highest connection count." },
572 { 'v', "vservers", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS), FEAT_HIS_STATS_v,
573 stats_servers_verbose, 1,
574 "Verbose server information." },
575 { 'V', "vserversmach", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS), FEAT_HIS_STATS_v,
576 stats_servers_verbose, 0,
577 "Verbose server information." },
578 { 'w', "userload", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_w,
580 "Userload statistics." },
582 { 'x', "memusage", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_x,
584 "List usage information (Debug only)." },
586 { 'y', "classes", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_y,
588 "Connection classes." },
589 { 'z', "memory", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_z,
591 "Memory/Structure allocation information." },
592 { '*', "help", (STAT_FLAG_CASESENS | STAT_FLAG_VARPARAM), FEAT_LAST_F,
594 "Send help for stats." },
595 { '\0', 0, FEAT_LAST_F, 0, 0, 0 }
598 /** Maps from characters to statistics descriptors.
599 * Statistics descriptors with no single-character alias are not included.
601 static struct StatDesc *statsmap[256];
602 /** Number of statistics descriptors. */
603 static int statscount;
605 /** Compare two StatDesc structures by long name (StatDesc::sd_name).
606 * @param[in] a_ Pointer to a StatDesc.
607 * @param[in] b_ Pointer to a StatDesc.
608 * @return Less than, equal to, or greater than zero if \a a_ is
609 * lexicographically less than, equal to, or greater than \a b_.
612 stats_cmp(const void *a_, const void *b_)
614 const struct StatDesc *a = a_;
615 const struct StatDesc *b = b_;
616 return ircd_strcmp(a->sd_name, b->sd_name);
619 /** Compare a StatDesc's name against a string.
620 * @param[in] key Pointer to a null-terminated string.
621 * @param[in] sd_ Pointer to a StatDesc.
622 * @return Less than,e qual to, or greater than zero if \a key is
623 * lexicographically less than, equal to, or greater than \a
627 stats_search(const void *key, const void *sd_)
629 const struct StatDesc *sd = sd_;
630 return ircd_strcmp(key, sd->sd_name);
633 /** Look up a stats handler. If name_or_char is just one character
634 * long, use that as a character index; otherwise, look it up by name
636 * @param[in] name_or_char Null-terminated string to look up.
637 * @return The statistics descriptor for \a name_or_char (NULL if none).
639 const struct StatDesc *
640 stats_find(const char *name_or_char)
642 if (!name_or_char[1])
643 return statsmap[(int)name_or_char[0]];
645 return bsearch(name_or_char, statsinfo, statscount, sizeof(statsinfo[0]), stats_search);
648 /** Build statsmap from the statsinfo array. */
655 /* Make darn sure the statsmap array is initialized to all zeros */
656 for (i = 0; i < 256; i++)
659 /* Count number of stats entries and sort them. */
660 for (statscount = 0, sd = statsinfo; sd->sd_name; sd++, statscount++) {}
661 qsort(statsinfo, statscount, sizeof(statsinfo[0]), stats_cmp);
663 /* Build the mapping */
664 for (sd = statsinfo; sd->sd_name; sd++)
668 else if (sd->sd_flags & STAT_FLAG_CASESENS)
669 /* case sensitive character... */
670 statsmap[(int)sd->sd_c] = sd;
673 /* case insensitive--make sure to put in two entries */
674 statsmap[(int)ToLower((int)sd->sd_c)] = sd;
675 statsmap[(int)ToUpper((int)sd->sd_c)] = sd;