Skip to content
Running on a schedule

Running discovery on a schedule

Lemano has no daemon. A pass happens when something runs lemano discover: you, a systemd timer or cron (or, after a recorded playbook run, the Ansible callback, which may also start the full package check in the background at low priority; see Recording Ansible runs). This page shows the timer and cron and how Lemano tells you when a pass did not happen. After each pass the jobs below write lemano summary to a file; seeing what changed every day shows what to do with it.

Where to run it

Run the schedule on a machine that is always on and reaches the servers: a bastion, or an administration VM next to them. Not a laptop: while it sleeps, no pass runs, and every server’s blind window opens.

The store lives on that machine, and one machine keeps one store. Two machines that discover the same servers keep two separate histories, each with gaps where the other ran.

The interval

Each target file says how often a pass is expected:

apiVersion: lemano.dev/v1alpha1
kind: Target
metadata:
  name: web-07
spec:
  connector:
    type: ssh
    host: web-07.example.com
  discovery:
    interval: 1h

interval is a duration from 1m to 8760h (30m, 1h, 6h); the default is one hour. Lemano does not use it to start anything. It uses it to judge what it has recorded:

  • Next pass. lemano target list shows, in the NEXT PASS column, when the next pass is due (one interval after the latest pass started, whatever its result), as in 42m, due once that time has come, or overdue 3h when it is later than half an interval (at most ten minutes) past it, the slack a timer’s RandomizedDelaySec needs. lemano target show NAME also states the interval, and -o json and -o yaml carry it as status.schedule.
  • Blind windows. A server is blind for the time nobody looked at it. A blind window opens when no pass succeeded for two intervals: a timer that fires late, a pass that runs long or one missed pass is not a window. lemano history NAME --passes lists the windows, from the end of the last pass that reached the server to the start of the next, or open while it has not been reached since, with the number of unreachable passes inside. A window opens whether the passes ran and failed or did not run at all, so a timer that silently stopped shows up. Nothing is blind before the first pass that reached the server. Windows are computed when shown, from the pass log and the target’s current interval.

Set the timer or cron line to the same rhythm as the interval.

The account that runs it

The schedule runs as an account on the machine that runs Lemano. The examples call it lemano; it is not the lemano login that Getting started creates on each server, and the two need not share a name. The store, its key and the pinned host keys belong to this account, so create it with a home folder, then run lemano init and the first discovery as it:

sudo useradd --create-home --shell /usr/sbin/nologin lemano
sudo -H -u lemano lemano init
sudo -H -u lemano lemano discover -f /etc/lemano/targets --accept-new-host-key

init prints where it put things, and asks you to back up the key file. With the account’s home as the default, they are:

WhatWhere
the store/home/lemano/.local/share/lemano/lemano.db
its key/home/lemano/.config/lemano/key
the pinned host keys/home/lemano/.config/lemano/known_hosts
the latest summary/var/lib/lemano/summary.txt, written by the unit below

A systemd unit with User=lemano runs with that account’s home, so it finds the same files. StateDirectory=lemano only makes /var/lib/lemano for the summary: the store is not there. To keep the store elsewhere, set LEMANO_STORE (or pass --store) in the unit and in your own shell; the key and known_hosts follow XDG_CONFIG_HOME, which an account without a home must also set.

Put the target files in /etc/lemano/targets, and the private keys they name beside them, in a folder only this account can read: with credential: file:../keys/web-07, a key at /etc/lemano/keys/web-07, owned by lemano, mode 0600. A timer or cron job has no SSH agent, so use file:<path> or env:<NAME> rather than agent. Pin the host keys once by hand, as above, before scheduling: an unknown or changed key is refused, and the jobs below do not pass the flag that accepts a new one.

Two passes never overlap: a pass takes the target’s lock, and a second command meets it as exit code 5. A pass that outlasts the interval makes the next one fail with that code, not run twice.

A systemd timer

/etc/systemd/system/lemano-discover.service:

[Unit]
Description=Lemano discovery pass and summary
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
User=lemano
StateDirectory=lemano
ExecStart=/usr/local/bin/lemano discover -f /etc/lemano/targets
ExecStopPost=/usr/local/bin/lemano summary --out /var/lib/lemano/summary.txt

