File check_mk.spec of Package check_mk

#
# spec file for package check_mk
#
# Copyright (c) 2019 SUSE LINUX GmbH, Nuernberg, Germany.
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.

# Please submit bugfixes or comments via https://bugs.opensuse.org/
#


%define         nnmmsg logger -t %{name}/rpm
%define         __debug_install_post   %{nil}
#Compat macro for new _fillupdir macro introduced in Nov 2017
%if ! %{defined _fillupdir}
  %define _fillupdir %{_localstatedir}/adm/fillup-templates
%endif
%if 0%{?suse_version} >= 01230
%bcond_without systemd
%else
%bcond_with systemd
%endif
%if 0%{?suse_version} >= 01500
%bcond_without firewalld
%else
%bcond_with firewalld
%endif
Name:           check_mk
Version:        1.2.8p27
Release:        0
Summary:        Nagios-plugin for retrieving data
License:        GPL-2.0-or-later
Group:          System/Monitoring
URL:            https://mathias-kettner.de/checkmk_index.html
Source0:        https://mathias-kettner.de/support/%{version}/%{name}-%{version}.tar.gz
Source1:        check_mk-rpmlintrc
Source2:        check_mk-logwatch.cfg
Source3:        check_mk-README.SUSE
Source4:        zzz_check_mk.conf
Source5:        permissions.check_mk
Source6:        permissions.mkeventd
Source7:        check_mk-mysql.cfg
Source8:        check_mk-livestatus-README.SUSE
Source9:        check_mk.service
Source10:       check_mk.socket
Source11:       check_mk-service.fw2
Source12:       check_mk-systemd-socket-activation.README.SUSE
Source13:       mkeventd.service
Source14:       mkeventd.sysconfig
Source15:       check_mk.xml
Patch1:         setup.sh.patch
Patch9:         check_mk-pnp-templates-check_mk-if.php.patch
Patch10:        check_mk-agent-apache_status.diff
BuildRequires:  dos2unix
BuildRequires:  gcc-c++
BuildRequires:  nagios-rpm-macros
# Avoid unneeded dependencies generated because of the checks
# Require needed stuff manually instead
Requires:       bash
Requires:       expect
Requires:       mk-livestatus
Requires:       php
Requires:       python
Requires(pre):  /bin/logger
Requires(pre):  coreutils
Requires(pre):  sed
%if 0%{?suse_version} == 1110
BuildRequires:  gcc48
BuildRequires:  gcc48-c++
#!BuildIgnore:  libgcc43
#!BuildIgnore:  libgcc_s1-32bit
#!BuildIgnore:  libgomp43
#BuildRequires:  libgomp48
#BuildRequires:  libgcc48
%endif
%if 0%{?suse_version} <= 1320
Requires(pre):  xinetd
%endif
%if 0%{?suse_version}
BuildRequires:  apache2-devel
# unzip is needed for /usr/lib/rpm/brp-check-bytecode-version
BuildRequires:  unzip
%if 0%{?suse_version} != 1110 && 0%{?suse_version} != 1315 && 0%{?suse_version} < 1330
# diet is needed for eventd
BuildRequires:  dietlibc
%endif
%endif
%if 0%{?fedora_version} ||  0%{?rhel_version} || 0%{?centos_version}
BuildRequires:  httpd-devel
%endif
%if 0%{?mandriva_version}
BuildRequires:  apache-devel
%endif
%if 0%{?suse_version} > 1030
BuildRequires:  fdupes
%endif
%if 0%{?sles_version}
BuildRequires:  unzip
%endif
# systemd
%if %{with systemd}
BuildRequires:  systemd
%{?systemd_requires}
%endif
BuildRoot:      %{_tmppath}/%{name}-%{version}-build 

%description
Check_mk adopts a new a approach for collecting data
from operating systems and network components. It
obsoletes NRPE, check_by_ssh, NSClient and
check_snmp. It has many benefits, the most important
of which are:

* Significant reduction of CPU usage on the Nagios
* host.
* Automatic inventory of items to be checked on
* hosts.

%package agent
Summary:        Agent for check_mk
Group:          System/Monitoring
%if 0%{?suse_version}
Recommends:     xinetd
%endif
%if ! %{with systemd}
Requires(pre):  %insserv_prereq
%endif

%description agent
Check_mk adopts a new a approach for collecting data
from operating systems and network components. It
obsoletes NRPE, check_by_ssh, NSClient and
check_snmp. It has many benefits, the most important
of which are:

* Significant reduction of CPU usage on the Nagios
* host.
* Automatic inventory of items to be checked on
* hosts.

%package caching-agent
Summary:        Caching Linux-Agent for check_mk
Group:          System/Monitoring
Requires:       check_mk-agent
%if 0%{?suse_version}
Recommends:     xinetd
%endif
AutoReq:        off
AutoProv:       off

%description caching-agent
This package contains the agent for check_mk with an xinetd
configuration that wrap the agent with the check_mk_caching_agent
wrapper. Use it when doing fully redundant monitoring, where
an agent is regularily polled by more than one monitoring
server.

