Skip to content

Commit 9789c70

Browse files
committed
use timeval for last_update
instead of integer unix timestamps, use timeval to get sub second precision.
1 parent 7000003 commit 9789c70

File tree

17 files changed

+190
-171
lines changed

17 files changed

+190
-171
lines changed

lib/nsutils.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "lnae-utils.h"
22
#include "nsutils.h"
33
#include <sys/types.h>
4+
#include <sys/time.h>
45
#include <stdarg.h>
56
#include <stdio.h>
67

@@ -67,6 +68,28 @@ float tv_delta_f(const struct timeval *start, const struct timeval *stop)
6768
return ret;
6869
}
6970

71+
/* format duration seconds into human readable string */
72+
const char* tv_str(struct timeval *tv) {
73+
return (char *)mkstr("%lu.%06lu", tv->tv_sec, tv->tv_usec);
74+
}
75+
76+
/* Convert string to timeval */
77+
int str2timeval(char *str, struct timeval *tv)
78+
{
79+
char *ptr, *ptr2;
80+
81+
tv->tv_sec = strtoul(str, &ptr, 10);
82+
if (ptr == str) {
83+
tv->tv_sec = tv->tv_usec = 0;
84+
return -1;
85+
}
86+
if (*ptr == '.' || *ptr == ',') {
87+
ptr2 = ptr + 1;
88+
tv->tv_usec = strtoul(ptr2, &ptr, 10);
89+
}
90+
return 0;
91+
}
92+
7093
#define MKSTR_BUFS 256 /* should be plenty */
7194
const char *mkstr(const char *fmt, ...)
7295
{
@@ -111,4 +134,4 @@ void close_standard_fds(void)
111134
open("/dev/null", O_WRONLY);
112135

113136
return;
114-
}
137+
}

lib/nsutils.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#endif
77

88
#include <sys/types.h>
9+
#include <sys/time.h>
910
#include <fcntl.h>
1011

1112
NAGIOS_BEGIN_DECL
@@ -123,6 +124,43 @@ extern int tv_delta_msec(const struct timeval *start, const struct timeval *stop
123124
*/
124125
extern float tv_delta_f(const struct timeval *start, const struct timeval *stop);
125126

127+
/**
128+
* clone source timestamp to destination timeval
129+
* @param tv1 Destination timeval
130+
* @param tv2 Source timeval
131+
* @return nothing
132+
*/
133+
static inline void tv_clone(struct timeval *dst, struct timeval *src)
134+
{
135+
dst->tv_sec = src->tv_sec;
136+
dst->tv_usec = src->tv_usec;
137+
}
138+
139+
/**
140+
* set timestamp to target timeval
141+
* @param tv Target timeval
142+
* @return nothing
143+
*/
144+
static inline void tv_set(struct timeval *timestamp)
145+
{
146+
gettimeofday(timestamp, NULL);
147+
}
148+
149+
/**
150+
* Convert timeval to str
151+
* @param tv Source timeval
152+
* @return A pointer to the formatted string on success.
153+
*/
154+
const char* tv_str(struct timeval *tv);
155+
156+
/**
157+
* Convert string to timeval
158+
* @param str The timeval string (sec.usec)
159+
* @param tv The target timeval
160+
* @return 0 on success, -1 on errors
161+
*/
162+
extern int str2timeval(char *str, struct timeval *tv);
163+
126164
/**
127165
* close and reopen stdin, stdout and stderr to /dev/null
128166
*/

src/naemon/broker.c

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ struct kvvec *get_global_store(void)
1919
return &global_store;
2020
}
2121

22-
/* gets timestamp for use by broker */
23-
static inline void get_broker_timestamp(struct timeval *timestamp)
24-
{
25-
gettimeofday(timestamp, NULL);
26-
}
27-
2822
/******************************************************************/
2923
/************************* EVENT FUNCTIONS ************************/
3024
/******************************************************************/
@@ -43,7 +37,7 @@ void broker_program_state(int type, int flags, int attr)
4337
ds.type = type;
4438
ds.flags = flags;
4539
ds.attr = attr;
46-
get_broker_timestamp(&ds.timestamp);
40+
tv_set(&ds.timestamp);
4741

