dhcpd-pools  3.0
ISC dhcpd lease usage analyser
dhcpd-pools.h
Go to the documentation of this file.
1 /*
2  * The dhcpd-pools has BSD 2-clause license which also known as "Simplified
3  * BSD License" or "FreeBSD License".
4  *
5  * Copyright 2006- Sami Kerola. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the
17  * distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * The views and conclusions contained in the software and documentation are
32  * those of the authors and should not be interpreted as representing
33  * official policies, either expressed or implied, of Sami Kerola.
34  */
35 
40 #ifndef DHCPD_POOLS_H
41 # define DHCPD_POOLS_H 1
42 
43 # include <config.h>
44 # include <arpa/inet.h>
45 # include <stddef.h>
46 # include <stdio.h>
47 # include <string.h>
48 # include <uthash.h>
49 
56 # ifdef HAVE_BUILTIN_EXPECT
57 # define likely(x) __builtin_expect(!!(x), 1)
58 # define unlikely(x) __builtin_expect(!!(x), 0)
59 # else
60 # define likely(x) (x)
61 # define unlikely(x) (x)
62 # endif
63 
69 # if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
70 # define _DP_ATTRIBUTE_HOT __attribute__ ((__hot__))
71 # else
72 # define _DP_ATTRIBUTE_HOT /* empty */
73 # endif
74 
77 union ipaddr_t {
79  unsigned char v6[16];
80 };
81 
89 };
90 
96 enum prefix_t {
106 };
107 
116 };
117 
123  char *name;
124  double available;
125  double used;
126  double touched;
127  double backups;
128  int netmask;
130 };
131 
135 struct range_t {
137  union ipaddr_t first_ip;
138  union ipaddr_t last_ip;
139  double count;
140  double touched;
141  double backups;
142 };
143 
148  int status;
149  double range_size;
150  double percent;
151  double tc;
152  double tcp;
153  double bup;
154 };
155 
159 enum ltype {
163 };
164 
168 struct leases_t {
169  union ipaddr_t ip; /* ip as key */
170  enum ltype type;
171  char *ethernet;
172  UT_hash_handle hh;
173 };
174 
178 enum limbits {
179  R_BIT = (1 << 0),
180  S_BIT = (1 << 1),
181  A_BIT = (1 << 2)
182 };
183 
187 # define STATE_OK 0
188 # define STATE_WARNING 1
189 # define STATE_CRITICAL 2
190 
194 typedef int (*comparer_t) (struct range_t *r1, struct range_t *r2);
195 
199 struct output_sort {
201  struct output_sort *next;
202 };
203 
207 struct conf_t {
210  struct range_t *ranges;
211  unsigned int num_ranges;
212  size_t ranges_size;
213  struct leases_t *leases;
214  enum dhcp_version ip_version;
215  const char *dhcpdconf_file;
216  const char *dhcpdlease_file;
218  struct output_sort *sorts;
219  const char *output_file;
220  const char *mustach_template;
221  double warning;
222  double critical;
223  double warn_count;
224  double crit_count;
225  double minsize;
226  unsigned int
227  reverse_order:1,
228  backups_found:1,
229  snet_alarms:1,
230  perfdata:1,
231  all_as_shared:1,
232  header_limit:4,
233  number_limit:3,
234  skip_ok:1,
235  skip_warning:1,
236  skip_critical:1,
237  skip_minsize:1,
238  skip_suppressed:1,
239  color_mode:2;
240 };
241 
242 /* Function prototypes */
243 
244 /* analyze.c */
245 extern void prepare_data(struct conf_t *state);
246 extern void do_counting(struct conf_t *state);
247 
248 /* getdata.c */
249 extern int parse_leases(struct conf_t *state, const int print_mac_addreses);
250 extern void parse_config(struct conf_t *state, const int is_include,
251  const char *restrict config_file,
252  struct shared_network_t *restrict shared_p);
253 
254 /* hash.c */
255 extern void (*add_lease) (struct conf_t *state, union ipaddr_t *addr, enum ltype type);
256 extern void add_lease_init(struct conf_t *state, union ipaddr_t *addr, enum ltype type);
257 extern void add_lease_v4(struct conf_t *state, union ipaddr_t *addr, enum ltype type);
258 extern void add_lease_v6(struct conf_t *state, union ipaddr_t *addr, enum ltype type);
259 
260 extern struct leases_t *(*find_lease) (struct conf_t *state, union ipaddr_t *addr);
261 extern struct leases_t *find_lease_init(struct conf_t *state, union ipaddr_t *addr);
262 extern struct leases_t *find_lease_v4(struct conf_t *state, union ipaddr_t *addr);
263 extern struct leases_t *find_lease_v6(struct conf_t *state, union ipaddr_t *addr);
264 
265 extern void delete_lease(struct conf_t *state, struct leases_t *lease);
266 extern void delete_all_leases(struct conf_t *state);
267 
268 /* mustach-dhcpd-pools.c */
269 extern int mustach_dhcpd_pools(struct conf_t *state);
270 
271 /* other.c */
272 extern void set_ipv_functions(struct conf_t *state, int version);
273 extern void flip_ranges(struct conf_t *state);
274 extern void clean_up(struct conf_t *state);
275 extern void parse_cidr(struct conf_t *state, struct range_t *range_p, const char *word);
276 extern int parse_color_mode(const char *restrict optarg);
277 extern double strtod_or_err(const char *restrict str, const char *restrict errmesg);
278 extern void __attribute__ ((noreturn)) print_version(void);
279 extern void __attribute__ ((noreturn)) usage(int status);
280 extern void dp_time_tool(FILE *file, const char *path, int epoch);
281 
282 extern int (*parse_ipaddr) (struct conf_t *state, const char *restrict src,
283  union ipaddr_t *restrict dst);
284 extern int parse_ipaddr_init(struct conf_t *state, const char *restrict src,
285  union ipaddr_t *restrict dst);
286 extern int parse_ipaddr_v4(struct conf_t *state, const char *restrict src,
287  union ipaddr_t *restrict dst);
288 extern int parse_ipaddr_v6(struct conf_t *state, const char *restrict src,
289  union ipaddr_t *restrict dst);
290 
291 extern int (*xstrstr) (struct conf_t *state, const char *restrict str);
292 extern int xstrstr_init(struct conf_t *state, const char *restrict str);
293 extern int xstrstr_v4(struct conf_t *state, const char *restrict str);
294 extern int xstrstr_v6(struct conf_t *state, const char *restrict str);
295 
296 extern void (*copy_ipaddr) (union ipaddr_t *restrict dst, const union ipaddr_t *restrict src);
297 extern void copy_ipaddr_init(union ipaddr_t *restrict dst, const union ipaddr_t *restrict src);
298 extern void copy_ipaddr_v4(union ipaddr_t *restrict dst, const union ipaddr_t *restrict src);
299 extern void copy_ipaddr_v6(union ipaddr_t *restrict dst, const union ipaddr_t *restrict src);
300 
301 extern const char *(*ntop_ipaddr) (const union ipaddr_t *ip);
302 extern const char *ntop_ipaddr_init(const union ipaddr_t *ip);
303 extern const char *ntop_ipaddr_v4(const union ipaddr_t *ip);
304 extern const char *ntop_ipaddr_v6(const union ipaddr_t *ip);
305 
306 extern double (*get_range_size) (const struct range_t *r);
307 extern double get_range_size_init(const struct range_t *r);
308 extern double get_range_size_v4(const struct range_t *r);
309 extern double get_range_size_v6(const struct range_t *r);
310 
311 /* output.c */
312 extern int range_output_helper(struct conf_t *state, struct output_helper_t *oh,
313  struct range_t *range_p);
314 extern int shnet_output_helper(struct conf_t *state, struct output_helper_t *oh,
315  struct shared_network_t *shared_p);
316 extern int output_analysis(struct conf_t *state, const char output_format);
317 
318 /* sort.c */
319 extern void mergesort_ranges(struct conf_t *state,
320  struct range_t *restrict orig, unsigned int size,
321  struct range_t *restrict temp, const int root_call);
322 
323 extern int (*leasecomp) (const struct leases_t *restrict a, const struct leases_t *restrict b);
324 extern int leasecomp_init(const struct leases_t *restrict a
325  __attribute__ ((unused)),
326  const struct leases_t *restrict b __attribute__ ((unused)));
327 extern int leasecomp_v4(const struct leases_t *restrict a, const struct leases_t *restrict b);
328 extern int leasecomp_v6(const struct leases_t *restrict a, const struct leases_t *restrict b);
329 
330 extern int (*ipcomp) (const union ipaddr_t *restrict a, const union ipaddr_t *restrict b);
331 extern int ipcomp_init(const union ipaddr_t *restrict a, const union ipaddr_t *restrict b);
332 extern int ipcomp_v4(const union ipaddr_t *restrict a, const union ipaddr_t *restrict b);
333 extern int ipcomp_v6(const union ipaddr_t *restrict a, const union ipaddr_t *restrict b);
334 
335 extern int rangecomp(const void *restrict r1, const void *restrict r2)
336  __attribute__ ((nonnull(1, 2)));
337 
338 extern int comp_cur(struct range_t *r1, struct range_t *r2);
339 extern int comp_double(double f1, double f2);
340 extern int comp_ip(struct range_t *r1, struct range_t *r2);
341 extern int comp_max(struct range_t *r1, struct range_t *r2);
342 extern int comp_percent(struct range_t *r1, struct range_t *r2);
343 extern int comp_tc(struct range_t *r1, struct range_t *r2);
344 extern int comp_tcperc(struct range_t *r1, struct range_t *r2);
345 extern int comp_touched(struct range_t *r1, struct range_t *r2);
346 
347 extern comparer_t field_selector(char c);
348 extern double ret_percent(struct range_t r);
349 extern double ret_tc(struct range_t r);
350 extern double ret_tcperc(struct range_t r);
351 
352 #endif /* DHCPD_POOLS_H */
void __attribute__((noreturn)) print_version(void)
const char * ntop_ipaddr_v6(const union ipaddr_t *ip)
Definition: other.c:308
color_mode
Enumeration whether to use or not color output.
Definition: dhcpd-pools.h:111
int parse_color_mode(const char *restrict optarg)
Parse option argument color mode.
Definition: other.c:479
void delete_all_leases(struct conf_t *state)
Delete all leases from hash array.
Definition: hash.c:130
double range_size
Definition: dhcpd-pools.h:149
prefix_t
Enumeration of interesting data in dhcpd.leases file, that has to be further examined, and saved.
Definition: dhcpd-pools.h:96
Range limit.
Definition: dhcpd-pools.h:179
struct shared_network_t * shared_net
Definition: dhcpd-pools.h:136
int parse_ipaddr_v4(struct conf_t *state, const char *restrict src, union ipaddr_t *restrict dst)
int comp_tcperc(struct range_t *r1, struct range_t *r2)
Compare two range_t by their touched and in use percentage.
Definition: sort.c:182
void set_ipv_functions(struct conf_t *state, int version)
Set function pointers depending on IP version.
Definition: other.c:68
void prepare_data(struct conf_t *state)
Prepare data for analysis.
Definition: analyze.c:50
Shared networks limit.
Definition: dhcpd-pools.h:180
void delete_lease(struct conf_t *state, struct leases_t *lease)
Delete a lease from hash array.
Definition: hash.c:110
int comp_touched(struct range_t *r1, struct range_t *r2)
Compare two range_t by their touched addresses.
Definition: sort.c:164
int(* parse_ipaddr)(struct conf_t *state, const char *restrict src, union ipaddr_t *restrict dst)
Definition: dhcpd-pools.c:59
const char * dhcpdlease_file
Path to dhcpd.leases file.
Definition: dhcpd-pools.h:216
int netmask
Definition: dhcpd-pools.h:128
const char * mustach_template
Mustach template file path.
Definition: dhcpd-pools.h:220
void(* add_lease)(struct conf_t *state, union ipaddr_t *addr, enum ltype type)
Definition: dhcpd-pools.c:66
int comp_ip(struct range_t *r1, struct range_t *r2)
Compare two range_t by their first_ip.
Definition: sort.c:128
double ret_percent(struct range_t r)
Percentage in use in range.
Definition: sort.c:191
struct shared_network_t * shared_net_head
Last entry in shared network linked list.
Definition: dhcpd-pools.h:209
const char * dhcpdconf_file
Path to dhcpd.conf file.
Definition: dhcpd-pools.h:215
int status
Definition: dhcpd-pools.h:148
void add_lease_init(struct conf_t *state, union ipaddr_t *addr, enum ltype type)
int xstrstr_init(struct conf_t *state, const char *restrict str)
Definition: other.c:355
double get_range_size_init(const struct range_t *r)
double percent
Definition: dhcpd-pools.h:150
void copy_ipaddr_v6(union ipaddr_t *restrict dst, const union ipaddr_t *restrict src)
Definition: other.c:279
struct leases_t * find_lease_init(struct conf_t *state, union ipaddr_t *addr)
Definition: dhcpd-pools.h:103
double critical
Critical percent threshold.
Definition: dhcpd-pools.h:222
Definition: dhcpd-pools.h:86
An individual lease.
Definition: dhcpd-pools.h:168
Definition: dhcpd-pools.h:102
char * name
Definition: dhcpd-pools.h:123
double get_range_size_v6(const struct range_t *r)
Definition: other.c:333
int(* comparer_t)(struct range_t *r1, struct range_t *r2)
Function pointer holding sort algorithm.
Definition: dhcpd-pools.h:194
unsigned int num_ranges
Number of ranges in the ranges array.
Definition: dhcpd-pools.h:211
uint32_t v4
Definition: dhcpd-pools.h:78
int leasecomp_v6(const struct leases_t *restrict a, const struct leases_t *restrict b)
Definition: sort.c:99
Linked list of sort functions.
Definition: dhcpd-pools.h:199
double touched
Definition: dhcpd-pools.h:140
Memory space for a binary IP address saving.
Definition: dhcpd-pools.h:77
struct leases_t * find_lease_v6(struct conf_t *state, union ipaddr_t *addr)
Definition: hash.c:100
int leasecomp_v4(const struct leases_t *restrict a, const struct leases_t *restrict b)
Definition: sort.c:90
double warning
Warning percent threshold.
Definition: dhcpd-pools.h:221
Various per range and shared net temporary calculation results.
Definition: dhcpd-pools.h:147
double crit_count
Maximum number of free IP&#39;s before critical.
Definition: dhcpd-pools.h:224
size_t ranges_size
Size of the ranges array.
Definition: dhcpd-pools.h:212
Definition: dhcpd-pools.h:112
Definition: dhcpd-pools.h:101
struct shared_network_t * shared_net_root
First entry in shared network linked list, that is the &#39;all networks&#39;,.
Definition: dhcpd-pools.h:208
void parse_cidr(struct conf_t *state, struct range_t *range_p, const char *word)
Convert a cidr notated address to a range.
Definition: other.c:234
void add_lease_v6(struct conf_t *state, union ipaddr_t *addr, enum ltype type)
Definition: hash.c:71
struct leases_t * leases
An array of individual leases from dhcpd.leases file.
Definition: dhcpd-pools.h:213
int output_analysis(struct conf_t *state, const char output_format)
Return output_format_names enum based on single char input.
Definition: output.c:1183
const char * ntop_ipaddr_v4(const union ipaddr_t *ip)
Definition: other.c:299
Definition: dhcpd-pools.h:97
int parse_ipaddr_init(struct conf_t *state, const char *restrict src, union ipaddr_t *restrict dst)
Convert text string IP address from either IPv4 or IPv6 to an integer.
Definition: other.c:126
Definition: dhcpd-pools.h:161
double tcp
Definition: dhcpd-pools.h:152
Definition: dhcpd-pools.h:99
Definition: dhcpd-pools.h:88
void do_counting(struct conf_t *state)
Perform counting.
Definition: analyze.c:60
__BEGIN_DECLS char * optarg
Definition: getopt.c:89
const char * output_file
Output file path.
Definition: dhcpd-pools.h:219
char * ethernet
Definition: dhcpd-pools.h:171
limbits
Output limit bits.
Definition: dhcpd-pools.h:178
double bup
Definition: dhcpd-pools.h:153
int(* ipcomp)(const union ipaddr_t *restrict a, const union ipaddr_t *restrict b)
Definition: dhcpd-pools.c:64
int(* leasecomp)(const struct leases_t *restrict a, const struct leases_t *restrict b)
Definition: dhcpd-pools.c:65
double(* get_range_size)(const struct range_t *r)
Definition: dhcpd-pools.c:62
All networks summary limit.
Definition: dhcpd-pools.h:181
Counters for an individual range.
Definition: dhcpd-pools.h:135
double backups
Definition: dhcpd-pools.h:141
double warn_count
Maximum number of free IP&#39;s before warning.
Definition: dhcpd-pools.h:223
int rangecomp(const void *restrict r1, const void *restrict r2) __attribute__((nonnull(1
int ipcomp_v6(const union ipaddr_t *restrict a, const union ipaddr_t *restrict b)
Definition: sort.c:74
struct shared_network_t * next
Definition: dhcpd-pools.h:129
void parse_config(struct conf_t *state, const int is_include, const char *restrict config_file, struct shared_network_t *restrict shared_p)
The dhcpd.conf file parser.
Definition: getdata.c:202
int ipcomp_init(const union ipaddr_t *restrict a, const union ipaddr_t *restrict b)
struct output_sort * next
Definition: dhcpd-pools.h:201
int xstrstr_v6(struct conf_t *state, const char *restrict str)
int comp_max(struct range_t *r1, struct range_t *r2)
Compare two range_t by their capacity.
Definition: sort.c:137
double backups
Definition: dhcpd-pools.h:127
comparer_t field_selector(char c)
Sort field selector.
Definition: sort.c:221
Runtime configuration state.
Definition: dhcpd-pools.h:207
Definition: dhcpd-pools.h:113
double ret_tcperc(struct range_t r)
Return percentage of addresses touched and in use in range.
Definition: sort.c:209
Definition: dhcpd-pools.h:87
int(* xstrstr)(struct conf_t *state, const char *restrict str)
Definition: dhcpd-pools.c:63
Definition: dhcpd-pools.h:114
int int comp_cur(struct range_t *r1, struct range_t *r2)
Compare two range_t by their current usage.
Definition: sort.c:146
Definition: dhcpd-pools.h:162
Definition: dhcpd-pools.h:104
double get_range_size_v4(const struct range_t *r)
Definition: other.c:328
Definition: dhcpd-pools.h:105
void clean_up(struct conf_t *state)
Free memory, flush buffers etc.
Definition: other.c:528
Counters for an individual shared network.
Definition: dhcpd-pools.h:122
struct leases_t * find_lease_v4(struct conf_t *state, union ipaddr_t *addr)
Definition: hash.c:92
double ret_tc(struct range_t r)
Touched and in use in range.
Definition: sort.c:200
struct output_sort * sorts
Linked list how to sort ranges.
Definition: dhcpd-pools.h:218
int comp_double(double f1, double f2)
Compare two doubles.
Definition: sort.c:119
void add_lease_v4(struct conf_t *state, union ipaddr_t *addr, enum ltype type)
Definition: hash.c:60
double available
Definition: dhcpd-pools.h:124
void mergesort_ranges(struct conf_t *state, struct range_t *restrict orig, unsigned int size, struct range_t *restrict temp, const int root_call)
Mergesort for range table.
Definition: sort.c:279
Definition: dhcpd-pools.h:100
double tc
Definition: dhcpd-pools.h:151
ltype
Lease state types.
Definition: dhcpd-pools.h:159
unsigned char v6[16]
Definition: dhcpd-pools.h:79
comparer_t func
Definition: dhcpd-pools.h:200
#define uint32_t
Definition: stdint.in.h:168
int parse_leases(struct conf_t *state, const int print_mac_addreses)
Lease file parser.
Definition: getdata.c:82
int parse_ipaddr_v6(struct conf_t *state, const char *restrict src, union ipaddr_t *restrict dst)
int leasecomp_init(const struct leases_t *restrict a __attribute__((unused)), const struct leases_t *restrict b __attribute__((unused)))
Compare IP address in leases_t structure, with IPv4/v6 determination.
Definition: sort.c:84
int mustach_dhcpd_pools(struct conf_t *state)
Start mustach processing.
Definition: mustach-dhcpd-pools.c:399
Default, use colors when output terminal is interactive.
Definition: dhcpd-pools.h:115
int shnet_output_helper(struct conf_t *state, struct output_helper_t *oh, struct shared_network_t *shared_p)
Calculate shared network percentages and such.
Definition: output.c:136
UT_hash_handle hh
Definition: dhcpd-pools.h:172
void copy_ipaddr_init(union ipaddr_t *restrict dst, const union ipaddr_t *restrict src)
unsigned int snet_alarms
Suppress alarming thresholds for ranges that are part of a shared network.
Definition: dhcpd-pools.h:227
void copy_ipaddr_v4(union ipaddr_t *restrict dst, const union ipaddr_t *restrict src)
Definition: other.c:274
double count
Definition: dhcpd-pools.h:139
int output_format
Column to use in color_tags array.
Definition: dhcpd-pools.h:217
int comp_tc(struct range_t *r1, struct range_t *r2)
Compare two range_t by their touched and in use addresses.
Definition: sort.c:173
#define restrict
Definition: config.h:1858
int comp_percent(struct range_t *r1, struct range_t *r2)
Compare two range_t by their current usage percentage.
Definition: sort.c:155
struct range_t * ranges
Array of ranges.
Definition: dhcpd-pools.h:210
dhcp_version
The IP version, IPv4 or IPv6, served by the dhcpd.
Definition: dhcpd-pools.h:85
const char * ntop_ipaddr_init(const union ipaddr_t *ip)
int range_output_helper(struct conf_t *state, struct output_helper_t *oh, struct range_t *range_p)
Calculate range percentages and such.
Definition: output.c:98
int xstrstr_v4(struct conf_t *state, const char *restrict str)
void(* copy_ipaddr)(union ipaddr_t *restrict dst, const union ipaddr_t *restrict src)
Definition: dhcpd-pools.c:60
Definition: dhcpd-pools.h:98
int ipcomp_v4(const union ipaddr_t *restrict a, const union ipaddr_t *restrict b)
Definition: sort.c:65
double minsize
Minimum size of range or shared network to be considered exceeding threshold.
Definition: dhcpd-pools.h:225
double used
Definition: dhcpd-pools.h:125
double strtod_or_err(const char *restrict str, const char *restrict errmesg)
Return a double floating point value.
Definition: other.c:496
void flip_ranges(struct conf_t *state)
Reverse range.
Definition: other.c:515
double touched
Definition: dhcpd-pools.h:126
Definition: dhcpd-pools.h:160
void dp_time_tool(FILE *file, const char *path, int epoch)
Print a time stamp of a path or now to output file.
Definition: other.c:550