{"cells":[{"cell_type":"markdown","execution_count":null,"metadata":{"tags":[]},"outputs":[],"source":["TSG121 - Supervisor mssql-server logs\n","=====================================\n","\n","These supervisor mssql-server logs can contain some more information\n","from Polybase, not available in errorlog or the polybase logs.\n","\n","Steps\n","-----\n","\n","### Parameters"]},{"cell_type":"code","execution_count":null,"metadata":{"tags":["parameters"]},"outputs":[],"source":["import re\n","\n","tail_lines = 500\n","\n","pod = None # All\n","container = \"mssql-server\"\n","log_files = [ \"/var/log/supervisor/log/mssql-server-*.log\" ]\n","\n","expressions_to_analyze = [\n"," re.compile(\".{26}[WARN ]\"),\n"," re.compile(\".{26}[ERROR]\")\n","]\n","\n","log_analyzer_rules = []"]},{"cell_type":"markdown","execution_count":null,"metadata":{"tags":[]},"outputs":[],"source":["### Instantiate Kubernetes client"]},{"cell_type":"code","execution_count":null,"metadata":{"tags":["hide_input"]},"outputs":[],"source":["# Instantiate the Python Kubernetes client into 'api' variable\n","\n","import os\n","from IPython.display import Markdown\n","\n","try:\n"," from kubernetes import client, config\n"," from kubernetes.stream import stream\n","except ImportError: \n","\n"," # Install the Kubernetes module\n"," import sys\n"," !{sys.executable} -m pip install kubernetes \n"," \n"," try:\n"," from kubernetes import client, config\n"," from kubernetes.stream import stream\n"," except ImportError:\n"," display(Markdown(f'HINT: Use [SOP059 - Install Kubernetes Python module](../install/sop059-install-kubernetes-module.ipynb) to resolve this issue.'))\n"," raise\n","\n","if \"KUBERNETES_SERVICE_PORT\" in os.environ and \"KUBERNETES_SERVICE_HOST\" in os.environ:\n"," config.load_incluster_config()\n","else:\n"," try:\n"," config.load_kube_config()\n"," except:\n"," display(Markdown(f'HINT: Use [TSG118 - Configure Kubernetes config](../repair/tsg118-configure-kube-config.ipynb) to resolve this issue.'))\n"," raise\n","\n","api = client.CoreV1Api()\n","\n","print('Kubernetes client instantiated')"]},{"cell_type":"markdown","execution_count":null,"metadata":{"tags":[]},"outputs":[],"source":["### Get the namespace for the big data cluster\n","\n","Get the namespace of the Big Data Cluster from the Kuberenetes API.\n","\n","**NOTE:**\n","\n","If there is more than one Big Data Cluster in the target Kubernetes\n","cluster, then either:\n","\n","- set \\[0\\] to the correct value for the big data cluster.\n","- set the environment variable AZDATA\\_NAMESPACE, before starting\n"," Azure Data Studio."]},{"cell_type":"code","execution_count":null,"metadata":{"tags":["hide_input"]},"outputs":[],"source":["# Place Kubernetes namespace name for BDC into 'namespace' variable\n","\n","if \"AZDATA_NAMESPACE\" in os.environ:\n"," namespace = os.environ[\"AZDATA_NAMESPACE\"]\n","else:\n"," try:\n"," namespace = api.list_namespace(label_selector='MSSQL_CLUSTER').items[0].metadata.name\n"," except IndexError:\n"," from IPython.display import Markdown\n"," display(Markdown(f'HINT: Use [TSG081 - Get namespaces (Kubernetes)](../monitor-k8s/tsg081-get-kubernetes-namespaces.ipynb) to resolve this issue.'))\n"," display(Markdown(f'HINT: Use [TSG010 - Get configuration contexts](../monitor-k8s/tsg010-get-kubernetes-contexts.ipynb) to resolve this issue.'))\n"," display(Markdown(f'HINT: Use [SOP011 - Set kubernetes configuration context](../common/sop011-set-kubernetes-context.ipynb) to resolve this issue.'))\n"," raise\n","\n","print('The kubernetes namespace for your big data cluster is: ' + namespace)"]},{"cell_type":"markdown","execution_count":null,"metadata":{"tags":[]},"outputs":[],"source":["### Get tail for log"]},{"cell_type":"code","execution_count":null,"metadata":{"tags":["hide_input"]},"outputs":[],"source":["# Display the last 'tail_lines' of files in 'log_files' list\n","\n","pods = api.list_namespaced_pod(namespace)\n","\n","entries_for_analysis = []\n","\n","for p in pods.items:\n"," if pod is None or p.metadata.name == pod:\n"," for c in p.spec.containers:\n"," if container is None or c.name == container:\n"," for log_file in log_files:\n"," print (f\"- LOGS: '{log_file}' for CONTAINER: '{c.name}' in POD: '{p.metadata.name}'\")\n"," try:\n"," output = stream(api.connect_get_namespaced_pod_exec, p.metadata.name, namespace, command=['/bin/sh', '-c', f'tail -n {tail_lines} {log_file}'], container=c.name, stderr=True, stdout=True)\n"," except Exception:\n"," print (f\"FAILED to get LOGS for CONTAINER: {c.name} in POD: {p.metadata.name}\")\n"," else:\n"," for line in output.split('\\n'):\n"," for expression in expressions_to_analyze:\n"," if expression.match(line):\n"," entries_for_analysis.append(line)\n"," print(line)\n","print(\"\")\n","print(f\"{len(entries_for_analysis)} log entries found for further analysis.\")"]},{"cell_type":"markdown","execution_count":null,"metadata":{"tags":[]},"outputs":[],"source":["### Analyze log entries and suggest relevant Troubleshooting Guides"]},{"cell_type":"code","execution_count":null,"metadata":{"tags":["hide_input"]},"outputs":[],"source":["# Analyze log entries and suggest further relevant troubleshooting guides\n","from IPython.display import Markdown\n","\n","print(f\"Applying the following {len(log_analyzer_rules)} rules to {len(entries_for_analysis)} log entries for analysis, looking for HINTs to further troubleshooting.\")\n","print(log_analyzer_rules)\n","hints = 0\n","if len(log_analyzer_rules) \u003e 0:\n"," for entry in entries_for_analysis:\n"," for rule in log_analyzer_rules:\n"," if entry.find(rule[0]) != -1:\n"," print (entry)\n","\n"," display(Markdown(f'HINT: Use [{rule[2]}]({rule[3]}) to resolve this issue.'))\n"," hints = hints + 1\n","\n","print(\"\")\n","print(f\"{len(entries_for_analysis)} log entries analyzed (using {len(log_analyzer_rules)} rules). {hints} further troubleshooting hints made inline.\")"]},{"cell_type":"code","execution_count":null,"metadata":{"tags":[]},"outputs":[],"source":["print(\"Notebook execution is complete.\")"]}],"nbformat":4,"nbformat_minor":5,"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3"},"pansop":{"related":"","test":{"strategy":"","types":null,"disable":{"reason":"","workitems":null,"types":null}},"target":{"current":"","final":""},"internal":{"parameters":null,"symlink":false},"timeout":"0"},"language_info":{"codemirror_mode":"{ Name: \"\", Version: \"\"}","file_extension":"","mimetype":"","name":"","nbconvert_exporter":"","pygments_lexer":"","version":""},"widgets":[]}}