File: graph_db.c

package info (click to toggle)
darkstat 3.0.715-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 816 kB
  • sloc: ansic: 6,542; sh: 355; makefile: 172; php: 15
file content (405 lines) | stat: -rw-r--r-- 10,783 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* darkstat 3
 * copyright (c) 2006-2011 Emil Mikulic.
 *
 * graph_db.c: round robin database for graph data
 *
 * You may use, modify and redistribute this file under the terms of the
 * GNU General Public License version 2. (see COPYING.GPL)
 */

#include <sys/types.h>

#include "cap.h"
#include "conv.h"
#include "db.h"
#include "acct.h"
#include "err.h"
#include "str.h"
#include "html.h"
#include "graph_db.h"
#include "now.h"
#include "opt.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h> /* for memcpy() */

#define GRAPH_WIDTH "320"
#define GRAPH_HEIGHT "200"

struct graph {
   uint64_t *in, *out;
   unsigned int offset; /* i.e. seconds start at 0, days start at 1 */
   unsigned int pos, num_bars;
   const char *unit;
   unsigned int bar_secs; /* one bar represents <n> seconds */
};

static struct graph
   graph_secs = {NULL, NULL, 0, 0, 60, "seconds", 1},
   graph_mins = {NULL, NULL, 0, 0, 60, "minutes", 60},
   graph_hrs  = {NULL, NULL, 0, 0, 24, "hours",   3600},
   graph_days = {NULL, NULL, 1, 0, 31, "days",    86400};

static struct graph *graph_db[] = {
   &graph_secs, &graph_mins, &graph_hrs, &graph_days
};

static unsigned int graph_db_size = sizeof(graph_db)/sizeof(*graph_db);

static time_t start_time, last_time;

void
graph_init(void)
{
   unsigned int i;
   for (i=0; i<graph_db_size; i++) {
      graph_db[i]->in  = xmalloc(sizeof(uint64_t) * graph_db[i]->num_bars);
      graph_db[i]->out = xmalloc(sizeof(uint64_t) * graph_db[i]->num_bars);
   }
   start_time = time(NULL);
   graph_reset();
}

static void
zero_graph(struct graph *g)
{
   memset(g->in,  0, sizeof(uint64_t) * g->num_bars);
   memset(g->out, 0, sizeof(uint64_t) * g->num_bars);
}

void
graph_reset(void)
{
   unsigned int i;
   for (i=0; i<graph_db_size; i++)
      zero_graph(graph_db[i]);
   last_time = 0;
}

void
graph_free(void)
{
   unsigned int i;
   for (i=0; i<graph_db_size; i++) {
      free(graph_db[i]->in);
      free(graph_db[i]->out);
   }
}

void
graph_acct(uint64_t amount, enum graph_dir dir)
{
   unsigned int i;
   for (i=0; i<graph_db_size; i++)
    switch (dir) {
     case GRAPH_IN:  graph_db[i]->in[  graph_db[i]->pos ] += amount; break;
     case GRAPH_OUT: graph_db[i]->out[ graph_db[i]->pos ] += amount; break;
     default: errx(1, "unknown graph_dir in graph_acct: %d", dir);
    }
}

/* Advance a graph: advance the pos, zeroing out bars as we move. */
static void
advance(struct graph *g, const unsigned int pos)
{
   if (g->pos == pos)
      return; /* didn't need to advance */
   do {
      g->pos = (g->pos + 1) % g->num_bars;
      g->in[g->pos] = g->out[g->pos] = 0;
   } while (g->pos != pos);
}

/* Rotate a graph: rotate all bars so that the bar at the current pos is moved
 * to the newly given pos.  This is non-destructive. */
static void
rotate(struct graph *g, const unsigned int pos)
{
   uint64_t *tmp;
   unsigned int i, ofs;
   size_t size;

   if (pos == g->pos)
      return; /* nothing to rotate */

   size = sizeof(*tmp) * g->num_bars;
   tmp = xmalloc(size);
   ofs = g->num_bars + pos - g->pos;

   for (i=0; i<g->num_bars; i++)
      tmp[ (i+ofs) % g->num_bars ] = g->in[i];
   memcpy(g->in, tmp, size);

   for (i=0; i<g->num_bars; i++)
      tmp[ (i+ofs) % g->num_bars ] = g->out[i];
   memcpy(g->out, tmp, size);

   free(tmp);
   assert(pos == ( (g->pos + ofs) % g->num_bars ));
   g->pos = pos;
}

