mirror of
https://git.proxmox.com/git/mirror_frr
synced 2025-05-04 19:43:17 +00:00
mgmtd: simplify commit id to just be a timeval string
Also fixes coverity warning Signed-off-by: Christian Hopps <chopps@labn.net>
This commit is contained in:
parent
27797fec2c
commit
d31d24c488
12
mgmtd/mgmt.h
12
mgmtd/mgmt.h
@ -108,16 +108,4 @@ extern void mgmt_master_init(struct event_loop *master, const int buffer_size);
|
|||||||
extern void mgmt_init(void);
|
extern void mgmt_init(void);
|
||||||
extern void mgmt_vty_init(void);
|
extern void mgmt_vty_init(void);
|
||||||
|
|
||||||
static inline char *mgmt_realtime_to_string(struct timeval *tv, char *buf,
|
|
||||||
size_t sz)
|
|
||||||
{
|
|
||||||
struct tm tm;
|
|
||||||
size_t n;
|
|
||||||
|
|
||||||
localtime_r((const time_t *)&tv->tv_sec, &tm);
|
|
||||||
n = strftime(buf, sz, "%Y-%m-%dT%H:%M:%S", &tm);
|
|
||||||
snprintf(&buf[n], sz - n, ",%06u000", (unsigned int)tv->tv_usec);
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif /* _FRR_MGMTD_H */
|
#endif /* _FRR_MGMTD_H */
|
||||||
|
@ -28,12 +28,9 @@
|
|||||||
for ((id) = MGMTD_DS_NONE; (id) < MGMTD_DS_MAX_ID; (id)++)
|
for ((id) = MGMTD_DS_NONE; (id) < MGMTD_DS_MAX_ID; (id)++)
|
||||||
|
|
||||||
#define MGMTD_MAX_COMMIT_LIST 10
|
#define MGMTD_MAX_COMMIT_LIST 10
|
||||||
#define MGMTD_MD5_HASH_LEN 16
|
|
||||||
#define MGMTD_MD5_HASH_STR_HEX_LEN 33
|
|
||||||
|
|
||||||
#define MGMTD_COMMIT_FILE_PATH DAEMON_DB_DIR "/commit-%s.json"
|
#define MGMTD_COMMIT_FILE_PATH DAEMON_DB_DIR "/commit-%s.json"
|
||||||
#define MGMTD_COMMIT_INDEX_FILE_NAME DAEMON_DB_DIR "/commit-index.dat"
|
#define MGMTD_COMMIT_INDEX_FILE_NAME DAEMON_DB_DIR "/commit-index.dat"
|
||||||
#define MGMTD_COMMIT_TIME_STR_LEN 100
|
|
||||||
|
|
||||||
extern struct nb_config *running_config;
|
extern struct nb_config *running_config;
|
||||||
|
|
||||||
|
@ -1714,7 +1714,7 @@ static void
|
|||||||
mgmt_fe_adapter_cmt_stats_write(struct vty *vty,
|
mgmt_fe_adapter_cmt_stats_write(struct vty *vty,
|
||||||
struct mgmt_fe_client_adapter *adapter)
|
struct mgmt_fe_client_adapter *adapter)
|
||||||
{
|
{
|
||||||
char buf[100] = {0};
|
char buf[MGMT_LONG_TIME_MAX_LEN];
|
||||||
|
|
||||||
if (!mm->perf_stats_en)
|
if (!mm->perf_stats_en)
|
||||||
return;
|
return;
|
||||||
@ -1795,7 +1795,7 @@ static void
|
|||||||
mgmt_fe_adapter_setcfg_stats_write(struct vty *vty,
|
mgmt_fe_adapter_setcfg_stats_write(struct vty *vty,
|
||||||
struct mgmt_fe_client_adapter *adapter)
|
struct mgmt_fe_client_adapter *adapter)
|
||||||
{
|
{
|
||||||
char buf[100] = {0};
|
char buf[MGMT_LONG_TIME_MAX_LEN];
|
||||||
|
|
||||||
if (!mm->perf_stats_en)
|
if (!mm->perf_stats_en)
|
||||||
return;
|
return;
|
||||||
|
@ -18,8 +18,8 @@
|
|||||||
struct mgmt_cmt_info_t {
|
struct mgmt_cmt_info_t {
|
||||||
struct mgmt_cmt_infos_item cmts;
|
struct mgmt_cmt_infos_item cmts;
|
||||||
|
|
||||||
char cmtid_str[MGMTD_MD5_HASH_STR_HEX_LEN];
|
char cmtid_str[MGMT_SHORT_TIME_MAX_LEN];
|
||||||
char time_str[MGMTD_COMMIT_TIME_STR_LEN];
|
char time_str[MGMT_LONG_TIME_MAX_LEN];
|
||||||
char cmt_json_file[PATH_MAX];
|
char cmt_json_file[PATH_MAX];
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -54,36 +54,30 @@ static void mgmt_history_remove_file(char *name)
|
|||||||
zlog_err("Old commit info deletion failed");
|
zlog_err("Old commit info deletion failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mgmt_history_hash(const char *input_str, char *hash)
|
static struct mgmt_cmt_info_t *mgmt_history_new_cmt_info(void)
|
||||||
{
|
{
|
||||||
int i;
|
struct mgmt_cmt_info_t *new;
|
||||||
unsigned char digest[MGMTD_MD5_HASH_LEN];
|
struct timespec tv;
|
||||||
MD5_CTX ctx;
|
struct tm tm;
|
||||||
|
|
||||||
memset(&ctx, 0, sizeof(ctx));
|
new = XCALLOC(MTYPE_MGMTD_CMT_INFO, sizeof(struct mgmt_cmt_info_t));
|
||||||
MD5Init(&ctx);
|
|
||||||
MD5Update(&ctx, input_str, strlen(input_str));
|
|
||||||
MD5Final(digest, &ctx);
|
|
||||||
|
|
||||||
for (i = 0; i < MGMTD_MD5_HASH_LEN; i++)
|
clock_gettime(CLOCK_REALTIME, &tv);
|
||||||
snprintf(&hash[i * 2], MGMTD_MD5_HASH_STR_HEX_LEN, "%02x",
|
localtime_r(&tv.tv_sec, &tm);
|
||||||
(unsigned int)digest[i]);
|
|
||||||
|
mgmt_time_to_string(&tv, true, new->time_str, sizeof(new->time_str));
|
||||||
|
mgmt_time_to_string(&tv, false, new->cmtid_str, sizeof(new->cmtid_str));
|
||||||
|
snprintf(new->cmt_json_file, sizeof(new->cmt_json_file),
|
||||||
|
MGMTD_COMMIT_FILE_PATH, new->cmtid_str);
|
||||||
|
|
||||||
|
return new;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct mgmt_cmt_info_t *mgmt_history_create_cmt_rec(void)
|
static struct mgmt_cmt_info_t *mgmt_history_create_cmt_rec(void)
|
||||||
{
|
{
|
||||||
struct mgmt_cmt_info_t *new;
|
struct mgmt_cmt_info_t *new = mgmt_history_new_cmt_info();
|
||||||
struct mgmt_cmt_info_t *cmt_info;
|
struct mgmt_cmt_info_t *cmt_info;
|
||||||
struct mgmt_cmt_info_t *last_cmt_info = NULL;
|
struct mgmt_cmt_info_t *last_cmt_info = NULL;
|
||||||
struct timeval cmt_recd_tv;
|
|
||||||
|
|
||||||
new = XCALLOC(MTYPE_MGMTD_CMT_INFO, sizeof(struct mgmt_cmt_info_t));
|
|
||||||
gettimeofday(&cmt_recd_tv, NULL);
|
|
||||||
mgmt_realtime_to_string(&cmt_recd_tv, new->time_str,
|
|
||||||
sizeof(new->time_str));
|
|
||||||
mgmt_history_hash(new->time_str, new->cmtid_str);
|
|
||||||
snprintf(new->cmt_json_file, sizeof(new->cmt_json_file) - 1,
|
|
||||||
MGMTD_COMMIT_FILE_PATH, new->cmtid_str);
|
|
||||||
|
|
||||||
if (mgmt_cmt_infos_count(&mm->cmts) == MGMTD_MAX_COMMIT_LIST) {
|
if (mgmt_cmt_infos_count(&mm->cmts) == MGMTD_MAX_COMMIT_LIST) {
|
||||||
FOREACH_CMT_REC (mm, cmt_info)
|
FOREACH_CMT_REC (mm, cmt_info)
|
||||||
@ -106,8 +100,7 @@ mgmt_history_find_cmt_record(const char *cmtid_str)
|
|||||||
struct mgmt_cmt_info_t *cmt_info;
|
struct mgmt_cmt_info_t *cmt_info;
|
||||||
|
|
||||||
FOREACH_CMT_REC (mm, cmt_info) {
|
FOREACH_CMT_REC (mm, cmt_info) {
|
||||||
if (strncmp(cmt_info->cmtid_str, cmtid_str,
|
if (strcmp(cmt_info->cmtid_str, cmtid_str) == 0)
|
||||||
MGMTD_MD5_HASH_STR_HEX_LEN) == 0)
|
|
||||||
return cmt_info;
|
return cmt_info;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -282,8 +275,7 @@ int mgmt_history_rollback_by_id(struct vty *vty, const char *cmtid_str)
|
|||||||
}
|
}
|
||||||
|
|
||||||
FOREACH_CMT_REC (mm, cmt_info) {
|
FOREACH_CMT_REC (mm, cmt_info) {
|
||||||
if (strncmp(cmt_info->cmtid_str, cmtid_str,
|
if (strcmp(cmt_info->cmtid_str, cmtid_str) == 0) {
|
||||||
MGMTD_MD5_HASH_STR_HEX_LEN) == 0) {
|
|
||||||
ret = mgmt_history_rollback_to_cmt(vty, cmt_info,
|
ret = mgmt_history_rollback_to_cmt(vty, cmt_info,
|
||||||
false);
|
false);
|
||||||
return ret;
|
return ret;
|
||||||
@ -349,9 +341,9 @@ void show_mgmt_cmt_history(struct vty *vty)
|
|||||||
int slno = 0;
|
int slno = 0;
|
||||||
|
|
||||||
vty_out(vty, "Last 10 commit history:\n");
|
vty_out(vty, "Last 10 commit history:\n");
|
||||||
vty_out(vty, " Sl.No\tCommit-ID(HEX)\t\t\t Commit-Record-Time\n");
|
vty_out(vty, "Slot Commit-ID Commit-Record-Time\n");
|
||||||
FOREACH_CMT_REC (mm, cmt_info) {
|
FOREACH_CMT_REC (mm, cmt_info) {
|
||||||
vty_out(vty, " %d\t%s %s\n", slno, cmt_info->cmtid_str,
|
vty_out(vty, "%4d %23s %s\n", slno, cmt_info->cmtid_str,
|
||||||
cmt_info->time_str);
|
cmt_info->time_str);
|
||||||
slno++;
|
slno++;
|
||||||
}
|
}
|
||||||
|
@ -54,4 +54,42 @@ extern void mgmt_history_new_record(struct mgmt_ds_ctx *ds_ctx);
|
|||||||
extern void mgmt_history_destroy(void);
|
extern void mgmt_history_destroy(void);
|
||||||
extern void mgmt_history_init(void);
|
extern void mgmt_history_init(void);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 012345678901234567890123456789
|
||||||
|
* 2023-12-31T12:12:12,012345678
|
||||||
|
* 20231231121212012345678
|
||||||
|
*/
|
||||||
|
#define MGMT_LONG_TIME_FMT "%Y-%m-%dT%H:%M:%S"
|
||||||
|
#define MGMT_LONG_TIME_MAX_LEN 30
|
||||||
|
#define MGMT_SHORT_TIME_FMT "%Y%m%d%H%M%S"
|
||||||
|
#define MGMT_SHORT_TIME_MAX_LEN 24
|
||||||
|
|
||||||
|
static inline const char *
|
||||||
|
mgmt_time_to_string(struct timespec *tv, bool long_fmt, char *buffer, size_t sz)
|
||||||
|
{
|
||||||
|
struct tm tm;
|
||||||
|
size_t n;
|
||||||
|
|
||||||
|
localtime_r(&tv->tv_sec, &tm);
|
||||||
|
|
||||||
|
if (long_fmt) {
|
||||||
|
n = strftime(buffer, sz, MGMT_LONG_TIME_FMT, &tm);
|
||||||
|
snprintf(&buffer[n], sz - n, ",%09lu", tv->tv_nsec);
|
||||||
|
} else {
|
||||||
|
n = strftime(buffer, sz, MGMT_SHORT_TIME_FMT, &tm);
|
||||||
|
snprintf(&buffer[n], sz - n, "%09lu", tv->tv_nsec);
|
||||||
|
}
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline const char *mgmt_realtime_to_string(struct timeval *tv, char *buf,
|
||||||
|
size_t sz)
|
||||||
|
{
|
||||||
|
struct timespec ts = {.tv_sec = tv->tv_sec,
|
||||||
|
.tv_nsec = tv->tv_usec * 1000};
|
||||||
|
|
||||||
|
return mgmt_time_to_string(&ts, true, buf, sz);
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* _FRR_MGMTD_HISTORY_H_ */
|
#endif /* _FRR_MGMTD_HISTORY_H_ */
|
||||||
|
Loading…
Reference in New Issue
Block a user