1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Collection of host system information including software versions, memory,
9 storage, and database configuration.
14 from pathlib import Path
15 from typing import List, Optional, Tuple, Union, cast
18 from psycopg2.extensions import make_dsn, parse_dsn
20 from nominatim.config import Configuration
21 from nominatim.db.connection import connect
22 from nominatim.typing import DictCursorResults
23 from nominatim.version import version_str
26 def convert_version(ver_tup: Tuple[int, int]) -> str:
27 """converts tuple version (ver_tup) to a string representation"""
28 return ".".join(map(str, ver_tup))
31 def friendly_memory_string(mem: float) -> str:
32 """Create a user friendly string for the amount of memory specified as mem"""
33 mem_magnitude = ("bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
35 # determine order of magnitude
40 return f"{mem:.1f} {mem_magnitude[mag]}"
43 def run_command(cmd: Union[str, List[str]]) -> str:
44 """Runs a command using the shell and returns the output from stdout"""
46 if sys.version_info < (3, 7):
47 cap_out = subprocess.run(cmd, stdout=subprocess.PIPE, check=False)
49 cap_out = subprocess.run(cmd, capture_output=True, check=False)
50 return cap_out.stdout.decode("utf-8")
51 except FileNotFoundError:
52 # non-Linux system should end up here
53 return f"Unknown (unable to find the '{cmd}' command)"
56 def os_name_info() -> str:
57 """Obtain Operating System Name (and possibly the version)"""
59 # man page os-release(5) details meaning of the fields
60 if Path("/etc/os-release").is_file():
61 os_info = from_file_find_line_portion(
62 "/etc/os-release", "PRETTY_NAME", "=")
63 # alternative location
64 elif Path("/usr/lib/os-release").is_file():
65 os_info = from_file_find_line_portion(
66 "/usr/lib/os-release", "PRETTY_NAME", "="
69 # fallback on Python's os name
70 if os_info is None or os_info == "":
73 # if the above is insufficient, take a look at neofetch's approach to OS detection
77 # Note: Intended to be used on informational files like /proc
78 def from_file_find_line_portion(
79 filename: str, start: str, sep: str, fieldnum: int = 1
81 """open filename, finds the line starting with the 'start' string.
82 Splits the line using seperator and returns a "fieldnum" from the split."""
83 with open(filename, encoding='utf8') as file:
86 if line.startswith(start):
87 result = line.split(sep)[fieldnum].strip()
91 def get_postgresql_config(version: int) -> str:
92 """Retrieve postgres configuration file"""
94 with open(f"/etc/postgresql/{version}/main/postgresql.conf", encoding='utf8') as file:
95 db_config = file.read()
99 return f"**Could not read '/etc/postgresql/{version}/main/postgresql.conf'**"
102 def report_system_information(config: Configuration) -> None:
103 """Generate a report about the host system including software versions, memory,
104 storage, and database configuration."""
106 with connect(make_dsn(config.get_libpq_dsn(), dbname='postgres')) as conn:
107 postgresql_ver: str = convert_version(conn.server_version_tuple())
109 with conn.cursor() as cur:
111 SELECT datname FROM pg_catalog.pg_database
112 WHERE datname='{parse_dsn(config.get_libpq_dsn())['dbname']}'""")
113 nominatim_db_exists = cast(Optional[DictCursorResults], cur.fetchall())
114 if nominatim_db_exists:
115 with connect(config.get_libpq_dsn()) as conn:
116 postgis_ver: str = convert_version(conn.postgis_version_tuple())
118 postgis_ver = "Unable to connect to database"
120 postgresql_config: str = get_postgresql_config(int(float(postgresql_ver)))
122 # Note: psutil.disk_partitions() is similar to run_command("lsblk")
124 # Note: run_command("systemd-detect-virt") only works on Linux, on other OSes
125 # should give a message: "Unknown (unable to find the 'systemd-detect-virt' command)"
127 # Generates the Markdown report.
131 Use this information in your issue report at https://github.com/osm-search/Nominatim/issues
132 Redirect the output to a file:
133 $ ./collect_os_info.py > report.md
136 **Software Environment:**
137 - Python version: {sys.version}
138 - Nominatim version: {version_str()}
139 - PostgreSQL version: {postgresql_ver}
140 - PostGIS version: {postgis_ver}
141 - OS: {os_name_info()}
144 **Hardware Configuration:**
145 - RAM: {friendly_memory_string(psutil.virtual_memory().total)}
146 - number of CPUs: {psutil.cpu_count(logical=False)}
147 - bare metal/AWS/other cloud service (per systemd-detect-virt(1)): {run_command("systemd-detect-virt")}
148 - type and size of disks:
149 **`df -h` - df - report file system disk space usage: **
151 {run_command(["df", "-h"])}
154 **lsblk - list block devices: **
156 {run_command("lsblk")}
160 **Postgresql Configuration:**
165 Please add any notes about anything above anything above that is incorrect.