# Copyright (c) Cloud Linux Software, Inc
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENCE.TXT
import json
import base64
import os
import time
import platform
import zlib
from . import kcare
from . import platform_utils
from . import http_utils
from . import utils
from . import constants
from . import auth
if False: # pragma: no cover
from typing import Dict, Any # noqa: F401
def server_info(reason, now=None):
data = dict() # type: Dict[str, Any]
data['ts'] = int(now or time.time())
data['reason'] = reason
data['machine'] = platform.machine()
data['processor'] = platform.processor()
data['release'] = platform.release()
data['system'] = platform.system()
data['version'] = platform.version()
distro = platform_utils.get_distro()
data['distro'] = distro[0]
data['distro_version'] = distro[1]
data['euname'] = kcare.kcare_uname()
data['kcare_version'] = utils.strip_version_timestamp(constants.VERSION)
data['last_stop'] = kcare.get_last_stop()
data['node'] = platform_utils.get_hostname()
data['uptime'] = platform_utils.get_uptime()
data['virt'] = platform_utils.get_virt()
data['proxy'] = http_utils.proxy_is_used()
description = kcare.parse_patch_description(kcare.loaded_patch_description())
data['ltimestamp'] = description['last-update']
data['patch_level'] = description['patch-level']
data['patch_type'] = description['patch-type']
data['kmod'] = kcare.get_current_kmod_version() or ''
data['crashreporter_ts'] = kcare.crashreporter_latest_event_timestamp()
data['kdump_status'] = kcare.kdump_status()
try:
data['kdump_ts'] = kcare.kdumps_latest_event_timestamp()
except Exception:
# Not critical data
pass
server_id = auth.get_serverid()
if server_id:
data['server_id'] = server_id
state = kcare.get_state()
if state is not None:
data['state'] = state
return data
def server_lib_info(reason, patch_level, now=None):
data = dict() # type: Dict[str, Any]
data['ts'] = int(now or time.time())
data['reason'] = reason
data['patch_level'] = patch_level
distro = platform_utils.get_distro()
data['distro'] = distro[0]
data['distro_version'] = distro[1]
data['machine'] = platform.machine()
data['kcare_version'] = utils.strip_version_timestamp(constants.VERSION)
data['node'] = platform_utils.get_hostname()
data['uptime'] = platform_utils.get_uptime()
data['virt'] = platform_utils.get_virt()
stop_ts = 0.0
if os.path.exists('/var/lib/libcare/stop'):
stop_ts = os.path.getctime('/var/lib/libcare/stop')
data['stop_ts'] = stop_ts
return data
def based_server_info(reason):
return utils.nstr(base64.b16encode(utils.bstr(str(server_info(reason)))))
def encode_server_lib_info(info):
data = json.dumps(info, ensure_ascii=False, separators=(',', ':'))
return utils.nstr(base64.urlsafe_b64encode(zlib.compress(utils.bstr(data, 'utf-8'))))