%package agent-logwatch
Summary:        Logwatch-Plugin for check_mk agent
Group:          System/Monitoring
Requires:       check_mk-agent = %{version}
Requires:       python
%if 0%{?suse_version} > 1130
BuildArch:      noarch
%endif

%description agent-logwatch
The logwatch plugin for the check_mk agent allows you to monitor
logfiles on Linux and UNIX. In one or more configuration files you
specify patters for log messages that should raise a warning or
critical state. For each logfile the current position is remembered.
This way only new messages are being sent.

%package agent-mysql
Summary:        MySQL-Plugin for check_mk agent
Group:          System/Monitoring
Requires:       check_mk-agent = %{version}
Requires:       mysql-client
%if 0%{?suse_version} > 1130
BuildArch:      noarch
%endif

%description agent-mysql
The MySQL plugin for the check_mk agent allows you to monitor
several aspects of MySQL databases. You need to adapt the
config %{_sysconfdir}/check_mk/mysql.cfg to your needs.

%package agent-oracle
Summary:        Oracle-Plugin for check_mk agent
Group:          System/Monitoring
Requires:       check_mk-agent = %{version}
%if 0%{?suse_version} > 1130
BuildArch:      noarch
%endif

%description agent-oracle
The ORACLE plugin for the check_mk agent allows you to monitor
several aspects of ORACLE databases. You need to adapt the
script %{_sysconfdir}/check_mk/sqlplus.sh to your needs.

%package agent-smart
Summary:        SMART-Plugin for check_mk agent
Group:          System/Monitoring
Requires:       check_mk-agent = %{version}
Requires:       smartmontools
%if 0%{?suse_version} > 1130
BuildArch:      noarch
%endif

%description agent-smart
The SMART plugin for the check_mk agent allows you to monitor
several SMART values of harddisks.

%package agent-jolokia
Summary:        Jolokia-Plugin for check_mk agent
Group:          System/Monitoring
Requires:       check_mk-agent = %{version}
%if 0%{?suse_version} > 1130
BuildArch:      noarch
%endif

%description agent-jolokia
The Jolokia plugin for the check_mk agent allows you to monitor
several aspect of Java application servers. You need to adapt the
config %{_sysconfdir}/check_mk/jolokia.cfg  to your needs.

%package agent-apache_status
Summary:        Apache Status Plugin for check_mk agent
Group:          System/Monitoring
Requires:       check_mk-agent = %{version}
%if 0%{?suse_version} > 1130
BuildArch:      noarch
%endif

%description agent-apache_status
The apache_status plugin for the check_mk agent allows you to monitor
and parses the information provided by the Apache status module. You
may need to adapt the config %{_sysconfdir}/check_mk/apache_status,cfg to your
needs. It requires that Apache mod_status be loaded.

%package multisite
Summary:        Web GUI for displaying monitoring status information
Group:          System/Monitoring
Requires:       %{name} >= %{version}
Requires:       apache2
Requires:       apache2-mod_python
Requires:       pnp4nagios
Requires:       python
Provides:       %{name}-web = %{version}
Obsoletes:      %{name}-web < %{version}
%if 0%{?suse_version} > 1130
BuildArch:      noarch
%endif

%description multisite
This package contains the Check_mk webpages. They allow you to
search for services and apply Nagios commands to the search results.

%package vsphere
Summary:        Check state of VMWare ESX via vSphere
Group:          System/Monitoring
Requires:       %{name} >= %{version}
Requires:       python-pysphere
%if 0%{?suse_version} > 1130
BuildArch:      noarch
%endif

%description vsphere
This package handles dependencies for the server-side vsphere agent.
See http://mathias-kettner.de/checkmk_vsphere.html for details.

%package -n mkeventd
Summary:        Check_MK Event Console
Group:          System/Monitoring
Requires:       %{name}-multisite
Requires:       python
Requires(pre):  %fillup_prereq
# systemd
%if %{with systemd}
BuildRequires:  systemd
%{?systemd_requires}
%else
%if 0%{?suse_version}
Requires(pre):  %insserv_prereq
%endif
%endif

%description -n mkeventd
The Check_MK Event Console adds real and native event processing to Nagios
without loosing the advantages of the state based monitoring. It is not a
standalone system, but fully integrated into the Multisite GUI. By means of an
own daemon it can:

* Receive messages from syslog, the SNMP trap daemon or other applications
* Process them by applying a custom set of rules at a rate of 1.000 messages
  per second and faster
* Classify messages based on all of the messages' attributes
* Correlate messages it receives
* Aggregate multiple similar messages into single events
* Rewrite the content of messages or add comments
* Automatically execute actions
* Create events if certain messages do not arrive
* Provide a current event status directly as a view in the Multisite GUI
* and much more...

%package -n mk-livestatus
Summary:        Accessing status and historic Nagios data
Group:          System/Monitoring
%if 0%{?mandriva_version}
Recommends:     nagios
%endif
%if 0%{?suse_version}
Recommends:     monitoring_daemon
%endif

%description -n mk-livestatus
Just as NDO, Livestatus make use of the Nagios Event Broker API and loads a
binary module into your Nagios process. But other then NDO, Livestatus does not
actively write out data. Instead, it opens a socket by which data can be
retrieved on demand.

