/[svn]/hopm/branches/1.0.x/src/stats.c
ViewVC logotype

Contents of /hopm/branches/1.0.x/src/stats.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5201 - (show annotations)
Mon Dec 29 17:49:44 2014 UTC (5 years, 7 months ago) by michael
File MIME type: text/x-chdr
File size: 5473 byte(s)
- Moved OPT_DEBUG prototype to main.h (where it belongs to) and removed extern.h

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

Properties

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

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28