diff --git a/src/host.c b/src/host.c index 10182ac8e46d31d06a7ecd727aa083abfe636b67..ba360ceee51f90f57c265e86c7e667cfc7e0c82d 100644 --- a/src/host.c +++ b/src/host.c @@ -780,6 +780,29 @@ int host_check_ip_addr(struct host_node *p) return ret; } +void host_change_active_state(struct host_node *p, uint8_t state) +{ + char mac_str[18] = { 0 }; + time_t tmp_t; + struct tm *info; + char str_tm[20]; + + dbg("Inside %s %d\n", __func__, __LINE__); + hwaddr_ntoa(p->hwaddr, mac_str); + p->active = state; + p->active_last_change = time(NULL); + //here we need to write this info in the config file + config_add_default_host_mac("hosts", "host", mac_str); + if (state == 0x0) + config_set_host_option("hosts", "host", "macaddr", mac_str, "active", "0"); + else + config_set_host_option("hosts", "host", "macaddr", mac_str, "active", "1"); + tmp_t = p->active_last_change; + info = localtime(&tmp_t); + strftime(str_tm, sizeof(str_tm), "%Y-%m-%dT%H:%M:%S", info); + config_set_host_option("hosts", "host", "macaddr", mac_str, "active_last_change", str_tm); +} + /*Here the below are checked for each host node in case of periodic run * 1. If Ipaddress is fetched from dhcp lease * 2. If node is eth then check reachable @@ -829,15 +852,7 @@ void host_run_status_check(struct topologyd_private *priv) if (p->active != 1) { //Here as the node is reachable //we are adding in map-topology - p->active = 1; - p->active_last_change = time(NULL); - //here we need to write this info in the config file - config_add_default_host_mac("hosts", "host", mac_str); - config_set_host_option("hosts", "host", "macaddr", mac_str, "active", "1"); - tmp_t = p->active_last_change; - info = localtime(&tmp_t); - strftime(str_tm, sizeof(str_tm), "%Y-%m-%dT%H:%M:%S", info); - config_set_host_option("hosts", "host", "macaddr", mac_str, "active_last_change", str_tm); + host_change_active_state(p, 1); } } else { /*Here as the status is not reachable @@ -850,29 +865,12 @@ void host_run_status_check(struct topologyd_private *priv) ret = host_get_arping_status(p->ipv4addr); if (ret != 0) { dbg("ETH node is unreachable " MACFMT "\n", MAC2STR(p->hwaddr)); - if (p->active != 0) { - p->active = 0; - p->active_last_change = time(NULL); - //here we need to write this info in the config file - config_add_default_host_mac("hosts", "host", mac_str); - config_set_host_option("hosts", "host", "macaddr", mac_str, "active", "0"); - tmp_t = p->active_last_change; - info = localtime(&tmp_t); - strftime(str_tm, sizeof(str_tm), "%Y-%m-%dT%H:%M:%S", info); - config_set_host_option("hosts", "host", "macaddr", mac_str, "active_last_change", str_tm); - } + if (p->active != 0) + host_change_active_state(p, 0); } else { if (p->active != 1) { dbg("ETH node is reachable " MACFMT "\n", MAC2STR(p->hwaddr)); - p->active = 1; - p->active_last_change = time(NULL); - //here we need to write this info in the config file - config_add_default_host_mac("hosts", "host", mac_str); - config_set_host_option("hosts", "host", "macaddr", mac_str, "active", "1"); - tmp_t = p->active_last_change; - info = localtime(&tmp_t); - strftime(str_tm, sizeof(str_tm), "%Y-%m-%dT%H:%M:%S", info); - config_set_host_option("hosts", "host", "macaddr", mac_str, "active_last_change", str_tm); + host_change_active_state(p, 1); } } }