Export host statistics in prometheus text format on /metrics.
[darkstat] / hosts_db.h
1 /* darkstat 3
2 * copyright (c) 2001-2014 Emil Mikulic.
3 *
4 * hosts_db.h: database of hosts, ports, protocols.
5 *
6 * You may use, modify and redistribute this file under the terms of the
7 * GNU General Public License version 2. (see COPYING.GPL)
8 */
9 #ifndef __DARKSTAT_HOSTS_DB_H
10 #define __DARKSTAT_HOSTS_DB_H
11
12 #include <sys/types.h> /* for uint64_t */
13
14 #include "addr.h"
15
16 struct hashtable;
17
18 struct host {
19 struct addr addr;
20 char *dns;
21 uint8_t mac_addr[6];
22 /* last_seen_mono is converted to/from time_t in export/import.
23 * It can be negative (due to machine reboots).
24 */
25 int64_t last_seen_mono;
26 struct hashtable *ports_tcp;
27 struct hashtable *ports_tcp_remote;
28 struct hashtable *ports_udp;
29 struct hashtable *ports_udp_remote;
30 struct hashtable *ip_protos;
31 };
32
33 struct port_tcp {
34 uint16_t port;
35 uint64_t syn;
36 };
37
38 struct port_udp {
39 uint16_t port;
40 };
41
42 struct ip_proto {
43 uint8_t proto;
44 };
45
46 struct bucket {
47 struct bucket *next;
48 uint64_t in, out, total;
49 union {
50 struct host host;
51 struct port_tcp port_tcp;
52 struct port_udp port_udp;
53 struct ip_proto ip_proto;
54 } u;
55 };
56
57 enum sort_dir { IN, OUT, TOTAL, LASTSEEN };
58
59 extern int hosts_db_show_macs;
60
61 void hosts_db_init(void);
62 void hosts_db_reduce(void);
63 void hosts_db_reset(void);
64 void hosts_db_free(void);
65 int hosts_db_import(const int fd);
66 int hosts_db_export(const int fd);
67
68 struct bucket *host_find(const struct addr *const a); /* can return NULL */
69 struct bucket *host_get(const struct addr *const a);
70 struct bucket *host_get_port_tcp(struct bucket *host, const uint16_t port);
71 struct bucket *host_get_port_tcp_remote(struct bucket *host,
72 const uint16_t port);
73 struct bucket *host_get_port_udp(struct bucket *host, const uint16_t port);
74 struct bucket *host_get_port_udp_remote(struct bucket *host,
75 const uint16_t port);
76 struct bucket *host_get_ip_proto(struct bucket *host, const uint8_t proto);
77
78 /* Web pages. */
79 struct str *html_hosts(const char *uri, const char *query);
80 struct str *text_metrics();
81
82 /* From hosts_sort */
83 void qsort_buckets(const struct bucket **a, size_t n,
84 size_t left, size_t right, const enum sort_dir d);
85
86 #endif /* __DARKSTAT_HOSTS_DB_H */
87 /* vim:set ts=3 sw=3 tw=78 expandtab: */