ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/hopm/branches/1.0.x/src/stats.c
(Generate patch)

Comparing hopm/trunk/src/stats.c (file contents):
Revision 5113 by michael, Mon Dec 22 11:56:03 2014 UTC vs.
Revision 5114 by michael, Wed Dec 24 22:12:05 2014 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines