#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Univention Monitoring
#
# Like what you see? Join us!
# https://www.univention.com/about-us/careers/vacancies/
#
# Copyright 2004-2024 Univention GmbH
#
# https://www.univention.de/
#
# All rights reserved.
#
# The source code of this program is made available
# under the terms of the GNU Affero General Public License version 3
# (GNU AGPL V3) as published by the Free Software Foundation.
#
# Binary versions of this program provided by Univention to you as
# well as other copyrighted, protected or trademarked materials like
# Logos, graphics, fonts, specific documentations and configurations,
# cryptographic keys etc. are subject to a license agreement between
# you and Univention and not subject to the GNU AGPL V3.
#
# In the case you use this program under the terms of the GNU AGPL V3,
# the program is provided in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License with the Debian GNU/Linux or Univention distribution in file
# /usr/share/common-licenses/AGPL-3; if not, see
# <https://www.gnu.org/licenses/>.

import univention.lib.fstab
from univention.config_registry import ucr
from univention.monitoring import Alert


class FSMountCheck(Alert):

    def write_metrics(self):
        nfscnt = 0
        mounted = 0
        umounted = 0
        msg = ''

        # read currently mounted filesystems
        try:
            mounts = univention.lib.fstab.File('/proc/mounts')
        except (OSError, EOFError) as err:
            self.write_metric('univention_nfsstatus', -1)
            self.log.debug('error while reading /proc/mounts, error: %s' % (err,))
            return

        mountlist = {
            entry.spec: entry.mount_point
            for entry in mounts.get(['nfs'])
        }

        # read desired mounted filesystems
        try:
            fstab = univention.lib.fstab.File('/etc/fstab')
        except (OSError, EOFError) as err:
            self.write_metric('univention_nfsstatus', -1)
            self.log.debug('error while reading /etc/fstab, EOF error: %s' % (err,))
            return

        for entry in fstab.get(['nfs']):
            if ucr.is_true('monitoring/nfs/allnfs') or not entry.hasopt('noauto'):
                source = entry.spec
                nfscnt += 1
                if source in mountlist and mountlist[source] == entry.mount_point:
                    mounted += 1
                else:
                    umounted += 1
                    msg += '"%s" not mounted, ' % source

        msg = msg.rstrip(', ')
        if nfscnt == 0:
            msg = 'no nfs shares in /etc/fstab present'
            state = -1
        elif umounted == 0:
            state = 0
            msg = '%s OK, %s - all nfs shares are correctly mounted' % (mounted, umounted)
        else:
            state = 1
            msg = '%s OK, %s - %s' % (mounted, umounted, msg)

        self.write_metric('univention_nfsstatus', state)
        self.log.debug(msg)


if __name__ == '__main__':
    FSMountCheck.main()