The socket allows you to send a request for hosts, services or other pieces of
data and get an immediate answer. The data is directly read from Nagios'
internal data structures. Livestatus does not create its own copy of that data.
Beginning from version 1.1.2 you are also be able retrieve historic data from
the Nagios log files via Livestatus.

This package works with Nagios and Icinga - please choose the correct livestatus
library for either Nagios 3.x and Icinga or Nagios 4 in %{livestatus_libdir}:
* livestatus.o => Nagios 3.x and Icinga
* livestatus-nagios4.o => Nagios 4.x

%prep
%setup -q
%patch1
install -m 0644 %{SOURCE6} permissions.mkeventd
touch debugsourcefiles.list
touch debugsources.list
touch debugfiles.list

%build
tar -xf agents.tar.gz waitmax.c
gcc -s %{optflags} -o waitmax waitmax.c
install -m 644 %{SOURCE8} livestatus-README.SUSE

%install
%if 0%{?suse_version} == 1110
export CC=gcc-4.8
export CXX=g++-4.8
%endif

%if 0%{?suse_version} && 0%{?suse_version} < 1120
export NO_BRP_CHECK_BYTECODE_VERSION=true
%endif

export check_mk_version=%{version}
export default_config_dir='%{livestatus_check_mk_configdir}'
export bindir='%{_bindir}'
export confdir='%{livestatus_sysconfdir}'
export check_mk_configdir='%{livestatus_check_mk_configdir}'
export checksdir='%{livestatus_checksdir}'
export modulesdir='%{livestatus_modulesdir}'
export web_dir='%{livestatus_webdir}'
export mibsdir='%{_datadir}/snmp/mibs'
export docdir='%{_defaultdocdir}/%{name}'
export checkmandir='%{_defaultdocdir}/%{name}/checks'
export vardir='%{livestatus_vardir}'
export agentsdir='%{livestatus_agentsdir}'
export agentslibdir='%{livestatus_agentslibdir}'
export agentsconfdir='%{livestatus_agentsconfdir}'
export htdocsdir='%{livestatus_htdocsdir}'
export nagiosuser='%{nagios_user}'
export wwwuser='wwwrun'
export wwwgroup='%{nagios_command_group}'
export nagios_binary='%{_sbindir}/nagios'
export nagios_config_file='%{nagios_sysconfdir}/nagios.cfg'
export nagconfdir='%{livestatus_nagconfdir}'
%if 0%{?suse_version} >= 1310
export nagios_startscript='%{_sbindir}/rcnagios'
%else
export nagios_startscript='%{_sysconfdir}/init.d/nagios'
%endif
export nagpipe='%{nagios_command_file}'
export nagiosurl='/nagios'
export cgiurl='/nagios/cgi-bin'
export nagios_status_file='%{nagios_status_file}'
export check_icmp_path='%{nagios_plugindir}/check_icmp'
export checkmk_web_uri='/%{name}'
export apache_config_dir='%{_sysconfdir}/apache2/conf.d'
export htpasswd_file='%{nagios_sysconfdir}/htpasswd.users'
export nagios_auth_name='Nagios Access'
export pnpconfdir='%{pnp4nagios_sysconfdir}'
export pnprraconf='%{pnp4nagios_sysconfdir}'
export pnp_url='%{pnp4nagios_baseurl}'
export pnp_prefix='%{pnp4nagios_baseurl}/'
export rrddir='%{livestatus_rrddir}'
export pnptemplates='%{pnp4nagios_templatedir}'
export enable_livestatus='yes'
export libdir='%{livestatus_libdir}'
export livesock='%{livestatus_socket_file}'
export livebackendsdir='%{livestatus_livebackendsdir}'
export check_result_path='%{nagios_spooldir}'
export wwwuser='%{nagios_command_user}'
export DESTDIR=%{buildroot}
export enable_mkeventd='yes'
export mkeventdstatedir='%{_localstatedir}/lib/mkeventd'
export inventory_dir='%{livestatus_vardir}/inventory'
export log_dir='%{livestatus_log_dir}'
R=%{buildroot}

# install directories
install -d -m 755 %{buildroot}/$agentslibdir/{local,plugins,job}
install -d -m 755 %{buildroot}/${docdir}
install -d -m 755 %{buildroot}/${docdir}_agent
install -d -m 755 %{buildroot}/%{_sysconfdir}/init.d
install -d -m 755 %{buildroot}/%{livestatus_rrddir}
install -d -m 755 %{buildroot}/%{livestatus_socketdir}
install -d -m 755 %{buildroot}/%{livestatus_log_dir}
install -d -m 755 %{buildroot}/%{livestatus_vardir}/rrd
install -d -m 755 %{buildroot}/%{livestatus_vardir}/inventory
install -d -m 755 %{buildroot}/%{livestatus_vardir}/snmp_cache
install -d -m 755 %{buildroot}/%{livestatus_vardir}/wato
install -d -m 755 %{buildroot}/%{livestatus_vardir}/wato/{log,snapshots}
install -d -m 755 %{buildroot}/%{livestatus_vardir}/web
install -d -m 755 %{buildroot}/%{livestatus_check_mk_configdir}/wato
install -d -m 755 %{buildroot}/%{livestatus_check_mk_configdir}/multisite.d
install -d -m 755 %{buildroot}/%{livestatus_sysconfdir}/multisite.d
install -d -m 755 %{buildroot}/%{livestatus_sysconfdir}/multisite.d/wato
install -d -m 755 %{buildroot}/%{nagios_plugindir}
install -d -m 755 %{buildroot}/%{nagios_libdir}
install -d -m 755 %{buildroot}/%{_sbindir}
install -d -m 755 %{buildroot}/usr/lib/monitoring

