counters introduced, working

This commit is contained in:
Wolfgang Hottgenroth 2025-02-14 12:23:11 +01:00
parent 2f4aa1c412
commit dd67b4d5db
6 changed files with 89 additions and 4 deletions

View File

@ -305,6 +305,7 @@ client_dispatch(struct ntp_peer *p, u_int8_t settime, u_int8_t automatic)
} else
fatal("recvfrom");
}
counters.recv_client_pkts++;
if (somsg.msg_flags & MSG_TRUNC) {
client_log_error(p, "recvmsg packet", EMSGSIZE);

View File

@ -198,6 +198,7 @@ control_dispatch_msg(struct pollfd *pfd, u_int *ctl_cnt)
struct ctl_show_status c_status;
struct ctl_show_peer c_peer;
struct ctl_show_sensor c_sensor;
struct ntpd_counters c_counters;
int cnt;
ssize_t n;
@ -234,6 +235,11 @@ control_dispatch_msg(struct pollfd *pfd, u_int *ctl_cnt)
imsg_compose(&c->ibuf, IMSG_CTL_SHOW_STATUS, 0, 0, -1,
&c_status, sizeof (c_status));
break;
case IMSG_CTL_SHOW_COUNTERS:
build_show_counters(&c_counters);
imsg_compose(&c->ibuf, IMSG_CTL_SHOW_COUNTERS, 0, 0, -1,
&c_counters, sizeof (c_counters));
break;
case IMSG_CTL_SHOW_PEERS:
cnt = 0;
TAILQ_FOREACH(p, &conf->ntp_peers, entry) {
@ -261,6 +267,10 @@ control_dispatch_msg(struct pollfd *pfd, u_int *ctl_cnt)
imsg_compose(&c->ibuf, IMSG_CTL_SHOW_STATUS, 0, 0, -1,
&c_status, sizeof (c_status));
build_show_counters(&c_counters);
imsg_compose(&c->ibuf, IMSG_CTL_SHOW_COUNTERS, 0, 0, -1,
&c_counters, sizeof (c_counters));
cnt = 0;
TAILQ_FOREACH(p, &conf->ntp_peers, entry) {
build_show_peer(&c_peer, p);
@ -306,6 +316,24 @@ session_socket_nonblockmode(int fd)
fatal("fcntl F_SETFL");
}
void
init_ntpd_counters(struct ntpd_counters *c)
{
c->recv_client_pkts = 0;
c->recv_server_pkts = 0;
c->ignored_server_pkts = 0;
c->answered_server_pkts = 0;
}
void
build_show_counters(struct ntpd_counters *cc)
{
cc->recv_client_pkts = counters.recv_client_pkts;
cc->recv_server_pkts = counters.recv_server_pkts;
cc->ignored_server_pkts = counters.ignored_server_pkts;
cc->answered_server_pkts = counters.answered_server_pkts;
}
void
build_show_status(struct ctl_show_status *cs)
{

4
ntp.c
View File

@ -49,6 +49,8 @@ u_int peer_cnt;
u_int sensors_cnt;
extern u_int constraint_cnt;
struct ntpd_counters counters;
void ntp_sighdlr(int);
int ntp_dispatch_imsg(void);
int ntp_dispatch_imsg_dns(void);
@ -194,6 +196,8 @@ ntp_main(struct ntpd_conf *nconf, struct passwd *pw, int argc, char **argv)
TAILQ_INIT(&ctl_conns);
sensor_init();
init_ntpd_counters(&counters);
log_info("ntp engine ready");
ctl_cnt = 0;

35
ntpd.c
View File

@ -55,6 +55,7 @@ int writefreq(double);
void ctl_main(int, char*[]);
const char *ctl_lookup_option(char *, const char **);
void show_status_msg(struct imsg *);
void show_counters_msg(struct imsg *);
void show_peer_msg(struct imsg *, int);
void show_sensor_msg(struct imsg *, int);
@ -69,7 +70,7 @@ extern u_int constraint_cnt;
const char *showopt;
static const char *ctl_showopt_list[] = {
"peers", "Sensors", "status", "all", NULL
"counters", "peers", "Sensors", "status", "all", NULL
};
void
@ -96,7 +97,7 @@ usage(void)
if (strcmp(__progname, "ntpctl") == 0)
fprintf(stderr,
"usage: ntpctl -s all | peers | Sensors | status\n");
"usage: ntpctl -s all | peers | Sensors | status | counters\n");
else
fprintf(stderr, "usage: %s [-dnv] [-f file]\n",
__progname);
@ -648,6 +649,9 @@ ctl_main(int argc, char *argv[])
case 'a':
action = CTL_SHOW_ALL;
break;
case 'c':
action = CTL_SHOW_COUNTERS;
break;
}
}
if (action == -1)
@ -689,6 +693,10 @@ ctl_main(int argc, char *argv[])
imsg_compose(ibuf_ctl, IMSG_CTL_SHOW_ALL,
0, 0, -1, NULL, 0);
break;
case CTL_SHOW_COUNTERS:
imsg_compose(ibuf_ctl, IMSG_CTL_SHOW_COUNTERS,
0, 0, -1, NULL, 0);
break;
default:
errx(1, "invalid action");
break; /* NOTREACHED */
@ -716,6 +724,10 @@ ctl_main(int argc, char *argv[])
show_status_msg(&imsg);
done = 1;
break;
case CTL_SHOW_COUNTERS:
show_counters_msg(&imsg);
done = 1;
break;
case CTL_SHOW_PEERS:
show_peer_msg(&imsg, 0);
if (imsg.hdr.type ==
@ -733,6 +745,9 @@ ctl_main(int argc, char *argv[])
case IMSG_CTL_SHOW_STATUS:
show_status_msg(&imsg);
break;
case IMSG_CTL_SHOW_COUNTERS:
show_counters_msg(&imsg);
break;
case IMSG_CTL_SHOW_PEERS:
show_peer_msg(&imsg, 1);
break;
@ -777,6 +792,22 @@ ctl_lookup_option(char *cmd, const char **list)
return (item);
}
void
show_counters_msg(struct imsg *imsg)
{
struct ntpd_counters *ccounters;
if (imsg->hdr.len != IMSG_HEADER_SIZE + sizeof(struct ntpd_counters))
fatalx("invalid IMSG_CTL_SHOW_STATUS received");
ccounters = (struct ntpd_counters *)imsg->data;
printf("%lld packets received as client\n", ccounters->recv_client_pkts);
printf("%lld packets received as server\n", ccounters->recv_server_pkts);
printf("%lld packets ignored as server\n", ccounters->ignored_server_pkts);
printf("%lld packets answered as server\n", ccounters->answered_server_pkts);
}
void
show_status_msg(struct imsg *imsg)
{

14
ntpd.h
View File

@ -254,6 +254,15 @@ struct ntpd_conf {
int tmpfail;
};
struct ntpd_counters {
u_int64_t recv_client_pkts;
u_int64_t recv_server_pkts;
u_int64_t ignored_server_pkts;
u_int64_t answered_server_pkts;
};
void init_ntpd_counters(struct ntpd_counters *c);
struct ctl_show_status {
time_t constraint_median;
time_t constraint_last;
@ -316,6 +325,8 @@ enum imsg_type {
IMSG_CTL_SHOW_PEERS_END,
IMSG_CTL_SHOW_SENSORS,
IMSG_CTL_SHOW_SENSORS_END,
IMSG_CTL_SHOW_COUNTERS,
IMSG_CTL_SHOW_COUNTERS_END,
IMSG_CTL_SHOW_ALL,
IMSG_CTL_SHOW_ALL_END,
IMSG_SYNCED,
@ -327,6 +338,7 @@ enum ctl_actions {
CTL_SHOW_STATUS,
CTL_SHOW_PEERS,
CTL_SHOW_SENSORS,
CTL_SHOW_COUNTERS,
CTL_SHOW_ALL
};
@ -344,6 +356,7 @@ time_t scale_interval(time_t);
time_t error_interval(void);
extern struct ntpd_conf *conf;
extern struct ctl_conns ctl_conns;
extern struct ntpd_counters counters;
#define SCALE_INTERVAL(x) MAXIMUM(5, (x) / 10)
@ -428,6 +441,7 @@ struct ctl_conn *control_connbyfd(int);
int control_close(int);
int control_dispatch_msg(struct pollfd *, u_int *);
void session_socket_nonblockmode(int);
void build_show_counters(struct ntpd_counters *);
void build_show_status(struct ctl_show_status *);
void build_show_peer(struct ctl_show_peer *,
struct ntp_peer *);

View File

@ -29,6 +29,7 @@
#include "ntpd.h"
int
setup_listeners(struct servent *se, struct ntpd_conf *lconf, u_int *cnt)
{
@ -169,11 +170,14 @@ server_dispatch(int fd, struct ntpd_conf *lconf)
} else
fatal("recvfrom");
}
counters.recv_server_pkts++;
rectime = gettime_corrected();
if (ntp_getmsg((struct sockaddr *)&fsa, buf, size, &query) == -1)
if (ntp_getmsg((struct sockaddr *)&fsa, buf, size, &query) == -1) {
counters.ignored_server_pkts++;
return (0);
}
memset(&reply, 0, sizeof(reply));
if (lconf->status.synced)
@ -185,8 +189,10 @@ server_dispatch(int fd, struct ntpd_conf *lconf)
reply.status |= MODE_SERVER;
else if ((query.status & MODEMASK) == MODE_SYM_ACT)
reply.status |= MODE_SYM_PAS;
else /* ignore packets of different type (e.g. bcast) */
else { /* ignore packets of different type (e.g. bcast) */
counters.ignored_server_pkts++;
return (0);
}
reply.stratum = lconf->status.stratum;
reply.ppoll = query.ppoll;
@ -199,5 +205,6 @@ server_dispatch(int fd, struct ntpd_conf *lconf)
reply.refid = lconf->status.refid;
ntp_sendmsg(fd, (struct sockaddr *)&fsa, &reply);
counters.answered_server_pkts++;
return (0);
}