levisim.core.elk¶
levisim-logs: Elasticsearch and Kibana for levisim’s logs, without Docker
Elastic publishes self-contained archives of both (each bundles its own Java or Node.js), so on macOS (Apple silicon) and Linux (x86_64 and aarch64) the stack can run as two ordinary background processes. This module downloads them, checks them against Elastic’s SHA-512 sums, writes minimal single-node configs (security off, bound to this machine only), starts and stops them, and loads levisim’s JSON-lines log file into Elasticsearch:
levisim-logs install # download and unpack into ~/.levisim/elk/<version>/ (~1 GB download)
levisim-logs up # start both, wait until healthy, load the log file, create the data view
levisim-logs open # print (and open) the Kibana Discover URL for the logs
levisim-logs status # installed? running? reachable? how many documents?
levisim-logs ingest # load the log file(s) again, e.g. after working with the stack down
levisim-logs down # stop both
python -m levisim.core.elk ... is the same command. ingest, open and status also
work against the Docker stack of docker-compose.yaml, which is the fallback where no native archive
exists (Intel Macs: Elasticsearch has published no darwin-x86_64 archive since 9.5.4; Windows).
Environment variables: LEVISIM_ELK_HOME (default ~/.levisim/elk), LEVISIM_ELK_VERSION (default
ELK_VERSION), LEVISIM_LOG_DIR for the log files (see levisim.core.logger), and, for
status, ingest and open only, LEVISIM_ELASTICSEARCH_URL (default http://localhost:9200)
and LEVISIM_KIBANA_URL (default http://localhost:5601). up always runs the local stack on ports
9200 and 5601, and prepares and ingests into that.
Only the standard library is used: the elasticsearch client package is not needed here.
- exception levisim.core.elk.ElkError[source]¶
Bases:
RuntimeErrorA failure the command line reports as one message, without a traceback
- class levisim.core.elk.ElkPaths(root, version)[source]¶
Bases:
objectWhere one version of the stack lives:
<root>/<version>/{<product>-<version>, data, logs, run}- property base¶
- property logs¶
- property run¶
- property downloads¶
- levisim.core.elk.detect_platform(system=None, machine=None)[source]¶
Elastic’s archive platform name for this machine: darwin-aarch64, linux-x86_64 or linux-aarch64
darwin-x86_64 is returned for an Intel Mac, for which no current Elasticsearch archive exists; a Python running under Rosetta on Apple silicon is recognised as darwin-aarch64.
- levisim.core.elk.archive_url(product, version, platform_name)[source]¶
https://artifacts.elastic.co/downloads/<product>/<product>-<version>-<platform>.tar.gz
- levisim.core.elk.parse_sha512_file(text, expected_filename=None)[source]¶
The hex digest from an Elastic
.sha512file (<digest> <filename>)
- levisim.core.elk.download_verified(url, destination_dir, progress=True, chunk_size=1048576)[source]¶
Downloads url into destination_dir, checking it against
url + '.sha512'An archive already there with the right checksum is not downloaded again. On a mismatch the partial file is deleted and ElkError is raised. Returns the archive’s path.
- levisim.core.elk.extract_archive(archive, destination_dir)[source]¶
Unpacks a .tar.gz into destination_dir safely; returns the top-level directory it created
- levisim.core.elk.install(version=None, root=None, platform_name=None, force=False, keep_archives=False, progress=True)[source]¶
Downloads, verifies and unpacks Elasticsearch and Kibana version for this platform
Both archives are checked to exist before either is downloaded, so an unavailable platform fails fast. Returns the ElkPaths of the installation.
- levisim.core.elk.write_configs(paths, es_port=9200, kibana_port=5601)[source]¶
Writes config/elasticsearch.yml and config/kibana.yml, keeping each shipped file once as .orig
Returns the two paths written.
- levisim.core.elk.pid_is_running(pid, expect=None)[source]¶
True if process pid exists (and, given expect, its command line contains that text)
- levisim.core.elk.read_pid(paths, product)[source]¶
The pid of our running product, or None; a stale pid file (dead or reused pid) is removed
- levisim.core.elk.start_process(paths, product, extra_env=None)[source]¶
Starts bin/<product> in its own session, output to logs/<product>.out; returns the pid
- levisim.core.elk.stop_process(paths, product, timeout=30.0)[source]¶
Stops our product: SIGTERM to its process group, then SIGKILL to whatever is left of the group after timeout seconds. Returns once the whole group has gone (or, if even SIGKILL leaves something behind, after a further few seconds); True if it was running.
The whole group is waited for, not only the process started: bin/elasticsearch and bin/kibana are launcher scripts whose children (the Java server, the Node process) can outlive them.
- levisim.core.elk.elasticsearch_health(url=None, timeout=3)[source]¶
The cluster health status (‘green’, ‘yellow’, ‘red’), or None if Elasticsearch does not answer
- levisim.core.elk.kibana_level(url=None, timeout=3)[source]¶
Kibana’s overall status level (‘available’, ‘degraded’, …), or None if it does not answer
- levisim.core.elk.wait_until(check, timeout, interval=2.0, alive=None, what='service')[source]¶
Polls check() until truthy; stops early if alive() turns False. Returns check()’s value or None
- levisim.core.elk.up(version=None, root=None, timeout=180.0, heap='1g', run_ingest=True)[source]¶
Starts Elasticsearch, then Kibana, waits for both, and prepares the index and the data view
The ports are always 9200 and 5601 on localhost (LEVISIM_ELASTICSEARCH_URL and LEVISIM_KIBANA_URL are for the other commands). Anything already answering on them (e.g. the Docker stack) is used rather than started. Unless run_ingest is False, the log files are then loaded with
ingest().
- levisim.core.elk.down(version=None, root=None, timeout=30.0)[source]¶
Stops Kibana, then Elasticsearch, if levisim-logs started them; returns the products stopped
- levisim.core.elk.status(version=None, root=None)[source]¶
Prints what is installed, running and reachable; returns True if both services answer
- levisim.core.elk.ensure_index(url=None, index='loguru-logs')[source]¶
Puts an index template with levisim’s mapping, and creates index if it does not exist
The template (for
<index>*) also covers an index the live Elasticsearch sink creates first. Returns True if the index exists now.
- levisim.core.elk.log_files(log_dir=None)[source]¶
The live log file and its rotated predecessors, oldest first
- levisim.core.elk.ingest(files=None, url=None, index='loguru-logs', batch_size=1000, max_batch_bytes=5000000, quiet=False)[source]¶
Loads JSON-lines log files into Elasticsearch, idempotently
Every line gets the id
levisim.core.logger.log_line_id()gives it, the same id the live Elasticsearch sink uses, so lines already indexed (live, or by an earlier ingest) are counted as existing rather than duplicated. Lines that are not JSON objects are skipped and counted.- Parameters:
files – paths to read; default the live log file and its rotated predecessors
url – Elasticsearch URL; default LEVISIM_ELASTICSEARCH_URL or http://localhost:9200
batch_size – documents per _bulk request
max_batch_bytes – a request is also sent once its body reaches this size
- Returns:
files, lines, created, existing, malformed, failed
- Return type:
dict of counts
- levisim.core.elk.ensure_data_view(url=None, title='loguru-logs*', view_id='levisim-loguru-logs')[source]¶
Makes sure Kibana has a data view for title with time field
timestampReturns the id of the data view (an existing one with the same title is reused), or None if Kibana does not answer.
- levisim.core.elk.discover_url(url=None, view_id='levisim-loguru-logs', since='now-24h')[source]¶
Kibana Discover on the levisim logs, newest first