def perform_ping(started, server=DEFAULT_SERVER_URL): url = urljoin(server, "/api/v1/pingback") instance, _ = InstanceIDModel.get_or_create_current_instance() language = get_device_setting("language_id", "") try: timezone = get_current_timezone().zone except Exception: timezone = "" data = { "instance_id": instance.id, "version": kolibri.__version__, "mode": conf.OPTIONS["Deployment"]["RUN_MODE"], "platform": instance.platform, "sysversion": instance.sysversion, "database_id": instance.database.id, "system_id": instance.system_id, "node_id": instance.node_id, "language": language, "timezone": timezone, "uptime": int( (datetime.datetime.now() - started).total_seconds() / 60), "timestamp": localtime(), "installer": installation_type(), } logger.debug("Pingback data: {}".format(data)) jsondata = dump_zipped_json(data) response = requests.post(url, data=jsondata, timeout=60) response.raise_for_status() return json.loads(response.content.decode() or "{}")
def test_dev(self): sys_args = [ "kolibri", "--debug", "manage", "runserver", "--settings=kolibri.deployment.default.settings.dev", '"0.0.0.0:8000"', ] with mock.patch("sys.argv", sys_args): install_type = server.installation_type() assert install_type == "devserver"
def get(self, request, format=None): info = {} info["version"] = kolibri.__version__ status, urls = get_urls() if not urls: # Will not return anything when running the debug server, so at least return the current URL urls = [ request.build_absolute_uri(OPTIONS["Deployment"]["URL_PATH_PREFIX"]) ] filtered_urls = [ url for url in urls if "127.0.0.1" not in url and "localhost" not in url ] if filtered_urls: urls = filtered_urls info["urls"] = urls db_engine = settings.DATABASES["default"]["ENGINE"] if db_engine.endswith("sqlite3"): # Return path to .sqlite file (usually in KOLIBRI_HOME folder) info["database_path"] = settings.DATABASES["default"]["NAME"] elif db_engine.endswith("postgresql"): info["database_path"] = "postgresql" else: info["database_path"] = "unknown" instance_model = InstanceIDModel.get_or_create_current_instance()[0] info["device_id"] = instance_model.id info["os"] = instance_model.platform info["content_storage_free_space"] = get_free_space( OPTIONS["Paths"]["CONTENT_DIR"] ) # This returns the localized time for the server info["server_time"] = local_now() # Returns the named timezone for the server (the time above only includes the offset) info["server_timezone"] = settings.TIME_ZONE info["installer"] = installation_type() info["python_version"] = "{major}.{minor}.{micro}".format( major=version_info.major, minor=version_info.minor, micro=version_info.micro ) return Response(info)
def get(self, request, format=None): info = {} info["version"] = kolibri.__version__ status, urls = get_urls() if not urls: # Will not return anything when running the debug server, so at least return the current URL urls = [ request.build_absolute_uri( OPTIONS["Deployment"]["URL_PATH_PREFIX"]) ] filtered_urls = [ url for url in urls if "127.0.0.1" not in url and "localhost" not in url ] if filtered_urls: urls = filtered_urls info["urls"] = urls if settings.DATABASES["default"]["ENGINE"].endswith("sqlite3"): # If any other database backend, will not be file backed, so no database path to return info["database_path"] = settings.DATABASES["default"]["NAME"] instance_model = InstanceIDModel.get_or_create_current_instance()[0] info["device_name"] = instance_model.hostname info["device_id"] = instance_model.id info["os"] = instance_model.platform info["content_storage_free_space"] = get_free_space( OPTIONS["Paths"]["CONTENT_DIR"]) # This returns the localized time for the server info["server_time"] = local_now() # Returns the named timezone for the server (the time above only includes the offset) info["server_timezone"] = settings.TIME_ZONE info["installer"] = installation_type() return Response(info)
if not os.path.exists(SESSION_FILE_PATH): if not os.path.exists(conf.KOLIBRI_HOME): raise RuntimeError("The KOLIBRI_HOME dir does not exist") os.mkdir(SESSION_FILE_PATH) SESSION_COOKIE_NAME = "kolibri" SESSION_EXPIRE_AT_BROWSER_CLOSE = True SESSION_COOKIE_AGE = 1200 if conf.OPTIONS["Debug"]["SENTRY_BACKEND_DSN"]: import sentry_sdk from kolibri.utils.server import installation_type from sentry_sdk.integrations.django import DjangoIntegration sentry_sdk.init( dsn=conf.OPTIONS["Debug"]["SENTRY_BACKEND_DSN"], environment=conf.OPTIONS["Debug"]["SENTRY_ENVIRONMENT"], integrations=[DjangoIntegration()], release=kolibri.__version__, ) with sentry_sdk.configure_scope() as scope: scope.set_tag("mode", conf.OPTIONS["Deployment"]["RUN_MODE"]) scope.set_tag("installer", installation_type()) print("Sentry backend error logging is enabled")
def test_whl(self): install_type = server.installation_type() assert install_type == "whl"
def test_windows(self): install_type = server.installation_type() assert install_type == "Windows"
def test_apt(apt): with mock.patch("kolibri.utils.server.check_output", return_value="any repo"): install_type = server.installation_type() assert install_type == "apt"
def test_dpkg(self): with mock.patch("kolibri.utils.server.check_output", return_value=""): install_type = server.installation_type() assert install_type == "dpkg"
def test_pex(self): install_type = server.installation_type() assert install_type == "pex"
def handle(self, *args, **options): if not SUPPORTED_OS: print("This OS is not yet supported") sys.exit(1) try: get_kolibri_use() except NotRunning: sys.exit("Profile command executed while Kolibri server was not running") get_requests_info() self.messages = [] self.add_header("Sessions") session_parameters = ( "Active sessions (guests incl)", "Active users in (10 min)", "Active users in (1 min)", ) session_info = get_db_info() self.add_section(session_parameters, session_info) self.add_header("CPU") kolibri_cpu, kolibri_mem = get_kolibri_use() used_cpu, used_memory, total_memory, total_processes = get_machine_info() cpu_parameters = ("Total processes", "Used CPU", "Kolibri CPU usage") cpu_values = ( total_processes, "{} %".format(used_cpu), "{} %".format(kolibri_cpu), ) self.add_section(cpu_parameters, cpu_values) self.add_header("Memory") memory_parameters = ("Used memory", "Total memory", "Kolibri memory usage") memory_values = ( "{} Mb".format(used_memory), "{} Mb".format(total_memory), "{} Mb".format(kolibri_mem), ) self.add_section(memory_parameters, memory_values) self.add_header("Channels") channels_stats = get_channels_usage_info() self.messages.append(format_line("Total Channels", str(len(channels_stats)))) for channel in channels_stats: self.messages.append("\033[95m* {}\033[0m".format(channel.name)) self.messages.append(format_line("Accesses", channel.accesses, True)) self.messages.append(format_line("Time spent", channel.time_spent, True)) self.add_header("Requests timing") requests_stats = get_requests_info() requests_parameters = ("Homepage", "Recommended channels", "Channels") self.add_section(requests_parameters, requests_stats) self.add_header("Device info") instance_model = InstanceIDModel.get_or_create_current_instance()[0] self.messages.append(format_line("Version", kolibri.__version__)) self.messages.append(format_line("OS", instance_model.platform)) self.messages.append( format_line("Installer", installation_type(get_kolibri_process_cmd())) ) self.messages.append( format_line("Database", settings.DATABASES["default"]["NAME"]) ) self.messages.append(format_line("Device name", instance_model.hostname)) self.messages.append( format_line( "Free disk space", "{} Mb".format(get_free_space() / pow(2, 20)) ) ) self.messages.append(format_line("Server time", local_now())) self.messages.append(format_line("Server timezone", settings.TIME_ZONE)) self.messages.append("") print("\n".join(self.messages))