mirror of
git://git.code.sf.net/p/dhcpd-pools/code
synced 2025-12-15 23:36:59 +00:00
output: make output_analysis() to be regular function
This commit makes it possible to define alarming thresholds at the same time with other output formats. Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
parent
9deeae8c36
commit
abf5d04736
4 changed files with 73 additions and 74 deletions
|
|
@ -73,7 +73,6 @@ double (*get_range_size) (const struct range_t *r);
|
|||
int (*xstrstr) (const char *restrict str);
|
||||
int (*ipcomp) (const union ipaddr_t *restrict a, const union ipaddr_t *restrict b);
|
||||
int (*leasecomp) (const struct leases_t *restrict a, const struct leases_t *restrict b);
|
||||
int (*output_analysis) (void);
|
||||
void (*add_lease) (union ipaddr_t *ip, enum ltype type);
|
||||
struct leases_t *(*find_lease) (union ipaddr_t *ip);
|
||||
|
||||
|
|
@ -99,8 +98,9 @@ static int return_limit(const char c)
|
|||
int main(int argc, char **argv)
|
||||
{
|
||||
int option_index = 0;
|
||||
char output_format = '\0';
|
||||
int alarming = 0;
|
||||
char const *tmp;
|
||||
const char *print_mac_addreses_tmp;
|
||||
struct range_t *tmp_ranges;
|
||||
enum {
|
||||
OPT_SNET_ALARMS = CHAR_MAX + 1,
|
||||
|
|
@ -159,8 +159,6 @@ int main(int argc, char **argv)
|
|||
config.header_limit = (*tmp - '0');
|
||||
tmp++;
|
||||
config.number_limit = (*tmp - '0');
|
||||
/* Make sure some output format is selected by default */
|
||||
print_mac_addreses_tmp = OUTPUT_FORMAT;
|
||||
/* Default sort order is by IPs small to big */
|
||||
config.reverse_order = 0;
|
||||
config.backups_found = 0;
|
||||
|
|
@ -186,7 +184,7 @@ int main(int argc, char **argv)
|
|||
break;
|
||||
case 'f':
|
||||
/* Output format */
|
||||
print_mac_addreses_tmp = optarg;
|
||||
output_format = optarg[0];
|
||||
break;
|
||||
case 's':
|
||||
{
|
||||
|
|
@ -225,19 +223,19 @@ int main(int argc, char **argv)
|
|||
config.snet_alarms = 1;
|
||||
break;
|
||||
case OPT_WARN:
|
||||
print_mac_addreses_tmp = "a";
|
||||
alarming = 1;
|
||||
config.warning = strtod_or_err(optarg, "illegal argument");
|
||||
break;
|
||||
case OPT_CRIT:
|
||||
print_mac_addreses_tmp = "a";
|
||||
alarming = 1;
|
||||
config.critical = strtod_or_err(optarg, "illegal argument");
|
||||
break;
|
||||
case OPT_WARN_COUNT:
|
||||
print_mac_addreses_tmp = "a";
|
||||
alarming = 1;
|
||||
config.warn_count = strtod_or_err(optarg, "illegal argument");
|
||||
break;
|
||||
case OPT_CRIT_COUNT:
|
||||
print_mac_addreses_tmp = "a";
|
||||
alarming = 1;
|
||||
config.crit_count = strtod_or_err(optarg, "illegal argument");
|
||||
break;
|
||||
case OPT_MINSIZE:
|
||||
|
|
@ -274,50 +272,22 @@ int main(int argc, char **argv)
|
|||
program_name);
|
||||
}
|
||||
}
|
||||
/* Output function selection */
|
||||
switch (print_mac_addreses_tmp[0]) {
|
||||
case 't':
|
||||
output_analysis = output_txt;
|
||||
config.print_mac_addreses = 0;
|
||||
break;
|
||||
case 'a':
|
||||
output_analysis = output_alarming;
|
||||
config.print_mac_addreses = 0;
|
||||
break;
|
||||
case 'h':
|
||||
error(EXIT_FAILURE, 0, "html table only output format is deprecated");
|
||||
break;
|
||||
case 'H':
|
||||
output_analysis = output_html;
|
||||
config.print_mac_addreses = 0;
|
||||
break;
|
||||
case 'x':
|
||||
output_analysis = output_xml;
|
||||
config.print_mac_addreses = 0;
|
||||
break;
|
||||
case 'X':
|
||||
output_analysis = output_xml;
|
||||
config.print_mac_addreses = 1;
|
||||
break;
|
||||
case 'j':
|
||||
output_analysis = output_json;
|
||||
config.print_mac_addreses = 0;
|
||||
break;
|
||||
case 'J':
|
||||
output_analysis = output_json;
|
||||
config.print_mac_addreses = 1;
|
||||
break;
|
||||
case 'c':
|
||||
output_analysis = output_csv;
|
||||
config.print_mac_addreses = 0;
|
||||
break;
|
||||
default:
|
||||
clean_up();
|
||||
error(EXIT_FAILURE, 0, "unknown output format: %s", quote(print_mac_addreses_tmp));
|
||||
/* Output format is not defined, if alarm thresholds are then it's
|
||||
* alarming, else use the default. */
|
||||
if (output_format == '\0') {
|
||||
if (alarming == 1)
|
||||
output_format = 'a';
|
||||
else {
|
||||
const char *const def = OUTPUT_FORMAT;
|
||||
output_format = def[0];
|
||||
}
|
||||
}
|
||||
/* Do the job */
|
||||
parse_config(1, config.dhcpdconf_file, shared_networks);
|
||||
parse_leases();
|
||||
if (output_format == 'X' || output_format == 'J')
|
||||
parse_leases(1);
|
||||
else
|
||||
parse_leases(0);
|
||||
prepare_data();
|
||||
do_counting();
|
||||
tmp_ranges = xmalloc(sizeof(struct range_t) * num_ranges);
|
||||
|
|
@ -326,7 +296,7 @@ int main(int argc, char **argv)
|
|||
if (config.reverse_order == 1)
|
||||
flip_ranges(ranges, tmp_ranges);
|
||||
free(tmp_ranges);
|
||||
ret_val = output_analysis();
|
||||
ret_val = output_analysis(output_format);
|
||||
clean_up();
|
||||
return (ret_val);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -186,6 +186,7 @@ struct configuration_t {
|
|||
enum dhcp_version ip_version;
|
||||
char *dhcpdconf_file;
|
||||
char *dhcpdlease_file;
|
||||
int output_format;
|
||||
struct output_sort *sorts;
|
||||
char *output_file;
|
||||
double warning;
|
||||
|
|
@ -197,7 +198,6 @@ struct configuration_t {
|
|||
reverse_order:1,
|
||||
backups_found:1,
|
||||
snet_alarms:1,
|
||||
print_mac_addreses:1,
|
||||
perfdata:1,
|
||||
all_as_shared:1,
|
||||
header_limit:3,
|
||||
|
|
@ -224,7 +224,7 @@ extern unsigned int RANGES;
|
|||
/* Function prototypes */
|
||||
extern void prepare_memory(void);
|
||||
extern void set_ipv_functions(int version);
|
||||
extern int parse_leases(void);
|
||||
extern int parse_leases(const int print_mac_addreses);
|
||||
extern void parse_config(int, const char *restrict, struct shared_network_t *restrict)
|
||||
__attribute__ ((nonnull(2, 3)));
|
||||
extern void prepare_data(void);
|
||||
|
|
@ -301,14 +301,8 @@ extern double ret_tcperc(struct range_t r);
|
|||
extern void mergesort_ranges(struct range_t *restrict orig, int size,
|
||||
struct range_t *restrict temp)
|
||||
__attribute__ ((nonnull(1, 3)));
|
||||
/* output function pointer and functions */
|
||||
extern int (*output_analysis) (void);
|
||||
extern int output_txt(void);
|
||||
extern int output_html(void);
|
||||
extern int output_xml(void);
|
||||
extern int output_json(void);
|
||||
extern int output_csv(void);
|
||||
extern int output_alarming(void);
|
||||
/* output function */
|
||||
extern int output_analysis(const char);
|
||||
/* Memory release, file closing etc */
|
||||
extern void clean_up(void);
|
||||
/* Hash functions */
|
||||
|
|
|
|||
|
|
@ -58,13 +58,12 @@
|
|||
|
||||
/*! \brief Lease file parser. The parser can only read ISC DHCPD
|
||||
* dhcpd.leases file format. */
|
||||
int parse_leases(void)
|
||||
int parse_leases(const int print_mac_addreses)
|
||||
{
|
||||
FILE *dhcpd_leases;
|
||||
char *line, *ipstring, macstring[20], *stop;
|
||||
union ipaddr_t addr;
|
||||
struct stat lease_file_stats;
|
||||
int ethernets = 0; /* boolean */
|
||||
struct leases_t *lease;
|
||||
|
||||
dhcpd_leases = fopen(config.dhcpdlease_file, "r");
|
||||
|
|
@ -86,8 +85,6 @@ int parse_leases(void)
|
|||
line[0] = '\0';
|
||||
ipstring = xmalloc(sizeof(char) * MAXLEN);
|
||||
ipstring[0] = '\0';
|
||||
if (config.print_mac_addreses == 1)
|
||||
ethernets = 1;
|
||||
while (!feof(dhcpd_leases)) {
|
||||
if (!fgets(line, MAXLEN, dhcpd_leases) && ferror(dhcpd_leases))
|
||||
error(EXIT_FAILURE, errno, "parse_leases: %s", config.dhcpdlease_file);
|
||||
|
|
@ -126,7 +123,7 @@ int parse_leases(void)
|
|||
config.backups_found = 1;
|
||||
break;
|
||||
case PREFIX_HARDWARE_ETHERNET:
|
||||
if (ethernets == 0)
|
||||
if (print_mac_addreses == 0)
|
||||
break;
|
||||
memcpy(macstring, line + 20, 17);
|
||||
macstring[17] = '\0';
|
||||
|
|
|
|||
54
src/output.c
54
src/output.c
|
|
@ -59,7 +59,7 @@
|
|||
#include "dhcpd-pools.h"
|
||||
|
||||
/*! \brief Text output format, which is the default. */
|
||||
int output_txt(void)
|
||||
static int output_txt(void)
|
||||
{
|
||||
unsigned int i;
|
||||
struct range_t *range_p;
|
||||
|
|
@ -214,7 +214,7 @@ int output_txt(void)
|
|||
}
|
||||
|
||||
/*! \brief The xml output formats. */
|
||||
int output_xml(void)
|
||||
static int output_xml(const int print_mac_addreses)
|
||||
{
|
||||
unsigned int i;
|
||||
struct range_t *range_p;
|
||||
|
|
@ -238,7 +238,7 @@ int output_xml(void)
|
|||
|
||||
fprintf(outfile, "<dhcpstatus>\n");
|
||||
|
||||
if (config.print_mac_addreses == 1) {
|
||||
if (print_mac_addreses == 1) {
|
||||
struct leases_t *l;
|
||||
|
||||
for (l = leases; l != NULL; l = l->hh.next) {
|
||||
|
|
@ -317,7 +317,7 @@ int output_xml(void)
|
|||
}
|
||||
|
||||
/*! \brief The json output formats. */
|
||||
int output_json(void)
|
||||
static int output_json(const int print_mac_addreses)
|
||||
{
|
||||
unsigned int i = 0;
|
||||
struct range_t *range_p;
|
||||
|
|
@ -343,7 +343,7 @@ int output_json(void)
|
|||
|
||||
fprintf(outfile, "{\n");
|
||||
|
||||
if (config.print_mac_addreses == 1) {
|
||||
if (print_mac_addreses == 1) {
|
||||
struct leases_t *l;
|
||||
|
||||
fprintf(outfile, " \"active_leases\": [");
|
||||
|
|
@ -595,7 +595,7 @@ static void newsection(FILE *restrict f, char const *restrict title)
|
|||
}
|
||||
|
||||
/*! \brief Output html format. */
|
||||
int output_html(void)
|
||||
static int output_html(void)
|
||||
{
|
||||
unsigned int i;
|
||||
struct range_t *range_p;
|
||||
|
|
@ -783,7 +783,7 @@ int output_html(void)
|
|||
}
|
||||
|
||||
/*! \brief Output cvs format. */
|
||||
int output_csv(void)
|
||||
static int output_csv(void)
|
||||
{
|
||||
unsigned int i;
|
||||
struct range_t *range_p;
|
||||
|
|
@ -925,7 +925,7 @@ int output_csv(void)
|
|||
}
|
||||
|
||||
/*! \brief Output alarm text, and return program exit value. */
|
||||
int output_alarming(void)
|
||||
static int output_alarming(void)
|
||||
{
|
||||
FILE *outfile;
|
||||
struct range_t *range_p;
|
||||
|
|
@ -1093,3 +1093,41 @@ int output_alarming(void)
|
|||
}
|
||||
return ret_val;
|
||||
}
|
||||
|
||||
/*! \brief Return output_format_names enum based on single char input. */
|
||||
int output_analysis(const char c)
|
||||
{
|
||||
int ret = 1;
|
||||
switch (c) {
|
||||
case 't':
|
||||
ret = output_txt();
|
||||
break;
|
||||
case 'a':
|
||||
ret = output_alarming();
|
||||
break;
|
||||
case 'h':
|
||||
error(EXIT_FAILURE, 0, "html table only output format is deprecated");
|
||||
break;
|
||||
case 'H':
|
||||
ret = output_html();
|
||||
break;
|
||||
case 'x':
|
||||
ret = output_xml(0);
|
||||
break;
|
||||
case 'X':
|
||||
ret = output_xml(1);
|
||||
break;
|
||||
case 'j':
|
||||
ret = output_json(0);
|
||||
break;
|
||||
case 'J':
|
||||
ret = output_json(1);
|
||||
break;
|
||||
case 'c':
|
||||
ret = output_csv();
|
||||
break;
|
||||
default:
|
||||
error(EXIT_FAILURE, 0, "unknown output format: '%c'", c);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue