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