ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/hopm/trunk/src/stats.c
Revision: 8563
Committed: Sun Sep 23 11:39:44 2018 UTC (7 years, 10 months ago) by michael
Content type: text/x-csrc
File size: 5387 byte(s)
Log Message:
- Update copyright years

File Contents

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

Properties

Name Value
svn:eol-style native
svn:keywords Id