#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""monita-agent - lightweight monitoring agent for the monita server.

Collects CPU / RAM / swap / load / disk metrics, runs local checks (systemd
units, processes, listening ports) and reports them via
mutual-TLS to the monita server. Certificates are obtained puppet-style:
the agent submits a CSR and waits until an administrator accepts it in
the web UI.

Python 3.6+ stdlib only (psutil / cryptography are used if available).
"""

__version__ = "1.2.0"

import argparse
import base64
import configparser
import fnmatch
import hashlib
import http.client
import json
import logging
import os
import platform
import re
import pwd
import shutil
import signal
import socket
import ssl
import stat
import subprocess
import sys
import tempfile
import threading
import time

try:  # optional
    import psutil  # type: ignore
except Exception:  # pragma: no cover
    psutil = None

DEFAULT_CONFIG = "/etc/monita/agent.conf"
DEFAULT_SSLDIR = "/etc/monita/ssl"
DEFAULT_PORT = 443
INSTALL_DIR = "/opt/monita"
INSTALL_PATH = INSTALL_DIR + "/monita-agent.py"
UNIT_PATH = "/etc/systemd/system/monita-agent.service"
DEFAULT_REPORT_INTERVAL = 60
DEFAULT_UPDATE_INTERVAL = 3600

PSEUDO_FS = {
    "tmpfs", "devtmpfs", "overlay", "overlayfs", "squashfs", "proc", "sysfs",
    "devpts", "mqueue", "tracefs", "debugfs", "securityfs", "pstore", "bpf",
    "autofs", "fusectl", "configfs", "nsfs", "ramfs", "hugetlbfs", "efivarfs",
    "binfmt_misc", "rpc_pipefs", "nfsd", "selinuxfs", "cgroup", "cgroup2",
    "rootfs", "aufs", "devfs", "fdescfs", "linprocfs", "linsysfs", "zram",
    "iso9660", "udf", "shm", "none", "fuse.portal", "fuse.gvfsd-fuse",
    "fuse.lxcfs", "fuse.snapfuse", "fuse.xdg-document-portal", "fuse.revokefs-fuse",
    "fuse.jetbrains-toolbox", "fuse.appimagefuse",
}

log = logging.getLogger("monita-agent")
STOP = threading.Event()


class AuthError(Exception):
    """Server says our certificate is revoked / unknown."""


# --------------------------------------------------------------------------
# configuration
# --------------------------------------------------------------------------

class Config(object):
    def __init__(self):
        self.path = DEFAULT_CONFIG
        self.server = None
        self.certname = None
        self.ssldir = DEFAULT_SSLDIR
        self.exclude_fs_types = []
        self.exclude_mounts = []
        self.ca_fingerprint = None
        self.log_level = "INFO"
        self.wait = 30

    @staticmethod
    def _list(v):
        return [x.strip() for x in (v or "").split(",") if x.strip()]

    def load(self, args):
        self.path = getattr(args, "config", None) or DEFAULT_CONFIG
        cp = configparser.ConfigParser()
        if os.path.exists(self.path):
            try:
                cp.read(self.path)
            except Exception as e:
                log.warning("cannot read config %s: %s", self.path, e)
        sec = cp["agent"] if cp.has_section("agent") else {}
        self.server = sec.get("server") or None
        self.certname = sec.get("certname") or None
        self.ssldir = sec.get("ssldir") or DEFAULT_SSLDIR
        self.exclude_fs_types = self._list(sec.get("exclude_fs_types"))
        self.exclude_mounts = self._list(sec.get("exclude_mounts"))
        self.ca_fingerprint = sec.get("ca_fingerprint") or None
        self.log_level = sec.get("log_level") or "INFO"
        try:
            self.wait = int(sec.get("wait") or 30)
        except ValueError:
            self.wait = 30
        # CLI overrides
        for k in ("server", "certname", "ssldir", "ca_fingerprint", "log_level", "wait"):
            v = getattr(args, k, None)
            if v not in (None, ""):
                setattr(self, k, v)
        if not self.certname:
            self.certname = default_certname()
        self.certname = self.certname.strip().lower()
        return self

    # file locations
    @property
    def ca_path(self):
        return os.path.join(self.ssldir, "ca.pem")

    @property
    def key_path(self):
        return os.path.join(self.ssldir, "agent.key")

    @property
    def csr_path(self):
        return os.path.join(self.ssldir, "agent.csr")

    @property
    def crt_path(self):
        return os.path.join(self.ssldir, "agent.crt")

    def host_port(self):
        if not self.server:
            raise SystemExit("no server configured (use --server or set server= in %s)" % self.path)
        return parse_server(self.server)


def parse_server(s):
    s = s.strip()
    for pfx in ("https://", "http://"):
        if s.startswith(pfx):
            s = s[len(pfx):]
    s = s.split("/")[0]
    if s.startswith("["):  # [ipv6]:port
        host, _, rest = s[1:].partition("]")
        port = int(rest[1:]) if rest.startswith(":") else DEFAULT_PORT
        return host, port
    if s.count(":") == 1:
        host, port = s.split(":")
        return host, int(port)
    return s, DEFAULT_PORT


def default_certname():
    fq = socket.getfqdn()
    if not fq or fq.startswith("localhost"):
        fq = socket.gethostname()
    return fq


# --------------------------------------------------------------------------
# facts & metrics
# --------------------------------------------------------------------------

def read_file(path, default=None):
    try:
        with open(path, "r") as f:
            return f.read()
    except Exception:
        return default


def os_release():
    data = read_file("/etc/os-release") or read_file("/usr/lib/os-release") or ""
    for line in data.splitlines():
        if line.startswith("PRETTY_NAME="):
            return line.split("=", 1)[1].strip().strip('"').strip("'")
    return "%s %s" % (platform.system(), platform.release())


def uptime_seconds():
    up = read_file("/proc/uptime")
    if up:
        try:
            return float(up.split()[0])
        except Exception:
            pass
    if psutil is not None:
        try:
            return time.time() - psutil.boot_time()
        except Exception:
            pass
    return None


def primary_ip(server):
    """local IP used to reach the server (no packet is sent) - the address server checks should use"""
    try:
        host, port = parse_server(server)
        fam = socket.AF_INET6 if ":" in host else socket.AF_INET
        info = socket.getaddrinfo(host, port, 0, socket.SOCK_DGRAM)
        fam, _, _, _, addr = info[0]
        s = socket.socket(fam, socket.SOCK_DGRAM)
        try:
            s.connect(addr)
            return s.getsockname()[0]
        finally:
            s.close()
    except Exception:
        return None


def facts(server=None):
    return {
        "primary_ip": primary_ip(server) if server else None,
        "hostname": socket.gethostname(),
        "fqdn": socket.getfqdn(),
        "os": platform.system(),
        "os_release": os_release(),
        "kernel": platform.release(),
        "arch": platform.machine(),
        "cpus": os.cpu_count() or 1,
        "uptime": uptime_seconds(),
        "python": platform.python_version(),
    }


def _unescape_mount(s):
    # /proc/mounts escapes space, tab, newline, backslash as octal
    out, i = [], 0
    while i < len(s):
        if s[i] == "\\" and s[i + 1:i + 4].isdigit() and len(s[i + 1:i + 4]) == 3:
            out.append(chr(int(s[i + 1:i + 4], 8)))
            i += 4
        else:
            out.append(s[i])
            i += 1
    return "".join(out)


class Collector(object):
    def __init__(self, cfg):
        self.cfg = cfg
        self.prev_cpu = None
        self.linux = os.path.exists("/proc/stat")

    # ---- cpu ----
    @staticmethod
    def _read_proc_stat():
        data = read_file("/proc/stat") or ""
        for line in data.splitlines():
            if line.startswith("cpu "):
                vals = [int(x) for x in line.split()[1:]]
                vals += [0] * (8 - len(vals))
                total = sum(vals[:8])  # guest is already in user
                idle = vals[3] + vals[4]
                return total, idle, vals[4]
        return None

    def prime(self):
        """Take an initial CPU sample (1s) so the first report has data."""
        if self.linux:
            self.prev_cpu = self._read_proc_stat()
            STOP.wait(1.0)
        elif psutil is not None:
            try:
                psutil.cpu_percent(interval=None)
                psutil.cpu_times_percent(interval=None)
            except Exception:
                pass
            STOP.wait(1.0)

    def cpu(self):
        if self.linux:
            cur = self._read_proc_stat()
            if cur is None:
                return None
            if self.prev_cpu is None:
                self.prev_cpu = cur
                time.sleep(1.0)
                cur = self._read_proc_stat()
            dt = cur[0] - self.prev_cpu[0]
            di = cur[1] - self.prev_cpu[1]
            dw = cur[2] - self.prev_cpu[2]
            self.prev_cpu = cur
            if dt <= 0:
                return None
            return (max(0.0, min(100.0, (dt - di) * 100.0 / dt)),
                    max(0.0, min(100.0, dw * 100.0 / dt)))
        if psutil is not None:
            try:
                used = psutil.cpu_percent(interval=None)
                iow = getattr(psutil.cpu_times_percent(interval=None), "iowait", 0.0)
                return float(used), float(iow)
            except Exception:
                return None
        return None

    # ---- memory ----
    def memory(self):
        data = read_file("/proc/meminfo")
        if data:
            mi = {}
            for line in data.splitlines():
                parts = line.split()
                if len(parts) >= 2:
                    try:
                        mi[parts[0].rstrip(":")] = int(parts[1]) * 1024
                    except ValueError:
                        pass
            total = mi.get("MemTotal", 0)
            avail = mi.get("MemAvailable")
            if avail is None:
                avail = mi.get("MemFree", 0) + mi.get("Buffers", 0) + mi.get("Cached", 0)
            stotal = mi.get("SwapTotal", 0)
            sfree = mi.get("SwapFree", 0)
            return total, avail, stotal, stotal - sfree
        if psutil is not None:
            try:
                vm = psutil.virtual_memory()
                sw = psutil.swap_memory()
                return vm.total, vm.available, sw.total, sw.used
            except Exception:
                pass
        return None

    # ---- disks ----
    def _mounts(self):
        mounts = []
        data = read_file("/proc/self/mounts") or read_file("/proc/mounts")
        if data:
            for line in data.splitlines():
                p = line.split()
                if len(p) >= 3:
                    mounts.append((_unescape_mount(p[0]), _unescape_mount(p[1]), p[2]))
        elif psutil is not None:
            try:
                for pt in psutil.disk_partitions(all=False):
                    mounts.append((pt.device, pt.mountpoint, pt.fstype))
            except Exception:
                pass
        else:
            mounts.append(("root", "/", "unknown"))
        return mounts

    def _skip_fs(self, fstype, mountpoint):
        ft = fstype.lower()
        if ft in PSEUDO_FS or ft.startswith("cgroup"):
            return True
        if ft.startswith("fuse.") and ft not in (
                "fuse.sshfs", "fuse.glusterfs", "fuse.ceph", "fuse.mergerfs", "fuse.rclone", "fuse.s3fs"):
            return True
        for pat in self.cfg.exclude_fs_types:
            if fnmatch.fnmatch(ft, pat.lower()):
                return True
        for pat in self.cfg.exclude_mounts:
            if fnmatch.fnmatch(mountpoint, pat):
                return True
        if mountpoint in ("/proc", "/sys", "/dev") or mountpoint.startswith((
                "/proc/", "/sys/", "/dev/", "/run/user/", "/var/lib/docker/",
                "/var/lib/containers/", "/snap/")):
            return True
        return False

    def disks(self):
        # one entry per filesystem; for bind mounts / btrfs subvolumes of the same
        # device keep the shortest mountpoint ("/" beats "/etc", "/var", ...)
        best = {}
        for dev, mp, fstype in self._mounts():
            if self._skip_fs(fstype, mp):
                continue
            try:
                st = os.statvfs(mp)
            except Exception:
                continue
            try:
                devid = os.stat(mp).st_dev
            except Exception:
                devid = dev
            key = dev if dev.startswith("/dev/") else (dev, devid)
            if key in best and (len(best[key][0]), best[key][0]) <= (len(mp), mp):
                continue
            best[key] = (mp, st)
        out = []
        for mp, st in sorted(best.values(), key=lambda x: x[0]):
            frsize = st.f_frsize or st.f_bsize
            total = st.f_blocks * frsize
            if total <= 0:
                continue
            free = st.f_bfree * frsize
            avail = st.f_bavail * frsize
            used = total - free
            denom = used + avail
            pct = (used * 100.0 / denom) if denom > 0 else 0.0
            ipct = 0.0
            if st.f_files and st.f_files > 0:
                ipct = (st.f_files - st.f_ffree) * 100.0 / st.f_files
            out.append((mp, total, used, pct, ipct))
        return out

    def collect(self):
        m = []

        def add(metric, value, item=""):
            if value is None:
                return
            m.append({"metric": metric, "item": item, "value": round(float(value), 4)})

        c = self.cpu()
        if c:
            add("cpu.used_pct", c[0])
            add("cpu.iowait_pct", c[1])
        try:
            l1, l5, l15 = os.getloadavg()
            add("load.1", l1)
            add("load.5", l5)
            add("load.15", l15)
            add("load.per_core", l5 / float(os.cpu_count() or 1))
        except (OSError, AttributeError):
            pass
        mem = self.memory()
        if mem:
            total, avail, stotal, sused = mem
            used = total - avail
            add("mem.total_bytes", total)
            add("mem.available_bytes", avail)
            add("mem.used_bytes", used)
            add("mem.used_pct", used * 100.0 / total if total else 0.0)
            add("swap.total_bytes", stotal)
            add("swap.used_bytes", sused)
            add("swap.used_pct", sused * 100.0 / stotal if stotal else 0.0)
        add("uptime.seconds", uptime_seconds())
        for mp, total, used, pct, ipct in self.disks():
            add("disk.total_bytes", total, mp)
            add("disk.used_bytes", used, mp)
            add("disk.used_pct", pct, mp)
            add("disk.inode_used_pct", ipct, mp)
        return m


# --------------------------------------------------------------------------
# local checks (defined on the server, executed here with every report)
#   systemd: unit state      process: running processes      listen: listening ports
# Nothing here runs a shell or arbitrary commands: only "systemctl show" with validated unit names.
# --------------------------------------------------------------------------

ST_OK, ST_WARN, ST_CRIT, ST_UNKNOWN = 0, 1, 2, 3
UNIT_RE = re.compile(r"^[A-Za-z0-9@._:-]+$")


def _int(v, default=None):
    try:
        return int(v)
    except (TypeError, ValueError):
        return default


def check_systemd(cfg):
    units = [u for u in str(cfg.get("unit") or "").replace(",", " ").split() if UNIT_RE.match(u)]
    if not units:
        return ST_UNKNOWN, "no valid unit configured", None
    systemctl = shutil.which("systemctl")
    if not systemctl:
        return ST_UNKNOWN, "systemctl not found on this machine", None
    for unit in units:
        if "." not in unit:
            unit += ".service"
        try:
            p = subprocess.run([systemctl, "show", "--no-pager",
                                "--property=LoadState,ActiveState,SubState,MainPID,ActiveEnterTimestamp", "--", unit],
                               stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, timeout=10)
        except Exception as e:
            return ST_UNKNOWN, "systemctl failed: %s" % e, None
        props = dict(l.split("=", 1) for l in p.stdout.splitlines() if "=" in l)
        if props.get("LoadState") in (None, "", "not-found"):
            continue  # try the next alternative name (e.g. "sshd ssh")
        active, sub = props.get("ActiveState", "?"), props.get("SubState", "?")
        pid = _int(props.get("MainPID"), 0)
        msg = "%s %s (%s)%s" % (unit, active, sub, ", PID %d" % pid if pid else "")
        if active == "active":
            return ST_OK, msg, 1
        if active in ("activating", "reloading", "deactivating"):
            return ST_WARN, msg, 0
        return ST_CRIT, msg, 0
    return ST_CRIT, "unit not found: %s" % ", ".join(units), 0


def _processes():
    """yield (pid, comm, argv0 basename, cmdline, uid) for all processes"""
    me = os.getpid()
    if os.path.isdir("/proc/self"):
        for pid in os.listdir("/proc"):
            if not pid.isdigit() or int(pid) == me:
                continue
            base = "/proc/" + pid
            try:
                with open(base + "/cmdline", "rb") as f:
                    raw = f.read()
                comm = (read_file(base + "/comm") or "").strip()
                uid = os.stat(base).st_uid
            except Exception:
                continue
            argv = [a.decode("utf-8", "replace") for a in raw.split(b"\0") if a]
            yield int(pid), comm, os.path.basename(argv[0]) if argv else "", " ".join(argv) or "[%s]" % comm, uid
    elif psutil is not None:
        for p in psutil.process_iter():
            try:
                if p.pid == me:
                    continue
                argv = p.cmdline()
                yield p.pid, p.name(), os.path.basename(argv[0]) if argv else "", " ".join(argv), p.uids().real
            except Exception:
                continue


def check_process(cfg):
    pattern = str(cfg.get("pattern") or "").strip()
    if not pattern:
        return ST_UNKNOWN, "no process pattern configured", None
    use_regex = cfg.get("match") == "regex"
    rx = None
    if use_regex:
        try:
            rx = re.compile(pattern)
        except re.error as e:
            return ST_UNKNOWN, "invalid regex: %s" % e, None
    want_uid = None
    if cfg.get("user"):
        try:
            want_uid = pwd.getpwnam(str(cfg["user"])).pw_uid
        except KeyError:
            return ST_UNKNOWN, "user %s does not exist" % cfg["user"], None
    n = 0
    for pid, comm, argv0, cmdline, uid in _processes():
        if want_uid is not None and uid != want_uid:
            continue
        if rx is not None:
            hit = rx.search(cmdline) is not None
        else:  # comm is cut to 15 chars by the kernel, so also compare argv[0]
            hit = pattern in (comm, argv0) or (len(pattern) > 15 and comm == pattern[:15])
        if hit:
            n += 1
    mn = _int(cfg.get("min"), 1)
    mx = _int(cfg.get("max"))
    what = "processes matching /%s/" % pattern if use_regex else "'%s' processes" % pattern
    msg = "%d %s%s" % (n, what, " (user %s)" % cfg["user"] if cfg.get("user") else "")
    if mn is not None and n < mn:
        return ST_CRIT, "%s, expected at least %d" % (msg, mn), n
    if mx is not None and n > mx:
        return (ST_WARN if cfg.get("max_severity") == "warning" else ST_CRIT), "%s, expected at most %d" % (msg, mx), n
    return ST_OK, msg, n


def _listening(proto):
    """set of local ports in LISTEN (tcp) / bound (udp) state"""
    ports = set()
    for fam in ("", "6"):
        data = read_file("/proc/net/%s%s" % (proto, fam)) or ""
        for line in data.splitlines()[1:]:
            p = line.split()
            if len(p) < 4:
                continue
            if proto == "tcp" and p[3] != "0A":  # 0A = LISTEN
                continue
            try:
                ports.add(int(p[1].rsplit(":", 1)[1], 16))
            except (ValueError, IndexError):
                pass
    return ports


def check_listen(cfg):
    port = _int(cfg.get("port"))
    proto = "udp" if cfg.get("proto") == "udp" else "tcp"
    if not port:
        return ST_UNKNOWN, "no port configured", None
    if not os.path.exists("/proc/net/" + proto):
        return ST_UNKNOWN, "/proc/net/%s not available" % proto, None
    if port in _listening(proto):
        return ST_OK, "%s port %d is listening" % (proto, port), 1
    return ST_CRIT, "nothing listens on %s port %d" % (proto, port), 0


LOCAL_CHECKS = {"systemd": check_systemd, "process": check_process, "listen": check_listen}


def run_local_checks(checks):
    out = []
    for c in checks or []:
        if not isinstance(c, dict) or "id" not in c:
            continue
        fn = LOCAL_CHECKS.get(c.get("type"))
        try:
            st, msg, val = fn(c.get("config") or {}) if fn else (ST_UNKNOWN, "check type %s not supported by agent %s" % (c.get("type"), __version__), None)
        except Exception as e:  # never let a check break the report
            st, msg, val = ST_UNKNOWN, "check error: %s" % e, None
        out.append({"id": c["id"], "name": c.get("name"), "state": st, "message": msg, "value": val})
    return out


# --------------------------------------------------------------------------
# crypto helpers
# --------------------------------------------------------------------------

def pem_to_der(pem):
    lines = [l.strip() for l in pem.strip().splitlines()]
    body = "".join(l for l in lines if l and not l.startswith("-----"))
    return base64.b64decode(body)


def fingerprint(der):
    h = hashlib.sha256(der).hexdigest().upper()
    return ":".join(h[i:i + 2] for i in range(0, len(h), 2))


def norm_fp(fp):
    return (fp or "").replace(":", "").replace(" ", "").lower()


def _run(cmd, inp=None):
    p = subprocess.run(cmd, input=inp, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                       universal_newlines=True)
    if p.returncode != 0:
        raise RuntimeError("%s failed: %s" % (" ".join(cmd[:2]), p.stderr.strip()))
    return p.stdout


def write_private(path, data, mode=0o600):
    d = os.path.dirname(path)
    fd, tmp = tempfile.mkstemp(dir=d, prefix=".tmp-")
    try:
        os.fchmod(fd, mode)
        with os.fdopen(fd, "w") as f:
            f.write(data)
        os.replace(tmp, path)
    except Exception:
        try:
            os.unlink(tmp)
        except OSError:
            pass
        raise


def ensure_ssldir(cfg):
    if not os.path.isdir(cfg.ssldir):
        os.makedirs(cfg.ssldir, 0o700)


def generate_key_and_csr(cfg):
    ensure_ssldir(cfg)
    cn = cfg.certname
    if shutil.which("openssl"):
        if not os.path.exists(cfg.key_path):
            key = _run(["openssl", "genpkey", "-algorithm", "RSA", "-pkeyopt", "rsa_keygen_bits:2048"])
            write_private(cfg.key_path, key)
        csr = _run(["openssl", "req", "-new", "-key", cfg.key_path, "-subj", "/CN=" + cn.replace("/", "")])
    else:
        try:
            from cryptography import x509
            from cryptography.hazmat.primitives import hashes, serialization
            from cryptography.hazmat.primitives.asymmetric import rsa
            from cryptography.x509.oid import NameOID
        except ImportError:
            raise SystemExit("neither the openssl binary nor the python 'cryptography' module is available")
        if os.path.exists(cfg.key_path):
            with open(cfg.key_path, "rb") as f:
                key = serialization.load_pem_private_key(f.read(), password=None)
        else:
            key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
            write_private(cfg.key_path, key.private_bytes(
                serialization.Encoding.PEM, serialization.PrivateFormat.PKCS8,
                serialization.NoEncryption()).decode())
        req = x509.CertificateSigningRequestBuilder().subject_name(
            x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, cn)])).sign(key, hashes.SHA256())
        csr = req.public_bytes(serialization.Encoding.PEM).decode()
    write_private(cfg.csr_path, csr, 0o644)
    return csr


def ensure_csr(cfg):
    if os.path.exists(cfg.key_path) and os.path.exists(cfg.csr_path):
        return read_file(cfg.csr_path)
    if os.path.exists(cfg.csr_path) and not os.path.exists(cfg.key_path):
        os.unlink(cfg.csr_path)
    return generate_key_and_csr(cfg)


def csr_fingerprint(cfg):
    csr = read_file(cfg.csr_path)
    return fingerprint(pem_to_der(csr)) if csr else None


def cert_matches_key(cfg):
    if not (os.path.exists(cfg.crt_path) and os.path.exists(cfg.key_path)):
        return False
    try:
        ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
        ctx.load_cert_chain(cfg.crt_path, cfg.key_path)
        return True
    except (ssl.SSLError, OSError) as e:
        log.warning("certificate does not match key: %s", e)
        return False


def decode_cert(path):
    try:
        return ssl._ssl._test_decode_cert(path)  # noqa (private but stable for years)
    except Exception:
        return None


def reset_identity(cfg, new_key=True):
    files = [cfg.crt_path, cfg.csr_path] + ([cfg.key_path] if new_key else [])
    for p in files:
        try:
            os.unlink(p)
        except FileNotFoundError:
            pass


# --------------------------------------------------------------------------
# HTTP client
# --------------------------------------------------------------------------

class Client(object):
    def __init__(self, cfg):
        self.cfg = cfg
        self.host, self.port = cfg.host_port()

    def _ctx(self, mtls=True, verify=True):
        if not verify:
            ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
            ctx.check_hostname = False
            ctx.verify_mode = ssl.CERT_NONE
            return ctx
        ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
        ctx.check_hostname = True
        ctx.verify_mode = ssl.CERT_REQUIRED
        ctx.load_verify_locations(self.cfg.ca_path)
        # the shared port may present a public (ACME / Let's Encrypt) certificate
        # for the UI hostname - trust the system CAs as well as the Monita CA
        try:
            ctx.load_default_certs()
        except Exception:
            pass
        if mtls:
            ctx.load_cert_chain(self.cfg.crt_path, self.cfg.key_path)
        return ctx

    def request(self, method, path, body=None, mtls=True, verify=True, timeout=30):
        headers = {"User-Agent": "monita-agent/%s" % __version__, "Accept": "application/json"}
        data = None
        if body is not None:
            data = json.dumps(body).encode("utf-8")
            headers["Content-Type"] = "application/json"
        conn = http.client.HTTPSConnection(self.host, self.port, timeout=timeout,
                                           context=self._ctx(mtls, verify))
        try:
            conn.request(method, path, body=data, headers=headers)
            resp = conn.getresponse()
            raw = resp.read()
            status = resp.status
        finally:
            conn.close()
        if mtls and status in (401, 403):
            err = _json(raw).get("error") if _json(raw) else None
            raise AuthError(err or "HTTP %d" % status)
        return status, raw

    def json(self, method, path, body=None, **kw):
        status, raw = self.request(method, path, body, **kw)
        if status != 200:
            raise RuntimeError("%s %s -> HTTP %d: %s" % (method, path, status, raw[:200]))
        return json.loads(raw.decode("utf-8"))


def _json(raw):
    try:
        v = json.loads(raw.decode("utf-8"))
        return v if isinstance(v, dict) else {}
    except Exception:
        return {}


# --------------------------------------------------------------------------
# certificate bootstrap
# --------------------------------------------------------------------------

def ensure_ca(cfg, client):
    if os.path.exists(cfg.ca_path):
        return True
    ensure_ssldir(cfg)
    status, raw = client.request("GET", "/api/v1/ca", mtls=False, verify=False)
    if status != 200 or b"BEGIN CERTIFICATE" not in raw:
        raise RuntimeError("could not fetch CA certificate (HTTP %d)" % status)
    pem = raw.decode("ascii")
    fp = fingerprint(pem_to_der(pem))
    if cfg.ca_fingerprint and norm_fp(cfg.ca_fingerprint) != norm_fp(fp):
        raise SystemExit("CA fingerprint mismatch! expected %s got %s - refusing" % (cfg.ca_fingerprint, fp))
    write_private(cfg.ca_path, pem, 0o644)
    log.info("stored server CA certificate (SHA256 %s)%s", fp,
             "" if cfg.ca_fingerprint else " - trusted on first use, verify this fingerprint!")
    return True


def submit_csr(cfg, client):
    csr = ensure_csr(cfg)
    status, raw = client.request("POST", "/api/v1/csr",
                                 {"certname": cfg.certname, "csr": csr, "facts": facts()}, mtls=False)
    st = _json(raw).get("status")
    log.info("CSR for '%s' submitted (fingerprint %s): HTTP %d %s",
             cfg.certname, csr_fingerprint(cfg), status, st or "")
    if status == 409:
        log.error("certname '%s' already has a signed certificate with another key - "
                  "an admin has to revoke/delete it in the web UI", cfg.certname)
    elif status == 403:
        log.error("certificate request for '%s' was rejected by the server", cfg.certname)
    return status


def fetch_cert(cfg, client):
    """Returns 'signed', 'pending', 'unknown', 'rejected' or 'error'."""
    status, raw = client.request("GET", "/api/v1/cert/" + cfg.certname, mtls=False)
    if status == 200 and b"BEGIN CERTIFICATE" in raw:
        write_private(cfg.crt_path, raw.decode("ascii"), 0o644)
        if cert_matches_key(cfg):
            log.info("received signed certificate for '%s'", cfg.certname)
            return "signed"
        os.unlink(cfg.crt_path)
        log.error("server returned a certificate that does not match our key "
                  "(old certificate for '%s'?) - an admin has to revoke it", cfg.certname)
        return "mismatch"
    if status == 202:
        return "pending"
    if status == 404:
        return "unknown"
    if status == 403:
        return "rejected"
    log.warning("unexpected answer for cert request: HTTP %d", status)
    return "error"


def ensure_cert(cfg, client, wait=True):
    """Run the CSR flow until a certificate is present. Returns True on success."""
    if os.path.exists(cfg.crt_path) and cert_matches_key(cfg):
        return True
    if os.path.exists(cfg.crt_path):
        os.unlink(cfg.crt_path)
    submitted = False
    announced = False
    while not STOP.is_set():
        delay = cfg.wait
        try:
            ensure_ca(cfg, client)
            if not submitted:
                st = submit_csr(cfg, client)
                submitted = st in (200, 202)
                if st in (403, 409):
                    delay = cfg.wait * 5
            if submitted:
                r = fetch_cert(cfg, client)
                if r == "signed":
                    return True
                if r == "pending":
                    if not announced:
                        log.info("waiting for an administrator to accept '%s' in the web UI "
                                 "(CSR fingerprint %s), polling every %ss",
                                 cfg.certname, csr_fingerprint(cfg), cfg.wait)
                        announced = True
                elif r == "unknown":
                    submitted = False
                elif r in ("rejected", "mismatch"):
                    submitted = False
                    delay = cfg.wait * 5
        except SystemExit:
            raise
        except Exception as e:
            log.warning("certificate bootstrap: %s", e)
        if not wait:
            return False
        STOP.wait(delay)
    return False


# --------------------------------------------------------------------------
# self update
# --------------------------------------------------------------------------

def self_path():
    return os.path.realpath(os.path.abspath(__file__))


def check_update(client):
    info = client.json("GET", "/api/v1/agent/version")
    ver, sha = info.get("version"), (info.get("sha256") or "").lower()
    if not ver or ver == __version__:
        log.debug("agent is up to date (%s)", __version__)
        return False
    log.info("new agent version available: %s (running %s)", ver, __version__)
    status, raw = client.request("GET", "/api/v1/agent/download", timeout=120)
    if status != 200:
        raise RuntimeError("download failed: HTTP %d" % status)
    got = hashlib.sha256(raw).hexdigest()
    if sha and got != sha:
        raise RuntimeError("checksum mismatch (expected %s, got %s)" % (sha, got))
    src = raw.decode("utf-8")
    compile(src, "monita-agent.py", "exec")  # refuse broken code
    if "__version__" not in src:
        raise RuntimeError("downloaded file does not look like the agent")
    path = self_path()
    d = os.path.dirname(path)
    fd, tmp = tempfile.mkstemp(dir=d, prefix=".monita-agent-")
    try:
        with os.fdopen(fd, "wb") as f:
            f.write(raw)
        os.chmod(tmp, 0o755)
        os.replace(tmp, path)
    except Exception:
        try:
            os.unlink(tmp)
        except OSError:
            pass
        raise
    log.info("updated %s to version %s - restarting", path, ver)
    logging.shutdown()
    os.execv(sys.executable, [sys.executable, path] + sys.argv[1:])
    return True  # not reached


# --------------------------------------------------------------------------
# commands
# --------------------------------------------------------------------------

def cmd_run(cfg, args):
    def _stop(signum, frame):
        log.info("signal %d received, stopping", signum)
        STOP.set()

    signal.signal(signal.SIGTERM, _stop)
    signal.signal(signal.SIGINT, _stop)
    client = Client(cfg)
    log.info("monita-agent %s starting (certname %s, server %s:%d)",
             __version__, cfg.certname, client.host, client.port)
    collector = Collector(cfg)
    report_interval = DEFAULT_REPORT_INTERVAL
    update_interval = DEFAULT_UPDATE_INTERVAL
    next_update = 0.0
    checks = []

    while not STOP.is_set():
        if not ensure_cert(cfg, client):
            break
        collector.prime()
        try:  # fetch check definitions so that the first report already contains their results
            checks = client.json("GET", "/api/v1/config").get("checks") or []
        except AuthError:
            pass  # handled by the report below
        except Exception as e:
            log.warning("cannot fetch config: %s", e)
        next_report = time.time()
        try:
            while not STOP.is_set():
                now = time.time()
                if now >= next_report:
                    try:
                        resp = client.json("POST", "/api/v1/report", {
                            "agent_version": __version__,
                            "ts": time.time(),
                            "facts": facts(cfg.server),
                            "metrics": collector.collect(),
                            "check_results": run_local_checks(checks),
                        })
                        conf = resp.get("config") or {}
                        report_interval = max(5, int(conf.get("report_interval") or report_interval))
                        update_interval = max(60, int(conf.get("update_check_interval") or update_interval))
                        if isinstance(conf.get("checks"), list):
                            checks = conf["checks"]
                        log.debug("report sent, next in %ss", report_interval)
                    except AuthError:
                        raise
                    except Exception as e:
                        log.warning("report failed: %s", e)
                    next_report += report_interval
                    if next_report < time.time():
                        next_report = time.time() + report_interval
                if time.time() >= next_update:
                    next_update = time.time() + update_interval
                    try:
                        check_update(client)
                    except AuthError:
                        raise
                    except Exception as e:
                        log.warning("update check failed: %s", e)
                STOP.wait(max(0.5, min(next_report, next_update) - time.time()))
        except AuthError as e:
            log.error("server rejected our certificate (%s) - requesting a new one", e)
            reset_identity(cfg, new_key=True)
            STOP.wait(5)
    log.info("stopped")
    return 0


def cmd_once(cfg, args):
    c = Collector(cfg)
    c.prime()
    print(json.dumps({"agent_version": __version__, "ts": time.time(), "facts": facts(),
                      "metrics": c.collect()}, indent=2))
    return 0


def cmd_request_cert(cfg, args):
    signal.signal(signal.SIGINT, lambda s, f: STOP.set())
    client = Client(cfg)
    ok = ensure_cert(cfg, client, wait=not args.no_wait)
    print("certificate: %s" % ("present" if ok else "not yet signed"))
    return 0 if ok else 1


def cmd_status(cfg, args):
    print("monita-agent version : %s" % __version__)
    print("script               : %s" % self_path())
    print("config               : %s%s" % (cfg.path, "" if os.path.exists(cfg.path) else " (missing)"))
    print("server               : %s" % (cfg.server or "-"))
    print("certname             : %s" % cfg.certname)
    print("ssldir               : %s" % cfg.ssldir)
    if os.path.exists(cfg.ca_path):
        print("CA fingerprint       : %s" % fingerprint(pem_to_der(read_file(cfg.ca_path))))
    else:
        print("CA                   : not fetched yet")
    if os.path.exists(cfg.csr_path):
        print("CSR fingerprint      : %s" % csr_fingerprint(cfg))
    if os.path.exists(cfg.crt_path):
        d = decode_cert(cfg.crt_path) or {}
        print("certificate          : present, %s, valid until %s" % (
            "matches key" if cert_matches_key(cfg) else "DOES NOT MATCH KEY", d.get("notAfter", "?")))
    else:
        print("certificate          : not signed yet")
    if shutil.which("systemctl") and os.path.exists(UNIT_PATH):
        p = subprocess.run(["systemctl", "is-active", "monita-agent"], stdout=subprocess.PIPE,
                           universal_newlines=True)
        print("systemd service      : %s" % p.stdout.strip())
    if cfg.server and os.path.exists(cfg.crt_path) and os.path.exists(cfg.ca_path):
        try:
            conf = Client(cfg).json("GET", "/api/v1/config")
            print("server connection    : OK (report_interval=%s, update_check_interval=%s)" % (
                conf.get("report_interval"), conf.get("update_check_interval")))
        except Exception as e:
            print("server connection    : FAILED (%s)" % e)
    return 0


def _require_systemd_root():
    if platform.system() != "Linux":
        raise SystemExit("install is only supported on Linux")
    if os.geteuid() != 0:
        raise SystemExit("install/uninstall must be run as root")
    if not shutil.which("systemctl") or not os.path.isdir("/run/systemd/system"):
        raise SystemExit("systemd not detected - run '%s run' with your own init system instead" % sys.argv[0])


def _ensure_user(name):
    try:
        pwd.getpwnam(name)
        return
    except KeyError:
        pass
    shell = next((s for s in ("/usr/sbin/nologin", "/sbin/nologin", "/bin/false") if os.path.exists(s)), "/bin/false")
    useradd = shutil.which("useradd")
    if not useradd:
        raise SystemExit("useradd not found - create user '%s' manually or use --user root" % name)
    _run([useradd, "--system", "--no-create-home", "--home-dir", INSTALL_DIR, "--shell", shell, name])
    print("created system user '%s'" % name)


def _chown_tree(path, uid, gid):
    os.chown(path, uid, gid)
    for root, dirs, files in os.walk(path):
        for n in dirs + files:
            os.chown(os.path.join(root, n), uid, gid)


UNIT_TEMPLATE = """[Unit]
Description=monita monitoring agent
Documentation=https://github.com/
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User={user}
ExecStart=/usr/bin/env python3 {path} run --config {config}
Restart=always
RestartSec=10
NoNewPrivileges=true
ProtectSystem=full
ProtectHome=read-only
PrivateTmp=true
ReadWritePaths={install_dir} /etc/monita

