2 * copyright (c) 2006-2011 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>
25 #include <string.h> /* for memcpy() */
27 #define GRAPH_WIDTH "320"
28 #define GRAPH_HEIGHT "200"
32 unsigned int offset
; /* i.e. seconds start at 0, days start at 1 */
33 unsigned int pos
, num_bars
;
35 unsigned int bar_secs
; /* one bar represents <n> seconds */
39 graph_secs
= {NULL
, NULL
, 0, 0, 60, "seconds", 1},
40 graph_mins
= {NULL
, NULL
, 0, 0, 60, "minutes", 60},
41 graph_hrs
= {NULL
, NULL
, 0, 0, 24, "hours", 3600},
42 graph_days
= {NULL
, NULL
, 1, 0, 31, "days", 86400};
44 static struct graph
*graph_db
[] = {
45 &graph_secs
, &graph_mins
, &graph_hrs
, &graph_days
48 static unsigned int graph_db_size
= sizeof(graph_db
)/sizeof(*graph_db
);
50 static time_t start_time
, last_time
;
56 for (i
=0; i
<graph_db_size
; i
++) {
57 graph_db
[i
]->in
= xmalloc(sizeof(uint64_t) * graph_db
[i
]->num_bars
);
58 graph_db
[i
]->out
= xmalloc(sizeof(uint64_t) * graph_db
[i
]->num_bars
);
60 start_time
= time(NULL
);
65 zero_graph(struct graph
*g
)
67 memset(g
->in
, 0, sizeof(uint64_t) * g
->num_bars
);
68 memset(g
->out
, 0, sizeof(uint64_t) * g
->num_bars
);
75 for (i
=0; i
<graph_db_size
; i
++)
76 zero_graph(graph_db
[i
]);
84 for (i
=0; i
<graph_db_size
; i
++) {
85 free(graph_db
[i
]->in
);
86 free(graph_db
[i
]->out
);
91 graph_acct(uint64_t amount
, enum graph_dir dir
)
94 for (i
=0; i
<graph_db_size
; i
++)
96 case GRAPH_IN
: graph_db
[i
]->in
[ graph_db
[i
]->pos
] += amount
; break;
97 case GRAPH_OUT
: graph_db
[i
]->out
[ graph_db
[i
]->pos
] += amount
; break;
98 default: errx(1, "unknown graph_dir in graph_acct: %d", dir
);
102 /* Advance a graph: advance the pos, zeroing out bars as we move. */
104 advance(struct graph
*g
, const unsigned int pos
)
107 return; /* didn't need to advance */
109 g
->pos
= (g
->pos
+ 1) % g
->num_bars
;
110 g
->in
[g
->pos
] = g
->out
[g
->pos
] = 0;
111 } while (g
->pos
!= pos
);
114 /* Rotate a graph: rotate all bars so that the bar at the current pos is moved
115 * to the newly given pos. This is non-destructive. */
117 rotate(struct graph
*g
, const unsigned int pos
)
124 return; /* nothing to rotate */
126 size
= sizeof(*tmp
) * g
->num_bars
;
128 ofs
= g
->num_bars
+ pos
- g
->pos
;
130 for (i
=0; i
<g
->num_bars
; i
++)
131 tmp
[ (i
+ofs
) % g
->num_bars
] = g
->in
[i
];
132 memcpy(g
->in
, tmp
, size
);
134 for (i
=0; i
<g
->num_bars
; i
++)
135 tmp
[ (i
+ofs
) % g
->num_bars
] = g
->out
[i
];
136 memcpy(g
->out
, tmp
, size
);
139 assert(pos
== ( (g
->pos
+ ofs
) % g
->num_bars
));
144 graph_resync(const time_t new_time
)
148 * If time went backwards, we assume that real time is continuous and that
149 * the time adjustment should only affect display. i.e., if we have:
151 * second 15: 12 bytes
152 * second 16: 345 bytes
153 * second 17: <-- current pos
155 * and time goes backwards to second 8, we will shift the graph around to
159 * second 7: 345 bytes
160 * second 8: <-- current pos
162 * Note that we don't make any corrections for time being stepped forward.
163 * We rely on graph advancement to happen at the correct real time to
164 * account for, for example, bandwidth used per day.
166 assert(new_time
< last_time
);
168 tm
= localtime(&new_time
);
169 if (tm
->tm_sec
== 60)
170 tm
->tm_sec
= 59; /* mis-handle leap seconds */
172 rotate(&graph_secs
, tm
->tm_sec
);
173 rotate(&graph_mins
, tm
->tm_min
);
174 rotate(&graph_hrs
, tm
->tm_hour
);
175 rotate(&graph_days
, tm
->tm_mday
- 1);
177 last_time
= new_time
;
189 if (last_time
== 0) {
190 verbosef("first rotate");
193 if (tm
->tm_sec
== 60)
194 tm
->tm_sec
= 59; /* mis-handle leap seconds */
196 graph_secs
.pos
= tm
->tm_sec
;
197 graph_mins
.pos
= tm
->tm_min
;
198 graph_hrs
.pos
= tm
->tm_hour
;
199 graph_days
.pos
= tm
->tm_mday
- 1;
204 return; /* superfluous rotate */
207 verbosef("time went backwards! (from %u to %u, offset is %d)",
208 (unsigned int)last_time
, (unsigned int)t
, (int)(t
- last_time
));
213 /* else, normal rotation */
217 if (tm
->tm_sec
== 60)
218 tm
->tm_sec
= 59; /* mis-handle leap seconds */
220 /* zero out graphs which have been completely rotated through */
221 for (i
=0; i
<graph_db_size
; i
++)
222 if (td
>= (int)(graph_db
[i
]->num_bars
* graph_db
[i
]->bar_secs
))
223 zero_graph(graph_db
[i
]);
225 /* advance the current position, zeroing up to it */
226 advance(&graph_secs
, tm
->tm_sec
);
227 advance(&graph_mins
, tm
->tm_min
);
228 advance(&graph_hrs
, tm
->tm_hour
);
229 advance(&graph_days
, tm
->tm_mday
- 1);
232 /* ---------------------------------------------------------------------------
233 * Database Import: Grab graphs from a file provided by the caller.
235 * This function will retrieve the data sans the header. We expect the caller
236 * to have validated the header of the segment, and left the file position at
237 * the start of the data.
240 graph_import(const int fd
)
245 if (!read64(fd
, &last
)) return 0;
246 last_time
= (time_t)last
;
248 for (i
=0; i
<graph_db_size
; i
++) {
249 unsigned char num_bars
, pos
;
250 unsigned int filepos
= xtell(fd
);
252 if (!read8(fd
, &num_bars
)) return 0;
253 if (!read8(fd
, &pos
)) return 0;
255 verbosef("at file pos %u, importing graph with %u bars",
256 filepos
, (unsigned int)num_bars
);
258 if (pos
>= num_bars
) {
259 warn("pos is %u, should be < num_bars which is %u",
260 (unsigned int)pos
, (unsigned int)num_bars
);
264 if (graph_db
[i
]->num_bars
!= num_bars
) {
265 warn("num_bars is %u, expecting %u",
266 (unsigned int)num_bars
, graph_db
[i
]->num_bars
);
270 graph_db
[i
]->pos
= pos
;
271 for (j
=0; j
<num_bars
; j
++) {
272 if (!read64(fd
, &(graph_db
[i
]->in
[j
]))) return 0;
273 if (!read64(fd
, &(graph_db
[i
]->out
[j
]))) return 0;
280 /* ---------------------------------------------------------------------------
281 * Database Export: Dump hosts_db into a file provided by the caller.
282 * The caller is responsible for writing out the header first.
285 graph_export(const int fd
)
289 if (!write64(fd
, (uint64_t)last_time
)) return 0;
290 for (i
=0; i
<graph_db_size
; i
++) {
291 if (!write8(fd
, graph_db
[i
]->num_bars
)) return 0;
292 if (!write8(fd
, graph_db
[i
]->pos
)) return 0;
294 for (j
=0; j
<graph_db
[i
]->num_bars
; j
++) {
295 if (!write64(fd
, graph_db
[i
]->in
[j
])) return 0;
296 if (!write64(fd
, graph_db
[i
]->out
[j
])) return 0;
302 /* ---------------------------------------------------------------------------
303 * Web interface: front page!
306 html_front_page(void)
308 struct str
*buf
, *rf
;
310 char start_when
[100];
313 html_open(buf
, "Graphs", /*path_depth=*/0, /*want_graph_js=*/1);
315 str_append(buf
, "<p>\n");
316 str_append(buf
, "<b>Running for</b> <span id=\"rf\">");
317 rf
= length_of_time(now
- start_time
);
318 /* FIXME: use a more monotonic clock perhaps? */
319 str_appendstr(buf
, rf
);
321 str_append(buf
, "</span>");
323 if (strftime(start_when
, sizeof(start_when
),
324 "%Y-%m-%d %H:%M:%S %Z%z", localtime(&start_time
)) != 0)
325 str_appendf(buf
, "<b>, since</b> %s", start_when
);
327 str_appendf(buf
,"<b>.</b><br>\n"
328 "<b>Total</b> <span id=\"tb\">%'qu</span> <b>bytes, "
329 "in</b> <span id=\"tp\">%'qu</span> <b>packets.</b> "
330 "(<span id=\"pc\">%'u</span> <b>captured,</b> "
331 "<span id=\"pd\">%'u</span> <b>dropped)</b><br>\n"
335 cap_pkts_recv
, cap_pkts_drop
);
338 "<div id=\"graphs\">\n"
339 "Graphs require JavaScript.\n"
340 "<script type=\"text/javascript\">\n"
342 "var graph_width = " GRAPH_WIDTH
";\n"
343 "var graph_height = " GRAPH_HEIGHT
";\n"
345 "var graphs_uri = \"graphs.xml\";\n"
349 for (i
=0; i
<graph_db_size
; i
++)
353 "title:\"last %u %s\", "
356 i
, graph_db
[i
]->unit
, graph_db
[i
]->num_bars
, graph_db
[i
]->unit
,
357 graph_db
[i
]->bar_secs
, (i
< graph_db_size
-1) ? "," : "");
358 /* trailing comma breaks on IE, makes the array one element longer */
362 "window.onload = graphs_init;\n"
372 /* ---------------------------------------------------------------------------
373 * Web interface: graphs.xml
379 struct str
*buf
= str_make(), *rf
;
381 str_appendf(buf
, "<graphs tp=\"%qu\" tb=\"%qu\" pc=\"%u\" pd=\"%u\" rf=\"",
382 acct_total_packets
, acct_total_bytes
, cap_pkts_recv
, cap_pkts_drop
);
383 rf
= length_of_time(now
- start_time
);
384 str_appendstr(buf
, rf
);
386 str_append(buf
, "\">\n");
388 for (i
=0; i
<graph_db_size
; i
++) {
389 const struct graph
*g
= graph_db
[i
];
391 str_appendf(buf
, "<%s>\n", g
->unit
);
394 j
= (j
+ 1) % g
->num_bars
;
395 /* <element pos="" in="" out=""/> */
396 str_appendf(buf
, "<e p=\"%u\" i=\"%qu\" o=\"%qu\"/>\n",
397 g
->offset
+ j
, g
->in
[j
], g
->out
[j
]);
398 } while (j
!= g
->pos
);
399 str_appendf(buf
, "</%s>\n", g
->unit
);
401 str_append(buf
, "</graphs>\n");
405 /* vim:set ts=3 sw=3 tw=78 expandtab: */