static void
graph_resync(const time_t new_time)
{
   struct tm *tm;
   /*
    * If time went backwards, we assume that real time is continuous and that
    * the time adjustment should only affect display.  i.e., if we have:
    *
    * second 15: 12  bytes
    * second 16: 345 bytes
    * second 17: <-- current pos
    *
    * and time goes backwards to second 8, we will shift the graph around to
    * get:
    *
    * second 6: 12  bytes
    * second 7: 345 bytes
    * second 8: <-- current pos
    *
    * Note that we don't make any corrections for time being stepped forward.
    * We rely on graph advancement to happen at the correct real time to
    * account for, for example, bandwidth used per day.
    */
   assert(new_time < last_time);

   tm = localtime(&new_time);
   if (tm->tm_sec == 60)
      tm->tm_sec = 59; /* mis-handle leap seconds */

   rotate(&graph_secs, tm->tm_sec);
   rotate(&graph_mins, tm->tm_min);
   rotate(&graph_hrs, tm->tm_hour);
   rotate(&graph_days, tm->tm_mday - 1);

   last_time = new_time;
}

void
graph_rotate(void)
{
   time_t t, td;
   struct tm *tm;
   unsigned int i;

   t = now;

   if (last_time == 0) {
      verbosef("first rotate");
      last_time = t;
      tm = localtime(&t);
      if (tm->tm_sec == 60)
         tm->tm_sec = 59; /* mis-handle leap seconds */

      graph_secs.pos = tm->tm_sec;
      graph_mins.pos = tm->tm_min;
      graph_hrs.pos = tm->tm_hour;
      graph_days.pos = tm->tm_mday - 1;
      return;
   }

   if (t == last_time)
      return; /* superfluous rotate */

   if (t < last_time) {
      verbosef("time went backwards! (from %u to %u, offset is %d)",
         (unsigned int)last_time, (unsigned int)t, (int)(t - last_time));
      graph_resync(t);
      return;
   }

   /* else, normal rotation */
   td = t - last_time;
   last_time = t;
   tm = localtime(&t);
   if (tm->tm_sec == 60)
      tm->tm_sec = 59; /* mis-handle leap seconds */

   /* zero out graphs which have been completely rotated through */
   for (i=0; i<graph_db_size; i++)
      if (td >= (int)(graph_db[i]->num_bars * graph_db[i]->bar_secs))
         zero_graph(graph_db[i]);

   /* advance the current position, zeroing up to it */
   advance(&graph_secs, tm->tm_sec);
   advance(&graph_mins, tm->tm_min);
   advance(&graph_hrs, tm->tm_hour);
   advance(&graph_days, tm->tm_mday - 1);
}

/* ---------------------------------------------------------------------------
 * Database Import: Grab graphs from a file provided by the caller.
 *
 * This function will retrieve the data sans the header.  We expect the caller
 * to have validated the header of the segment, and left the file position at
 * the start of the data.
 */
int
graph_import(const int fd)
{
   uint64_t last;
   unsigned int i, j;

   if (!read64(fd, &last)) return 0;
   last_time = (time_t)last;

   for (i=0; i<graph_db_size; i++) {
      unsigned char num_bars, pos;
      unsigned int filepos = xtell(fd);

      if (!read8(fd, &num_bars)) return 0;
      if (!read8(fd, &pos)) return 0;

      verbosef("at file pos %u, importing graph with %u bars",
         filepos, (unsigned int)num_bars);

      if (pos >= num_bars) {
         warn("pos is %u, should be < num_bars which is %u",
            (unsigned int)pos, (unsigned int)num_bars);
         return 0;
      }

      if (graph_db[i]->num_bars != num_bars) {
         warn("num_bars is %u, expecting %u",
            (unsigned int)num_bars, graph_db[i]->num_bars);
         return 0;
      }

      graph_db[i]->pos = pos;
      for (j=0; j<num_bars; j++) {
         if (!read64(fd, &(graph_db[i]->in[j]))) return 0;
         if (!read64(fd, &(graph_db[i]->out[j]))) return 0;
      }
   }

   return 1;
}

/* ---------------------------------------------------------------------------
 * Database Export: Dump hosts_db into a file provided by the caller.
 * The caller is responsible for writing out the header first.
 */
