ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/hopm/trunk/src/stats.c
Revision: 6166
Committed: Fri Jun 19 16:33:47 2015 UTC (11 years, 1 month ago) by michael
Content type: text/x-csrc
File size: 5193 byte(s)
Log Message:
- main.c, stats.c: replace bopm occurrences with hopm in some comments

File Contents

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

Properties

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