4842
/* make callbacks */
4943
neb_make_callbacks(NEBCALLBACK_PROCESS_DATA, (void *)&ds);
@@ -64,7 +58,7 @@ void broker_log_data(int type, int flags, int attr, char *data, unsigned long da
6458
ds.type = type;
6559
ds.flags = flags;
6660
ds.attr = attr;
67-
get_broker_timestamp(&ds.timestamp);
61+
tv_set(&ds.timestamp);
6862

6963
ds.entry_time = entry_time;
7064
ds.data_type = data_type;
@@ -92,7 +86,7 @@ void broker_system_command(int type, int flags, int attr, struct timeval start_t
9286
ds.type = type;
9387
ds.flags = flags;
9488
ds.attr = attr;
95-
get_broker_timestamp(&ds.timestamp);
89+
tv_set(&ds.timestamp);
9690

9791
ds.start_time = start_time;
9892
ds.end_time = end_time;
@@ -138,7 +132,7 @@ int broker_event_handler(int type, int flags, int attr, int eventhandler_type, v
138132
ds.type = type;
139133
ds.flags = flags;
140134
ds.attr = attr;
141-
get_broker_timestamp(&ds.timestamp);
135+
tv_set(&ds.timestamp);
142136

143137
ds.eventhandler_type = eventhandler_type;
144138
if (eventhandler_type == SERVICE_EVENTHANDLER || eventhandler_type == GLOBAL_SERVICE_EVENTHANDLER) {
@@ -199,7 +193,7 @@ int broker_host_check(int type, int flags, int attr, host *hst, int check_type,
199193
ds.type = type;
200194
ds.flags = flags;
201195
ds.attr = attr;
202-
get_broker_timestamp(&ds.timestamp);
196+
tv_set(&ds.timestamp);
203197

204198
ds.host_name = hst->name;
205199
ds.object_ptr = (void *)hst;
@@ -259,7 +253,7 @@ int broker_service_check(int type, int flags, int attr, service *svc, int check_
259253
ds.type = type;
260254
ds.flags = flags;
261255
ds.attr = attr;
262-
get_broker_timestamp(&ds.timestamp);
256+
tv_set(&ds.timestamp);
263257

264258
ds.host_name = svc->host_name;
265259
ds.service_description = svc->description;
@@ -306,7 +300,7 @@ void broker_comment_data(int type, int flags, int attr, int comment_type, int en
306300
ds.type = type;
307301
ds.flags = flags;
308302
ds.attr = attr;
309-
get_broker_timestamp(&ds.timestamp);
303+
tv_set(&ds.timestamp);
310304

311305
ds.comment_type = comment_type;
312306
ds.entry_type = entry_type;
@@ -341,7 +335,7 @@ void broker_downtime_data(int type, int flags, int attr, int downtime_type, char
341335
ds.type = type;
342336
ds.flags = flags;
343337
ds.attr = attr;
344-
get_broker_timestamp(&ds.timestamp);
338+
tv_set(&ds.timestamp);
345339

346340
ds.downtime_type = downtime_type;
347341
ds.host_name = host_name;
@@ -381,7 +375,7 @@ void broker_flapping_data(int type, int flags, int attr, int flapping_type, void
381375
ds.type = type;
382376
ds.flags = flags;
383377
ds.attr = attr;
384-
get_broker_timestamp(&ds.timestamp);
378+
tv_set(&ds.timestamp);
385379

386380
ds.flapping_type = flapping_type;
387381
if (flapping_type == SERVICE_FLAPPING) {
@@ -419,7 +413,7 @@ void broker_program_status(int type, int flags, int attr)
419413
ds.type = type;
420414
ds.flags = flags;
421415
ds.attr = attr;
422-
get_broker_timestamp(&ds.timestamp);
416+
tv_set(&ds.timestamp);
423417

424418
ds.program_start = program_start;
425419
ds.pid = nagios_pid;
@@ -461,8 +455,8 @@ void broker_host_status(int type, int flags, int attr, host *hst)
461455
ds.type = type;
462456
ds.flags = flags;
463457
ds.attr = attr;
464-
get_broker_timestamp(&ds.timestamp);
465-
hst->last_update = ds.timestamp.tv_sec;
458+
tv_set(&ds.timestamp);
459+
tv_clone(&hst->last_update, &ds.timestamp);
466460

467461
ds.object_ptr = (void *)hst;
468462

@@ -485,8 +479,8 @@ void broker_service_status(int type, int flags, int attr, service *svc)
485479
ds.type = type;
486480
ds.flags = flags;
487481
ds.attr = attr;
488-
get_broker_timestamp(&ds.timestamp);
489-
svc->last_update = ds.timestamp.tv_sec;
482+
tv_set(&ds.timestamp);
483+
tv_clone(&svc->last_update, &ds.timestamp);
490484

491485
ds.object_ptr = (void *)svc;
492486

@@ -509,7 +503,7 @@ void broker_contact_status(int type, int flags, int attr, contact *cntct)
509503
ds.type = type;
510504
ds.flags = flags;
511505
ds.attr = attr;
512-
get_broker_timestamp(&ds.timestamp);
506+
tv_set(&ds.timestamp);
513507

514508
ds.object_ptr = (void *)cntct;
515509

@@ -534,7 +528,7 @@ neb_cb_resultset *broker_notification_data(int type, int flags, int attr, int no
534528
ds.type = type;
535529
ds.flags = flags;
536530
ds.attr = attr;
537-
get_broker_timestamp(&ds.timestamp);
531+
tv_set(&ds.timestamp);
538532

539533
ds.notification_type = notification_type;
540534
ds.start_time = start_time;
@@ -578,7 +572,7 @@ int broker_contact_notification_data(int type, int flags, int attr, int notifica
578572
ds.type = type;
579573
ds.flags = flags;
580574
ds.attr = attr;
581-
get_broker_timestamp(&ds.timestamp);
575+
tv_set(&ds.timestamp);
582576

583577
ds.notification_type = notification_type;
584578
ds.start_time = start_time;
@@ -638,7 +632,7 @@ int broker_contact_notification_method_data(int type, int flags, int attr, int n
638632
ds.type = type;
639633
ds.flags = flags;
640634
ds.attr = attr;
641-
get_broker_timestamp(&ds.timestamp);
635+
tv_set(&ds.timestamp);
642636

643637
ds.notification_type = notification_type;
644638
ds.start_time = start_time;
@@ -689,7 +683,7 @@ void broker_adaptive_program_data(int type, int flags, int attr, int command_typ
689683
ds.type = type;
690684
ds.flags = flags;
691685
ds.attr = attr;
692-
get_broker_timestamp(&ds.timestamp);
686+
tv_set(&ds.timestamp);
693687

694688
ds.command_type = command_type;
695689
ds.modified_host_attribute = modhattr;
@@ -716,7 +710,7 @@ void broker_adaptive_host_data(int type, int flags, int attr, host *hst, int com
716710
ds.type = type;
717711
ds.flags = flags;
718712
ds.attr = attr;
719-
get_broker_timestamp(&ds.timestamp);
713+
tv_set(&ds.timestamp);
720714

721715
ds.command_type = command_type;
722716
ds.modified_attribute = modattr;
@@ -742,7 +736,7 @@ void broker_adaptive_service_data(int type, int flags, int attr, service *svc, i
742736
ds.type = type;
743737
ds.flags = flags;
744738
ds.attr = attr;
745-
get_broker_timestamp(&ds.timestamp);
739+
tv_set(&ds.timestamp);
746740

747741
ds.command_type = command_type;
748742
ds.modified_attribute = modattr;
@@ -768,7 +762,7 @@ void broker_adaptive_contact_data(int type, int flags, int attr, contact *cntct,
768762
ds.type = type;
769763
ds.flags = flags;
770764
ds.attr = attr;
771-
get_broker_timestamp(&ds.timestamp);
765+
tv_set(&ds.timestamp);
772766

773767
ds.command_type = command_type;
774768
ds.modified_attribute = modattr;
@@ -798,7 +792,7 @@ int broker_external_command(int type, int flags, int attr, int command_type, tim
798792
ds.type = type;
799793
ds.flags = flags;
800794
ds.attr = attr;
801-
get_broker_timestamp(&ds.timestamp);
795+
tv_set(&ds.timestamp);
802796

803797
ds.command_type = command_type;
804798
ds.entry_time = entry_time;
@@ -822,7 +816,7 @@ void broker_aggregated_status_data(int type, int flags, int attr)
822816
ds.type = type;
823817
ds.flags = flags;
824818
ds.attr = attr;
825-
get_broker_timestamp(&ds.timestamp);
819+
tv_set(&ds.timestamp);
826820

827821
/* make callbacks */
828822
neb_make_callbacks(NEBCALLBACK_AGGREGATED_STATUS_DATA, (void *)&ds);
@@ -843,7 +837,7 @@ void broker_retention_data(int type, int flags, int attr)
843837
ds.type = type;
844838
ds.flags = flags;
845839
ds.attr = attr;
846-
get_broker_timestamp(&ds.timestamp);
840+
tv_set(&ds.timestamp);
847841

848842
/* make callbacks */
849843
neb_make_callbacks(NEBCALLBACK_RETENTION_DATA, (void *)&ds);
@@ -866,7 +860,7 @@ void broker_acknowledgement_data(int type, int flags, int attr, int acknowledgem
866860
ds.type = type;
867861
ds.flags = flags;
868862
ds.attr = attr;
869-
get_broker_timestamp(&ds.timestamp);
863+
tv_set(&ds.timestamp);
870864

871865
ds.acknowledgement_type = acknowledgement_type;
872866
if (acknowledgement_type == SERVICE_ACKNOWLEDGEMENT) {
@@ -909,7 +903,7 @@ void broker_statechange_data(int type, int flags, int attr, int statechange_type
909903
ds.type = type;
910904
ds.flags = flags;
911905
ds.attr = attr;
912-
get_broker_timestamp(&ds.timestamp);
906+
tv_set(&ds.timestamp);
913907

914908
ds.statechange_type = statechange_type;
915909
if (statechange_type == SERVICE_STATECHANGE) {

0 commit comments

Comments
 (0)