def test_state(): assert state(0) is state.OK assert state.OK == 0 assert state(1) is state.WARN assert state.WARN == 1 assert state(2) is state.CRIT assert state.CRIT == 2 assert state(3) is state.UNKNOWN assert state.UNKNOWN == 3
def _aggregate_results( subresults: Iterable[Union[checking_types.Metric, checking_types.Result, checking_types.IgnoreResults]] ) -> ServiceCheckResult: perfdata: List[Metric] = [] summaries: List[str] = [] details: List[str] = [] status = checking_types.state(0) for subr in list(subresults): # consume *everything* here, before we may raise! if isinstance(subr, checking_types.IgnoreResults): raise checking_types.IgnoreResultsError(str(subr)) if isinstance(subr, checking_types.Metric): perfdata.append((subr.name, subr.value) + subr.levels + subr.boundaries) continue status = checking_types.state_worst(status, subr.state) state_marker = check_api_utils.state_markers[int(subr.state)] if subr.summary: summaries.append(subr.summary + state_marker) details.append(subr.details + state_marker) # Empty list? Check returned nothing if not details: return ITEM_NOT_FOUND if not summaries: count = len(details) summaries.append("Everything looks OK - %d detail%s available" % (count, "" if count == 1 else "s")) all_text = [", ".join(summaries)] + details return int(status), "\n".join(all_text).strip(), perfdata
def _create_new_result( is_details, # type: bool legacy_state, # type: int legacy_text, # type: str legacy_metrics=(), # type: Union[Tuple, List] ): # type: (...) -> Generator[Union[Metric, Result], None, bool] result_state = state(legacy_state) if legacy_state or legacy_text: # skip "Null"-Result if is_details: summary = None # type: Optional[str] details = legacy_text # type: Optional[str] else: is_details = "\n" in legacy_text summary, details = legacy_text.split( "\n", 1) if is_details else (legacy_text, None) yield Result(state=result_state, summary=summary, details=details) for metric in legacy_metrics: # fill up with None: name, value, warn, crit, min_, max_ = ( v for v, _ in itertools.zip_longest(metric, range(6))) yield Metric(name, value, levels=(warn, crit), boundaries=(min_, max_)) return is_details
def _create_new_result( implicit_newline, # type: str legacy_state, # type: int legacy_text, # type: str legacy_metrics=(), # type: Union[Tuple, List] ): # type: (...) -> Generator[Union[Metric, Result], None, str] kwargs = {"state": state(legacy_state)} # type: Dict[str, Any] components = (implicit_newline + legacy_text).split("\n", 1) kwargs["summary"] = components[0] if len(components) > 1: # make sure its longer than summary kwargs["details"] = ("%%-%ds" % (len(components[0]) + 1)) % components[1] yield Result(**kwargs) for metric in legacy_metrics: # fill up with None: name, value, warn, crit, min_, max_ = ( v for v, _ in itertools.zip_longest(metric, range(6))) yield Metric(name, value, levels=(warn, crit), boundaries=(min_, max_)) return "\n" if len(components) > 1 else ""