int
graph_export(const int fd)
{
   unsigned int i, j;

   if (!write64(fd, (uint64_t)last_time)) return 0;
   for (i=0; i<graph_db_size; i++) {
      if (!write8(fd, graph_db[i]->num_bars)) return 0;
      if (!write8(fd, graph_db[i]->pos)) return 0;

      for (j=0; j<graph_db[i]->num_bars; j++) {
         if (!write64(fd, graph_db[i]->in[j])) return 0;
         if (!write64(fd, graph_db[i]->out[j])) return 0;
      }
   }
   return 1;
}

/* ---------------------------------------------------------------------------
 * Web interface: front page!
 */
struct str *
html_front_page(void)
{
   struct str *buf, *rf;
   unsigned int i;
   char start_when[100];

   buf = str_make();
   html_open(buf, "Graphs", /*path_depth=*/0, /*want_graph_js=*/1);

   str_append(buf, "<p>\n");
   str_append(buf, "<b>Running for</b> <span id=\"rf\">");
   rf = length_of_time(now - start_time);
   /* FIXME: use a more monotonic clock perhaps? */
   str_appendstr(buf, rf);
   str_free(rf);
   str_append(buf, "</span>");

   if (strftime(start_when, sizeof(start_when),
      "%Y-%m-%d %H:%M:%S %Z%z", localtime(&start_time)) != 0)
      str_appendf(buf, "<b>, since</b> %s", start_when);

   str_appendf(buf,"<b>.</b><br>\n"
      "<b>Total</b> <span id=\"tb\">%'qu</span> <b>bytes, "
      "in</b> <span id=\"tp\">%'qu</span> <b>packets.</b> "
      "(<span id=\"pc\">%'u</span> <b>captured,</b> "
      "<span id=\"pd\">%'u</span> <b>dropped)</b><br>\n"
      "</p>\n",
      acct_total_bytes,
      acct_total_packets,
      cap_pkts_recv, cap_pkts_drop);

   str_append(buf,
      "<div id=\"graphs\">\n"
      "Graphs require JavaScript.\n"
      "<script type=\"text/javascript\">\n"
      "//<![CDATA[\n"
      "var graph_width = " GRAPH_WIDTH ";\n"
      "var graph_height = " GRAPH_HEIGHT ";\n"
      "var bar_gap = 1;\n"
      "var graphs_uri = \"graphs.xml\";\n"
      "var graphs = [\n"
   );

   for (i=0; i<graph_db_size; i++)
      str_appendf(buf,
         " { id:\"g%u\", "
            "name:\"%s\", "
            "title:\"last %u %s\", "
            "bar_secs:%u"
         " }%s\n",
         i, graph_db[i]->unit, graph_db[i]->num_bars, graph_db[i]->unit,
         graph_db[i]->bar_secs, (i < graph_db_size-1) ? "," : "");
      /* trailing comma breaks on IE, makes the array one element longer */

   str_append(buf,
      "];\n"
      "window.onload = graphs_init;\n"
      "//]]>\n"
      "</script>\n"
      "</div>\n"
   );

   html_close(buf);
   return (buf);
}

/* ---------------------------------------------------------------------------
 * Web interface: graphs.xml
 */
struct str *
xml_graphs(void)
{
   unsigned int i, j;
   struct str *buf = str_make(), *rf;

   str_appendf(buf, "<graphs tp=\"%qu\" tb=\"%qu\" pc=\"%u\" pd=\"%u\" rf=\"",
      acct_total_packets, acct_total_bytes, cap_pkts_recv, cap_pkts_drop);
   rf = length_of_time(now - start_time);
   str_appendstr(buf, rf);
   str_free(rf);
   str_append(buf, "\">\n");

   for (i=0; i<graph_db_size; i++) {
      const struct graph *g = graph_db[i];

      str_appendf(buf, "<%s>\n", g->unit);
      j = g->pos;
      do {
         j = (j + 1) % g->num_bars;
         /* <element pos="" in="" out=""/> */
         str_appendf(buf, "<e p=\"%u\" i=\"%qu\" o=\"%qu\"/>\n",
            g->offset + j, g->in[j], g->out[j]);
      } while (j != g->pos);
      str_appendf(buf, "</%s>\n", g->unit);
   }
   str_append(buf, "</graphs>\n");
   return (buf);
}

/* vim:set ts=3 sw=3 tw=78 expandtab: */