def check_and_recover_nic_state()

in azurelinuxagent/common/osutil/redhat.py [0:0]


    def check_and_recover_nic_state(self, ifname):
        """
        Checks if the provided network interface is in an 'up' state. If the network interface is in a 'down' state,
        attempt to recover the interface by restarting the Network Manager service.

        Raises an exception if an attempt to bring the interface into an 'up' state fails, or if the state
         of the network interface cannot be determined.
        """
        nic_operstate, nic_general_state = self.get_nic_operational_and_general_states(ifname)
        if nic_operstate == "down" or "disconnected" in nic_general_state:
            logger.info("Restarting the Network Manager service to recover network interface {0}".format(ifname))
            self.restart_network_manager()
            # Interface does not come up immediately after NetworkManager restart. Wait 5 seconds before checking
            # network interface state.
            time.sleep(5)
            nic_operstate, nic_general_state = self.get_nic_operational_and_general_states(ifname)
            # It is possible for network interface to be in an unknown or unmanaged state. Log warning if state is not
            # down, disconnected, up, or connected
            if nic_operstate != "up" or nic_general_state != "100 (connected)":
                msg = "Network Manager restart failed to bring network interface {0} into 'up' and 'connected' state".format(ifname)
                logger.warn(msg)
                raise Exception(msg)
            else:
                logger.info("Network Manager restart successfully brought the network interface {0} into 'up' and 'connected' state".format(ifname))
        elif nic_operstate != "up" or nic_general_state != "100 (connected)":
            # We already logged a warning with the network interface state in get_nic_operstate(). Raise an exception
            # for the env thread to send to telemetry.
            raise Exception("The primary network interface {0} operational state is '{1}' and general state is '{2}'.".format(ifname, nic_operstate, nic_general_state))