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;
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 maximum = tmp->maximum;
100 port = tmp->address.port;
101 if (tmp->status & CONF_UWORLD)
102 send_reply(sptr, RPL_STATSULINE, name);
103 else if (tmp->status & CONF_SERVER)
104 send_reply(sptr, RPL_STATSCLINE, name, port, get_conf_class(tmp));
105 else if (tmp->status & CONF_CLIENT)
106 send_reply(sptr, RPL_STATSILINE, host, maximum, name, port, get_conf_class(tmp));
107 else if (tmp->status & CONF_OPERATOR)
108 send_reply(sptr, RPL_STATSOLINE, host, name, port, get_conf_class(tmp));
113 /** Report connection rules from conf_get_crule_list().
114 * Uses sd->sd_funcdata as a filter for CRuleConf::type.
115 * @param[in] to Client requesting statistics.
116 * @param[in] sd Stats descriptor for request.
117 * @param[in] param Extra parameter from user (ignored).
120 stats_crule_list(struct Client* to, const struct StatDesc *sd,
123 const struct CRuleConf* p = conf_get_crule_list();
125 for ( ; p; p = p->next)
127 if (p->type & sd->sd_funcdata)
128 send_reply(to, RPL_STATSDLINE, sd->sd_c, p->hostmask, p->rule);
132 /** Report active event engine name.
133 * @param[in] to Client requesting statistics.
134 * @param[in] sd Stats descriptor for request (ignored).
135 * @param[in] param Extra parameter from user (ignored).
138 stats_engine(struct Client *to, const struct StatDesc *sd, char *param)
140 send_reply(to, RPL_STATSENGINE, engine_name());
143 /** Report client access lists.
144 * @param[in] to Client requesting statistics.
145 * @param[in] sd Stats descriptor for request.
146 * @param[in] param Filter for hostname or IP (NULL to show all).
149 stats_access(struct Client *to, const struct StatDesc *sd, char *param)
151 struct ConfItem *aconf;
157 stats_configured_links(to, sd, param);
161 wilds = string_has_wildcards(param);
163 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
165 if (aconf->status != CONF_CLIENT)
167 if ((!wilds && (!match(aconf->host, param) ||
168 !match(aconf->name, param))) ||
169 (wilds && (!mmatch(param, aconf->host) ||
170 !mmatch(param, aconf->name))))
172 send_reply(to, RPL_STATSILINE, 'I', aconf->host, aconf->name,
173 aconf->address.port, get_conf_class(aconf));
181 /** Report DenyConf entries.
182 * @param[in] to Client requesting list.
185 report_deny_list(struct Client* to)
187 const struct DenyConf* p = conf_get_deny_list();
188 for ( ; p; p = p->next)
189 send_reply(to, RPL_STATSKLINE, (p->flags & DENY_FLAGS_IP) ? 'k' : 'K',
190 p->hostmask, p->message, p->usermask);
193 /** Report K/k-lines to a user.
194 * @param[in] sptr Client requesting statistics.
195 * @param[in] sd Stats descriptor for request (ignored).
196 * @param[in] mask Filter for hostmasks to show.
199 stats_klines(struct Client *sptr, const struct StatDesc *sd, char *mask)
206 const struct DenyConf* conf;
214 need_more_params(sptr, "STATS K");
216 report_deny_list(sptr);
222 wilds = string_has_wildcards(mask);
225 if ((host = strchr(mask, '@')))
233 for (conf = conf_get_deny_list(); conf; conf = conf->next)
235 if ((!wilds && ((user || conf->hostmask) &&
236 !match(conf->hostmask, host) &&
237 (!user || !match(conf->usermask, user)))) ||
238 (wilds && !mmatch(host, conf->hostmask) &&
239 (!user || !mmatch(user, conf->usermask))))
241 send_reply(sptr, RPL_STATSKLINE,
242 (conf->flags & DENY_FLAGS_IP) ? 'k' : 'K',
243 conf->hostmask, conf->message, conf->usermask);
250 /** Report on servers and/or clients connected to the network.
251 * @param[in] sptr Client requesting statistics.
252 * @param[in] sd Stats descriptor for request (ignored).
253 * @param[in] name Filter for client names to show.
256 stats_links(struct Client* sptr, const struct StatDesc* sd, char* name)
258 struct Client *acptr;
263 wilds = string_has_wildcards(name);
266 * Send info about connections which match, or all if the
267 * mask matches me.name. Only restrictions are on those who
268 * are invisible not being visible to 'foreigners' who use
269 * a wild card based search to list it.
271 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
272 "SendM SendKBytes RcveM RcveKBytes :Open since");
273 for (i = 0; i <= HighestFd; i++)
275 if (!(acptr = LocalClientArray[i]))
277 /* Don't return clients when this is a request for `all' */
278 if (!name && IsUser(acptr))
280 /* Don't show invisible people to non opers unless they know the nick */
281 if (IsInvisible(acptr) && (!name || wilds) && !IsAnOper(acptr) &&
284 /* Only show the ones that match the given mask - if any */
285 if (name && wilds && match(name, cli_name(acptr)))
287 /* Skip all that do not match the specific query */
288 if (!(!name || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
290 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
291 "%s %u %u %u %u %u :%Tu",
292 (*(cli_name(acptr))) ? cli_name(acptr) : "<unregistered>",
293 (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
294 (int)cli_sendK(acptr), (int)cli_receiveM(acptr),
295 (int)cli_receiveK(acptr), CurrentTime - cli_firsttime(acptr));
299 /** Report on loaded modules.
300 * @param[in] to Client requesting statistics.
301 * @param[in] sd Stats descriptor for request (ignored).
302 * @param[in] param Extra parameter from user (ignored).
305 stats_modules(struct Client* to, const struct StatDesc* sd, char* param)
307 crypt_mechs_t* mechs;
309 send_reply(to, SND_EXPLICIT | RPL_STATSLLINE,
310 "Module Description Entry Point");
312 /* atm the only "modules" we have are the crypto mechanisms,
313 eventualy they'll be part of a global dl module list, for now
314 i'll just output data about them -- hikari */
316 if(crypt_mechs_root == NULL)
319 mechs = crypt_mechs_root->next;
326 send_reply(to, SND_EXPLICIT | RPL_STATSLLINE,
328 mechs->mech->shortname, mechs->mech->description,
329 mechs->mech->crypt_function);
336 /** Report how many times each command has been used.
337 * @param[in] to Client requesting statistics.
338 * @param[in] sd Stats descriptor for request (ignored).
339 * @param[in] param Extra parameter from user (ignored).
342 stats_commands(struct Client* to, const struct StatDesc* sd, char* param)
344 struct Message *mptr;
346 for (mptr = msgtab; mptr->cmd; mptr++)
348 send_reply(to, RPL_STATSCOMMANDS, mptr->cmd, mptr->count, mptr->bytes);
351 /** List channel quarantines.
352 * @param[in] to Client requesting statistics.
353 * @param[in] sd Stats descriptor for request (ignored).
354 * @param[in] param Filter for quarantined channel names.
357 stats_quarantine(struct Client* to, const struct StatDesc* sd, char* param)
361 for (qline = GlobalQuarantineList; qline; qline = qline->next)
363 if (param && match(param, qline->chname)) /* narrow search */
365 send_reply(to, RPL_STATSQLINE, qline->chname, qline->reason);
369 /** List service pseudo-command mappings.
370 * @param[in] to Client requesting statistics.
371 * @param[in] sd Stats descriptor for request (ignored).
372 * @param[in] param Extra parameter from user (ignored).
375 stats_mapping(struct Client *to, const struct StatDesc* sd, char* param)
379 send_reply(to, RPL_STATSRLINE, "Command", "Name", "Prepend", "Target");
380 for (map = GlobalServiceMapList; map; map = map->next) {
381 struct nick_host *nh;
382 for (nh = map->services; nh; nh = nh->next) {
383 send_reply(to, RPL_STATSRLINE, map->command, map->name,
384 (map->prepend ? map->prepend : "*"), nh->nick);
389 /** Report server uptime and maximum connection/client counts.
390 * @param[in] to Client requesting statistics.
391 * @param[in] sd Stats descriptor for request (ignored).
392 * @param[in] param Extra parameter from user (ignored).
395 stats_uptime(struct Client* to, const struct StatDesc* sd, char* param)
399 nowr = CurrentTime - cli_since(&me);
400 send_reply(to, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
401 (nowr / 60) % 60, nowr % 60);
402 send_reply(to, RPL_STATSCONN, max_connection_count, max_client_count);
405 /** Verbosely report on servers connected to the network.
406 * If sd->sd_funcdata != 0, then display in a more human-friendly format.
407 * @param[in] sptr Client requesting statistics.
408 * @param[in] sd Stats descriptor for request.
409 * @param[in] param Filter for server names to display.
412 stats_servers_verbose(struct Client* sptr, const struct StatDesc* sd,
415 struct Client *acptr;
419 * lowercase 'v' is for human-readable,
420 * uppercase 'V' is for machine-readable
422 if (sd->sd_funcdata) {
423 send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE,
424 "%-20s %-20s Flags Hops Numeric Lag RTT Up Down "
425 "Clients/Max Proto %-10s :Info", "Servername", "Uplink",
427 fmt = "%-20s %-20s %c%c%c%c %4i %s %-4i %5i %4i %4i %4i %5i %5i P%-2i %Tu :%s";
429 fmt = "%s %s %c%c%c%c %i %s %i %i %i %i %i %i %i P%i %Tu :%s";
432 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
434 if (!IsServer(acptr) && !IsMe(acptr))
437 if (param && match(param, cli_name(acptr)))
439 send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE, fmt,
441 cli_name(cli_serv(acptr)->up),
442 IsBurst(acptr) ? 'B' : '-',
443 IsBurstAck(acptr) ? 'A' : '-',
444 IsHub(acptr) ? 'H' : '-',
445 IsService(acptr) ? 'S' : '-',
448 base64toint(cli_yxx(acptr)),
449 cli_serv(acptr)->lag,
450 cli_serv(acptr)->asll_rtt,
451 cli_serv(acptr)->asll_to,
452 cli_serv(acptr)->asll_from,
453 cli_serv(acptr)->clients,
454 cli_serv(acptr)->nn_mask,
455 cli_serv(acptr)->prot,
456 cli_serv(acptr)->timestamp,
462 /** Display objects allocated (and total memory used by them) for
463 * several types of structures.
464 * @param[in] to Client requesting statistics.
465 * @param[in] sd Stats descriptor for request (ignored).
466 * @param[in] param Extra parameter from user (ignored).
469 stats_meminfo(struct Client* to, const struct StatDesc* sd, char* param)
471 class_send_meminfo(to);
472 send_listinfo(to, 0);
476 /** Send a list of available statistics.
477 * @param[in] to Client requesting statistics.
478 * @param[in] sd Stats descriptor for request.
479 * @param[in] param Extra parameter from user (ignored).
482 stats_help(struct Client* to, const struct StatDesc* sd, char* param)
484 struct StatDesc *asd;
486 /* only if it's my user */
488 for (asd = statsinfo; asd->sd_name; asd++)
489 if (asd != sd) /* don't send the help for us */
490 sendcmdto_one(&me, CMD_NOTICE, to, "%C :%c (%s) - %s", to, asd->sd_c,
491 asd->sd_name, asd->sd_desc);
494 /** Contains information about all statistics. */
495 struct StatDesc statsinfo[] = {
496 { 'a', "nameservers", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_a,
497 report_dns_servers, 0,
499 { 'c', "connect", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_c,
500 stats_configured_links, CONF_SERVER,
501 "Remote server connection lines." },
502 { 'd', "maskrules", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_d,
503 stats_crule_list, CRULE_MASK,
504 "Dynamic routing configuration." },
505 { 'D', "crules", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_d,
506 stats_crule_list, CRULE_ALL,
507 "Dynamic routing configuration." },
508 { 'e', "engine", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_e,
510 "Report server event loop engine." },
511 { 'f', "features", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_f,
513 "Feature settings." },
514 { 'g', "glines", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_g,
516 "Global bans (G-lines)." },
517 { 'i', "access", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_i,
518 stats_access, CONF_CLIENT,
519 "Connection authorization lines." },
520 { 'j', "histogram", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_j,
522 "Message length histogram." },
523 { 'k', "klines", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_k,
525 "Local bans (K-Lines)." },
526 { 'l', "links", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS),
529 "Current connections information." },
530 { 'L', "modules", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS),
533 "Dynamically loaded modules." },
534 { 'm', "commands", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_m,
536 "Message usage information." },
537 { 'o', "operators", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_o,
538 stats_configured_links, CONF_OPERATOR,
539 "Operator information." },
540 { 'p', "ports", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_p,
542 "Listening ports." },
543 { 'q', "quarantines", (STAT_FLAG_OPERONLY | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_q,
545 "Quarantined channels list." },
546 { 'R', "mappings", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_R,
548 "Service mappings." },
550 { 'r', "usage", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_r,
552 "System resource usage (Debug only)." },
554 { 'T', "motds", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_T,
556 "Configured Message Of The Day files." },
557 { 't', "locals", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_t,
559 "Local connection statistics (Total SND/RCV, etc)." },
560 { 'U', "uworld", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_U,
561 stats_configured_links, CONF_UWORLD,
562 "Service server & nick jupes information." },
563 { 'u', "uptime", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_u,
565 "Current uptime & highest connection count." },
566 { 'v', "vservers", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS), FEAT_HIS_STATS_v,
567 stats_servers_verbose, 1,
568 "Verbose server information." },
569 { 'V', "vserversmach", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS), FEAT_HIS_STATS_v,
570 stats_servers_verbose, 0,
571 "Verbose server information." },
572 { 'w', "userload", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_w,
574 "Userload statistics." },
576 { 'x', "memusage", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_x,
578 "List usage information (Debug only)." },
580 { 'y', "classes", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_y,
582 "Connection classes." },
583 { 'z', "memory", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_z,
585 "Memory/Structure allocation information." },
586 { '*', "help", (STAT_FLAG_CASESENS | STAT_FLAG_VARPARAM), FEAT_LAST_F,
588 "Send help for stats." },
589 { '\0', 0, FEAT_LAST_F, 0, 0, 0 }
592 /** Maps from characters to statistics descriptors.
593 * Statistics descriptors with no single-character alias are not included.
595 static struct StatDesc *statsmap[256];
596 /** Number of statistics descriptors. */
597 static int statscount;
599 /** Compare two StatDesc structures by long name (StatDesc::sd_name).
600 * @param[in] a_ Pointer to a StatDesc.
601 * @param[in] b_ Pointer to a StatDesc.
602 * @return Less than, equal to, or greater than zero if \a a_ is
603 * lexicographically less than, equal to, or greater than \a b_.
606 stats_cmp(const void *a_, const void *b_)
608 const struct StatDesc *a = a_;
609 const struct StatDesc *b = b_;
610 return ircd_strcmp(a->sd_name, b->sd_name);
613 /** Compare a StatDesc's name against a string.
614 * @param[in] key Pointer to a null-terminated string.
615 * @param[in] sd_ Pointer to a StatDesc.
616 * @return Less than,e qual to, or greater than zero if \a key is
617 * lexicographically less than, equal to, or greater than \a
621 stats_search(const void *key, const void *sd_)
623 const struct StatDesc *sd = sd_;
624 return ircd_strcmp(key, sd->sd_name);
627 /** Look up a stats handler. If name_or_char is just one character
628 * long, use that as a character index; otherwise, look it up by name
630 * @param[in] name_or_char Null-terminated string to look up.
631 * @return The statistics descriptor for \a name_or_char (NULL if none).
633 const struct StatDesc *
634 stats_find(const char *name_or_char)
636 if (!name_or_char[1])
637 return statsmap[(int)name_or_char[0]];
639 return bsearch(name_or_char, statsinfo, statscount, sizeof(statsinfo[0]), stats_search);
642 /** Build statsmap from the statsinfo array. */
649 /* Make darn sure the statsmap array is initialized to all zeros */
650 for (i = 0; i < 256; i++)
653 /* Count number of stats entries and sort them. */
654 for (statscount = 0, sd = statsinfo; sd->sd_name; sd++, statscount++) {}
655 qsort(statsinfo, statscount, sizeof(statsinfo[0]), stats_cmp);
657 /* Build the mapping */
658 for (sd = statsinfo; sd->sd_name; sd++)
662 else if (sd->sd_flags & STAT_FLAG_CASESENS)
663 /* case sensitive character... */
664 statsmap[(int)sd->sd_c] = sd;
667 /* case insensitive--make sure to put in two entries */
668 statsmap[(int)ToLower((int)sd->sd_c)] = sd;
669 statsmap[(int)ToUpper((int)sd->sd_c)] = sd;