def _fileinfo_check_conjunctions( check_definition: List[MetricInfo], params: Mapping[str, Any], ) -> CheckResult: conjunctions = params.get("conjunctions", []) for conjunction_state, levels in conjunctions: levels = dict(levels) match_texts = [] matches = 0 for title, key, value, readable_f in check_definition: level = levels.get(key) if level is not None and value >= level: match_texts.append("%s at %s" % (title.lower(), readable_f(level))) matches += 1 level_lower = levels.get("%s_lower" % key) if level_lower is not None and value < level_lower: match_texts.append("%s below %s" % (title.lower(), readable_f(level_lower))) matches += 1 if matches == len(levels): yield Result( state=State(conjunction_state), summary="Conjunction: %s" % " AND ".join(match_texts), )
def check_terminated(params: Mapping[str, int], state: ContainerTerminatedState) -> CheckResult: result_state = State.OK status = "Succeeded" if state.exit_code != 0: result_state = State(params["failed_state"]) status = "Failed" summary = f"Status: {status} ({state.reason}: {state.detail})" yield Result(state=result_state, summary=summary) end_time = render.datetime(state.end_time) duration = render.timespan(state.end_time - state.start_time) summary = f"End time: {end_time} Run duration: {duration}" yield Result(state=State.OK, summary=summary)
def check_cisco_asa_failover(params: (Mapping[str, Any]), section: Section) -> CheckResult: yield Result( state=State.OK, summary= f'Device ({section.local_role}) is the {section.local_status_detail}', ) if params[section.local_role] != _get_cisco_asa_state_name( section.local_status): yield Result( state=State(params['failover_state']), summary= f'(The {section.local_role} device should be {params[section.local_role]})', ) if section.local_status not in ['9', '10']: # local not active/standby yield Result( state=State(params['not_active_standby_state']), summary= f'Unhandled state {_get_cisco_asa_state_name(section.local_status)} reported', ) if section.remote_status not in ['9', '10']: # remote not active/standby yield Result( state=State(params['not_active_standby_state']), summary= (f'Unhandled state {_get_cisco_asa_state_name(section.remote_status)} for ' 'remote device reported'), ) if section.failover_link_status != '2': # not up yield Result( state=State(params['failover_link_state']), summary=( f'Failover link {section.failover_link_name} state is ' f'{_get_cisco_asa_state_name(section.failover_link_status)}'), )
def check_cisco_asa_failover(params: (Mapping[str, Any]), section: Section) -> CheckResult: yield Result( state=State.OK, summary= f"Device ({section.local_role}) is the {section.local_status_detail}", ) if params[section.local_role] != _get_cisco_asa_state_name( section.local_status): yield Result( state=State(params["failover_state"]), summary= f"(The {section.local_role} device should be {params[section.local_role]})", ) if section.local_status not in ["9", "10"]: # local not active/standby yield Result( state=State(params["not_active_standby_state"]), summary= f"Unhandled state {_get_cisco_asa_state_name(section.local_status)} reported", ) if section.remote_status not in ["9", "10"]: # remote not active/standby yield Result( state=State(params["not_active_standby_state"]), summary= (f"Unhandled state {_get_cisco_asa_state_name(section.remote_status)} for " "remote device reported"), ) if section.failover_link_status != "2": # not up yield Result( state=State(params["failover_link_state"]), summary=( f"Failover link {section.failover_link_name} state is " f"{_get_cisco_asa_state_name(section.failover_link_status)}"), )
def _check_node_conditions(params: Mapping[str, int], section: NodeConditions): cond: Union[Optional[FalsyNodeCondition], FalsyNodeCondition, TruthyNodeCondition] = None for name, cond in section: if not cond: continue if cond.is_ok(): yield Result( state=State.OK, summary=condition_short_description(name, cond.status), details=condition_detailed_description(name, cond.status, cond.reason, cond.detail), ) else: yield Result( state=State(params[name]), summary=condition_detailed_description(name, cond.status, cond.reason, cond.detail), )
def check_mssql_mirroring( item: str, params: Mapping[str, int], # the int is actually a Checkmk state section: MirroringSection, ) -> CheckResult: mirroring_config = section.get(item) if not mirroring_config: return yield Result( state=State.OK, summary=f'Principal: {mirroring_config.server_name}', ) yield Result( state=State.OK, summary=f'Mirror: {mirroring_config.mirroring_partner_instance}', ) # For an explanation of state mappings, see comment at beginning for state_to_check, desired_state, criticality in [ ('mirroring_state', 4, params['mirroring_state_criticality']), ('mirroring_witness_state', 1, params['mirroring_witness_state_criticality']) ]: state = State.OK if getattr(mirroring_config, state_to_check) != desired_state: state = State(criticality) yield Result( state=state, notice=_details_text(f'{state_to_check}_desc', mirroring_config), ) for detail in [ 'mirroring_safety_level_desc', 'mirroring_partner_name', 'mirroring_witness_name', ]: yield Result(state=State.OK, notice=_details_text(detail, mirroring_config))
def _check_individual_files( params: Mapping[str, Any], file_name: str, file_size: int, file_age: int, skip_ok_files: bool, ) -> CheckResult: ''' This function checks individual files against levels defined for the file group. This is done to generate information for the long output. ''' for key, value in [ ("age_oldest", file_age), ("age_newest", file_age), ("size_smallest", file_size), ("size_largest", file_size), ]: levels_upper = params.get("max" + key, (None, None)) levels_lower = params.get("min" + key, (None, None)) results = check_levels( value, metric_name=key, levels_upper=levels_upper, levels_lower=levels_lower, ) overall_state = max(r.state.value for r in results if isinstance(r, Result)) if skip_ok_files and State(overall_state) == State.OK: return age = render.timespan(file_age) size = render.filesize(file_size) yield Result( state=State.OK, notice=f"[{file_name}] Age: {age}, Size: {size}", )
def check(params: Mapping[str, int], section: NodeConditions) -> CheckResult: cond: Union[Optional[FalsyNodeCondition], FalsyNodeCondition, TruthyNodeCondition] = None if all(cond.is_ok() for _, cond in section if cond): details = "\n".join( f"{name.upper()}: {cond.status} ({cond.reason}: {cond.detail})" for name, cond in section if cond ) yield Result(state=State.OK, summary="Ready, all conditions passed", details=details) return for name, cond in section: if not cond: continue if cond.is_ok(): yield Result( state=State.OK, summary=f"{name.upper()}: {cond.status}", details=f"{name.upper()}: {cond.status} ({cond.reason}: {cond.detail})", ) else: yield Result( state=State(params[name]), summary=f"{name.upper()}: {cond.status} ({cond.reason}: {cond.detail})", )
if section_gcp_service_cloud_sql is None: return if item not in section_gcp_service_cloud_sql: return metrics = { "up": gcp.MetricSpec("cloudsql.googleapis.com/database/up", str, dtype="int"), } timeseries = section_gcp_service_cloud_sql[item].rows yield from gcp.generic_check(metrics, timeseries, {}) metric_type = "cloudsql.googleapis.com/database/state" if (metric := next((r for r in timeseries if r.ts.metric.type == metric_type), None)) is None: yield Result(state=State.UNKNOWN, summary="No data available") return gcp_state = metric.ts.points[0].value.string_value state = State(params[gcp_state]) summary = f"State: {gcp_state}" yield Result(state=state, summary=summary) register.check_plugin( name="gcp_sql_status", sections=["gcp_service_cloud_sql", "gcp_assets"], service_name="GCP Cloud SQL: %s", check_ruleset_name="gcp_sql_status", discovery_function=discover, check_function=check_gcp_sql_status, check_default_parameters={ "RUNNING": int(State.OK), "SUSPEND": int(State.WARN), "RUNNABLE": int(State.OK),
def test_check_result_terminated_non_zero_exit_code_status_specified_params( failed_state, check_result): result, _, _, _ = check_result assert result.state == State(failed_state) assert result.summary == "Status: Failed (VeryReason: so detail)"
section: EntitySensorSection) -> DiscoveryResult: yield from (Service(item=item) for item in section.get('power_presence', {})) def check_entity_sensors_power_presence( item: str, params: Mapping[str, Any], section: EntitySensorSection, ) -> CheckResult: if not (sensor_reading := section.get('power_presence', {}).get(item)): return if sensor_reading.reading == 1: yield Result(state=State.OK, summary='Powered on') return yield Result(state=State(params['power_off_criticality']), summary='Powered off') register.check_plugin( name='entity_sensors_power_presence', sections=['entity_sensors'], service_name='Power %s', discovery_function=discover_entity_sensors_power_presence, check_function=check_entity_sensors_power_presence, check_ruleset_name='power_presence', check_default_parameters={'power_off_criticality': 1}, # customer request )
def discover_entity_sensors_power_presence(section: EntitySensorSection) -> DiscoveryResult: yield from (Service(item=item) for item in section.get('power_presence', {})) def check_entity_sensors_power_presence( item: str, params: Mapping[str, Any], section: EntitySensorSection, ) -> CheckResult: if not (sensor_reading := section.get('power_presence', {}).get(item)): return if sensor_reading.reading == 1: yield Result(state=State.OK, summary='Powered on') return yield Result(state=State(params['power_off_criticality']), summary='Powered off') register.check_plugin( name='entity_sensors_power_presence', sections=['entity_sensors'], service_name='Power %s', discovery_function=discover_entity_sensors_power_presence, check_function=check_entity_sensors_power_presence, check_ruleset_name='power_presence', check_default_parameters={'power_off_criticality': 1}, # customer request )
def discover_entity_sensors_power_presence(section: EntitySensorSection) -> DiscoveryResult: yield from (Service(item=item) for item in section.get("power_presence", {})) def check_entity_sensors_power_presence( item: str, params: Mapping[str, Any], section: EntitySensorSection, ) -> CheckResult: if not (sensor_reading := section.get("power_presence", {}).get(item)): return if sensor_reading.reading == 1: yield Result(state=State.OK, summary="Powered on") return yield Result(state=State(params["power_off_criticality"]), summary="Powered off") register.check_plugin( name="entity_sensors_power_presence", sections=["entity_sensors"], service_name="Power %s", discovery_function=discover_entity_sensors_power_presence, check_function=check_entity_sensors_power_presence, check_ruleset_name="power_presence", check_default_parameters={"power_off_criticality": 1}, # customer request )