# fix setup.sh to not use abuild for sudo call
sed -i 's/sudo -u $(id -un)/sudo -u root/' ./setup.sh

# install livestatus library for Nagios Version 4.x
export nagios_version=4.4
DESTDIR=%{buildroot} bash ./setup.sh --yes || cat ./livestatus.log
if [ -f %{buildroot}/%{livestatus_libdir}/livestatus.o ]; then
	mv %{buildroot}/%{livestatus_libdir}/livestatus.o %{buildroot}/%{livestatus_libdir}/livestatus-nagios4.o
fi

# install livestatus library for Nagios Version 3.x
export nagios_version=3.5.0
DESTDIR=%{buildroot} bash ./setup.sh --yes || cat ./livestatus.log

if [ ! -e  "%{buildroot}/%{livestatus_vardir}/log" ]; then
    ln -s "%{livestatus_log_dir}" "%{buildroot}/%{livestatus_vardir}/log"
fi

# auth.serials require for WATO/multisite
install -d -m 755 %{buildroot}/%{nagios_sysconfdir}
install -m 600 /dev/null %{buildroot}/%{nagios_sysconfdir}/auth.serials

# documentation
install -m 644 COPYING %{buildroot}/${docdir}_agent/
install -m 644 COPYING ChangeLog AUTHORS %{buildroot}/${docdir}/
install -m 644 %{SOURCE3} %{buildroot}/${docdir}/README.SUSE
install -m 644 %{SOURCE5} %{buildroot}/${docdir}/permissions.check_mk
if [ -f %{buildroot}/%{_defaultdocdir}/%{name}/helpers/headrify ]; then
	bashdir=$(which bash)
	sed -i "s|%{_bindir}/bash|${bashdir}|g" %{buildroot}/%{_defaultdocdir}/%{name}/helpers/headrify
	chmod -x %{buildroot}/%{_defaultdocdir}/%{name}/helpers/headrify
fi

# move files to the right place
install -m755 %{buildroot}%{livestatus_agentsdir}/check_mk_agent.linux %{buildroot}%{_bindir}/check_mk_agent
install -m755 %{buildroot}%{livestatus_agentsdir}/check_mk_caching_agent.linux %{buildroot}%{_bindir}/check_mk_caching_agent
install -m755 %{buildroot}%{livestatus_agentsdir}/plugins/mk_logwatch %{buildroot}%{livestatus_agentslibdir}/plugins

# mysql plugin
install -m755 %{buildroot}%{livestatus_agentsdir}/plugins/mk_mysql %{buildroot}%{livestatus_agentslibdir}/plugins

# oracle plugin
install -m755 %{buildroot}%{livestatus_agentsdir}/plugins/mk_oracle %{buildroot}%{livestatus_agentslibdir}/plugins
install -m755 %{buildroot}%{livestatus_agentsdir}/cfg_examples/sqlplus.sh %{buildroot}%{livestatus_agentsconfdir}/

# smart plugin
install -m755 %{buildroot}%{livestatus_agentsdir}/plugins/smart %{buildroot}%{livestatus_agentslibdir}/plugins

# jolokia plugin
install -m755 %{buildroot}%{livestatus_agentsdir}/plugins/mk_jolokia %{buildroot}%{livestatus_agentslibdir}/plugins
install -m644 %{buildroot}%{livestatus_agentsdir}/cfg_examples/jolokia.cfg %{buildroot}%{livestatus_agentsconfdir}/

# apache_status plugin
patch -i %{PATCH10} -p0 -d %{buildroot}%{livestatus_agentsdir}
install -m755 %{buildroot}%{livestatus_agentsdir}/plugins/apache_status %{buildroot}%{livestatus_agentslibdir}/plugins
install -m644 %{buildroot}%{livestatus_agentsdir}/cfg_examples/apache_status.cfg %{buildroot}%{livestatus_agentsconfdir}/

# mk-job installation
install -m755 %{buildroot}%{livestatus_agentsdir}/mk-job %{buildroot}%{_bindir}/mk-job

# fix wrong bash patch for solaris plugin
bashdir=$(which bash)
# this one was actually meant to call the bash in /usr/bin but we have to avoid the dependency
sed -i "s|%{_bindir}/bash|${bashdir}|g" %{buildroot}%{livestatus_agentsdir}/check_mk_agent.solaris
#
sed -i "s|%{_bindir}/bash|${bashdir}|g" %{buildroot}%{livestatus_agentsdir}/plugins/nfsexports.solaris
chmod -x %{buildroot}%{livestatus_agentsdir}/plugins/nfsexports.solaris
# try to avoid dep for ksh93
chmod -x %{buildroot}%{livestatus_agentsdir}/check_mk_agent.aix

