# Activity log

Who signed in, changed what, and did what to which machine: *Settings > Activity*, for admins.

- **The record is a set of text files** in the data folder: `audit.log`, and before it
  `audit.log.1` to `audit.log.9`, the oldest last. One entry a line, in JSON: `time` (seconds
  since 1970), `event`, `ip`, `user` (the account, or a token's name with " (token)"), and what
  it concerned: a machine by its name (`device`) and, since 0.2.0, its id (`device_id`). They
  are written first, by a thread of their own, as they were in 0.1.x; never rewritten; what
  `--audit-syslog` sends on; what grep and tail read when qKVM is down. When the newest is
  full it becomes `audit.log.1`, each of the others one older, and the oldest goes; a step of
  that which fails (a file held open by another program, on Windows) is taken up again at the
  next entry, from where it stopped, and no other file goes. A line cut off by a power cut is
  ended before the next entry, which is not lost with it.
- **How much is kept:** 50 MB, in ten files of 5 MB. A busy wall (64 machines with alerts
  on, three people working at them: live views, keyboards taken, actions, sign-ins, some 1,200
  entries a day) writes about 190 kB a day: 50 MB is some nine months of it. An entry is 90 to
  180 bytes (since 0.4.0 some 136 bytes more, its place in the chain: about five months then);
  a live view is two, keyboard and mouse taken and given back two more. At the worst,
  64 machines whose state changes at every look of the alerts (every half minute) write some
  27 MB a day, and 50 MB is two days of that. The size is `activity_mb`
  (10 to 100000; nothing, or 0, is 50), read at the start. Change it with
  [offline server settings](https://qkvm.com/docs/advanced-server-settings). A wall whose settings say a
  size already keeps it.
- **The page reads an index of it**, `audit.sqlite` beside the files: the same entries, with
  what they are found by, so that a search of a long log is answered at once (on a test PC, a
  100 MB log of 630,000 entries: a word that is nowhere in 0.2 s, one machine's alerts on a
  day a year ago in 0.2 s, the newest page in a few thousandths; reading the files for the same
  takes 2 to 5 s). It is made from the files and from nothing else, and holds what they hold
  and no more: an entry that goes from the files goes from it. It takes about two and a half
  times the files' size (some 125 MB beside their 50 MB, as it comes). At the first start of
  0.2.0 it is made from the files an earlier version wrote (a few seconds for 20 MB), and
  again whenever it is damaged, missing, of another
  version, or finds that a file no longer holds what it read of it (a power cut can take the
  last lines of a text file, which is not written through to the disk at every entry, and leave
  them in the index) (some 15 s for 100 MB): nothing of the log is lost, and the log of qKVM
  says so.
  Nothing waits for it: the text files are written whatever it does, the wall answers as fast,
  and where it is behind (being made, its disk refusing) the page reads the files themselves,
  slower, with the same answers, for 5 s at the most (a page of a download, 15 s, and 20 s at
  the most with its wait for the index, below): a question that takes longer there is answered
  503 `activity_unavailable`, to be asked again in a moment or narrowed down. Whenever its
  `-wal` file has grown past 16 MB, what that holds goes into the index once qKVM's own reads
  under way have ended, and a read asked for meanwhile waits until that is done (two seconds at
  the most, with its wait for the index to catch up; then 503 `activity_unavailable`, to be
  asked again in a moment), as the history's reads do: reads that kept coming, one beginning
  before the other ended, kept it from ever being done. A program outside qKVM that reads the
  index that way, or keeps a read of it open, still can; qKVM then waits for it a second at
  each try (once a minute), and the log's reads wait that second with it and are answered. A
  download waits so too, its first page two seconds as a read does and each page
  after it up to 15 s, and only then, or where the index is made again meanwhile, goes on from
  the files: after the last entry the index gave it (from the newest where it gave none), each
  entry once (refused with 503 `activity_unavailable` where the files no longer hold the last
  page it was given). It leaves the record its room: where the disk has less free than two text
  files and 32 MB more, it waits (the log of qKVM says so), and the page reads the files.
  Where two of the files begin with the same line (a copy made by hand), it steps aside until
  one of them goes, and is made again then. The newest file's last line counts once it has
  ended, for the index and the reading of the files alike (a line written into the file by
  hand without its end is run into by qKVM's next entry, as in 0.1.x, and is no entry for
  either; one cut off by a power cut is ended at the next start).
- **In the page** the log is searched (under the names the page shows, or the ones in the
  file), narrowed to sign-ins, actions on machines, changes or alerts, to a machine (what was
  done to it; what was written before 0.2.0 named it only, and is found by the name it has
  now), to an account or a token (typed: one that was removed is in the log all the same), and
  to a time from and to; older entries come a page at a time (those of the moment a page ended
  in come on the next one: a removal of many machines can write hundreds with one time; of a
  moment with more than 500, the newest 500 are shown). It is downloaded as a table (CSV)
  or as the lines themselves, all of it or what is shown: made a page at a time, however long
  it is (never the whole log in memory: beyond 8 MB in a temporary file in the data folder,
  gone once it is sent, or at the next start where a power cut left it), and sent once it is
  whole, with its length, so that one cut off on its way shows as cut off; one that cannot be
  made whole is refused (503 `activity_unavailable`). It takes none of the record's room: where
  the disk would keep less than the index waits for, it is refused (507 `not_saved`). Three are
  made or sent at once, one of each account (beyond that, 503 `busy`), and one whose asker went
  away is made no further. What goes from the log while a long one
  is made (the oldest file, when the newest is full) is not in it; made from the files
  themselves, it is of the files as they were when it began (on a share, the newest file is
  then moved on only once it is made: nothing is lost, the file grows past its size meanwhile).
  Enlarged, a tile shows an admin the last three things done to its machine beside its
  history (live views left out: "Nothing but live views" where its last 100 entries are only
  those), and *All of it* opens the log on that machine.
- **What is written:** sign-ins and sign-outs, wrong passwords and codes; every change of the
  settings, accounts, tokens, machines and saved actions; every action on a machine, and one
  that failed; a live view opened and ended, with how long it was open; keyboard and mouse
  taken, taken over and given back; a server's console opened, or asked for and not had;
  alerts (one of a [watched screen](https://qkvm.com/docs/alerts#watching-a-screen) with `watch`, `minutes`, `back` where it
  is over, and `picture` and `picture_at` where a picture was kept); a file of the wall exported or taken in, and a download of this log with what it was
  of. Never a password, a key pressed, or a text that was typed. An account's live views write
  120 entries an hour at the most, and the next one says how many were left out: two entries a
  view, a script opening lines in a loop would otherwise push out of the log what was done
  before it. The same for a server's console opened, and for one asked for and not had: 120
  entries an hour of each account, each; and an opening of a server that no entry of that
  account named in the last hour is written whatever is left, so that every server whose
  console an account opened is named in the log within the hour (openings beyond that are
  counted).
- **Read in turns,** as the history is: one read at a time of each account (its next waits its
  turn, and after a read as long again as it took), three at once from everybody together;
  beyond that, 503 `busy` with `Retry-After`. A read of the index is given up after 5 s
  (503 `activity_unavailable`: narrow it down). A read that waited asks again whether its asker
  may still have it (signed out, removed, or no longer an admin meanwhile: refused), and so does
  a download before each page.
- **A chain that shows an edit (since 0.4.0).** Each line the wall writes carries, at its end,
  its place in a chain: `"chain": {"n": 1234, "prev": "...", "mac": "..."}`. `n` counts the
  lines from the first the log ever had. `prev` is the head of the chain before the line (64
  hex digits), and `mac` signs the head after it (32 hex digits). The head after line `n` is
  SHA-256 over, one after the other: the text `qkvm activity log link 1` and a line feed, the
  head before it (32 bytes), `n` as 8 bytes (the highest first), and the SHA-256 of the line's
  canonical form. The canonical form is the line's fields but `chain`, as JSON with the names
  in the order of their characters (code points), no blanks, each character beyond ASCII as a
  `\u` escape (two of them beyond `\uffff`), whole numbers in decimal and others in the shortest
  form that reads back the same (`1763300000.123456`, `0.1`, `1e-07`), `true`, `false` and
  `null`: what Python's `json.dumps(fields, sort_keys=True, separators=(",", ":"))` writes. So
  a line hashes the same on every system and Python, in the files and in a download. `mac` is
  the first 16 bytes of HMAC-SHA256 over the text `qkvm activity log mac 1`, a line feed and
  the head, with the log's key. The writer does this as it writes, in its order, one line at
  a time; a line cut off by a power cut is no line of the chain, and the log checks to its last
  whole line. It costs some 136 bytes a line (a typical entry of 164 bytes becomes 300, so the
  same `activity_mb` keeps some 45% fewer lines: about five months of the busy wall above, where
  it was nine) and some 14 microseconds of the writer's time; nobody waits for it.
- **The key** is made at the first start of 0.4.0 and kept in the wall's settings, which are
  sealed with the vault key outside the data folder, never in the activity log and never in
  the clear. The settings also keep where the chain had come to: the line and head of its last
  seal, kept at each (at the start and the stop, every hour, the daily seal). Before its first
  line the wall holds the files to that, and what it finds wrong there it says in a seal of its
  own (`broken`), which every check counts as a problem.
- **What is caught**, of somebody who edits the files or a copy of them and does not hold the
  vault key: a line changed, taken out, moved or put in among chained lines; the log written
  anew with the chain worked out again; the chain taken away from lines (to have them taken in
  as an earlier version's: a later start says so), from all of it (a chain is begun again only
  where the settings know of none), or from seals; lines put in where the wall did not stop;
  the end cut off while the wall ran (its own last head), or while it was stopped (the settings
  say where it had come to); the oldest file taken away without its seal.
- **What is not caught:** somebody who holds the running wall, or the data folder together with
  the vault key (as it comes, the vault key lies in a folder beside the data folder: whoever has
  the whole disk has both). They can write the log anew and sign it, settings and all; only heads
  kept elsewhere show that (below). Nor, without such heads: lines written after the last seal
  the settings kept, cut off after a crash (an hour's at the most; a wall that stops as it should
  keeps its stop seal); settings put back from an older copy together with a log cut back to it;
  and lines put in after a stop seal while the wall was stopped, which look as an earlier
  version's do (`qkvm --roll-back`): they are taken in at the next start, and every check says
  so as lines not proved, never as plainly intact. A log put back from a backup without the
  settings, or deleted, shows as broken: each start writes a seal that says so until the chain
  is past the line the settings keep, and every check names those seals until the log lets
  their file go as it grows (put back the settings and the log together, from one backup, and
  the start finds them as they were).
  The key stays the same for the life of the wall: a wall that goes back to 0.3.2 keeps it in
  its settings, and a password reset does not touch it. A wall whose settings are lost and
  made anew makes a new key, and its earlier lines then check as not signed by it.
- **Seals** are lines of the chain written by the wall itself, `log_sealed` with `why`:
  `genesis`, the first line of the chain, which takes in the lines an earlier version wrote as
  they are (`before`: how many, numbered from the first line of the oldest file; `files`: the
  line and head at the end of each of their files), so that they are checked as "from before
  the chain" and never as broken; `start` and `stop`, as the wall begins and ends to serve
  (`key`: what the key it signs with is known by, never the key); `hourly`, every hour in which
  something was written; `pruned`, written just before the oldest file goes: the last line it
  holds and the head after it (`to`, `head`), or of a file from before the chain how many lines
  it holds (`let_go`), so that what the log lets go of as it grows is not missing; `unchained`,
  lines without the chain that a start finds right after a stop seal (an earlier version ran in
  between, `qkvm --roll-back`), none of them a seal, taken in as they are (`count`) and said as
  not proved; `broken`, what a start found wrong (`said`; `held`: where the settings said the
  chain had come to), always a problem; and `daily`, for the alert destinations below. A run
  whose stop seal is missing (a crash, a power cut, the program killed) is said by a check, and
  is not broken.
- **Heads kept outside the wall.** Every line goes to syslog with its chain
  (`--audit-syslog`): what a collector keeps is the head of every line. And an alert destination
  with *Daily seal of the activity log* ticked (not offered for PagerDuty, where every event
  opens an incident) is sent, ten minutes after each start and then once a day (within a pause
  too; in its quiet hours, when they end), a message that is no trouble: `qKVM activity log seal: line N head H`,
  through the alerts' own way out, with no secret in it and nothing of what the log says. With
  those heads kept elsewhere, even a log written anew with the key shows.
- **Check the log** (*Settings > Activity*, beside the downloads) reads the files through on the
  wall (some 3 s for 50 MB), with the key, and against the head the wall wrote last and the place
  its settings keep, in whatever order the lines are. It says
  intact, with the first and last line and the head; or broken at line N, and what: a line not
  as the wall wrote it (its signature does not match: changed, or written by somebody without
  the key), lines missing, a line out of its order or there twice, a line put in without the
  chain, lines from before the chain not as they were when it began, the oldest lines gone with
  no seal saying they were let go, a seal that does not fit, the end cut off while the wall ran
  or while it was stopped, and what a start found wrong. Intact means the lines are as the wall
  wrote them. It cannot show an older copy of the whole data folder put back (the settings and
  the log together, from a backup or a copy of the disk), which checks as the wall wrote it then:
  only heads kept outside the wall show that (below).
  One check at a time for the whole wall; the check itself is then a line of the log
  (`log_checked`: who checked, whether it was intact, the last line and its head). Lines not
  proved are said wherever the check is (`unproved`: how many, in its answer and its line):
  such a log is intact only where it is chained, never plainly.
- **Away from the wall:** `python -m qkvm.checklog FILE`, where `FILE` is the log's own files,
  `audit.log.9` to `audit.log`, the oldest first, or a download as JSON lines (all of it: what a
  search leaves out shows as missing; a table, CSV, cannot be checked). It reads them and
  nothing else. Without a key it checks how the lines hang together; with `--key-file KEY`
  each signature too (`python -m qkvm.checklog --save-key KEY --data-dir DATA` writes the key
  into a new private file: whoever has it can write lines that check, so keep it as the vault
  key is kept), or with `--data-dir DATA` the key is read from the wall's settings (with
  `--vault-key-file` where the vault key is elsewhere), and the log is held to the place the
  settings keep for its chain as well. `--heads FILE` holds the log to heads kept outside: lines
  as syslog keeps them, the daily seal messages, or lines `N HEX`.
  What is given is checked as the whole log: where the settings, or a head kept outside, name a
  line after its last, lines were cut off its end, and it is broken, in whatever order its lines
  are. A download ends where it was made, and the wall writes on: check it with `--part`. Lines
  known to come after it are then said, and it is intact as far as it goes and not proved
  complete (compare with when it was downloaded). Only you say that a file is a part: the order
  of its lines never does. A log cut short shows in a part too once the wall has written after
  it, as the numbers it gives again do not fit the heads kept, or are missing.
  Intact cannot show an older copy of the whole data folder put back (the settings and the log
  together), which checks as the wall wrote it then: only heads kept outside the wall show that.
  `--json` says it as JSON (`part`, and `later`: the last line known to come after a part).
  Exit status 0 intact; 1 broken; 2 could not check (a file that cannot be read, a CSV, a log
  without the chain, a key that is not the log's); 3 intact where it is chained, with lines not
  proved (taken in after the wall stopped, as above); 4 (`--part`) intact as far as it goes, and
  not proved complete: lines are known to come after it. Where more than one holds, the first of
  1, 4, 3 and 0: a part known to go on that holds lines not proved is 4 (what it says, and its
  JSON, say both).
- **The first start of 0.4.0** takes the log in once before its first line (some 3 s for
  50 MB; what is done meanwhile waits in memory and is written right after). A 0.3.2 wall's
  files and index are used as they are; the downloads keep their format, with `chain` added to
  each entry (in the table, under `more`).
- **For a program** (an admin's session; a token is never an admin): `GET /api/v1/audit` with
  `q` (words, any of them, in any case; several `q`), `kind` (`signin`, `action`, `change`,
  `alert`), `device` (a machine's id), `user` (an account's name, or a token's as the log
  writes it, `night script (token)`; in any case),
  `since` and `until` (seconds since 1970), `before` (older than this: the next page),
  `limit` (1 to 500, 100 left out) answers `entries` (newest first, as written), `more`, and
  `missing` (entries that could not be written, or null). `GET /api/v1/audit/export` takes the
  same but `before` and `limit`, and `format` (`csv` or `jsonl`), and answers the file with its
  `Content-Length`, or 503 `activity_unavailable` or `busy`, 507 `not_saved`. `POST
  /api/v1/audit/check` checks the chain and answers `running`, `said` and `result` (`intact`,
  `first`, `last`, `head`, `problems` with `line`, `what` and `said`, `notes`), or 202 with how
  far it got where it takes longer than 20 s, or 409 `no_chain` where the wall writes its log
  without the chain (its key could not be saved at the start: the log of qKVM says why); `GET
  /api/v1/audit/check` answers the same, of the check under way or the last one. Of these, what is
  said of one machine (`GET /api/v1/audit` with `device` and `limit`, which the Desktop App reads) is
  promised in [The API](https://qkvm.com/docs/the-api), with the picture of a screen alert; the rest is the page's own,
  and changes with it. `qkvm status` says how large the files and the index are.