[Install]
WantedBy=multi-user.target
"""


def cmd_install(cfg, args):
    _require_systemd_root()
    if not cfg.server:
        raise SystemExit("--server is required")
    os.makedirs(INSTALL_DIR, 0o755, exist_ok=True)
    os.makedirs(os.path.dirname(cfg.path) or "/etc/monita", 0o755, exist_ok=True)
    ensure_ssldir(cfg)
    os.chmod(cfg.ssldir, 0o700)

    src = self_path()
    if src != os.path.realpath(INSTALL_PATH):
        shutil.copyfile(src, INSTALL_PATH)
    os.chmod(INSTALL_PATH, 0o755)
    print("installed agent to %s" % INSTALL_PATH)

    cp = configparser.ConfigParser()
    if os.path.exists(cfg.path):
        cp.read(cfg.path)
    if not cp.has_section("agent"):
        cp.add_section("agent")
    a = cp["agent"]
    a["server"] = cfg.server
    a["certname"] = cfg.certname
    a["ssldir"] = cfg.ssldir
    if cfg.ca_fingerprint:
        a["ca_fingerprint"] = cfg.ca_fingerprint
    a.setdefault("exclude_fs_types", "")
    a.setdefault("exclude_mounts", "")
    a.setdefault("log_level", "INFO")
    with open(cfg.path, "w") as f:
        cp.write(f)
    os.chmod(cfg.path, 0o644)
    print("wrote config %s" % cfg.path)

    # bootstrap: fetch CA and submit CSR right away so the admin sees the request
    try:
        client = Client(cfg)
        ensure_ca(cfg, client)
        print("server CA fingerprint: %s" % fingerprint(pem_to_der(read_file(cfg.ca_path))))
        if not (os.path.exists(cfg.crt_path) and cert_matches_key(cfg)):
            submit_csr(cfg, client)
            fetch_cert(cfg, client)
    except SystemExit:
        raise
    except Exception as e:
        print("WARNING: could not contact server yet (%s); the service will keep retrying" % e)

    user = args.user
    if user != "root":
        _ensure_user(user)
        pw = pwd.getpwnam(user)
        _chown_tree(INSTALL_DIR, pw.pw_uid, pw.pw_gid)
        _chown_tree(cfg.ssldir, pw.pw_uid, pw.pw_gid)

    with open(UNIT_PATH, "w") as f:
        f.write(UNIT_TEMPLATE.format(user=user, path=INSTALL_PATH, config=cfg.path, install_dir=INSTALL_DIR))
    print("wrote %s" % UNIT_PATH)
    _run(["systemctl", "daemon-reload"])
    _run(["systemctl", "enable", "monita-agent"])
    _run(["systemctl", "restart", "monita-agent"])
    print("service monita-agent enabled and started")
    print("")
    if os.path.exists(cfg.crt_path):
        print("Certificate is already signed - the agent is reporting.")
    else:
        print("NEXT STEP: accept the host '%s' in the monita web UI (Hosts -> Pending)." % cfg.certname)
        print("           CSR fingerprint: %s" % csr_fingerprint(cfg))
    print("Logs: journalctl -u monita-agent -f")
    return 0


def cmd_uninstall(cfg, args):
    _require_systemd_root()
    subprocess.run(["systemctl", "disable", "--now", "monita-agent"])
    for p in (UNIT_PATH,):
        if os.path.exists(p):
            os.unlink(p)
    subprocess.run(["systemctl", "daemon-reload"])
    if os.path.isdir(INSTALL_DIR):
        shutil.rmtree(INSTALL_DIR)
    print("removed service and %s" % INSTALL_DIR)
    if args.purge:
        if os.path.isdir("/etc/monita"):
            shutil.rmtree("/etc/monita")
        try:
            pwd.getpwnam("monita")
            if shutil.which("userdel"):
                subprocess.run(["userdel", "monita"])
        except KeyError:
            pass
        print("purged /etc/monita and user 'monita'")
    else:
        print("kept /etc/monita (config + certificates); use --purge to remove")
    return 0


# --------------------------------------------------------------------------
# main
# --------------------------------------------------------------------------

def build_parser():
    common = argparse.ArgumentParser(add_help=False)
    S = argparse.SUPPRESS
    common.add_argument("--config", default=S, help="config file (default %s)" % DEFAULT_CONFIG)
    common.add_argument("--server", default=S, help="server HOST[:PORT] (default port %d)" % DEFAULT_PORT)
    common.add_argument("--certname", default=S, help="certificate name (default: FQDN)")
    common.add_argument("--ssldir", default=S, help="directory for keys/certs (default %s)" % DEFAULT_SSLDIR)
    common.add_argument("--ca-fingerprint", dest="ca_fingerprint", default=S,
                        help="expected SHA256 fingerprint of the server CA (pinning)")
    common.add_argument("--wait", type=int, default=S, help="poll interval while waiting for signing (s)")
    common.add_argument("--log-level", dest="log_level", default=S, help="DEBUG, INFO, WARNING, ERROR")

    p = argparse.ArgumentParser(prog="monita-agent", parents=[common],
                                description="monita monitoring agent %s" % __version__)
    p.add_argument("--version", action="version", version=__version__)
    sub = p.add_subparsers(dest="command")
    sub.add_parser("run", parents=[common], help="run the agent in the foreground (default)")
    sub.add_parser("once", parents=[common], help="collect metrics once and print them as JSON")
    rc = sub.add_parser("request-cert", parents=[common], help="request a certificate and wait for signing")
    rc.add_argument("--no-wait", action="store_true", help="submit/poll once and exit")
    sub.add_parser("status", parents=[common], help="show agent / certificate status")
    ins = sub.add_parser("install", parents=[common], help="install as systemd service (root)")
    ins.add_argument("--user", default="monita", help="service user: monita (default) or root")
    un = sub.add_parser("uninstall", parents=[common], help="remove the systemd service (root)")
    un.add_argument("--purge", action="store_true", help="also remove /etc/monita and the monita user")
    return p


def main(argv=None):
    args = build_parser().parse_args(argv)
    cfg = Config().load(args)
    level = getattr(logging, str(cfg.log_level).upper(), logging.INFO)
    logging.basicConfig(level=level, format="%(asctime)s %(levelname)s %(message)s"
                        if sys.stderr.isatty() or not os.environ.get("INVOCATION_ID")
                        else "%(levelname)s %(message)s")
    cmd = args.command or "run"
    return {
        "run": cmd_run, "once": cmd_once, "request-cert": cmd_request_cert, "status": cmd_status,
        "install": cmd_install, "uninstall": cmd_uninstall,
    }[cmd](cfg, args)


if __name__ == "__main__":
    try:
        sys.exit(main())
    except KeyboardInterrupt:
        sys.exit(130)
