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;
103 if (tmp->status & CONF_UWORLD)
104 send_reply(sptr, RPL_STATSULINE, host);
105 else if (tmp->status & CONF_SERVER)
106 send_reply(sptr, RPL_STATSCLINE, name, (host[0] == ':' ? "0" : ""), host, port, maximum, hub_limit, get_conf_class(tmp));
107 else if (tmp->status & CONF_CLIENT)
108 send_reply(sptr, RPL_STATSILINE, host, maximum, (name[0] == ':' ? "0" : ""), name, port, get_conf_class(tmp));
109 else if (tmp->status & CONF_OPERATOR)
110 send_reply(sptr, RPL_STATSOLINE, username, host, name, get_conf_class(tmp));
115 /** Report connection rules from conf_get_crule_list().
116 * Uses sd->sd_funcdata as a filter for CRuleConf::type.
117 * @param[in] to Client requesting statistics.
118 * @param[in] sd Stats descriptor for request.
119 * @param[in] param Extra parameter from user (ignored).
122 stats_crule_list(struct Client* to, const struct StatDesc *sd,
125 const struct CRuleConf* p = conf_get_crule_list();
127 for ( ; p; p = p->next)
129 if (p->type & sd->sd_funcdata)
130 send_reply(to, RPL_STATSDLINE, sd->sd_c, p->hostmask, p->rule);
134 /** Report active event engine name.
135 * @param[in] to Client requesting statistics.
136 * @param[in] sd Stats descriptor for request (ignored).
137 * @param[in] param Extra parameter from user (ignored).
140 stats_engine(struct Client *to, const struct StatDesc *sd, char *param)
142 send_reply(to, RPL_STATSENGINE, engine_name());
145 /** Report client access lists.
146 * @param[in] to Client requesting statistics.
147 * @param[in] sd Stats descriptor for request.
148 * @param[in] param Filter for hostname or IP (NULL to show all).
151 stats_access(struct Client *to, const struct StatDesc *sd, char *param)
153 struct ConfItem *aconf;
159 stats_configured_links(to, sd, param);
163 wilds = string_has_wildcards(param);
165 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
167 if (aconf->status != CONF_CLIENT)
169 if ((!wilds && (!match(aconf->host, param) ||
170 !match(aconf->name, param))) ||
171 (wilds && (!mmatch(param, aconf->host) ||
172 !mmatch(param, aconf->name))))
174 send_reply(to, RPL_STATSILINE, 'I', aconf->host, aconf->name,
175 aconf->address.port, get_conf_class(aconf));
183 /** Report DenyConf entries.
184 * @param[in] to Client requesting list.
187 report_deny_list(struct Client* to)
189 const struct DenyConf* p = conf_get_deny_list();
190 for ( ; p; p = p->next)
191 send_reply(to, RPL_STATSKLINE, (p->flags & DENY_FLAGS_IP) ? 'k' : 'K',
192 p->hostmask, p->message, p->usermask);
195 /** Report K/k-lines to a user.
196 * @param[in] sptr Client requesting statistics.
197 * @param[in] sd Stats descriptor for request (ignored).
198 * @param[in] mask Filter for hostmasks to show.
201 stats_klines(struct Client *sptr, const struct StatDesc *sd, char *mask)
208 const struct DenyConf* conf;
216 need_more_params(sptr, "STATS K");
218 report_deny_list(sptr);
224 wilds = string_has_wildcards(mask);
227 if ((host = strchr(mask, '@')))
235 for (conf = conf_get_deny_list(); conf; conf = conf->next)
237 if ((!wilds && ((user || conf->hostmask) &&
238 !match(conf->hostmask, host) &&
239 (!user || !match(conf->usermask, user)))) ||
240 (wilds && !mmatch(host, conf->hostmask) &&
241 (!user || !mmatch(user, conf->usermask))))
243 send_reply(sptr, RPL_STATSKLINE,
244 (conf->flags & DENY_FLAGS_IP) ? 'k' : 'K',
245 conf->hostmask, conf->message, conf->usermask);
252 /** Report on servers and/or clients connected to the network.
253 * @param[in] sptr Client requesting statistics.
254 * @param[in] sd Stats descriptor for request (ignored).
255 * @param[in] name Filter for client names to show.
258 stats_links(struct Client* sptr, const struct StatDesc* sd, char* name)
260 struct Client *acptr;
265 wilds = string_has_wildcards(name);
268 * Send info about connections which match, or all if the
269 * mask matches me.name. Only restrictions are on those who
270 * are invisible not being visible to 'foreigners' who use
271 * a wild card based search to list it.
273 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
274 "SendM SendKBytes RcveM RcveKBytes :Open since");
275 for (i = 0; i <= HighestFd; i++)
277 if (!(acptr = LocalClientArray[i]))
279 /* Don't return clients when this is a request for `all' */
280 if (!name && IsUser(acptr))
282 /* Don't show invisible people to non opers unless they know the nick */
283 if (IsInvisible(acptr) && (!name || wilds) && !IsAnOper(acptr) &&
286 /* Only show the ones that match the given mask - if any */
287 if (name && wilds && match(name, cli_name(acptr)))
289 /* Skip all that do not match the specific query */
290 if (!(!name || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
292 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
293 "%s %u %u %Lu %u %Lu :%Tu",
294 (*(cli_name(acptr))) ? cli_name(acptr) : "<unregistered>",
295 (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
296 (cli_sendB(acptr) >> 10), (int)cli_receiveM(acptr),
297 (cli_receiveB(acptr) >> 10), CurrentTime - cli_firsttime(acptr));
301 /** Report on loaded modules.
302 * @param[in] to Client requesting statistics.
303 * @param[in] sd Stats descriptor for request (ignored).
304 * @param[in] param Extra parameter from user (ignored).
307 stats_modules(struct Client* to, const struct StatDesc* sd, char* param)
309 crypt_mechs_t* mechs;
311 send_reply(to, SND_EXPLICIT | RPL_STATSLLINE,
312 "Module Description Entry Point");
314 /* atm the only "modules" we have are the crypto mechanisms,
315 eventualy they'll be part of a global dl module list, for now
316 i'll just output data about them -- hikari */
318 if(crypt_mechs_root == NULL)
321 mechs = crypt_mechs_root->next;
328 send_reply(to, SND_EXPLICIT | RPL_STATSLLINE,
330 mechs->mech->shortname, mechs->mech->description,
331 mechs->mech->crypt_function);
338 /** Report how many times each command has been used.
339 * @param[in] to Client requesting statistics.
340 * @param[in] sd Stats descriptor for request (ignored).
341 * @param[in] param Extra parameter from user (ignored).
344 stats_commands(struct Client* to, const struct StatDesc* sd, char* param)
346 struct Message *mptr;
348 for (mptr = msgtab; mptr->cmd; mptr++)
350 send_reply(to, RPL_STATSCOMMANDS, mptr->cmd, mptr->count, mptr->bytes);
353 /** List channel quarantines.
354 * @param[in] to Client requesting statistics.
355 * @param[in] sd Stats descriptor for request (ignored).
356 * @param[in] param Filter for quarantined channel names.
359 stats_quarantine(struct Client* to, const struct StatDesc* sd, char* param)
363 for (qline = GlobalQuarantineList; qline; qline = qline->next)
365 if (param && match(param, qline->chname)) /* narrow search */
367 send_reply(to, RPL_STATSQLINE, qline->chname, qline->reason);
371 /** List service pseudo-command mappings.
372 * @param[in] to Client requesting statistics.
373 * @param[in] sd Stats descriptor for request (ignored).
374 * @param[in] param Extra parameter from user (ignored).
377 stats_mapping(struct Client *to, const struct StatDesc* sd, char* param)
381 send_reply(to, RPL_STATSRLINE, "Command", "Name", "Prepend", "Target");
382 for (map = GlobalServiceMapList; map; map = map->next) {
383 struct nick_host *nh;
384 for (nh = map->services; nh; nh = nh->next) {
385 send_reply(to, RPL_STATSRLINE, map->command, map->name,
386 (map->prepend ? map->prepend : "*"), nh->nick);
391 /** Report server uptime and maximum connection/client counts.
392 * @param[in] to Client requesting statistics.
393 * @param[in] sd Stats descriptor for request (ignored).
394 * @param[in] param Extra parameter from user (ignored).
397 stats_uptime(struct Client* to, const struct StatDesc* sd, char* param)
401 nowr = CurrentTime - cli_since(&me);
402 send_reply(to, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
403 (nowr / 60) % 60, nowr % 60);
404 send_reply(to, RPL_STATSCONN, max_connection_count, max_client_count);
407 /** Verbosely report on servers connected to the network.
408 * If sd->sd_funcdata != 0, then display in a more human-friendly format.
409 * @param[in] sptr Client requesting statistics.
410 * @param[in] sd Stats descriptor for request.
411 * @param[in] param Filter for server names to display.
414 stats_servers_verbose(struct Client* sptr, const struct StatDesc* sd,
417 struct Client *acptr;
421 * lowercase 'v' is for human-readable,
422 * uppercase 'V' is for machine-readable
424 if (sd->sd_funcdata) {
425 send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE,
426 "%-20s %-20s Flags Hops Numeric Lag RTT Up Down "
427 "Clients/Max Proto %-10s :Info", "Servername", "Uplink",
429 fmt = "%-20s %-20s %c%c%c%c %4i %s %-4i %5i %4i %4i %4i %5i %5i P%-2i %Tu :%s";
431 fmt = "%s %s %c%c%c%c %i %s %i %i %i %i %i %i %i P%i %Tu :%s";
434 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
436 if (!IsServer(acptr) && !IsMe(acptr))
439 if (param && match(param, cli_name(acptr)))
441 send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE, fmt,
443 cli_name(cli_serv(acptr)->up),
444 IsBurst(acptr) ? 'B' : '-',
445 IsBurstAck(acptr) ? 'A' : '-',
446 IsHub(acptr) ? 'H' : '-',
447 IsService(acptr) ? 'S' : '-',
450 base64toint(cli_yxx(acptr)),
451 cli_serv(acptr)->lag,
452 cli_serv(acptr)->asll_rtt,
453 cli_serv(acptr)->asll_to,
454 cli_serv(acptr)->asll_from,
455 cli_serv(acptr)->clients,
456 cli_serv(acptr)->nn_mask,
457 cli_serv(acptr)->prot,
458 cli_serv(acptr)->timestamp,
464 /** Display objects allocated (and total memory used by them) for
465 * several types of structures.
466 * @param[in] to Client requesting statistics.
467 * @param[in] sd Stats descriptor for request (ignored).
468 * @param[in] param Extra parameter from user (ignored).
471 stats_meminfo(struct Client* to, const struct StatDesc* sd, char* param)
473 class_send_meminfo(to);
474 send_listinfo(to, 0);
478 /** Send a list of available statistics.
479 * @param[in] to Client requesting statistics.
480 * @param[in] sd Stats descriptor for request.
481 * @param[in] param Extra parameter from user (ignored).
484 stats_help(struct Client* to, const struct StatDesc* sd, char* param)
486 struct StatDesc *asd;
488 /* only if it's my user */
490 for (asd = statsinfo; asd->sd_name; asd++)
491 if (asd != sd) /* don't send the help for us */
492 sendcmdto_one(&me, CMD_NOTICE, to, "%C :%c (%s) - %s", to, asd->sd_c,
493 asd->sd_name, asd->sd_desc);
496 /** Contains information about all statistics. */
497 struct StatDesc statsinfo[] = {
498 { 'a', "nameservers", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_a,
499 report_dns_servers, 0,
501 { 'c', "connect", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_c,
502 stats_configured_links, CONF_SERVER,
503 "Remote server connection lines." },
504 { 'd', "maskrules", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_d,
505 stats_crule_list, CRULE_MASK,
506 "Dynamic routing configuration." },
507 { 'D', "crules", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_d,
508 stats_crule_list, CRULE_ALL,
509 "Dynamic routing configuration." },
510 { 'e', "engine", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_e,
512 "Report server event loop engine." },
513 { 'f', "features", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_f,
515 "Feature settings." },
516 { 'g', "glines", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_g,
518 "Global bans (G-lines)." },
519 { 'i', "access", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_i,
520 stats_access, CONF_CLIENT,
521 "Connection authorization lines." },
522 { 'j', "histogram", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_j,
524 "Message length histogram." },
525 { 'k', "klines", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_k,
527 "Local bans (K-Lines)." },
528 { 'l', "links", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS),
531 "Current connections information." },
532 { 'L', "modules", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS),
535 "Dynamically loaded modules." },
536 { 'm', "commands", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_m,
538 "Message usage information." },
539 { 'o', "operators", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_o,
540 stats_configured_links, CONF_OPERATOR,
541 "Operator information." },
542 { 'p', "ports", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_p,
544 "Listening ports." },
545 { 'q', "quarantines", (STAT_FLAG_OPERONLY | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_q,
547 "Quarantined channels list." },
548 { 'R', "mappings", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_R,
550 "Service mappings." },
552 { 'r', "usage", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_r,
554 "System resource usage (Debug only)." },
556 { 'T', "motds", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_T,
558 "Configured Message Of The Day files." },
559 { 't', "locals", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_t,
561 "Local connection statistics (Total SND/RCV, etc)." },
562 { 'U', "uworld", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_U,
563 stats_configured_links, CONF_UWORLD,
564 "Service server & nick jupes information." },
565 { 'u', "uptime", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_u,
567 "Current uptime & highest connection count." },
568 { 'v', "vservers", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS), FEAT_HIS_STATS_v,
569 stats_servers_verbose, 1,
570 "Verbose server information." },
571 { 'V', "vserversmach", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS), FEAT_HIS_STATS_v,
572 stats_servers_verbose, 0,
573 "Verbose server information." },
574 { 'w', "userload", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_w,
576 "Userload statistics." },
578 { 'x', "memusage", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_x,
580 "List usage information (Debug only)." },
582 { 'y', "classes", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_y,
584 "Connection classes." },
585 { 'z', "memory", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_z,
587 "Memory/Structure allocation information." },
588 { '*', "help", (STAT_FLAG_CASESENS | STAT_FLAG_VARPARAM), FEAT_LAST_F,
590 "Send help for stats." },
591 { '\0', 0, FEAT_LAST_F, 0, 0, 0 }
594 /** Maps from characters to statistics descriptors.
595 * Statistics descriptors with no single-character alias are not included.
597 static struct StatDesc *statsmap[256];
598 /** Number of statistics descriptors. */
599 static int statscount;
601 /** Compare two StatDesc structures by long name (StatDesc::sd_name).
602 * @param[in] a_ Pointer to a StatDesc.
603 * @param[in] b_ Pointer to a StatDesc.
604 * @return Less than, equal to, or greater than zero if \a a_ is
605 * lexicographically less than, equal to, or greater than \a b_.
608 stats_cmp(const void *a_, const void *b_)
610 const struct StatDesc *a = a_;
611 const struct StatDesc *b = b_;
612 return ircd_strcmp(a->sd_name, b->sd_name);
615 /** Compare a StatDesc's name against a string.
616 * @param[in] key Pointer to a null-terminated string.
617 * @param[in] sd_ Pointer to a StatDesc.
618 * @return Less than,e qual to, or greater than zero if \a key is
619 * lexicographically less than, equal to, or greater than \a
623 stats_search(const void *key, const void *sd_)
625 const struct StatDesc *sd = sd_;
626 return ircd_strcmp(key, sd->sd_name);
629 /** Look up a stats handler. If name_or_char is just one character
630 * long, use that as a character index; otherwise, look it up by name
632 * @param[in] name_or_char Null-terminated string to look up.
633 * @return The statistics descriptor for \a name_or_char (NULL if none).
635 const struct StatDesc *
636 stats_find(const char *name_or_char)
638 if (!name_or_char[1])
639 return statsmap[(int)name_or_char[0]];
641 return bsearch(name_or_char, statsinfo, statscount, sizeof(statsinfo[0]), stats_search);
644 /** Build statsmap from the statsinfo array. */
651 /* Make darn sure the statsmap array is initialized to all zeros */
652 for (i = 0; i < 256; i++)
655 /* Count number of stats entries and sort them. */
656 for (statscount = 0, sd = statsinfo; sd->sd_name; sd++, statscount++) {}
657 qsort(statsinfo, statscount, sizeof(statsinfo[0]), stats_cmp);
659 /* Build the mapping */
660 for (sd = statsinfo; sd->sd_name; sd++)
664 else if (sd->sd_flags & STAT_FLAG_CASESENS)
665 /* case sensitive character... */
666 statsmap[(int)sd->sd_c] = sd;
669 /* case insensitive--make sure to put in two entries */
670 statsmap[(int)ToLower((int)sd->sd_c)] = sd;
671 statsmap[(int)ToUpper((int)sd->sd_c)] = sd;