#!/usr/bin/env python3
# -*- coding: utf-8; py-indent-offset: 4 -*-
#
# Author:  Linuxfabrik GmbH, Zurich, Switzerland
# Contact: info (at) linuxfabrik (dot) ch
#          https://www.linuxfabrik.ch/
# License: The Unlicense, see LICENSE file.

# https://github.com/Linuxfabrik/monitoring-plugins/blob/main/CONTRIBUTING.md

"""See the check's README for more details.
"""

import argparse  # pylint: disable=C0413
import sys  # pylint: disable=C0413

import lib.args  # pylint: disable=C0413
import lib.base  # pylint: disable=C0413
import lib.disk  # pylint: disable=C0413
import lib.lftest  # pylint: disable=C0413
import lib.txt  # pylint: disable=C0413
from lib.globals import (STATE_OK, STATE_UNKNOWN,  # pylint: disable=C0413
                          STATE_WARN)

__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2025021501'

DESCRIPTION = """This plugin checks for read-only mount points, such as `/` mounted read-only due
                 to file system errors, mounted CD-ROMs or ISO files, etc. It always ignores ramfs
                 and squashfs (snapd) by default."""

DEFAULT_IGNORE = ['/dev/loop', '/proc', '/snap', '/sys/fs']


def parse_args():
    """Parse command line arguments using argparse.
    """
    parser = argparse.ArgumentParser(description=DESCRIPTION)

    parser.add_argument(
        '-V', '--version',
        action='version',
        version='%(prog)s: v{} by {}'.format(__version__, __author__)
    )

    parser.add_argument(
        '--always-ok',
        help='Always returns OK.',
        dest='ALWAYS_OK',
        action='store_true',
        default=False,
    )

    parser.add_argument(
        '--ignore',
        help="""Mount point that should be ignored (repeatable). For example, if you provide
                `/sys/fs`, all mount points starting with `/sys/fs` will be ignored.
                Default: %(default)s""",
        action='append',
        dest='IGNORE',
        default=DEFAULT_IGNORE,
    )

    parser.add_argument(
        '--test',
        help='For unit tests. Needs "path-to-stdout-file,path-to-stderr-file,expected-retc".',
        dest='TEST',
        type=lib.args.csv,
    )

    return parser.parse_args()


def main():
    """The main function. Hier spielt die Musik.
    """

    # parse the command line, exit with UNKNOWN if it fails
    try:
        args = parse_args()
    except SystemExit:
        sys.exit(STATE_UNKNOWN)

    if args.TEST is None:
        # get the values the normal way
        mounts = lib.base.coe(lib.disk.read_file('/proc/mounts'))
    else:
        # do not call the command, put in test data
        stdout, _, _ = lib.lftest.test(args.TEST)
        mounts = stdout

    cnt = 0
    ros = [] # read-only mount points
    for mount in mounts.splitlines():
        mnt = mount.split()
        if len(mnt) != 6:
            lib.base.oao(
                'Unexpected format of /proc/mounts.',
                STATE_UNKNOWN,
            )
        mount_spec = mnt[0]
        mount_point = mnt[1]
        mount_type = mnt[2]

        if mount_type in ['ramfs', 'squashfs']:
            continue
        mount_options = mnt[3]
        if any(mount_point.startswith(item) for item in args.IGNORE):
            continue
        cnt += 1
        if 'rw' in mount_options:
            continue
        ros.append('{} on {} (type {})'.format(mount_spec, mount_point, mount_type))

    # build the message
    if len(ros) > 0:
        msg = '{} read-only mount {} found: '.format(
            len(ros),
            lib.txt.pluralize('point', len(ros)),
        )
        if len(ros) == 1:
            # show mount point info on first line when there is only one hit
            msg += ros[0]
        else:
            for item in ros:
                msg += '\n* {}'.format(item)
        state = STATE_WARN
    else:
        msg = 'Everything is ok. {} mount {} checked.'.format(cnt, lib.txt.pluralize('point', cnt))
        state = STATE_OK

    # over and out
    lib.base.oao(msg, state, always_ok=args.ALWAYS_OK)


if __name__ == '__main__':
    try:
        main()
    except Exception:   # pylint: disable=W0703
        lib.base.cu()
