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, *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 hub_limit = BadPtr(tmp->hub_limit) ? null : tmp->hub_limit;
100 maximum = tmp->maximum;
101 port = tmp->address.port;
102 if (tmp->status & CONF_UWORLD)
103 send_reply(sptr, RPL_STATSULINE, host);
104 else if (tmp->status & CONF_SERVER)
105 send_reply(sptr, RPL_STATSCLINE, name, host, port, maximum, hub_limit, get_conf_class(tmp));
106 else if (tmp->status & CONF_CLIENT)
107 send_reply(sptr, RPL_STATSILINE, host, maximum, name, port, get_conf_class(tmp));
108 else if (tmp->status & CONF_OPERATOR)
109 send_reply(sptr, RPL_STATSOLINE, host, name, port, get_conf_class(tmp));
114 /** Report connection rules from conf_get_crule_list().
115 * Uses sd->sd_funcdata as a filter for CRuleConf::type.
116 * @param[in] to Client requesting statistics.
117 * @param[in] sd Stats descriptor for request.
118 * @param[in] param Extra parameter from user (ignored).
121 stats_crule_list(struct Client* to, const struct StatDesc *sd,
124 const struct CRuleConf* p = conf_get_crule_list();
126 for ( ; p; p = p->next)
128 if (p->type & sd->sd_funcdata)
129 send_reply(to, RPL_STATSDLINE, sd->sd_c, p->hostmask, p->rule);
133 /** Report active event engine name.
134 * @param[in] to Client requesting statistics.
135 * @param[in] sd Stats descriptor for request (ignored).
136 * @param[in] param Extra parameter from user (ignored).
139 stats_engine(struct Client *to, const struct StatDesc *sd, char *param)
141 send_reply(to, RPL_STATSENGINE, engine_name());
144 /** Report client access lists.
145 * @param[in] to Client requesting statistics.
146 * @param[in] sd Stats descriptor for request.
147 * @param[in] param Filter for hostname or IP (NULL to show all).
150 stats_access(struct Client *to, const struct StatDesc *sd, char *param)
152 struct ConfItem *aconf;
158 stats_configured_links(to, sd, param);
162 wilds = string_has_wildcards(param);
164 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
166 if (aconf->status != CONF_CLIENT)
168 if ((!wilds && (!match(aconf->host, param) ||
169 !match(aconf->name, param))) ||
170 (wilds && (!mmatch(param, aconf->host) ||
171 !mmatch(param, aconf->name))))
173 send_reply(to, RPL_STATSILINE, 'I', aconf->host, aconf->name,
174 aconf->address.port, get_conf_class(aconf));
182 /** Report DenyConf entries.
183 * @param[in] to Client requesting list.
186 report_deny_list(struct Client* to)
188 const struct DenyConf* p = conf_get_deny_list();
189 for ( ; p; p = p->next)
190 send_reply(to, RPL_STATSKLINE, (p->flags & DENY_FLAGS_IP) ? 'k' : 'K',
191 p->hostmask, p->message, p->usermask);
194 /** Report K/k-lines to a user.
195 * @param[in] sptr Client requesting statistics.
196 * @param[in] sd Stats descriptor for request (ignored).
197 * @param[in] mask Filter for hostmasks to show.
200 stats_klines(struct Client *sptr, const struct StatDesc *sd, char *mask)
207 const struct DenyConf* conf;
215 need_more_params(sptr, "STATS K");
217 report_deny_list(sptr);
223 wilds = string_has_wildcards(mask);
226 if ((host = strchr(mask, '@')))
234 for (conf = conf_get_deny_list(); conf; conf = conf->next)
236 if ((!wilds && ((user || conf->hostmask) &&
237 !match(conf->hostmask, host) &&
238 (!user || !match(conf->usermask, user)))) ||
239 (wilds && !mmatch(host, conf->hostmask) &&
240 (!user || !mmatch(user, conf->usermask))))
242 send_reply(sptr, RPL_STATSKLINE,
243 (conf->flags & DENY_FLAGS_IP) ? 'k' : 'K',
244 conf->hostmask, conf->message, conf->usermask);
251 /** Report on servers and/or clients connected to the network.
252 * @param[in] sptr Client requesting statistics.
253 * @param[in] sd Stats descriptor for request (ignored).
254 * @param[in] name Filter for client names to show.
257 stats_links(struct Client* sptr, const struct StatDesc* sd, char* name)
259 struct Client *acptr;
264 wilds = string_has_wildcards(name);
267 * Send info about connections which match, or all if the
268 * mask matches me.name. Only restrictions are on those who
269 * are invisible not being visible to 'foreigners' who use
270 * a wild card based search to list it.
272 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
273 "SendM SendKBytes RcveM RcveKBytes :Open since");
274 for (i = 0; i <= HighestFd; i++)
276 if (!(acptr = LocalClientArray[i]))
278 /* Don't return clients when this is a request for `all' */
279 if (!name && IsUser(acptr))
281 /* Don't show invisible people to non opers unless they know the nick */
282 if (IsInvisible(acptr) && (!name || wilds) && !IsAnOper(acptr) &&
285 /* Only show the ones that match the given mask - if any */
286 if (name && wilds && match(name, cli_name(acptr)))
288 /* Skip all that do not match the specific query */
289 if (!(!name || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
291 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
292 "%s %u %u %Lu %u %Lu :%Tu",
293 (*(cli_name(acptr))) ? cli_name(acptr) : "<unregistered>",
294 (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
295 (cli_sendB(acptr) >> 10), (int)cli_receiveM(acptr),
296 (cli_receiveB(acptr) >> 10), CurrentTime - cli_firsttime(acptr));
300 /** Report on loaded modules.
301 * @param[in] to Client requesting statistics.
302 * @param[in] sd Stats descriptor for request (ignored).
303 * @param[in] param Extra parameter from user (ignored).
306 stats_modules(struct Client* to, const struct StatDesc* sd, char* param)
308 crypt_mechs_t* mechs;
310 send_reply(to, SND_EXPLICIT | RPL_STATSLLINE,
311 "Module Description Entry Point");
313 /* atm the only "modules" we have are the crypto mechanisms,
314 eventualy they'll be part of a global dl module list, for now
315 i'll just output data about them -- hikari */
317 if(crypt_mechs_root == NULL)
320 mechs = crypt_mechs_root->next;
327 send_reply(to, SND_EXPLICIT | RPL_STATSLLINE,
329 mechs->mech->shortname, mechs->mech->description,
330 mechs->mech->crypt_function);
337 /** Report how many times each command has been used.
338 * @param[in] to Client requesting statistics.
339 * @param[in] sd Stats descriptor for request (ignored).
340 * @param[in] param Extra parameter from user (ignored).
343 stats_commands(struct Client* to, const struct StatDesc* sd, char* param)
345 struct Message *mptr;
347 for (mptr = msgtab; mptr->cmd; mptr++)
349 send_reply(to, RPL_STATSCOMMANDS, mptr->cmd, mptr->count, mptr->bytes);
352 /** List channel quarantines.
353 * @param[in] to Client requesting statistics.
354 * @param[in] sd Stats descriptor for request (ignored).
355 * @param[in] param Filter for quarantined channel names.
358 stats_quarantine(struct Client* to, const struct StatDesc* sd, char* param)
362 for (qline = GlobalQuarantineList; qline; qline = qline->next)
364 if (param && match(param, qline->chname)) /* narrow search */
366 send_reply(to, RPL_STATSQLINE, qline->chname, qline->reason);
370 /** List service pseudo-command mappings.
371 * @param[in] to Client requesting statistics.
372 * @param[in] sd Stats descriptor for request (ignored).
373 * @param[in] param Extra parameter from user (ignored).
376 stats_mapping(struct Client *to, const struct StatDesc* sd, char* param)
380 send_reply(to, RPL_STATSRLINE, "Command", "Name", "Prepend", "Target");
381 for (map = GlobalServiceMapList; map; map = map->next) {
382 struct nick_host *nh;
383 for (nh = map->services; nh; nh = nh->next) {
384 send_reply(to, RPL_STATSRLINE, map->command, map->name,
385 (map->prepend ? map->prepend : "*"), nh->nick);
390 /** Report server uptime and maximum connection/client counts.
391 * @param[in] to Client requesting statistics.
392 * @param[in] sd Stats descriptor for request (ignored).
393 * @param[in] param Extra parameter from user (ignored).
396 stats_uptime(struct Client* to, const struct StatDesc* sd, char* param)
400 nowr = CurrentTime - cli_since(&me);
401 send_reply(to, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
402 (nowr / 60) % 60, nowr % 60);
403 send_reply(to, RPL_STATSCONN, max_connection_count, max_client_count);
406 /** Verbosely report on servers connected to the network.
407 * If sd->sd_funcdata != 0, then display in a more human-friendly format.
408 * @param[in] sptr Client requesting statistics.
409 * @param[in] sd Stats descriptor for request.
410 * @param[in] param Filter for server names to display.
413 stats_servers_verbose(struct Client* sptr, const struct StatDesc* sd,
416 struct Client *acptr;
420 * lowercase 'v' is for human-readable,
421 * uppercase 'V' is for machine-readable
423 if (sd->sd_funcdata) {
424 send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE,
425 "%-20s %-20s Flags Hops Numeric Lag RTT Up Down "
426 "Clients/Max Proto %-10s :Info", "Servername", "Uplink",
428 fmt = "%-20s %-20s %c%c%c%c %4i %s %-4i %5i %4i %4i %4i %5i %5i P%-2i %Tu :%s";
430 fmt = "%s %s %c%c%c%c %i %s %i %i %i %i %i %i %i P%i %Tu :%s";
433 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
435 if (!IsServer(acptr) && !IsMe(acptr))
438 if (param && match(param, cli_name(acptr)))
440 send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE, fmt,
442 cli_name(cli_serv(acptr)->up),
443 IsBurst(acptr) ? 'B' : '-',
444 IsBurstAck(acptr) ? 'A' : '-',
445 IsHub(acptr) ? 'H' : '-',
446 IsService(acptr) ? 'S' : '-',
449 base64toint(cli_yxx(acptr)),
450 cli_serv(acptr)->lag,
451 cli_serv(acptr)->asll_rtt,
452 cli_serv(acptr)->asll_to,
453 cli_serv(acptr)->asll_from,
454 cli_serv(acptr)->clients,
455 cli_serv(acptr)->nn_mask,
456 cli_serv(acptr)->prot,
457 cli_serv(acptr)->timestamp,
463 /** Display objects allocated (and total memory used by them) for
464 * several types of structures.
465 * @param[in] to Client requesting statistics.
466 * @param[in] sd Stats descriptor for request (ignored).
467 * @param[in] param Extra parameter from user (ignored).
470 stats_meminfo(struct Client* to, const struct StatDesc* sd, char* param)
472 class_send_meminfo(to);
473 send_listinfo(to, 0);
477 /** Send a list of available statistics.
478 * @param[in] to Client requesting statistics.
479 * @param[in] sd Stats descriptor for request.
480 * @param[in] param Extra parameter from user (ignored).
483 stats_help(struct Client* to, const struct StatDesc* sd, char* param)
485 struct StatDesc *asd;
487 /* only if it's my user */
489 for (asd = statsinfo; asd->sd_name; asd++)
490 if (asd != sd) /* don't send the help for us */
491 sendcmdto_one(&me, CMD_NOTICE, to, "%C :%c (%s) - %s", to, asd->sd_c,
492 asd->sd_name, asd->sd_desc);
495 /** Contains information about all statistics. */
496 struct StatDesc statsinfo[] = {
497 { 'a', "nameservers", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_a,
498 report_dns_servers, 0,
500 { 'c', "connect", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_c,
501 stats_configured_links, CONF_SERVER,
502 "Remote server connection lines." },
503 { 'd', "maskrules", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_d,
504 stats_crule_list, CRULE_MASK,
505 "Dynamic routing configuration." },
506 { 'D', "crules", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_d,
507 stats_crule_list, CRULE_ALL,
508 "Dynamic routing configuration." },
509 { 'e', "engine", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_e,
511 "Report server event loop engine." },
512 { 'f', "features", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_f,
514 "Feature settings." },
515 { 'g', "glines", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_g,
517 "Global bans (G-lines)." },
518 { 'i', "access", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_i,
519 stats_access, CONF_CLIENT,
520 "Connection authorization lines." },
521 { 'j', "histogram", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_j,
523 "Message length histogram." },
524 { 'k', "klines", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_k,
526 "Local bans (K-Lines)." },
527 { 'l', "links", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS),
530 "Current connections information." },
531 { 'L', "modules", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS),
534 "Dynamically loaded modules." },
535 { 'm', "commands", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_m,
537 "Message usage information." },
538 { 'o', "operators", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_o,
539 stats_configured_links, CONF_OPERATOR,
540 "Operator information." },
541 { 'p', "ports", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_p,
543 "Listening ports." },
544 { 'q', "quarantines", (STAT_FLAG_OPERONLY | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_q,
546 "Quarantined channels list." },
547 { 'R', "mappings", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_R,
549 "Service mappings." },
551 { 'r', "usage", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_r,
553 "System resource usage (Debug only)." },
555 { 'T', "motds", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_T,
557 "Configured Message Of The Day files." },
558 { 't', "locals", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_t,
560 "Local connection statistics (Total SND/RCV, etc)." },
561 { 'U', "uworld", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_U,
562 stats_configured_links, CONF_UWORLD,
563 "Service server & nick jupes information." },
564 { 'u', "uptime", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_u,
566 "Current uptime & highest connection count." },
567 { 'v', "vservers", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS), FEAT_HIS_STATS_v,
568 stats_servers_verbose, 1,
569 "Verbose server information." },
570 { 'V', "vserversmach", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS), FEAT_HIS_STATS_v,
571 stats_servers_verbose, 0,
572 "Verbose server information." },
573 { 'w', "userload", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_w,
575 "Userload statistics." },
577 { 'x', "memusage", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_x,
579 "List usage information (Debug only)." },
581 { 'y', "classes", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_y,
583 "Connection classes." },
584 { 'z', "memory", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_z,
586 "Memory/Structure allocation information." },
587 { '*', "help", (STAT_FLAG_CASESENS | STAT_FLAG_VARPARAM), FEAT_LAST_F,
589 "Send help for stats." },
590 { '\0', 0, FEAT_LAST_F, 0, 0, 0 }
593 /** Maps from characters to statistics descriptors.
594 * Statistics descriptors with no single-character alias are not included.
596 static struct StatDesc *statsmap[256];
597 /** Number of statistics descriptors. */
598 static int statscount;
600 /** Compare two StatDesc structures by long name (StatDesc::sd_name).
601 * @param[in] a_ Pointer to a StatDesc.
602 * @param[in] b_ Pointer to a StatDesc.
603 * @return Less than, equal to, or greater than zero if \a a_ is
604 * lexicographically less than, equal to, or greater than \a b_.
607 stats_cmp(const void *a_, const void *b_)
609 const struct StatDesc *a = a_;
610 const struct StatDesc *b = b_;
611 return ircd_strcmp(a->sd_name, b->sd_name);
614 /** Compare a StatDesc's name against a string.
615 * @param[in] key Pointer to a null-terminated string.
616 * @param[in] sd_ Pointer to a StatDesc.
617 * @return Less than,e qual to, or greater than zero if \a key is
618 * lexicographically less than, equal to, or greater than \a
622 stats_search(const void *key, const void *sd_)
624 const struct StatDesc *sd = sd_;
625 return ircd_strcmp(key, sd->sd_name);
628 /** Look up a stats handler. If name_or_char is just one character
629 * long, use that as a character index; otherwise, look it up by name
631 * @param[in] name_or_char Null-terminated string to look up.
632 * @return The statistics descriptor for \a name_or_char (NULL if none).
634 const struct StatDesc *
635 stats_find(const char *name_or_char)
637 if (!name_or_char[1])
638 return statsmap[(int)name_or_char[0]];
640 return bsearch(name_or_char, statsinfo, statscount, sizeof(statsinfo[0]), stats_search);
643 /** Build statsmap from the statsinfo array. */
650 /* Make darn sure the statsmap array is initialized to all zeros */
651 for (i = 0; i < 256; i++)
654 /* Count number of stats entries and sort them. */
655 for (statscount = 0, sd = statsinfo; sd->sd_name; sd++, statscount++) {}
656 qsort(statsinfo, statscount, sizeof(statsinfo[0]), stats_cmp);
658 /* Build the mapping */
659 for (sd = statsinfo; sd->sd_name; sd++)
663 else if (sd->sd_flags & STAT_FLAG_CASESENS)
664 /* case sensitive character... */
665 statsmap[(int)sd->sd_c] = sd;
668 /* case insensitive--make sure to put in two entries */
669 statsmap[(int)ToLower((int)sd->sd_c)] = sd;
670 statsmap[(int)ToUpper((int)sd->sd_c)] = sd;