1 |
/* |
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 |
{ |
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 |
|
84 |
|
85 |
/* stats_init |
86 |
* |
87 |
* Perform initialization of bopm stats |
88 |
* |
89 |
* Parameters: NONE |
90 |
* Return: NONE |
91 |
* |
92 |
*/ |
93 |
void |
94 |
stats_init(void) |
95 |
{ |
96 |
time(&STATS_UPTIME); |
97 |
} |
98 |
|
99 |
/* stats_openproxy |
100 |
* |
101 |
* Record open proxy. |
102 |
* |
103 |
* |
104 |
* Parameters: NONE |
105 |
* Return: NONE |
106 |
* |
107 |
*/ |
108 |
void |
109 |
stats_openproxy(int type) |
110 |
{ |
111 |
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 |
} |
115 |
|
116 |
/* stats_connect |
117 |
* |
118 |
* Record IRC connect. |
119 |
* |
120 |
* |
121 |
* Parameters: NONE |
122 |
* Return: NONE |
123 |
* |
124 |
*/ |
125 |
void |
126 |
stats_connect(void) |
127 |
{ |
128 |
++STATS_CONNECTIONS; |
129 |
} |
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 |
void |
140 |
stats_dnsblrecv(struct BlacklistConf *bl) |
141 |
{ |
142 |
++bl->stats_recv; |
143 |
} |
144 |
|
145 |
/* stats_dnsblsend |
146 |
* |
147 |
* Record a sent report |
148 |
* |
149 |
* Parameters: NONE |
150 |
* Return: NONE |
151 |
* |
152 |
*/ |
153 |
void |
154 |
stats_dnsblsend(void) |
155 |
{ |
156 |
++STATS_DNSBLSENT; |
157 |
} |
158 |
|
159 |
/* stats_output |
160 |
* |
161 |
* Output stats to target via privmsg |
162 |
* |
163 |
* |
164 |
* Parameters: NONE |
165 |
* Return: NONE |
166 |
* |
167 |
*/ |
168 |
void |
169 |
stats_output(char *target) |
170 |
{ |
171 |
time_t present; |
172 |
time_t uptime; |
173 |
node_t *p; |
174 |
|
175 |
time(&present); |
176 |
uptime = present - STATS_UPTIME; |
177 |
|
178 |
irc_send("PRIVMSG %s :Uptime: %s", target, dissect_time(uptime)); |
179 |
|
180 |
LIST_FOREACH(p, OpmItem->blacklists->head) |
181 |
{ |
182 |
struct BlacklistConf *bl = p->data; |
183 |
|
184 |
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 |
|
189 |
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 |
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 |
void |
213 |
fdstats_output(char *target) |
214 |
{ |
215 |
struct rlimit rlim; |
216 |
unsigned int total_fd_use = 0; |
217 |
|
218 |
/* 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 |
|
227 |
/* |
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 |
|
235 |
if (newfd > 0) |
236 |
{ |
237 |
++total_fd_use; |
238 |
close(newfd); |
239 |
} |
240 |
else |
241 |
{ |
242 |
switch (errno) |
243 |
{ |
244 |
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 |
|
252 |
case EBADF: |
253 |
/* Not an FD in use. */ |
254 |
break; |
255 |
|
256 |
case EINTR: |
257 |
/* Try again. */ |
258 |
--i; |
259 |
break; |
260 |
|
261 |
default: |
262 |
/* We don't expect any other errors. */ |
263 |
log_printf("fd %u errno = %u (%s)", i, errno, strerror(errno)); |
264 |
break; |
265 |
} |
266 |
} |
267 |
} |
268 |
|
269 |
irc_send("PRIVMSG %s :Total open FD: %u/%d", target, total_fd_use, rlim.rlim_cur); |
270 |
} |