| 1 |
/* |
| 2 |
* Copyright (c) 2002 Erik Fears |
| 3 |
* Copyright (c) 2014-2015 ircd-hybrid development team |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 |
* USA |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "setup.h" |
| 22 |
|
| 23 |
#include <stdio.h> |
| 24 |
#include <unistd.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <string.h> |
| 27 |
#include <sys/time.h> |
| 28 |
#include <time.h> |
| 29 |
#include <sys/types.h> |
| 30 |
#include <sys/socket.h> |
| 31 |
#include <netinet/in.h> |
| 32 |
#include <arpa/inet.h> |
| 33 |
|
| 34 |
#include <sys/resource.h> /* getrlimit */ |
| 35 |
#include <errno.h> |
| 36 |
|
| 37 |
#include "irc.h" |
| 38 |
#include "log.h" |
| 39 |
#include "misc.h" |
| 40 |
#include "config.h" |
| 41 |
#include "stats.h" |
| 42 |
#include "libopm/src/opm_types.h" |
| 43 |
|
| 44 |
static time_t STATS_UPTIME; |
| 45 |
static unsigned int STATS_CONNECTIONS; |
| 46 |
static unsigned int STATS_DNSBLSENT; |
| 47 |
|
| 48 |
static struct StatsHash STATS_PROXIES[] = |
| 49 |
{ |
| 50 |
{ OPM_TYPE_HTTP, 0, "HTTP" }, |
| 51 |
{ OPM_TYPE_HTTPPOST, 0, "HTTPPOST" }, |
| 52 |
{ OPM_TYPE_SOCKS4, 0, "SOCKS4" }, |
| 53 |
{ OPM_TYPE_SOCKS5, 0, "SOCKS5" }, |
| 54 |
{ OPM_TYPE_ROUTER, 0, "ROUTER" }, |
| 55 |
{ OPM_TYPE_WINGATE, 0, "WINGATE" } |
| 56 |
}; |
| 57 |
|
| 58 |
|
| 59 |
/* stats_init |
| 60 |
* |
| 61 |
* Perform initialization of bopm stats |
| 62 |
* |
| 63 |
* Parameters: NONE |
| 64 |
* Return: NONE |
| 65 |
* |
| 66 |
*/ |
| 67 |
void |
| 68 |
stats_init(void) |
| 69 |
{ |
| 70 |
time(&STATS_UPTIME); |
| 71 |
} |
| 72 |
|
| 73 |
/* stats_openproxy |
| 74 |
* |
| 75 |
* Record open proxy. |
| 76 |
* |
| 77 |
* |
| 78 |
* Parameters: NONE |
| 79 |
* Return: NONE |
| 80 |
* |
| 81 |
*/ |
| 82 |
void |
| 83 |
stats_openproxy(int type) |
| 84 |
{ |
| 85 |
for (unsigned int i = 0; i < (sizeof(STATS_PROXIES) / sizeof(struct StatsHash)); ++i) |
| 86 |
if (STATS_PROXIES[i].type == type) |
| 87 |
++STATS_PROXIES[i].count; |
| 88 |
} |
| 89 |
|
| 90 |
/* stats_connect |
| 91 |
* |
| 92 |
* Record IRC connect. |
| 93 |
* |
| 94 |
* |
| 95 |
* Parameters: NONE |
| 96 |
* Return: NONE |
| 97 |
* |
| 98 |
*/ |
| 99 |
void |
| 100 |
stats_connect(void) |
| 101 |
{ |
| 102 |
++STATS_CONNECTIONS; |
| 103 |
} |
| 104 |
|
| 105 |
/* stats_dnsblrecv |
| 106 |
* |
| 107 |
* Record that a user was found in the blacklist. |
| 108 |
* |
| 109 |
* Parameters: BlacklistConf structure |
| 110 |
* Return: NONE |
| 111 |
* |
| 112 |
*/ |
| 113 |
void |
| 114 |
stats_dnsblrecv(struct BlacklistConf *bl) |
| 115 |
{ |
| 116 |
++bl->stats_recv; |
| 117 |
} |
| 118 |
|
| 119 |
/* stats_dnsblsend |
| 120 |
* |
| 121 |
* Record a sent report |
| 122 |
* |
| 123 |
* Parameters: NONE |
| 124 |
* Return: NONE |
| 125 |
* |
| 126 |
*/ |
| 127 |
void |
| 128 |
stats_dnsblsend(void) |
| 129 |
{ |
| 130 |
++STATS_DNSBLSENT; |
| 131 |
} |
| 132 |
|
| 133 |
/* stats_output |
| 134 |
* |
| 135 |
* Output stats to target via privmsg |
| 136 |
* |
| 137 |
* |
| 138 |
* Parameters: NONE |
| 139 |
* Return: NONE |
| 140 |
* |
| 141 |
*/ |
| 142 |
void |
| 143 |
stats_output(const char *target) |
| 144 |
{ |
| 145 |
time_t present; |
| 146 |
time_t uptime; |
| 147 |
node_t *p; |
| 148 |
|
| 149 |
time(&present); |
| 150 |
uptime = present - STATS_UPTIME; |
| 151 |
|
| 152 |
irc_send("PRIVMSG %s :Uptime: %s", target, dissect_time(uptime)); |
| 153 |
|
| 154 |
LIST_FOREACH(p, OpmItem->blacklists->head) |
| 155 |
{ |
| 156 |
const struct BlacklistConf *bl = p->data; |
| 157 |
|
| 158 |
if (bl->stats_recv > 0) |
| 159 |
irc_send("PRIVMSG %s :DNSBL: %u successful lookups from %s", |
| 160 |
target, bl->stats_recv, bl->name); |
| 161 |
} |
| 162 |
|
| 163 |
if (STATS_DNSBLSENT > 0) |
| 164 |
irc_send("PRIVMSG %s :DNSBL: %u reports sent", target, |
| 165 |
STATS_DNSBLSENT); |
| 166 |
|
| 167 |
for (unsigned int i = 0; i < (sizeof(STATS_PROXIES) / sizeof(struct StatsHash)); ++i) |
| 168 |
if (STATS_PROXIES[i].count > 0) |
| 169 |
irc_send("PRIVMSG %s :Found %u (%s) open.", target, |
| 170 |
STATS_PROXIES[i].count, STATS_PROXIES[i].name); |
| 171 |
|
| 172 |
irc_send("PRIVMSG %s :Number of connects: %u (%.2f/minute)", |
| 173 |
target, STATS_CONNECTIONS, STATS_CONNECTIONS ? |
| 174 |
(float)STATS_CONNECTIONS / ((float)uptime / 60.0) : 0.0); |
| 175 |
} |
| 176 |
|
| 177 |
/* fdstats_output |
| 178 |
* |
| 179 |
* Output file descriptor stats to target via privmsg |
| 180 |
* |
| 181 |
* |
| 182 |
* Parameters: NONE |
| 183 |
* Return: NONE |
| 184 |
* |
| 185 |
*/ |
| 186 |
void |
| 187 |
fdstats_output(const char *target) |
| 188 |
{ |
| 189 |
struct rlimit rlim; |
| 190 |
unsigned int total_fd_use = 0; |
| 191 |
|
| 192 |
/* Get file descriptor ceiling */ |
| 193 |
if (getrlimit(RLIMIT_NOFILE, &rlim) == -1) |
| 194 |
{ |
| 195 |
log_printf("FDSTAT -> getrlimit() error retrieving RLIMIT_NOFILE (%s)", strerror(errno)); |
| 196 |
irc_send("PRIVMSG %s :FDSTAT -> getrlimit() error retrieving RLIMIT_NOFILE (%s)", |
| 197 |
target, strerror(errno)); |
| 198 |
return; |
| 199 |
} |
| 200 |
|
| 201 |
/* |
| 202 |
* Check which file descriptors are active, based on suggestions from: |
| 203 |
* http://groups.google.com/groups?th=a48b9fe8ca43947c&rnum=1 |
| 204 |
*/ |
| 205 |
for (unsigned int i = 0; i < rlim.rlim_cur; ++i) |
| 206 |
{ |
| 207 |
int newfd = dup(i); |
| 208 |
|
| 209 |
if (newfd > 0) |
| 210 |
{ |
| 211 |
++total_fd_use; |
| 212 |
close(newfd); |
| 213 |
} |
| 214 |
else |
| 215 |
{ |
| 216 |
switch (errno) |
| 217 |
{ |
| 218 |
case EMFILE: |
| 219 |
/* |
| 220 |
* We ran out of FDs whilst trying to dup an existing one, |
| 221 |
* so all fds are open and we can stop checking here. |
| 222 |
*/ |
| 223 |
i = total_fd_use = rlim.rlim_cur; |
| 224 |
break; |
| 225 |
|
| 226 |
case EBADF: |
| 227 |
/* Not an FD in use. */ |
| 228 |
break; |
| 229 |
|
| 230 |
case EINTR: |
| 231 |
/* Try again. */ |
| 232 |
--i; |
| 233 |
break; |
| 234 |
|
| 235 |
default: |
| 236 |
/* We don't expect any other errors. */ |
| 237 |
log_printf("fd %u errno = %u (%s)", i, errno, strerror(errno)); |
| 238 |
break; |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
irc_send("PRIVMSG %s :Total open FD: %u/%d", target, total_fd_use, rlim.rlim_cur); |
| 244 |
} |