ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/hopm/trunk/src/stats.c
Revision: 5209
Committed: Mon Dec 29 19:50:33 2014 UTC (11 years, 6 months ago) by michael
Content type: text/x-csrc
File size: 5365 byte(s)
Log Message:
- Removed configure header test for strings.h; removed strings.h header includes

File Contents

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

Properties

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