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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5201 - (hide annotations)
Mon Dec 29 17:49:44 2014 UTC (5 years, 3 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 michael 5052 /*
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 michael 5114 {
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 michael 5052
83    
84     /* stats_init
85     *
86 michael 5114 * Perform initialization of bopm stats
87 michael 5052 *
88     * Parameters: NONE
89     * Return: NONE
90 michael 5114 *
91 michael 5052 */
92 michael 5114 void
93     stats_init(void)
94 michael 5052 {
95 michael 5114 time(&STATS_UPTIME);
96 michael 5052 }
97    
98     /* stats_openproxy
99     *
100     * Record open proxy.
101     *
102     *
103     * Parameters: NONE
104     * Return: NONE
105 michael 5114 *
106 michael 5052 */
107 michael 5114 void
108     stats_openproxy(int type)
109 michael 5052 {
110 michael 5114 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 michael 5052 }
114    
115     /* stats_connect
116     *
117     * Record IRC connect.
118     *
119     *
120     * Parameters: NONE
121     * Return: NONE
122 michael 5114 *
123 michael 5052 */
124 michael 5114 void
125     stats_connect(void)
126 michael 5052 {
127 michael 5114 ++STATS_CONNECTIONS;
128 michael 5052 }
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 michael 5114 void
139     stats_dnsblrecv(struct BlacklistConf *bl)
140 michael 5052 {
141 michael 5114 ++bl->stats_recv;
142 michael 5052 }
143    
144     /* stats_dnsblsend
145     *
146     * Record a sent report
147     *
148     * Parameters: NONE
149     * Return: NONE
150     *
151     */
152 michael 5114 void
153     stats_dnsblsend(void)
154 michael 5052 {
155 michael 5114 ++STATS_DNSBLSENT;
156 michael 5052 }
157    
158     /* stats_output
159     *
160     * Output stats to target via privmsg
161     *
162     *
163     * Parameters: NONE
164     * Return: NONE
165 michael 5114 *
166 michael 5052 */
167 michael 5114 void
168 michael 5196 stats_output(const char *target)
169 michael 5052 {
170 michael 5114 time_t present;
171     time_t uptime;
172     node_t *p;
173 michael 5052
174 michael 5114 time(&present);
175     uptime = present - STATS_UPTIME;
176 michael 5052
177 michael 5114 irc_send("PRIVMSG %s :Uptime: %s", target, dissect_time(uptime));
178 michael 5052
179 michael 5114 LIST_FOREACH(p, OpmItem->blacklists->head)
180     {
181     struct BlacklistConf *bl = p->data;
182 michael 5052
183 michael 5114 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 michael 5052
188 michael 5114 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 michael 5052 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 michael 5114 void
212 michael 5196 fdstats_output(const char *target)
213 michael 5052 {
214 michael 5114 struct rlimit rlim;
215     unsigned int total_fd_use = 0;
216 michael 5052
217 michael 5114 /* 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 michael 5052
226 michael 5114 /*
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 michael 5052
234 michael 5114 if (newfd > 0)
235     {
236     ++total_fd_use;
237     close(newfd);
238     }
239     else
240     {
241     switch (errno)
242 michael 5052 {
243 michael 5114 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 michael 5052
251 michael 5114 case EBADF:
252     /* Not an FD in use. */
253     break;
254 michael 5052
255 michael 5114 case EINTR:
256     /* Try again. */
257     --i;
258     break;
259 michael 5052
260 michael 5114 default:
261     /* We don't expect any other errors. */
262     log_printf("fd %u errno = %u (%s)", i, errno, strerror(errno));
263     break;
264 michael 5052 }
265 michael 5114 }
266     }
267 michael 5052
268 michael 5114 irc_send("PRIVMSG %s :Total open FD: %u/%d", target, total_fd_use, rlim.rlim_cur);
269 michael 5052 }

Properties

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

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