2 * copyright (c) 2006-2008 Emil Mikulic.
4 * graph_db.c: round robin database for graph data
6 * You may use, modify and redistribute this file under the terms of the
7 * GNU General Public License version 2. (see COPYING.GPL)
10 #include <sys/types.h>
26 #include <string.h> /* for memcpy() */
28 #define GRAPH_WIDTH "320"
29 #define GRAPH_HEIGHT "200"
31 extern const char *interface
;
35 unsigned int offset
; /* i.e. seconds start at 0, days start at 1 */
36 unsigned int pos
, num_bars
;
38 unsigned int bar_secs
; /* one bar represents <n> seconds */
42 graph_secs
= {NULL
, NULL
, 0, 0, 60, "seconds", 1},
43 graph_mins
= {NULL
, NULL
, 0, 0, 60, "minutes", 60},
44 graph_hrs
= {NULL
, NULL
, 0, 0, 24, "hours", 3600},
45 graph_days
= {NULL
, NULL
, 1, 0, 31, "days", 86400};
47 static struct graph
*graph_db
[] = {
48 &graph_secs
, &graph_mins
, &graph_hrs
, &graph_days
51 static unsigned int graph_db_size
= sizeof(graph_db
)/sizeof(*graph_db
);
53 static time_t start_time
, last_time
;
59 for (i
=0; i
<graph_db_size
; i
++) {
60 graph_db
[i
]->in
= xmalloc(sizeof(uint64_t) * graph_db
[i
]->num_bars
);
61 graph_db
[i
]->out
= xmalloc(sizeof(uint64_t) * graph_db
[i
]->num_bars
);
63 start_time
= time(NULL
);
68 zero_graph(struct graph
*g
)
70 memset(g
->in
, 0, sizeof(uint64_t) * g
->num_bars
);
71 memset(g
->out
, 0, sizeof(uint64_t) * g
->num_bars
);
78 for (i
=0; i
<graph_db_size
; i
++)
79 zero_graph(graph_db
[i
]);
87 for (i
=0; i
<graph_db_size
; i
++) {
88 free(graph_db
[i
]->in
);
89 free(graph_db
[i
]->out
);
94 graph_acct(uint64_t amount
, enum graph_dir dir
)
97 for (i
=0; i
<graph_db_size
; i
++)
99 case GRAPH_IN
: graph_db
[i
]->in
[ graph_db
[i
]->pos
] += amount
; break;
100 case GRAPH_OUT
: graph_db
[i
]->out
[ graph_db
[i
]->pos
] += amount
; break;
101 default: errx(1, "unknown graph_dir in graph_acct: %d", dir
);
105 /* Advance a graph: advance the pos, zeroing out bars as we move. */
107 advance(struct graph
*g
, const unsigned int pos
)
110 return; /* didn't need to advance */
112 g
->pos
= (g
->pos
+ 1) % g
->num_bars
;
113 g
->in
[g
->pos
] = g
->out
[g
->pos
] = 0;
114 } while (g
->pos
!= pos
);
117 /* Rotate a graph: rotate all bars so that the bar at the current pos is moved
118 * to the newly given pos. This is non-destructive. */
120 rotate(struct graph
*g
, const unsigned int pos
)
127 return; /* nothing to rotate */
129 size
= sizeof(*tmp
) * g
->num_bars
;
131 ofs
= g
->num_bars
+ pos
- g
->pos
;
133 for (i
=0; i
<g
->num_bars
; i
++)
134 tmp
[ (i
+ofs
) % g
->num_bars
] = g
->in
[i
];
135 memcpy(g
->in
, tmp
, size
);
137 for (i
=0; i
<g
->num_bars
; i
++)
138 tmp
[ (i
+ofs
) % g
->num_bars
] = g
->out
[i
];
139 memcpy(g
->out
, tmp
, size
);
142 assert(pos
== ( (g
->pos
+ ofs
) % g
->num_bars
));
147 graph_resync(const time_t new_time
)
151 * If time went backwards, we assume that real time is continuous and that
152 * the time adjustment should only affect display. i.e., if we have:
154 * second 15: 12 bytes
155 * second 16: 345 bytes
156 * second 17: <-- current pos
158 * and time goes backwards to second 8, we will shift the graph around to
162 * second 7: 345 bytes
163 * second 8: <-- current pos
165 * Note that we don't make any corrections for time being stepped forward.
166 * We rely on graph advancement to happen at the correct real time to
167 * account for, for example, bandwidth used per day.
169 assert(new_time
< last_time
);
171 tm
= localtime(&new_time
);
172 if (tm
->tm_sec
== 60)
173 tm
->tm_sec
= 59; /* mis-handle leap seconds */
175 rotate(&graph_secs
, tm
->tm_sec
);
176 rotate(&graph_mins
, tm
->tm_min
);
177 rotate(&graph_hrs
, tm
->tm_hour
);
178 rotate(&graph_days
, tm
->tm_mday
- 1);
180 last_time
= new_time
;
192 if (last_time
== 0) {
193 verbosef("first rotate");
196 if (tm
->tm_sec
== 60)
197 tm
->tm_sec
= 59; /* mis-handle leap seconds */
199 graph_secs
.pos
= tm
->tm_sec
;
200 graph_mins
.pos
= tm
->tm_min
;
201 graph_hrs
.pos
= tm
->tm_hour
;
202 graph_days
.pos
= tm
->tm_mday
- 1;
207 return; /* superfluous rotate */
210 verbosef("time went backwards! (from %u to %u, offset is %d)",
211 (unsigned int)last_time
, (unsigned int)t
, (int)(t
- last_time
));
216 /* else, normal rotation */
220 if (tm
->tm_sec
== 60)
221 tm
->tm_sec
= 59; /* mis-handle leap seconds */
223 /* zero out graphs which have been completely rotated through */
224 for (i
=0; i
<graph_db_size
; i
++)
225 if (td
>= (int)(graph_db
[i
]->num_bars
* graph_db
[i
]->bar_secs
))
226 zero_graph(graph_db
[i
]);
228 /* advance the current position, zeroing up to it */
229 advance(&graph_secs
, tm
->tm_sec
);
230 advance(&graph_mins
, tm
->tm_min
);
231 advance(&graph_hrs
, tm
->tm_hour
);
232 advance(&graph_days
, tm
->tm_mday
- 1);
235 /* ---------------------------------------------------------------------------
236 * Database Import: Grab graphs from a file provided by the caller.
238 * This function will retrieve the data sans the header. We expect the caller
239 * to have validated the header of the segment, and left the file position at
240 * the start of the data.
243 graph_import(const int fd
)
248 if (!read64(fd
, &last
)) return 0;
249 last_time
= (time_t)last
;
251 for (i
=0; i
<graph_db_size
; i
++) {
252 unsigned char num_bars
, pos
;
253 unsigned int filepos
= xtell(fd
);
255 if (!read8(fd
, &num_bars
)) return 0;
256 if (!read8(fd
, &pos
)) return 0;
258 verbosef("at file pos %u, importing graph with %u bars",
259 filepos
, (unsigned int)num_bars
);
261 if (pos
>= num_bars
) {
262 warn("pos is %u, should be < num_bars which is %u",
263 (unsigned int)pos
, (unsigned int)num_bars
);
267 if (graph_db
[i
]->num_bars
!= num_bars
) {
268 warn("num_bars is %u, expecting %u",
269 (unsigned int)num_bars
, graph_db
[i
]->num_bars
);
273 graph_db
[i
]->pos
= pos
;
274 for (j
=0; j
<num_bars
; j
++) {
275 if (!read64(fd
, &(graph_db
[i
]->in
[j
]))) return 0;
276 if (!read64(fd
, &(graph_db
[i
]->out
[j
]))) return 0;
283 /* ---------------------------------------------------------------------------
284 * Database Export: Dump hosts_db into a file provided by the caller.
285 * The caller is responsible for writing out the header first.
288 graph_export(const int fd
)
292 if (!write64(fd
, (uint64_t)last_time
)) return 0;
293 for (i
=0; i
<graph_db_size
; i
++) {
294 if (!write8(fd
, graph_db
[i
]->num_bars
)) return 0;
295 if (!write8(fd
, graph_db
[i
]->pos
)) return 0;
297 for (j
=0; j
<graph_db
[i
]->num_bars
; j
++) {
298 if (!write64(fd
, graph_db
[i
]->in
[j
])) return 0;
299 if (!write64(fd
, graph_db
[i
]->out
[j
])) return 0;
305 /* ---------------------------------------------------------------------------
306 * Web interface: front page!
309 html_front_page(void)
311 struct str
*buf
, *rf
;
313 char start_when
[100];
316 html_open(buf
, "Graphs", interface
, /*want_graph_js=*/1);
318 str_append(buf
, "<p>\n");
319 str_append(buf
, "<b>Running for</b> <span id=\"rf\">");
320 rf
= length_of_time(now
- start_time
);
321 /* FIXME: use a more monotonic clock perhaps? */
322 str_appendstr(buf
, rf
);
324 str_append(buf
, "</span>");
326 if (strftime(start_when
, sizeof(start_when
),
327 "%Y-%m-%d %H:%M:%S %Z%z", localtime(&start_time
)) != 0)
328 str_appendf(buf
, "<b>, since</b> %s", start_when
);
330 str_appendf(buf
,"<b>.</b><br>\n"
331 "<b>Total</b> <span id=\"tb\">%'qu</span> <b>bytes, "
332 "in</b> <span id=\"tp\">%'qu</span> <b>packets.</b> "
333 "(<span id=\"pc\">%'u</span> <b>captured,</b> "
334 "<span id=\"pd\">%'u</span> <b>dropped)</b><br>\n"
338 pkts_recv
, pkts_drop
);
341 "<div id=\"graphs\">\n"
342 "Graphs require JavaScript.\n"
343 "<script type=\"text/javascript\">\n"
345 "var graph_width = " GRAPH_WIDTH
";\n"
346 "var graph_height = " GRAPH_HEIGHT
";\n"
348 "var graphs_uri = \"/graphs.xml\";\n"
352 for (i
=0; i
<graph_db_size
; i
++)
356 "title:\"last %u %s\", "
359 i
, graph_db
[i
]->unit
, graph_db
[i
]->num_bars
, graph_db
[i
]->unit
,
360 graph_db
[i
]->bar_secs
, (i
< graph_db_size
-1) ? "," : "");
361 /* trailing comma breaks on IE, makes the array one element longer */
365 "window.onload = graphs_init;\n"
375 /* ---------------------------------------------------------------------------
376 * Web interface: graphs.xml
382 struct str
*buf
= str_make(), *rf
;
384 str_appendf(buf
, "<graphs tp=\"%qu\" tb=\"%qu\" pc=\"%u\" pd=\"%u\" rf=\"",
385 total_packets
, total_bytes
, pkts_recv
, pkts_drop
);
386 rf
= length_of_time(now
- start_time
);
387 str_appendstr(buf
, rf
);
389 str_append(buf
, "\">\n");
391 for (i
=0; i
<graph_db_size
; i
++) {
392 const struct graph
*g
= graph_db
[i
];
394 str_appendf(buf
, "<%s>\n", g
->unit
);
397 j
= (j
+ 1) % g
->num_bars
;
398 /* <element pos="" in="" out=""/> */
399 str_appendf(buf
, "<e p=\"%u\" i=\"%qu\" o=\"%qu\"/>\n",
400 g
->offset
+ j
, g
->in
[j
], g
->out
[j
]);
401 } while (j
!= g
->pos
);
402 str_appendf(buf
, "</%s>\n", g
->unit
);
404 str_append(buf
, "</graphs>\n");
408 /* vim:set ts=3 sw=3 tw=78 expandtab: */