def _popen(cmd): # pragma: no cover """Try catched subprocess.popen. raises explicit error """ try: proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = proc.communicate() return stdout, stderr except OSError as e: message = "%s" % e raise CheckError(message)
def manage_value_from_json(metric_value): """ Manage single value expectation in metric from JSON data :param metric_value: Metric value :type metric_value: list :returns: Extracted value from JSON path result :rtype: string|int """ # This context only manage one element check_result = ContextUtils.check_size(metric_value, 1, 1) if check_result: raise CheckError(check_result) # Get value return metric_value[0].value
def _find_ip_arp_table(self, system=system): """Runs 'bgpctl show'.""" _cmd = "/usr/sbin/arp -na" _log.debug("running '%s'", _cmd) result = 0 stdout, stderr = _popen(_cmd.split()) if not isinstance(stderr, str): # pragma: no cover stderr = stderr.decode() if not isinstance(stdout, str): # pragma: no cover stdout = stdout.decode() if stderr: message = "%s" % (stderr.splitlines()[-1]) _log.info(message) raise CheckError(message) if stdout: if system == 'OpenBSD': output = stdout.splitlines()[1:] else: output = stdout.splitlines() if output: ArpEntry = namedtuple('ArpEntry', platforms[system]['fields']) for line in output: data = line.split()[0:4] entry = ArpEntry(*data) ip = entry.Host if system in ('Linux', 'FreeBSD'): # here we remove () around ip ip = ip[1:-1] if ip == self.ip: if len(entry.Mac.split(':')) == 6: result = 1 break return result