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