1 |
michael |
5052 |
/* |
2 |
michael |
5351 |
* 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 |
michael |
5052 |
|
21 |
|
|
#include "setup.h" |
22 |
|
|
|
23 |
|
|
#include <stdio.h> |
24 |
|
|
#include <unistd.h> |
25 |
michael |
5207 |
#include <stdlib.h> |
26 |
michael |
5209 |
#include <string.h> |
27 |
michael |
5266 |
#include <sys/time.h> |
28 |
|
|
#include <time.h> |
29 |
michael |
5052 |
#include <sys/types.h> |
30 |
|
|
#include <sys/resource.h> /* getrlimit */ |
31 |
|
|
#include <errno.h> |
32 |
|
|
|
33 |
|
|
#include "irc.h" |
34 |
|
|
#include "log.h" |
35 |
|
|
#include "misc.h" |
36 |
|
|
#include "config.h" |
37 |
|
|
#include "stats.h" |
38 |
|
|
#include "libopm/src/opm_types.h" |
39 |
|
|
|
40 |
|
|
static time_t STATS_UPTIME; |
41 |
|
|
static unsigned int STATS_CONNECTIONS; |
42 |
|
|
static unsigned int STATS_DNSBLSENT; |
43 |
|
|
|
44 |
|
|
static struct StatsHash STATS_PROXIES[] = |
45 |
michael |
5114 |
{ |
46 |
michael |
6222 |
{ OPM_TYPE_HTTP, 0, "HTTP" }, |
47 |
|
|
{ OPM_TYPE_HTTPPOST, 0, "HTTPPOST" }, |
48 |
|
|
{ OPM_TYPE_HTTPS, 0, "HTTPS" }, |
49 |
|
|
{ OPM_TYPE_HTTPSPOST, 0, "HTTPSPOST" }, |
50 |
|
|
{ OPM_TYPE_SOCKS4, 0, "SOCKS4" }, |
51 |
|
|
{ OPM_TYPE_SOCKS5, 0, "SOCKS5" }, |
52 |
|
|
{ OPM_TYPE_ROUTER, 0, "ROUTER" }, |
53 |
|
|
{ OPM_TYPE_WINGATE, 0, "WINGATE" }, |
54 |
|
|
{ OPM_TYPE_DREAMBOX, 0, "DREAMBOX" }, |
55 |
michael |
6024 |
{ 0, 0, NULL } |
56 |
michael |
5114 |
}; |
57 |
michael |
5052 |
|
58 |
|
|
|
59 |
|
|
/* stats_init |
60 |
|
|
* |
61 |
michael |
6166 |
* Perform initialization of HOPM stats |
62 |
michael |
5052 |
* |
63 |
|
|
* Parameters: NONE |
64 |
|
|
* Return: NONE |
65 |
michael |
5114 |
* |
66 |
michael |
5052 |
*/ |
67 |
michael |
5114 |
void |
68 |
|
|
stats_init(void) |
69 |
michael |
5052 |
{ |
70 |
michael |
5114 |
time(&STATS_UPTIME); |
71 |
michael |
5052 |
} |
72 |
|
|
|
73 |
|
|
/* stats_openproxy |
74 |
|
|
* |
75 |
|
|
* Record open proxy. |
76 |
|
|
* |
77 |
|
|
* |
78 |
|
|
* Parameters: NONE |
79 |
|
|
* Return: NONE |
80 |
michael |
5114 |
* |
81 |
michael |
5052 |
*/ |
82 |
michael |
5114 |
void |
83 |
michael |
6195 |
stats_openproxy(unsigned int type) |
84 |
michael |
5052 |
{ |
85 |
michael |
6024 |
for (struct StatsHash *tab = STATS_PROXIES; tab->name; ++tab) |
86 |
michael |
6021 |
{ |
87 |
michael |
6024 |
if (tab->type == type) |
88 |
michael |
6021 |
{ |
89 |
michael |
6024 |
++tab->count; |
90 |
michael |
6021 |
break; |
91 |
|
|
} |
92 |
|
|
} |
93 |
michael |
5052 |
} |
94 |
|
|
|
95 |
|
|
/* stats_connect |
96 |
|
|
* |
97 |
|
|
* Record IRC connect. |
98 |
|
|
* |
99 |
|
|
* |
100 |
|
|
* Parameters: NONE |
101 |
|
|
* Return: NONE |
102 |
michael |
5114 |
* |
103 |
michael |
5052 |
*/ |
104 |
michael |
5114 |
void |
105 |
|
|
stats_connect(void) |
106 |
michael |
5052 |
{ |
107 |
michael |
5114 |
++STATS_CONNECTIONS; |
108 |
michael |
5052 |
} |
109 |
|
|
|
110 |
|
|
/* stats_dnsblrecv |
111 |
|
|
* |
112 |
|
|
* Record that a user was found in the blacklist. |
113 |
|
|
* |
114 |
|
|
* Parameters: BlacklistConf structure |
115 |
|
|
* Return: NONE |
116 |
|
|
* |
117 |
|
|
*/ |
118 |
michael |
5114 |
void |
119 |
|
|
stats_dnsblrecv(struct BlacklistConf *bl) |
120 |
michael |
5052 |
{ |
121 |
michael |
5114 |
++bl->stats_recv; |
122 |
michael |
5052 |
} |
123 |
|
|
|
124 |
|
|
/* stats_dnsblsend |
125 |
|
|
* |
126 |
|
|
* Record a sent report |
127 |
|
|
* |
128 |
|
|
* Parameters: NONE |
129 |
|
|
* Return: NONE |
130 |
|
|
* |
131 |
|
|
*/ |
132 |
michael |
5114 |
void |
133 |
|
|
stats_dnsblsend(void) |
134 |
michael |
5052 |
{ |
135 |
michael |
5114 |
++STATS_DNSBLSENT; |
136 |
michael |
5052 |
} |
137 |
|
|
|
138 |
|
|
/* stats_output |
139 |
|
|
* |
140 |
|
|
* Output stats to target via privmsg |
141 |
|
|
* |
142 |
|
|
* |
143 |
|
|
* Parameters: NONE |
144 |
|
|
* Return: NONE |
145 |
michael |
5114 |
* |
146 |
michael |
5052 |
*/ |
147 |
michael |
5114 |
void |
148 |
michael |
5195 |
stats_output(const char *target) |
149 |
michael |
5052 |
{ |
150 |
michael |
5114 |
time_t present; |
151 |
|
|
time_t uptime; |
152 |
|
|
node_t *p; |
153 |
michael |
5052 |
|
154 |
michael |
5114 |
time(&present); |
155 |
|
|
uptime = present - STATS_UPTIME; |
156 |
michael |
5052 |
|
157 |
michael |
6968 |
irc_send("PRIVMSG %s :Uptime: %s", target, time_dissect(uptime)); |
158 |
michael |
5052 |
|
159 |
michael |
5114 |
LIST_FOREACH(p, OpmItem->blacklists->head) |
160 |
|
|
{ |
161 |
michael |
5348 |
const struct BlacklistConf *bl = p->data; |
162 |
michael |
5052 |
|
163 |
michael |
5114 |
if (bl->stats_recv > 0) |
164 |
|
|
irc_send("PRIVMSG %s :DNSBL: %u successful lookups from %s", |
165 |
|
|
target, bl->stats_recv, bl->name); |
166 |
|
|
} |
167 |
michael |
5052 |
|
168 |
michael |
5114 |
if (STATS_DNSBLSENT > 0) |
169 |
|
|
irc_send("PRIVMSG %s :DNSBL: %u reports sent", target, |
170 |
|
|
STATS_DNSBLSENT); |
171 |
|
|
|
172 |
michael |
6024 |
for (const struct StatsHash *tab = STATS_PROXIES; tab->name; ++tab) |
173 |
|
|
if (tab->count) |
174 |
michael |
5114 |
irc_send("PRIVMSG %s :Found %u (%s) open.", target, |
175 |
michael |
6024 |
tab->count, tab->name); |
176 |
michael |
5052 |
|
177 |
|
|
irc_send("PRIVMSG %s :Number of connects: %u (%.2f/minute)", |
178 |
|
|
target, STATS_CONNECTIONS, STATS_CONNECTIONS ? |
179 |
|
|
(float)STATS_CONNECTIONS / ((float)uptime / 60.0) : 0.0); |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
/* fdstats_output |
183 |
|
|
* |
184 |
|
|
* Output file descriptor stats to target via privmsg |
185 |
|
|
* |
186 |
|
|
* |
187 |
|
|
* Parameters: NONE |
188 |
|
|
* Return: NONE |
189 |
|
|
* |
190 |
|
|
*/ |
191 |
michael |
5114 |
void |
192 |
michael |
5195 |
fdstats_output(const char *target) |
193 |
michael |
5052 |
{ |
194 |
michael |
5114 |
struct rlimit rlim; |
195 |
|
|
unsigned int total_fd_use = 0; |
196 |
michael |
5052 |
|
197 |
michael |
5114 |
/* Get file descriptor ceiling */ |
198 |
|
|
if (getrlimit(RLIMIT_NOFILE, &rlim) == -1) |
199 |
|
|
{ |
200 |
|
|
log_printf("FDSTAT -> getrlimit() error retrieving RLIMIT_NOFILE (%s)", strerror(errno)); |
201 |
|
|
irc_send("PRIVMSG %s :FDSTAT -> getrlimit() error retrieving RLIMIT_NOFILE (%s)", |
202 |
|
|
target, strerror(errno)); |
203 |
|
|
return; |
204 |
|
|
} |
205 |
michael |
5052 |
|
206 |
michael |
5114 |
/* |
207 |
|
|
* Check which file descriptors are active, based on suggestions from: |
208 |
|
|
* http://groups.google.com/groups?th=a48b9fe8ca43947c&rnum=1 |
209 |
|
|
*/ |
210 |
|
|
for (unsigned int i = 0; i < rlim.rlim_cur; ++i) |
211 |
|
|
{ |
212 |
|
|
int newfd = dup(i); |
213 |
michael |
5052 |
|
214 |
michael |
6219 |
if (newfd > -1) |
215 |
michael |
5114 |
{ |
216 |
|
|
++total_fd_use; |
217 |
|
|
close(newfd); |
218 |
|
|
} |
219 |
|
|
else |
220 |
|
|
{ |
221 |
|
|
switch (errno) |
222 |
michael |
5052 |
{ |
223 |
michael |
5114 |
case EMFILE: |
224 |
|
|
/* |
225 |
|
|
* We ran out of FDs whilst trying to dup an existing one, |
226 |
|
|
* so all fds are open and we can stop checking here. |
227 |
|
|
*/ |
228 |
|
|
i = total_fd_use = rlim.rlim_cur; |
229 |
|
|
break; |
230 |
michael |
5052 |
|
231 |
michael |
5114 |
case EBADF: |
232 |
|
|
/* Not an FD in use. */ |
233 |
|
|
break; |
234 |
michael |
5052 |
|
235 |
michael |
5114 |
case EINTR: |
236 |
|
|
/* Try again. */ |
237 |
|
|
--i; |
238 |
|
|
break; |
239 |
michael |
5052 |
|
240 |
michael |
5114 |
default: |
241 |
|
|
/* We don't expect any other errors. */ |
242 |
|
|
log_printf("fd %u errno = %u (%s)", i, errno, strerror(errno)); |
243 |
|
|
break; |
244 |
michael |
5052 |
} |
245 |
michael |
5114 |
} |
246 |
|
|
} |
247 |
michael |
5052 |
|
248 |
michael |
5114 |
irc_send("PRIVMSG %s :Total open FD: %u/%d", target, total_fd_use, rlim.rlim_cur); |
249 |
michael |
5052 |
} |