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