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