def report_diagnostic_event(str): """Report a diagnostic event""" evt = events.ReportingEvent(DIAGNOSTIC_EVENT_TYPE, 'diagnostic message', str, events.DEFAULT_EVENT_ORIGIN) events.report_event(evt) # return the event for unit testing purpose return evt
def report_diagnostic_event( msg: str, *, logger_func=None) -> events.ReportingEvent: """Report a diagnostic event""" if callable(logger_func): logger_func(msg) evt = events.ReportingEvent( DIAGNOSTIC_EVENT_TYPE, 'diagnostic message', msg, events.DEFAULT_EVENT_ORIGIN) events.report_event(evt, excluded_handler_types={"log"}) # return the event for unit testing purpose return evt
def report_compressed_event(event_name, event_content): """Report a compressed event""" compressed_data = base64.encodebytes(zlib.compress(event_content)) event_data = {"encoding": "gz+b64", "data": compressed_data.decode('ascii')} evt = events.ReportingEvent( COMPRESSED_EVENT_TYPE, event_name, json.dumps(event_data), events.DEFAULT_EVENT_ORIGIN) events.report_event(evt, excluded_handler_types={"log", "print", "webhook"}) # return the event for unit testing purpose return evt
def get_system_info(): """Collect and report system information""" info = util.system_info() evt = events.ReportingEvent( SYSTEMINFO_EVENT_TYPE, 'system information', "cloudinit_version=%s, kernel_version=%s, variant=%s, " "distro_name=%s, distro_version=%s, flavor=%s, " "python_version=%s" % (version.version_string(), info['release'], info['variant'], info['dist'][0], info['dist'][1], info['dist'][2], info['python']), events.DEFAULT_EVENT_ORIGIN) events.report_event(evt) # return the event for unit testing purpose return evt
def get_boot_telemetry(): """Report timestamps related to kernel initialization and systemd activation of cloud-init""" if not distros.uses_systemd(): raise RuntimeError("distro not using systemd, skipping boot telemetry") LOG.debug("Collecting boot telemetry") try: kernel_start = float(time.time()) - float(util.uptime()) except ValueError: raise RuntimeError("Failed to determine kernel start timestamp") try: out, _ = util.subp( ['/bin/systemctl', 'show', '-p', 'UserspaceTimestampMonotonic'], capture=True) tsm = None if out and '=' in out: tsm = out.split("=")[1] if not tsm: raise RuntimeError("Failed to parse " "UserspaceTimestampMonotonic from systemd") user_start = kernel_start + (float(tsm) / 1000000) except util.ProcessExecutionError as e: raise RuntimeError("Failed to get UserspaceTimestampMonotonic: %s" % e) except ValueError as e: raise RuntimeError("Failed to parse " "UserspaceTimestampMonotonic from systemd: %s" % e) try: out, _ = util.subp([ '/bin/systemctl', 'show', 'cloud-init-local', '-p', 'InactiveExitTimestampMonotonic' ], capture=True) tsm = None if out and '=' in out: tsm = out.split("=")[1] if not tsm: raise RuntimeError("Failed to parse " "InactiveExitTimestampMonotonic from systemd") cloudinit_activation = kernel_start + (float(tsm) / 1000000) except util.ProcessExecutionError as e: raise RuntimeError("Failed to get InactiveExitTimestampMonotonic: %s" % e) except ValueError as e: raise RuntimeError("Failed to parse " "InactiveExitTimestampMonotonic from systemd: %s" % e) evt = events.ReportingEvent( BOOT_EVENT_TYPE, 'boot-telemetry', "kernel_start=%s user_start=%s cloudinit_activation=%s" % (datetime.utcfromtimestamp(kernel_start).isoformat() + 'Z', datetime.utcfromtimestamp(user_start).isoformat() + 'Z', datetime.utcfromtimestamp(cloudinit_activation).isoformat() + 'Z'), events.DEFAULT_EVENT_ORIGIN) events.report_event(evt) # return the event for unit testing purpose return evt