summaryrefslogtreecommitdiffabout
path: root/src/stat.c
Unidiff
Diffstat (limited to 'src/stat.c') (more/less context) (ignore whitespace changes)
-rw-r--r--src/stat.c65
1 files changed, 35 insertions, 30 deletions
diff --git a/src/stat.c b/src/stat.c
index ed4ab8e..85b76d9 100644
--- a/src/stat.c
+++ b/src/stat.c
@@ -36,38 +36,38 @@ interpolate (queue_t *q,
36 time_t last_time, struct traffic_history *last_rates, 36 time_t last_time, struct traffic_history *last_rates,
37 time_t interval, double inrate, double outrate, 37 time_t interval, double inrate, double outrate,
38 ovf_t ovf, 38 ovf_t ovf,
39 struct traffic_record *tr) 39 struct traffic_record *tr)
40{ 40{
41 time_t next; 41 time_t next;
42 struct traffic_history th; 42 struct traffic_history th;
43 43
44 if (now - last_time <= step) 44 if (now - last_time <= step)
45 { 45 {
46 th.inrate = inrate; 46 th.inrate = inrate;
47 th.outrate = outrate; 47 th.outrate = outrate;
48 verbose (3, "Insert %lu %g %g", next, th.inrate, th.outrate); 48 verbose (3, _("insert %lu %g %g"), next, th.inrate, th.outrate);
49 queue_put (q, &th); 49 queue_put (q, &th);
50 if (ovf) 50 if (ovf)
51 ovf (&th, tr, now); 51 ovf (&th, tr, now);
52 return; 52 return;
53 } 53 }
54 54
55 for (next = last_time + step; next <= now; next += step) 55 for (next = last_time + step; next <= now; next += step)
56 { 56 {
57 th.inrate = (inrate - last_rates->inrate) * (next - last_time) 57 th.inrate = (inrate - last_rates->inrate) * (next - last_time)
58 / interval + last_rates->inrate; 58 / interval + last_rates->inrate;
59 th.outrate = (outrate - last_rates->outrate) * (next - last_time) 59 th.outrate = (outrate - last_rates->outrate) * (next - last_time)
60 / interval + last_rates->outrate; 60 / interval + last_rates->outrate;
61 verbose (3, "Insert %lu %g %g", next, th.inrate, th.outrate); 61 verbose (3, _("insert %lu %g %g"), next, th.inrate, th.outrate);
62 queue_put (q, &th); 62 queue_put (q, &th);
63 if (ovf) 63 if (ovf)
64 ovf (&th, tr, now); 64 ovf (&th, tr, now);
65 } 65 }
66} 66}
67 67
68int 68int
69overflow (struct traffic_history *th, 69overflow (struct traffic_history *th,
70 struct traffic_record *tr, 70 struct traffic_record *tr,
71 time_t now, 71 time_t now,
72 ovf_t ovf, 72 ovf_t ovf,
73 struct avg_acc *avg, 73 struct avg_acc *avg,
@@ -83,77 +83,80 @@ overflow (struct traffic_history *th,
83 step, 83 step,
84 now, 84 now,
85 avg->time, 85 avg->time,
86 lastp, 86 lastp,
87 now - avg->time, 87 now - avg->time,
88 avg->inrate, avg->outrate, 88 avg->inrate, avg->outrate,
89 ovf, tr); 89 ovf, tr);
90 else 90 else
91 { 91 {
92 struct traffic_history tmp; 92 struct traffic_history tmp;
93 tmp.inrate = avg->inrate; 93 tmp.inrate = avg->inrate;
94 tmp.outrate = avg->outrate; 94 tmp.outrate = avg->outrate;
95 verbose (3, "Insert %lu %g %g", now, tmp.inrate, tmp.outrate); 95 verbose (3, _("insert %lu %g %g"), now, tmp.inrate, tmp.outrate);
96 queue_put (q, &tmp); 96 queue_put (q, &tmp);
97 } 97 }
98 98
99 avg->inrate = avg->outrate = 0; 99 avg->inrate = avg->outrate = 0;
100 avg->count = 0; 100 avg->count = 0;
101 avg->time = now; 101 avg->time = now;
102 } 102 }
103 avg->inrate = (avg->count * avg->inrate + th->inrate) / (avg->count + 1); 103 avg->inrate = (avg->count * avg->inrate + th->inrate) / (avg->count + 1);
104 avg->outrate = (avg->count * avg->outrate + th->outrate) / (avg->count + 1); 104 avg->outrate = (avg->count * avg->outrate + th->outrate) / (avg->count + 1);
105 avg->count++; 105 avg->count++;
106} 106}
107 107
108int 108int
109ovf_monthly (struct traffic_history *th, struct traffic_record *tr, time_t now) 109ovf_monthly (struct traffic_history *th, struct traffic_record *tr, time_t now)
110{ 110{
111 verbose (2, "begin overflow_monthly %lu %g %g", now, th->inrate, th->outrate); 111 verbose (2, _("begin overflow_monthly %lu %g %g"),
112 now, th->inrate, th->outrate);
112 overflow (th, tr, now, NULL, &tr->year_avg, &tr->year_hist, 113 overflow (th, tr, now, NULL, &tr->year_avg, &tr->year_hist,
113 YEAR_COUNT, YEAR_SAMPLE); 114 YEAR_COUNT, YEAR_SAMPLE);
114 verbose (2, "end overflow_monthly"); 115 verbose (2, _("end overflow_monthly"));
115} 116}
116 117
117int 118int
118ovf_weekly (struct traffic_history *th, struct traffic_record *tr, time_t now) 119ovf_weekly (struct traffic_history *th, struct traffic_record *tr, time_t now)
119{ 120{
120 verbose (2, "begin overflow_weekly %lu %g %g", now, th->inrate, th->outrate); 121 verbose (2, _("begin overflow_weekly %lu %g %g"),
122 now, th->inrate, th->outrate);
121 overflow (th, tr, now, ovf_monthly, &tr->month_avg, &tr->month_hist, 123 overflow (th, tr, now, ovf_monthly, &tr->month_avg, &tr->month_hist,
122 MONTH_COUNT, MONTH_SAMPLE); 124 MONTH_COUNT, MONTH_SAMPLE);
123 verbose (2, "end overflow_daily"); 125 verbose (2, _("end overflow_daily"));
124} 126}
125 127
126int 128int
127ovf_daily (struct traffic_history *th, struct traffic_record *tr, time_t now) 129ovf_daily (struct traffic_history *th, struct traffic_record *tr, time_t now)
128{ 130{
129 verbose (2, "begin overflow_daily %lu %g %g", now, th->inrate, th->outrate); 131 verbose (2, _("begin overflow_daily %lu %g %g"),
132 now, th->inrate, th->outrate);
130 overflow (th, tr, now, ovf_weekly, &tr->week_avg, &tr->week_hist, 133 overflow (th, tr, now, ovf_weekly, &tr->week_avg, &tr->week_hist,
131 WEEK_COUNT, WEEK_SAMPLE); 134 WEEK_COUNT, WEEK_SAMPLE);
132 verbose (2, "end overflow_daily"); 135 verbose (2, _("end overflow_daily"));
133} 136}
134 137
135void 138void
136update_stats (struct monitor *mon, struct traffic_sample *sample, 139update_stats (struct monitor *mon, struct traffic_sample *sample,
137 struct traffic_record *tr) 140 struct traffic_record *tr)
138{ 141{
139 time_t interval; 142 time_t interval;
140 double inrate, outrate; 143 double inrate, outrate;
141 144
142 struct traffic_history *lastp = queue_get_tail (&tr->day_hist); 145 struct traffic_history *lastp = queue_get_tail (&tr->day_hist);
143 146
144 interval = sample->time - tr->last.time; 147 interval = sample->time - tr->last.time;
145 if (interval == 0) 148 if (interval == 0)
146 { 149 {
147 logmsg (L_ERR, "Ignoring zero interval"); 150 logmsg (L_ERR, _("ignoring zero interval"));
148 return; 151 return;
149 } 152 }
150 inrate = (double) sample->in / interval; 153 inrate = (double) sample->in / interval;
151 outrate = (double) sample->out / interval; 154 outrate = (double) sample->out / interval;
152 if (lastp) 155 if (lastp)
153 { 156 {
154 interpolate (&tr->day_hist, 157 interpolate (&tr->day_hist,
155 DAY_SAMPLE, 158 DAY_SAMPLE,
156 sample->time, 159 sample->time,
157 tr->last.time, 160 tr->last.time,
158 &tr->last_rates, 161 &tr->last_rates,
159 interval, 162 interval,
@@ -208,25 +211,25 @@ _convert (queue_t *q, ovf_t ovf,
208 for (hp = hist + count - 1; hp >= hist; hp--) 211 for (hp = hist + count - 1; hp >= hist; hp--)
209 { 212 {
210 time_t interval; 213 time_t interval;
211 double inrate, outrate; 214 double inrate, outrate;
212 struct traffic_history *lastp = queue_get_tail (q); 215 struct traffic_history *lastp = queue_get_tail (q);
213 216
214 interval = hp->time - tr->last.time; 217 interval = hp->time - tr->last.time;
215 inrate = (double) hp->in; 218 inrate = (double) hp->in;
216 outrate = (double) hp->out; 219 outrate = (double) hp->out;
217 220
218 if (interval == 0) 221 if (interval == 0)
219 { 222 {
220 logmsg (L_ERR, "Ignoring zero interval"); 223 logmsg (L_ERR, _("ignoring zero interval"));
221 break; 224 break;
222 } 225 }
223 if (lastp) 226 if (lastp)
224 { 227 {
225 interpolate (q, 228 interpolate (q,
226 sample_interval, 229 sample_interval,
227 hp->time, 230 hp->time,
228 tr->last.time, 231 tr->last.time,
229 &tr->last_rates, 232 &tr->last_rates,
230 interval, 233 interval,
231 inrate, outrate, 234 inrate, outrate,
232 ovf, tr); 235 ovf, tr);
@@ -243,64 +246,64 @@ _convert (queue_t *q, ovf_t ovf,
243 tr->last.time = hp->time; 246 tr->last.time = hp->time;
244 tr->last.in = hp->in; 247 tr->last.in = hp->in;
245 tr->last.out = hp->out; 248 tr->last.out = hp->out;
246 tr->last_rates.inrate = inrate; 249 tr->last_rates.inrate = inrate;
247 tr->last_rates.outrate = outrate; 250 tr->last_rates.outrate = outrate;
248 } 251 }
249} 252}
250 253
251static void 254static void
252convert_yearly (struct traffic_record *tr, struct traffic_sample *hp, 255convert_yearly (struct traffic_record *tr, struct traffic_sample *hp,
253 size_t count) 256 size_t count)
254{ 257{
255 verbose (2, "begin convert_yearly"); 258 verbose (2, _("begin convert_yearly"));
256 _convert (&tr->year_hist, NULL, tr, 259 _convert (&tr->year_hist, NULL, tr,
257 hp, count, YEAR_SAMPLE); 260 hp, count, YEAR_SAMPLE);
258 compute_avg (&tr->year_avg, &tr->year_hist, &tr->last); 261 compute_avg (&tr->year_avg, &tr->year_hist, &tr->last);
259 verbose (2, "end convert_yearly"); 262 verbose (2, _("end convert_yearly"));
260} 263}
261 264
262static void 265static void
263convert_monthly (struct traffic_record *tr, struct traffic_sample *hp, 266convert_monthly (struct traffic_record *tr, struct traffic_sample *hp,
264 size_t count) 267 size_t count)
265{ 268{
266 verbose (2, "begin convert_monthly"); 269 verbose (2, _("begin convert_monthly"));
267 if (count > MONTH_COUNT+1) 270 if (count > MONTH_COUNT+1)
268 convert_yearly (tr, hp + MONTH_COUNT + 1, count - (MONTH_COUNT + 1)); 271 convert_yearly (tr, hp + MONTH_COUNT + 1, count - (MONTH_COUNT + 1));
269 _convert (&tr->month_hist, ovf_monthly, tr, 272 _convert (&tr->month_hist, ovf_monthly, tr,
270 hp, MONTH_COUNT + 1, MONTH_SAMPLE); 273 hp, MONTH_COUNT + 1, MONTH_SAMPLE);
271 compute_avg (&tr->month_avg, &tr->month_hist, &tr->last); 274 compute_avg (&tr->month_avg, &tr->month_hist, &tr->last);
272 verbose (2, "end convert_monthly"); 275 verbose (2, _("end convert_monthly"));
273} 276}
274 277
275static void 278static void
276convert_weekly (struct traffic_record *tr, struct traffic_sample *hp, size_t count) 279convert_weekly (struct traffic_record *tr, struct traffic_sample *hp, size_t count)
277{ 280{
278 verbose (2, "begin convert_weekly"); 281 verbose (2, _("begin convert_weekly"));
279 if (count > WEEK_COUNT+1) 282 if (count > WEEK_COUNT+1)
280 convert_monthly (tr, hp + WEEK_COUNT + 1, count - (WEEK_COUNT + 1)); 283 convert_monthly (tr, hp + WEEK_COUNT + 1, count - (WEEK_COUNT + 1));
281 _convert (&tr->week_hist, ovf_weekly, 284 _convert (&tr->week_hist, ovf_weekly,
282 tr, hp, WEEK_COUNT + 1, WEEK_SAMPLE); 285 tr, hp, WEEK_COUNT + 1, WEEK_SAMPLE);
283 compute_avg (&tr->week_avg, &tr->week_hist, &tr->last); 286 compute_avg (&tr->week_avg, &tr->week_hist, &tr->last);
284 verbose (2, "end convert_weekly"); 287 verbose (2, _("end convert_weekly"));
285} 288}
286 289
287static void 290static void
288convert_daily (struct traffic_record *tr, struct traffic_sample *hp, size_t count) 291convert_daily (struct traffic_record *tr, struct traffic_sample *hp, size_t count)
289{ 292{
290 verbose (2, "begin convert_daily"); 293 verbose (2, _("begin convert_daily"));
291 if (count > DAY_COUNT+1) 294 if (count > DAY_COUNT+1)
292 convert_weekly (tr, hp + DAY_COUNT + 1, count - (DAY_COUNT + 1)); 295 convert_weekly (tr, hp + DAY_COUNT + 1, count - (DAY_COUNT + 1));
293 _convert (&tr->day_hist, ovf_daily, tr, hp, DAY_COUNT + 1, DAY_SAMPLE); 296 _convert (&tr->day_hist, ovf_daily, tr, hp, DAY_COUNT + 1, DAY_SAMPLE);
294 verbose (2, "end convert_daily"); 297 verbose (2, _("end convert_daily"));
295} 298}
296 299
297static void 300static void
298convert_stats (struct monitor *mon, struct traffic_sample *last, 301convert_stats (struct monitor *mon, struct traffic_sample *last,
299 struct traffic_sample *hp, size_t count) 302 struct traffic_sample *hp, size_t count)
300{ 303{
301 struct traffic_record *trp, tr; 304 struct traffic_record *trp, tr;
302 305
303 memset (&tr, 0, sizeof tr); 306 memset (&tr, 0, sizeof tr);
304 tr_init (&tr); 307 tr_init (&tr);
305 308
306 convert_daily (&tr, hp, count); 309 convert_daily (&tr, hp, count);
@@ -320,33 +323,33 @@ import_log (const char *name)
320 struct obstack stk; 323 struct obstack stk;
321 struct traffic_sample last, hist, *hp; 324 struct traffic_sample last, hist, *hp;
322 unsigned long inmax, outmax; 325 unsigned long inmax, outmax;
323 size_t count = 0; 326 size_t count = 0;
324 char *buf = NULL; 327 char *buf = NULL;
325 size_t bufsize = 0; 328 size_t bufsize = 0;
326 size_t line = 1; 329 size_t line = 1;
327 time_t cur; 330 time_t cur;
328 331
329 fp = fopen (name, "r"); 332 fp = fopen (name, "r");
330 if (!fp) 333 if (!fp)
331 { 334 {
332 logmsg (L_ERR, "cannot open `%s': %s", name, strerror (errno)); 335 logmsg (L_ERR, _("cannot open `%s': %s"), name, strerror (errno));
333 return 1; 336 return 1;
334 } 337 }
335 verbose (2, "Importing %s", name); 338 verbose (2, _("importing %s"), name);
336 339
337 340
338 if (fscanf (fp, "%ld %lu %lu\n", &last.time, &last.in, &last.out) != 3) 341 if (fscanf (fp, "%ld %lu %lu\n", &last.time, &last.in, &last.out) != 3)
339 { 342 {
340 logmsg (L_ERR, "%s:1: Unexpected number of fields", name); 343 logmsg (L_ERR, _("%s:1: unexpected number of fields"), name);
341 fclose (fp); 344 fclose (fp);
342 return 1; 345 return 1;
343 } 346 }
344 cur = last.time; 347 cur = last.time;
345 348
346 obstack_init (&stk); 349 obstack_init (&stk);
347 while (getline (&buf, &bufsize, fp) > 0) 350 while (getline (&buf, &bufsize, fp) > 0)
348 { 351 {
349 int i; 352 int i;
350 unsigned long rd[5]; 353 unsigned long rd[5];
351 354
352 line++; 355 line++;
@@ -355,25 +358,25 @@ import_log (const char *name)
355 &rd[0], &rd[1], &rd[2], &rd[3], &rd[4]) < 5) 358 &rd[0], &rd[1], &rd[2], &rd[3], &rd[4]) < 5)
356 { 359 {
357 rd[3] = rd[1]; 360 rd[3] = rd[1];
358 rd[4] = rd[2]; 361 rd[4] = rd[2];
359 } 362 }
360 363
361 for (i = 0; i <= 4; i++) 364 for (i = 0; i <= 4; i++)
362 rd[i] = rd[i] < 0 ? 0 : rd[i]; 365 rd[i] = rd[i] < 0 ? 0 : rd[i];
363 366
364 hist.time = rd[0]; 367 hist.time = rd[0];
365 if (hist.time > cur) 368 if (hist.time > cur)
366 { 369 {
367 logmsg (L_WARNING, "%s:%lu: is corrupted", name, line); 370 logmsg (L_WARNING, _("%s:%lu: is corrupted"), name, line);
368 break; 371 break;
369 } 372 }
370 cur = hist.time; 373 cur = hist.time;
371 hist.in = rd[1]; 374 hist.in = rd[1];
372 hist.out = rd[2]; 375 hist.out = rd[2];
373 if (inmax < rd[3]) 376 if (inmax < rd[3])
374 inmax = rd[3]; 377 inmax = rd[3];
375 if (inmax < hist.in) 378 if (inmax < hist.in)
376 inmax = hist.in; 379 inmax = hist.in;
377 if (outmax < rd[4]) 380 if (outmax < rd[4])
378 outmax = rd[4]; 381 outmax = rd[4];
379 if (outmax < hist.out) 382 if (outmax < hist.out)
@@ -390,45 +393,46 @@ import_log (const char *name)
390 { 393 {
391 struct monitor *mon; 394 struct monitor *mon;
392 395
393 char *p = strrchr (name, '/'); 396 char *p = strrchr (name, '/');
394 char *base = xstrdup (p ? p + 1 : name); 397 char *base = xstrdup (p ? p + 1 : name);
395 p = strrchr (base, '.'); 398 p = strrchr (base, '.');
396 if (p) 399 if (p)
397 *p = 0; 400 *p = 0;
398 401
399 mon = find_monitor (base); 402 mon = find_monitor (base);
400 if (!mon) 403 if (!mon)
401 { 404 {
402 logmsg (L_ERR, "cannot find monitor `%s'", base); 405 logmsg (L_ERR, _("cannot find monitor `%s'"), base);
403 rc = 1; 406 rc = 1;
404 } 407 }
405 else 408 else
406 convert_stats (mon, &last, hp, count); 409 convert_stats (mon, &last, hp, count);
407 } 410 }
408 411
409 obstack_free (&stk, NULL); 412 obstack_free (&stk, NULL);
410 return rc; 413 return rc;
411} 414}
412 415
413void 416void
414import (const char *dirname) 417import (const char *dirname)
415{ 418{
416 size_t count = 0; 419 size_t count = 0;
417 struct stat st; 420 struct stat st;
418 421
419 verbose (2, "Examining `%s'", dirname); 422 verbose (2, _("examining `%s'"), dirname);
420 423
421 if (stat (dirname, &st)) 424 if (stat (dirname, &st))
422 die (EX_OSERR, "cannot stat file `%s': %s", dirname, strerror (errno)); 425 die (EX_OSERR, _("cannot stat file `%s': %s"),
426 dirname, strerror (errno));
423 else if (S_ISREG (st.st_mode)) 427 else if (S_ISREG (st.st_mode))
424 { 428 {
425 open_db (TAGR_DB_WR); 429 open_db (TAGR_DB_WR);
426 if (import_log (dirname) == 0) 430 if (import_log (dirname) == 0)
427 count++; 431 count++;
428 close_db (); 432 close_db ();
429 } 433 }
430 else if (S_ISDIR (st.st_mode)) 434 else if (S_ISDIR (st.st_mode))
431 { 435 {
432 char *pattern; 436 char *pattern;
433 glob_t gl; 437 glob_t gl;
434 size_t i; 438 size_t i;
@@ -441,28 +445,29 @@ import (const char *dirname)
441 switch (rc) 445 switch (rc)
442 { 446 {
443 case 0: 447 case 0:
444 open_db (TAGR_DB_WR); 448 open_db (TAGR_DB_WR);
445 for (i = 0; i < gl.gl_pathc; i++) 449 for (i = 0; i < gl.gl_pathc; i++)
446 if (import_log (gl.gl_pathv[i]) == 0) 450 if (import_log (gl.gl_pathv[i]) == 0)
447 count++; 451 count++;
448 globfree (&gl); 452 globfree (&gl);
449 close_db (); 453 close_db ();
450 break; 454 break;
451 455
452 case GLOB_NOSPACE: 456 case GLOB_NOSPACE:
453 die (EX_UNAVAILABLE, "cannot scan directory: %s", strerror (ENOMEM)); 457 die (EX_UNAVAILABLE, _("cannot scan directory: %s"),
458 strerror (ENOMEM));
454 459
455 case GLOB_ABORTED: 460 case GLOB_ABORTED:
456 die (EX_UNAVAILABLE, "scanning aborted"); 461 die (EX_UNAVAILABLE, _("scanning aborted"));
457 462
458 case GLOB_NOMATCH: 463 case GLOB_NOMATCH:
459 break; 464 break;
460 465
461 default: 466 default:
462 die (EX_UNAVAILABLE, "cannot scan directory `%s'", dirname); 467 die (EX_UNAVAILABLE, _("cannot scan directory `%s'"), dirname);
463 } 468 }
464 } 469 }
465 470
466 verbose (1, "Number of imported log files: %d", count); 471 verbose (1, _("number of imported log files: %d"), count);
467} 472}
468 473

Return to:

Send suggestions and report system problems to the System administrator.