aboutsummaryrefslogtreecommitdiff
path: root/src/ping903.h
blob: e24e3565973571f4bcf7e1462d5af9526055456e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* This file is part of Ping903
   Copyright (C) 2020 Sergey Poznyakoff
  
   Ping903 is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
  
   Ping903 is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the GNU General Public License
   along with Ping903.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <pthread.h>
#include <stdarg.h>
#include <microhttpd.h>

void ping903(void);


/* Configuration file parser */

/* Callback mode */
enum {
	CF_PARSE,      /* parse input statement */
	CF_SERIALIZE   /* serialize configuration setting */
};

union cf_callback_arg {
	struct json_value *output;  /* Output value */
	struct cf_line {
		char const *val;    /* Statement value (after keyword) */
		char const *file;   /* Statement location: file name */
		unsigned line;      /*     ...  and input line number */
	} input;
};

enum {
	CF_RET_OK,
	CF_RET_FAIL,
	CF_RET_IGNORE
};

/* Configuration parser callback function */
typedef int (*CF_CALLBACK)(int mode, union cf_callback_arg *arg, void *data);

/* Built-in statement types */
enum {
	STMT_T_STRING,
	STMT_T_ULONG,
	STMT_T_BOOL,
	STMT_T_CALLBACK
};

int readconfig(char const *file);
int cf_trusted_ip(int mode, union cf_callback_arg *arg, void *data);
int cf_syslog_facility(int mode, union cf_callback_arg *arg, void *data);
void hostping_add(char const *name, struct sockaddr *addr, socklen_t addrlen);
int hostping_delete_by_name(char const *name);
int hostping_add_name(char const *name);

char *get_remote_ip(struct MHD_Connection *conn);

enum {
	HOST_STAT_INIT,      /* Initial state: data are being collected */
	HOST_STAT_VALID,     /* The record is valid */
	HOST_STAT_PENDING,   /* The record is valid; new data are being
				collected */
	HOST_STAT_INVALID    /* The record is invalid (host unreachable) */
};

struct host_stat {
	int status;
	struct timeval start_tv;
	struct timeval stop_tv;
	unsigned long xmit_count;
	unsigned long recv_count;
	double tmin;             /* minimum round trip time */
	double tmax;             /* maximum round trip time */
	double avg;
	double stddev;
};

static inline int host_stat_is_valid(struct host_stat const *hs) {
	return hs->status == HOST_STAT_VALID || hs->status == HOST_STAT_PENDING;
}

typedef struct hostping {
	char *name;
	struct sockaddr *addr;
	socklen_t addrlen;

	pthread_mutex_t mutex;
	
	/* Current ping statistics */
	struct timeval start_tv;
	struct timeval xmit_tv;
	struct timeval recv_tv;
	unsigned long xmit_count;
	unsigned long recv_count;
	double tmin;             /* minimum round trip time */
	double tmax;             /* maximum round trip time */
	double tsum;             /* sum of all times, for doing average */
	double tsumsq;           /* sum of all times squared, for std. dev. */

	/* Last probe statistics */
	struct host_stat stat_last;

	struct hostping *prev, *next;
} HOSTPING;

extern int verbose;
extern int fatal_signals[];
extern char *httpd_addr;
extern int httpd_access_log;
extern int httpd_log_verbose;
extern char *pidfile;
extern unsigned long probe_interval;
extern unsigned long ping_interval;
extern unsigned long ping_count;
extern unsigned long ping_tolerance;
extern size_t data_length;

struct json_value *config_to_json(void);
int get_hostname_stat(char const *name, struct json_value **retval);
int get_ipaddr_stat(struct sockaddr *sa, int salen, struct json_value **retval);
int get_all_hosts(struct json_value **);
int get_all_host_stat(struct json_value **);
struct addrinfo;
int get_host_matches(struct addrinfo **aip, struct json_value **retval);

int file_read_ip_list(FILE *fp, char const *fname);

void p903_init(void);
void *p903_sender(void *p);
void *p903_receiver(void *p);
void *p903_scheduler(void *p);

Return to:

Send suggestions and report system problems to the System administrator.