The second step writes the summary of the last 24 hours to /var/lib/lemano/summary.txt, replacing the file in one rename so that a reader never sees half of it. It is an ExecStopPost= line, not a second ExecStart=: systemd runs no further ExecStart= line once one fails, and ExecStopPost= runs after the pass whatever its exit code, so the summary is written after a partial pass too, and says so. StateDirectory=lemano makes systemd create /var/lib/lemano owned by the lemano account.

/etc/systemd/system/lemano-discover.timer:

[Unit]
Description=Run a Lemano discovery pass every hour

[Timer]
OnCalendar=hourly
RandomizedDelaySec=5min
Persistent=true

[Install]
WantedBy=timers.target

Enable it with:

systemctl daemon-reload
systemctl enable --now lemano-discover.timer
systemctl list-timers lemano-discover.timer
  • OnCalendar=hourly is the interval of one hour; for 30m write OnCalendar=*:0/30. Check an expression with systemd-analyze calendar '*:0/30'.
  • Persistent=true stores when the service last ran and, after the host was off at the time a run was due, runs it as soon as the timer is active again. It only has an effect on timers that use OnCalendar=.
  • RandomizedDelaySec=5min delays each run by a random time up to that value, so that several machines running the same timer do not start together. Keep it far below the interval.
  • The unit fails when lemano discover exits with any code other than 0, including 7 (a pass that could not read everything), so OnFailure= and systemctl --failed see a pass that went wrong.

Both unit files are in docs/examples/. Check them with systemd-analyze verify, and start the service once with systemctl start lemano-discover.service: it runs the pass, then the summary, and writes the file.

Telling a change from no change

--detailed-exitcode makes discover exit 8 when at least one generation was written and nothing failed. systemd counts any exit code other than 0 as a failure unless it is listed in SuccessExitStatus=, so a service that uses the flag needs it. A drop-in, /etc/systemd/system/lemano-discover.service.d/detailed-exitcode.conf:

[Service]
ExecStart=
ExecStart=/usr/local/bin/lemano discover --detailed-exitcode -f /etc/lemano/targets
SuccessExitStatus=8
ExecStopPost=/usr/local/bin/lemano-notify

ExecStopPost= runs after every pass with $EXIT_STATUS set to the code (8 after a change, 0 after none), so a script of yours (lemano-notify here) can send a message when it is 8. Any failure still fails the unit.

Cron

A line for /etc/cron.d/lemano-discover (the sixth field is the account):

7 * * * *  lemano  /usr/local/bin/lemano discover -f /etc/lemano/targets >/dev/null; /usr/local/bin/lemano summary --out /var/lib/lemano/summary.txt

It runs at minute 7 of every hour: a minute other than 0 keeps several machines from starting together. >/dev/null drops the results table, so that cron mails the owner only when something is written to standard error, which is what lemano discover does for every failure and every partial pass. The summary after the ; runs whatever the pass’s exit code, and writes to its file, not to standard output. Create /var/lib/lemano, owned by lemano, first. The line is docs/examples/lemano-discover.cron. Cron does not run a job it missed while the machine was off, so a host that is often down is better served by the timer above.

To act on a change, use the flag and read the code:

7 * * * *  lemano  /usr/local/bin/lemano discover --detailed-exitcode -f /etc/lemano/targets >/dev/null; [ $? -eq 8 ] && /usr/local/bin/lemano-notify

Most cron implementations run the line with /bin/sh, in which $? is the exit code of the command before the ;. A failure leaves $? other than 8, so nothing is sent for it, and its message still reaches the mail.

With Ansible

When your targets come from an Ansible inventory, for example made by recording playbook runs, the timer runs lemano ansible discover instead: it reaches each host with the inventory’s own settings and needs no target file. In the service unit:

ExecStart=/usr/local/bin/lemano ansible discover -i /etc/ansible/hosts.yml

or in cron:

7 * * * *  lemano  /usr/local/bin/lemano ansible discover -i /etc/ansible/hosts.yml >/dev/null; /usr/local/bin/lemano summary --out /var/lib/lemano/summary.txt

The account needs to read the inventory, and Ansible on its PATH to read it. Without -i, Ansible’s configured inventory is used, from the account’s ansible.cfg or ANSIBLE_INVENTORY. Its exit codes are those of lemano discover.

Exit codes

See exit codes.

Next