#!/usr/bin/python3
#
# Univention Monitoring Plugin
#  check_univention_s4_connector: check s4 connector status
#
# Like what you see? Join us!
# https://www.univention.com/about-us/careers/vacancies/
#
# Copyright 2015-2022 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 re
import subprocess

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

RE_REJECT = re.compile(r'^ *[1-9]+: *(S4|UCS) DN:.*')


class S4Connector(Alert):

	def write_metrics(self):
		# check whether the s4 connector is activated
		if not ucr.is_true('connector/s4/autostart', False):
			self.write_metric('univention_s4_connector', 0)
			self.log.debug('Connector not activated (connector/s4/autostart =! true)')
			return

		# check whether we can connect to the S4
		# CRITICAL: in case we cannot connect to the S4
		rc = self.exec_command(['univention-s4search', 'cn=users', 'cn=users'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)[0]
		self.write_metric('univention_s4_connector_samba_reachable', 1 if rc == 0 else 0)

		# check whether the s4 connector is running; for this
		# WARNING: if more than one instance of the same connector is running
		# CRITICAL: if no process is running
		rc, output = self.exec_command(['/usr/lib/nagios/plugins/check_procs', '-w', ':1', '-c', '1:', '--ereg-argument-array', '^([^ ]+)?python.*s4connector.s4.main(.py)?$'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
		output = {1: 'More than one s4 connector instance are running!', 2: 's4 connector is not running!'}.get(rc, 'Unknown state!')
		self.log.debug(output)
		self.write_metric('univention_s4_connector_running', rc if rc in (0, 1, 2) else -1)

		# count rejects by parsing the output of univention-s4connector-list-rejected
		# WARNING: if there are any rejects
		rc, output = self.exec_command(['univention-s4connector-list-rejected'])
		rejects = sum(1 for line in output.splitlines() if RE_REJECT.match(line))

		self.write_metric('univention_s4_connector_rejects', rejects)
		if rejects:
			self.log.debug('Found %d reject(s)! Please check output of univention-s4connector-list-rejected.' % (rejects,))


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