ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/hopm/trunk/src/stats.c
Revision: 5052
Committed: Mon Dec 22 11:56:03 2014 UTC (10 years, 8 months ago) by michael
Content type: text/x-csrc
File size: 5827 byte(s)
Log Message:
- Initial import of bopm 3.1.3

File Contents

# User Rev Content
1 michael 5052 /* vim: set shiftwidth=3 softtabstop=3 expandtab: */
2    
3     /*
4     Copyright (C) 2002 Erik Fears
5    
6     This program is free software; you can redistribute it and/or
7     modify it under the terms of the GNU General Public License
8     as published by the Free Software Foundation; either version 2
9     of the License, or (at your option) any later version.
10    
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     GNU General Public License for more details.
15    
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18    
19     Foundation, Inc.
20     59 Temple Place - Suite 330
21     Boston, MA 02111-1307, USA.
22    
23     */
24    
25     #include "setup.h"
26    
27     #include <stdio.h>
28     #include <unistd.h>
29    
30     #ifdef STDC_HEADERS
31     # include <stdlib.h>
32     #endif
33    
34     #ifdef HAVE_STRINGS_H
35     # include <strings.h>
36     #endif
37    
38     #if HAVE_STRING_H
39     # include <string.h>
40     #endif
41    
42     #ifdef TIME_WITH_SYS_TIME
43     # include <sys/time.h>
44     # include <time.h>
45     #else
46     # ifdef HAVE_SYS_TIME_H
47     # include <sys/time.h>
48     # else
49     # include <time.h>
50     # endif
51     #endif
52    
53     #include <sys/types.h>
54     #include <sys/socket.h>
55     #include <netinet/in.h>
56     #include <arpa/inet.h>
57    
58     #include <sys/resource.h> /* getrlimit */
59     #include <errno.h>
60    
61     #include "irc.h"
62     #include "log.h"
63     #include "misc.h"
64     #include "stats.h"
65     #include "config.h"
66     #include "extern.h"
67     #include "opercmd.h"
68     #include "scan.h"
69     #include "stats.h"
70     #include "libopm/src/opm_types.h"
71    
72     static time_t STATS_UPTIME;
73     static unsigned int STATS_CONNECTIONS;
74     static unsigned int STATS_DNSBLSENT;
75    
76     static struct StatsHash STATS_PROXIES[] =
77     {
78     {OPM_TYPE_HTTP, 0, "HTTP" },
79     {OPM_TYPE_HTTPPOST, 0, "HTTPPOST" },
80     {OPM_TYPE_SOCKS4, 0, "SOCKS4" },
81     {OPM_TYPE_SOCKS5, 0, "SOCKS5" },
82     {OPM_TYPE_ROUTER, 0, "ROUTER" },
83     {OPM_TYPE_WINGATE, 0, "WINGATE" }
84     };
85    
86    
87     /* stats_init
88     *
89     * Perform initialization of bopm stats
90     *
91     * Parameters: NONE
92     * Return: NONE
93     *
94     */
95    
96     void stats_init(void)
97     {
98     time(&STATS_UPTIME);
99     }
100    
101    
102    
103    
104     /* stats_openproxy
105     *
106     * Record open proxy.
107     *
108     *
109     * Parameters: NONE
110     * Return: NONE
111     *
112     */
113    
114     void stats_openproxy(int type)
115     {
116     unsigned int i;
117    
118     for(i = 0; i < (sizeof(STATS_PROXIES) / sizeof(struct StatsHash)); i++)
119     if(STATS_PROXIES[i].type == type)
120     STATS_PROXIES[i].count++;
121     }
122    
123    
124    
125     /* stats_connect
126     *
127     * Record IRC connect.
128     *
129     *
130     * Parameters: NONE
131     * Return: NONE
132     *
133     */
134    
135    
136     void stats_connect(void)
137     {
138     STATS_CONNECTIONS++;
139     }
140    
141    
142    
143     /* stats_dnsblrecv
144     *
145     * Record that a user was found in the blacklist.
146     *
147     * Parameters: BlacklistConf structure
148     * Return: NONE
149     *
150     */
151    
152     void stats_dnsblrecv(struct BlacklistConf *bl)
153     {
154     bl->stats_recv++;
155     }
156    
157    
158    
159    
160     /* stats_dnsblsend
161     *
162     * Record a sent report
163     *
164     * Parameters: NONE
165     * Return: NONE
166     *
167     */
168    
169     void stats_dnsblsend(void)
170     {
171     STATS_DNSBLSENT++;
172     }
173    
174    
175    
176    
177     /* stats_output
178     *
179     * Output stats to target via privmsg
180     *
181     *
182     * Parameters: NONE
183     * Return: NONE
184     *
185     */
186    
187     void stats_output(char *target)
188     {
189     unsigned int i;
190     time_t present;
191     time_t uptime;
192     node_t *p;
193     struct BlacklistConf *bl;
194    
195     time(&present);
196     uptime = present - STATS_UPTIME;
197    
198     irc_send("PRIVMSG %s :Uptime: %s", target, dissect_time(uptime));
199    
200     LIST_FOREACH(p, OpmItem->blacklists->head)
201     {
202     bl = p->data;
203     if(bl->stats_recv > 0)
204     {
205     irc_send("PRIVMSG %s :DNSBL: %u successful lookups from %s",
206     target, bl->stats_recv, bl->name);
207     }
208     }
209    
210     if(STATS_DNSBLSENT > 0)
211     {
212     irc_send("PRIVMSG %s :DNSBL: %u reports sent", target,
213     STATS_DNSBLSENT);
214     }
215    
216     for(i = 0; i < (sizeof(STATS_PROXIES) / sizeof(struct StatsHash)); i++)
217     {
218     if(STATS_PROXIES[i].count > 0)
219     {
220     irc_send("PRIVMSG %s :Found %u (%s) open.", target,
221     STATS_PROXIES[i].count, STATS_PROXIES[i].name);
222     }
223     }
224    
225     irc_send("PRIVMSG %s :Number of connects: %u (%.2f/minute)",
226     target, STATS_CONNECTIONS, STATS_CONNECTIONS ?
227     (float)STATS_CONNECTIONS / ((float)uptime / 60.0) : 0.0);
228    
229     }
230    
231    
232    
233     /* fdstats_output
234     *
235     * Output file descriptor stats to target via privmsg
236     *
237     *
238     * Parameters: NONE
239     * Return: NONE
240     *
241     */
242    
243     void fdstats_output(char *target)
244     {
245     struct rlimit rlim;
246     unsigned total_fd_use, i;
247     int newfd;
248    
249     /* Get file descriptor ceiling */
250     if(getrlimit(RLIMIT_NOFILE, &rlim) == -1)
251     {
252     log_printf("FDSTAT -> getrlimit() error retrieving RLIMIT_NOFILE (%s)", strerror(errno));
253     irc_send("PRIVMSG %s :FDSTAT -> getrlimit() error retrieving RLIMIT_NOFILE (%s)",
254     target, strerror(errno));
255     return;
256     }
257    
258     /*
259     * Check which file descriptors are active, based on suggestions from:
260     * http://groups.google.com/groups?th=a48b9fe8ca43947c&rnum=1
261     */
262     total_fd_use = 0;
263     for(i = 0; i < rlim.rlim_cur; i++)
264     {
265     newfd = dup(i);
266    
267     if(newfd > 0) {
268     total_fd_use++;
269     close(newfd);
270     }
271     else
272     {
273     switch (errno)
274     {
275     case EMFILE:
276     /*
277     * We ran out of FDs whilst trying to dup an existing one,
278     * so all fds are open and we can stop checking here.
279     */
280     i = total_fd_use = rlim.rlim_cur;
281     break;
282    
283     case EBADF:
284     /* Not an FD in use. */
285     break;
286    
287     case EINTR:
288     /* Try again. */
289     i--;
290     break;
291    
292     default:
293     /* We don't expect any other errors. */
294     log_printf("fd %u errno = %u (%s)", i, errno, strerror(errno));
295     break;
296     }
297     }
298     }
299    
300     irc_send("PRIVMSG %s :Total open FD: %u/%d", target, total_fd_use, rlim.rlim_cur);
301     }