install -m 755 waitmax %{buildroot}%{_bindir}
rm %{buildroot}%{livestatus_agentsdir}/waitmax*
ln -s %{_bindir}/waitmax %{buildroot}%{livestatus_agentsdir}/waitmax

# mkeventd
%if %{with systemd}
test -f %{buildroot}%{_sysconfdir}/init.d/mkeventd && rm %{buildroot}%{_sysconfdir}/init.d/mkeventd
install -D -m0644 %{SOURCE13} %{buildroot}/%{_unitdir}/mkeventd.service
ln -s %{_sbindir}/service %{buildroot}/%{_sbindir}/rcmkeventd
%else
ln -s %{_sysconfdir}/init.d/mkeventd %{buildroot}%{_sbindir}/rcmkeventd
%endif
# sysconfig file
install -D -m0644 %{SOURCE14} %{buildroot}%{_fillupdir}/sysconfig.mkeventd
#
rm %{buildroot}%{nagios_plugindir}/*
pushd mkeventd.src/lib
make
install -D -m755 check_mkevents %{buildroot}%{nagios_plugindir}/check_mkevents
test -f %{buildroot}%{_bindir}/mkevent && strip %{buildroot}%{_bindir}/mkevent
popd

# remove unneeded files
#rm %%{buildroot}%%{livestatus_agentsdir}/windows/check_mk_agent.cc
#rm %%{buildroot}%%{livestatus_agentsdir}/windows/Makefile
rm %{buildroot}%{livestatus_agentsdir}/check_mk_agent.freebsd

# install configuration for logwatch agent
install -m 644 %{SOURCE2} %{buildroot}/%{livestatus_agentsconfdir}/logwatch.cfg

# create logwatch directory
install -m 775 -d %{buildroot}/%{livestatus_vardir}/logwatch

# install configuration for mysql agent
install -m 644 %{SOURCE7} %{buildroot}/%{livestatus_agentsconfdir}/mysql.cfg

%if 0%{?suse_version} <= 1320
# install xinetd configuration
install -Dm 644 %{buildroot}%{livestatus_agentsdir}/cfg_examples/xinetd.conf %{buildroot}%{_sysconfdir}/xinetd.d/%{name}
install -Dm 644 %{buildroot}%{livestatus_agentsdir}/cfg_examples/xinetd_caching.conf %{buildroot}%{_sysconfdir}/xinetd.d/check_mk-caching
sed -i "s|.*#only_from.*|        only_from      = 127.0.0.1|g" %{buildroot}%{_sysconfdir}/xinetd.d/*
%endif

# install systemd service and socket units for socket activation
%if %{with systemd}
install -Dm 644 %{SOURCE9} %{buildroot}%{_unitdir}/%{name}-agent@.service
sed -e 's/-agent$/-caching-agent/' < %{buildroot}%{_unitdir}/%{name}-agent@.service > %{buildroot}%{_unitdir}/%{name}-caching-agent@.service
install -Dm 644 %{SOURCE10} %{buildroot}%{_unitdir}/%{name}-agent.socket
install -Dm 644 %{SOURCE10} %{buildroot}%{_unitdir}/%{name}-caching-agent.socket
install -m 644 %{SOURCE12} %{buildroot}/${docdir}_agent/README.systemd-socket-activation.SUSE
%endif

# remove softlink and install check_mk_templates.cfg
test -d "%{buildroot}/%{livestatus_nagconfdir}" || mkdir -p "%{buildroot}/%{livestatus_nagconfdir}"
if [ -L "%{buildroot}/%{livestatus_nagconfdir}/check_mk_templates.cfg" ]; then
	rm %{buildroot}/%{livestatus_nagconfdir}/check_mk_templates.cfg
	install -Dm 644 %{buildroot}/%{_datadir}/check_mk/check_mk_templates.cfg %{buildroot}%{livestatus_nagconfdir}/check_mk_templates.cfg
fi

# install directory for snmp walk output
install -d -m755 %{buildroot}/%{livestatus_vardir}/snmpwalks

# fix permissions
chmod +x %{buildroot}%{livestatus_checksdir}/*
chmod +x %{buildroot}%{livestatus_modulesdir}/*.py
for file in $(find %{buildroot}%{livestatus_webdir} -name "*.py"); do
	chmod +x "$file"
done
for file in $(find %{buildroot}%{livestatus_datadir} -name "*.{mk,openvms,freebsd,include}"); do
	chmod -x "$file"
done
chmod -x %{buildroot}%{livestatus_sysconfdir}/multisite.d/*.mk
find %{buildroot}/${docdir}/ -type f -exec chmod -x {} \;
chmod -x %{buildroot}%{livestatus_webdir}/htdocs/defaults.py

# make rpmlint happy
%if 0%{?suse_version} > 1030
%fdupes -s %{buildroot}/%{pnp4nagios_templatedir}
%fdupes -s %{buildroot}/%{livestatus_webdir}
%endif
strip %{buildroot}/%{_bindir}/mkeventd_open514
strip %{buildroot}/%{_bindir}/unixcat

# install /usr/share/check_mk/modules/defaults into _sysconfdir and create a symlink
mv %{buildroot}%{livestatus_modulesdir}/defaults %{buildroot}/%{livestatus_sysconfdir}/%{name}-defaults
ln -s %{livestatus_sysconfdir}/%{name}-defaults %{buildroot}%{livestatus_modulesdir}/defaults

# apache config
install -m 644 %{SOURCE4} %{buildroot}/%{_sysconfdir}/apache2/conf.d/
# apache sudo file
install -d -m 755 %{buildroot}%{_sysconfdir}/sudoers.d
cat >> %{buildroot}%{_sysconfdir}/sudoers.d/multisite << EOF
# Needed for  WATO - the Check_MK Web Administration Tool
Defaults:$wwwuser !requiretty
$wwwuser ALL = (root) NOPASSWD: $bindir/check_mk --automation *
EOF

%if %{with firewalld}
# firewalld service file
install -Dm 644 %{SOURCE15}  %{buildroot}%{_libexecdir}/firewalld/services/check_mk.xml
%else
# SuSEFirewall2 service file
install -Dm 644 %{SOURCE11} %{buildroot}/%{_sysconfdir}/sysconfig/SuSEfirewall2.d/services/check_mk
%endif

touch %{buildroot}/%{livestatus_vardir}/acknowledged_werks.mk
touch %{buildroot}/%{livestatus_vardir}/log/web.log

# fix for newest rrdtool with optparse: need to use the full option names instead of the short ones
cd %{buildroot}/%{pnp4nagios_templatedir}/
for file in $(find . -type f -name "*.php"); do
	sed -i "s|--upper=101|--upper-limit=101|g; \
 			s|--upper=\"|--upper-limit=\"|g; \
			s|--lower=0|--lower-limit=0|g; \
			s|--lower=\"|--lower-limit=\"|g" "$file"
done
# patch inline
patch < %{PATCH9}


%pre
# Create user and group on the system if necessary
%nagios_user_group_add
%nagios_command_user_group_add
if [ ${1:-0} -gt 1 ]; then
	# move the defaults config, if needed
	if [ -f %{livestatus_modulesdir}/defaults ]; then
		if [ ! -f %{livestatus_sysconfdir}/%{name}-defaults ]; then
			mv %{livestatus_modulesdir}/defaults %{livestatus_sysconfdir}/%{name}-defaults
			ln -s %{livestatus_sysconfdir}/%{name}-defaults %{livestatus_modulesdir}/defaults
		fi
	fi
fi

%post
echo "check_mk permissions are not set, i.e not setuid. See
%{_docdir}/check_mk/permissions.check_mk for details
on resolving this on SUSE systems"

%if %{with systemd}
%pre agent
%service_add_pre %{name}-agent.socket %{name}-agent.service
%endif

%post agent
%if 0%{?suse_version} >= 1210
%if 0%{?suse_version} <= 1320
# ensure xinetd is enabled if the package is installed for the first time
%service_add_post xinetd.service
%endif
%service_add_post %{name}-agent.socket %{name}-agent.service
%else
%if 0%{?suse_version} <= 1320
%{fillup_and_insserv -y xinetd}
%endif
%endif

%if %{with systemd}
%preun agent
%service_del_preun %{name}-agent.socket %{name}-agent.service
%endif

%postun agent
%if 0%{?suse_version} >= 1210
%if 0%{?suse_version} <= 1320
%service_del_postun xinetd.service
%endif
%service_del_postun %{name}-agent.socket %{name}-agent.service
%else
%if 0%{?suse_version} <= 1320
%restart_on_update xinetd
%endif
%endif

%if %{with systemd}
%pre caching-agent
%service_add_pre %{name}-caching-agent.socket %{name}-caching-agent.service
%endif

%post caching-agent
%if 0%{?suse_version} >= 1210
%if 0%{?suse_version} <= 1320
%service_add_post xinetd.service
%endif
%service_add_post %{name}-caching-agent.socket %{name}-caching-agent.service
%else
%if 0%{?suse_version} <= 1320
%{fillup_and_insserv -y xinetd}
%endif
%endif

%if %{with systemd}
%preun caching-agent
%service_del_preun %{name}-caching-agent.socket %{name}-caching-agent.service
%endif

%postun caching-agent
%if 0%{?suse_version} >= 1210
%if 0%{?suse_version} <= 1320
%service_del_postun xinetd.service
%endif
%service_del_postun %{name}-caching-agent.socket %{name}-caching-agent.service
%else
%if 0%{?suse_version} <= 1320
%restart_on_update xinetd
%endif
%endif

%post -n mk-livestatus
echo "check_mk permissions are not set, i.e not setuid. See
%{_docdir}/check_mk/permissions.check_mk for details
on resolving this on SUSE systems"

%post multisite
echo "check_mk permissions are not set, i.e not setuid. See
%{_docdir}/check_mk/permissions.check_mk for details
on resolving this on SUSE systems"

%if %{with systemd}
%pre -n mkeventd
%service_add_pre mkeventd.service
%endif

%post -n mkeventd
echo "mkeventd_open514 permissions are not set, i.e not setuid. See
%{_docdir}/mkeventd/permissions.mkeventd for details
on resolving this on SUSE systems"
%if %{with systemd}
%service_add_post mkeventd.service
%fillup_only -n mkeventd
%else
%{fillup_and_insserv mkeventd}
%endif

%preun -n mkeventd
%if %{with systemd}
%service_del_preun mkeventd.service
%else
%stop_on_removal mkeventd
%endif

%postun -n mkeventd
%if %{with systemd}
%service_del_postun mkeventd.service
%else
%restart_on_update mkeventd
%insserv_cleanup
%endif

%files
%defattr(-,root,root)
%doc %{_defaultdocdir}/%{name}
%dir %{livestatus_sysconfdir}
%dir %attr(0775,%{nagios_user},%{nagios_command_group})
%dir %{livestatus_check_mk_configdir}
%dir %attr(0775,%{nagios_user},%{nagios_command_group})
%dir %{livestatus_datadir}
%dir %{livestatus_datadir}/inventory
%{livestatus_datadir}/inventory/*
%dir %{livestatus_datadir}/werks
%{livestatus_datadir}/werks/*
%dir %{livestatus_datadir}/notifications
%{livestatus_datadir}/notifications/*
%dir %{livestatus_agentsdir}
%dir %{livestatus_agentsdir}/plugins
%dir %{livestatus_agentslibdir}
%dir %{livestatus_checksdir}
%dir %{livestatus_modulesdir}
%dir %{livestatus_notificationsdir}
%dir %{livestatus_nagconfdir}
%dir %{livestatus_rrddir}
%dir %{livestatus_libdir}
%dir %{pnp4nagios_datadir}
%dir %{pnp4nagios_templatedir}
%dir %{nagios_sysconfdir}
%dir %{livestatus_vardir}
%dir %{livestatus_vardir}/inventory
%dir %{livestatus_vardir}/packages
%verify(not mode) %dir %{livestatus_vardir}
%verify(not mode) %dir %{livestatus_vardir}/packages
%ghost %attr(0664,%{nagios_user},%{nagios_command_group}) %{livestatus_vardir}/acknowledged_werks.mk
%dir %attr(0775,%{nagios_user},%{nagios_command_group}) %{livestatus_vardir}/autochecks
%dir %attr(0775,%{nagios_user},%{nagios_command_group}) %{livestatus_vardir}/cache
%dir %attr(0775,%{nagios_user},%{nagios_command_group}) %{livestatus_vardir}/counters
%dir %attr(0775,%{nagios_user},%{nagios_command_group}) %{livestatus_vardir}/precompiled
%dir %attr(0775,%{nagios_user},%{nagios_command_group}) %{livestatus_vardir}/snmpwalks
%dir %attr(0775,%{nagios_user},%{nagios_command_group}) %{livestatus_vardir}/inventory
%dir %attr(0775,%{nagios_user},%{nagios_command_group}) %{livestatus_vardir}/snmp_cache
%dir %attr(0775,%{nagios_user},%{nagios_command_group}) %{livestatus_log_dir}
%{livestatus_vardir}/log
%ghost %attr(0664,%{nagios_user},%{nagios_command_group}) %{livestatus_vardir}/log/web.log
%config(noreplace) %{livestatus_sysconfdir}/*.mk*
%config(noreplace) %{livestatus_sysconfdir}/%{name}-defaults
%config %{livestatus_check_mk_configdir}/README
%config(noreplace) %{livestatus_nagconfdir}/check_mk_templates.cfg
%{_datadir}/check_mk/check_mk_templates.cfg
%{livestatus_agentsdir}/*
%{livestatus_checksdir}/*
%{livestatus_modulesdir}/*
%{livestatus_notificationsdir}/*
%{pnp4nagios_templatedir}/*
%{_bindir}/check_mk
%{_bindir}/cmk
%{_bindir}/mkp
%{livestatus_vardir}/packages/check_mk
%verify(not mode) %{livestatus_vardir}/packages/check_mk
%exclude %{livestatus_sysconfdir}/logwatch.cfg
%exclude %{livestatus_sysconfdir}/multisite.d
%exclude %{livestatus_datadir}/web
%exclude %{livestatus_agentsdir}/plugins/mk_logwatch
%exclude %{livestatus_vardir}/packages/check_mk
%exclude %{livestatus_vardir}/logwatch

%files -n mk-livestatus
%defattr(-,root,root)
%doc livestatus-README.SUSE
%dir %attr(-,%{nagios_user},%{nagios_command_group})
%{_bindir}/unixcat
%{livestatus_vardir}/packages/check_mk
%verify(not mode) %{livestatus_vardir}/packages/check_mk
%dir %attr(-,%{nagios_user},%{nagios_command_group}) %{livestatus_socketdir}
%dir %verify(not mode) %attr(-,%{nagios_user},%{nagios_command_group}) %{livestatus_socketdir}
%{livestatus_libdir}/livestatus.o
%{livestatus_libdir}/livestatus-nagios4.o

%files agent
%defattr(-,root,root)
%doc %{_defaultdocdir}/check_mk_agent
%{_bindir}/check_mk_agent
%{_bindir}/waitmax
%{_bindir}/mk-job
%if 0%{?suse_version} <= 1320
%config(noreplace) %{_sysconfdir}/xinetd.d/%{name}
%endif
%if 0%{?suse_version} >= 1210
%{_unitdir}/%{name}-agent@.service
%{_unitdir}/%{name}-agent.socket
%endif
%dir %{livestatus_agentslibdir}
%dir %{livestatus_agentslibdir}/job
%dir %{livestatus_agentslibdir}/local
%dir %{livestatus_agentslibdir}/plugins
%if %{with firewalld}
%dir %{_libexecdir}/firewalld
%dir %{_libexecdir}/firewalld/services
%{_libexecdir}/firewalld/services/check_mk.xml
%else
%config(noreplace) %{_sysconfdir}/sysconfig/SuSEfirewall2.d/services/check_mk
%endif

%files caching-agent
%defattr(-,root,root)
%{_bindir}/check_mk_caching_agent
%if 0%{?suse_version} <= 1320
%config(noreplace) %{_sysconfdir}/xinetd.d/%{name}-caching
%endif
%if 0%{?suse_version} >= 1210
%{_unitdir}/%{name}-caching-agent@.service
%{_unitdir}/%{name}-caching-agent.socket
%endif

%files agent-logwatch
%defattr(-,root,root)
%{livestatus_agentslibdir}/plugins/mk_logwatch
%config(noreplace) %{livestatus_sysconfdir}/logwatch.cfg
%dir %attr(0775,%{nagios_user},%{nagios_command_group}) %{livestatus_vardir}/logwatch

%files agent-mysql
%defattr(-,root,root)
%{livestatus_agentslibdir}/plugins/mk_mysql
%config(noreplace) %attr(0400,%{nagios_command_user},%{nagios_command_group}) %{livestatus_sysconfdir}/mysql.cfg

%files agent-oracle
%defattr(-,root,root)
%{livestatus_agentslibdir}/plugins/mk_oracle
%config(noreplace) %{livestatus_sysconfdir}/sqlplus.sh

%files agent-smart
%defattr(-,root,root)
%{livestatus_agentslibdir}/plugins/smart

%files agent-jolokia
%defattr(-,root,root)
%{livestatus_agentslibdir}/plugins/mk_jolokia
%config(noreplace) %{livestatus_sysconfdir}/jolokia.cfg

%files agent-apache_status
%defattr(-,root,root)
%{livestatus_agentslibdir}/plugins/apache_status
%config(noreplace) %{livestatus_sysconfdir}/apache_status.cfg

%files multisite
%defattr(-,root,root)
%dir %{_sysconfdir}/sudoers.d
%{livestatus_datadir}/web
%config(noreplace) %{_sysconfdir}/apache2/conf.d/*.conf
%config(noreplace) %attr(0660,%{nagios_command_user},%{nagios_command_group}) %{nagios_sysconfdir}/auth.serials
%config(noreplace) %{_sysconfdir}/sudoers.d/multisite
%dir %attr(0775,%{nagios_user},%{nagios_command_group}) %{livestatus_vardir}/wato
%dir %attr(0775,%{nagios_user},%{nagios_command_group}) %{livestatus_vardir}/wato/log
%dir %attr(0775,%{nagios_user},%{nagios_command_group}) %{livestatus_vardir}/wato/snapshots
%dir %attr(0775,%{nagios_user},%{nagios_command_group}) %{livestatus_vardir}/web
%dir %attr(0775,%{nagios_user},%{nagios_command_group}) %{livestatus_check_mk_configdir}/wato
%dir %attr(0775,%{nagios_user},%{nagios_command_group}) %{livestatus_check_mk_configdir}/multisite.d
%dir %attr(0775,%{nagios_user},%{nagios_command_group}) %{livestatus_sysconfdir}/multisite.d
%dir %attr(0775,%{nagios_user},%{nagios_command_group}) %{livestatus_sysconfdir}/multisite.d/wato

%files vsphere
%defattr(-,root,root)
%dir %{livestatus_datadir}/agents/special

%files -n mkeventd
%defattr(-,root,root)
%doc permissions.mkeventd
%if %{with systemd}
%{_unitdir}/mkeventd.service
%else
%{_sysconfdir}/init.d/mkeventd
%endif
%{_fillupdir}/sysconfig.mkeventd
%{_sbindir}/rcmkeventd
%{_bindir}/mkevent
%{_bindir}/mkeventd
%dir %attr(0775,%{nagios_user},%{nagios_command_group}) %{livestatus_sysconfdir}/multisite.d
%config(noreplace) %attr(0775,%{nagios_user},%{nagios_command_group}) %{livestatus_sysconfdir}/multisite.d/mkeventd.mk
%dir %{nagios_libdir}
%dir %{nagios_plugindir}
%dir /usr/lib/monitoring
%{nagios_plugindir}/check_mkevents
%attr(0754,root,root)%{_bindir}/mkeventd_open514

%changelog
openSUSE Build Service is sponsored by