File pnp4nagios.spec of Package pnp4nagios

#
# spec file for package pnp4nagios
#
# Copyright (c) 2012-2017 Lars Vogdt <lars@linux-schulserver.de>
#
# 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 http://bugs.opensuse.org/
#

%if ! %{defined _fillupdir}
  %define _fillupdir %{_localstatedir}/adm/fillup-templates
%endif

%if 0%{?suse_version} >= 1230
%bcond_without systemd
%else
%bcond_with  systemd
%endif

Name:           pnp4nagios
Version:        0.6.26
Release:        1
License:        GPL-2.0+
Summary:        Tool for producing graphs from monitoring plugins perfdata
Url:            http://docs.pnp4nagios.org/
Group:          System/Monitoring
BuildRequires:  apache2-devel
BuildRequires:  nagios-rpm-macros
BuildRequires:  monitoring_webfrontend
BuildRequires:  perl(Getopt::Long)
BuildRequires:  perl(Time::HiRes)
BuildRequires:  rrdtool
%if 0%{?suse_version} < 1500
BuildRequires:  xinetd
%endif
%if %{with systemd}
BuildRequires:  systemd
%{?systemd_requires}
%endif
PreReq:         permissions
Requires:       perl
Requires:       rrdtool
Requires:       syslog
Requires:       %{name}-directories == %{version}
Recommends:     dejavu
Recommends:     monitoring-plugins-pnp_rrds >= %{version}
# disabled as pnp4nagios can also be used together with icinga
# but the recommends triggers the installation of nagios
# Recommends:     nagios-www
%if 0%{?suse_version}
Requires:       php-gd
Requires:       php-zlib
%endif
%if 0%{?suse_version} > 1020
BuildRequires:  fdupes
BuildRequires:  rrdtool-devel
%else
BuildRequires:  rrdtool
%endif
Source0:        http://sourceforge.net/projects/%{name}/files/PNP-0.6/%{name}-%{version}.tar.gz
Source1:        %{name}-httpd.conf
Source2:        %{name}.quick-start.txt
Source3:        %{name}-init
Source4:        %{name}-rpmlintrc
Source5:        %{name}-%{name}.cfg
Source6:        %{name}-check_nrpe.cfg
Source7:        %{name}-xinetd.sample.conf
Source8:        %{name}-lighttpd.conf
Source9:        %{name}-nagios-permissions
Source10:       %{name}-icinga-permissions
Source11:       %{name}-README.SUSE-Icinga
Source12:       %{name}-pnp_gearman_worker-init
Source13:       %{name}-sysconfig
%if %{with systemd}
Source14:       %{name}-service.npcd
Source15:       %{name}-service.pnp_gearman_worker
Source16:       pnp4nagios-pnp_gearman_worker-tmpfiles.conf-nagios
Source17:       pnp4nagios-pnp_gearman_worker-tmpfiles.conf-icinga
%endif
Patch1:         %{name}-fix-pathnames-in-scripts.patch
BuildRoot:      %{_tmppath}/%{name}-%{version}-build
%define         apxs2 /usr/sbin/apxs2-prefork
%define         apache2_sysconfdir %(%{apxs2} -q SYSCONFDIR || echo "/etc/apache2")

%description
PNP is an addon for the Nagios or Icinga Network Monitoring System.

PNP provides easy to use, easy to configure RRDTools based performance charts
feeded by the performance data output of the Nagios or Icinga plugins.


%package icinga
Summary:        Icinga specific files for pnp4nagios
Group:          System/Monitoring
%if 0%{?suse_version} >= 1500
BuildRequires:  icingaweb2
%else
BuildRequires:  icinga-www
%endif
%if 0%{?suse_version} <= 1310
BuildRequires:	-post-build-checks
%endif
Recommends:     logrotate
PreReq:         permissions
%if 0%{?suse_version} >= 1500
Supplements:    icingaweb2
%else
Supplements:    icinga-www
Supplements:    icinga-web
%endif
Requires:       %{name} = %{version}-%{release}
Provides:       %{name}-directories = %{version}-%{release}
Conflicts:      %{name}-nagios

%description icinga
PNP is an addon for the Nagios or Icinga Network Monitoring System.

This package provides the directories with the correct permissions for
Icinga.


%package nagios
Summary:        Nagios specific files for pnp4nagios
Group:          System/Monitoring
BuildRequires:  nagios-www
%if 0%{?suse_version} <= 1310
BuildRequires:	-post-build-checks
%endif
Recommends:     logrotate
PreReq:         permissions
Supplements:    nagios-www
Requires:       %{name} = %{version}-%{release}
Provides:       %{name}-directories = %{version}-%{release}
Conflicts:      %{name}-icinga

%description nagios
PNP is an addon for the Nagios or Icinga Network Monitoring System.

This package provides the directories with the correct permissions for
Nagios.


%package -n monitoring-plugins-pnp_rrds
Summary:        Check freshness and state of your RRD files
Group:          System/Monitoring
Requires:       monitoring-plugins-common
Provides:       nagios-plugins-pnp_rrds = %{version}-%{release}
Obsoletes:      nagios-plugins-pnp_rrds < %{version}-%{release}

%description -n monitoring-plugins-pnp_rrds
Use this plugin to check the freshness of your host and service RRD files
that are used by pnp4nagios.


%package gearman-worker
Summary:        Gearman worker files for PNP
Group:          System/Monitoring
Requires:       %{name} == %{version}
Requires:       perl(Gearman::Worker)
%if %{with systemd}
Requires:       %{name}-directories = %{version}
%else
PreReq:         %insserv_prereq
%endif
PreReq:         %fillup_prereq

%description gearman-worker
Since version 0.6.12 PNP4Nagios can be driven as a gearman worker. This way
large Nagios environments are possible using mod_gearman. Nagios and PNP4Nagios
can be run on different machines.

This package provides the needed init script and a file /etc/sysconfig/pnp4nagios 
that allows to adapt the user the process is running under (should be the 
monitoring user).
 
%pre
%if %{with systemd}
%service_add_pre npcd.service
%endif

%pre nagios
getent group %{nagios_group} >/dev/null || groupadd -r %{nagios_group}
getent group %{nagios_command_group} >/dev/null || groupadd -r %{nagios_command_group}
getent passwd %{nagios_user} >/dev/null || useradd -r -g %{nagios_group} -d /var/lib/nagios -s /bin/bash -c "User for %{name}-nagios" %{nagios_user}
%{nagios_command_user_group_add}

%pre icinga
getent group %{icinga_group} >/dev/null || groupadd -r %{icinga_group}
getent group %{icinga_command_group} >/dev/null || groupadd -r %{icinga_command_group}
getent passwd %{icinga_user} >/dev/null || useradd -r -g %{icinga_group} -d /var/lib/icinga -s /bin/bash -c "User for %{name}-icinga" %{icinga_user}
%{icinga2_command_user_group_add}

%pre gearman-worker
%if %{with systemd}
%service_add_pre pnp_gearman_worker.service
%endif

%prep
%setup -q
%patch1 -p1

%build
%configure \
    --datadir="%{pnp4nagios_datadir}" \
    --localstatedir="%{pnp4nagios_logdir}" \
    --sysconfdir="%{pnp4nagios_sysconfdir}" \
    --libexecdir="%{nagios_plugindir}" \
    --datarootdir="%{pnp4nagios_datarootdir}" \
    --with-perfdata-logfile="%{pnp4nagios_perfdata_logfile}" \
    --with-perfdata-dir="%{pnp4nagios_perfdata_dir}" \
    --with-perfdata-spool-dir="%{pnp4nagios_perfdata_spooldir}" \
    --with-init-dir="%{_sysconfdir}/init.d" \
    --with-nagios-user="%{nagios_user}" \
    --with-nagios-group="%{nagios_group}" \
    --with-nagios_cmd_file="%{nagios_command_file}" \
    --with-base-url="%{pnp4nagios_baseurl}" \
    --with-kohana_system="%{pnp4nagios_datadir}/kohana/system" \
    --with-rrdtool="%{_bindir}/rrdtool"

make %{?_smp_mflags} all

%install
LOGDIR=$(dirname %{pnp4nagios_perfdata_logfile})
mkdir -p %{buildroot}/%{pnp4nagios_perfdata_dir} %{buildroot}/$LOGDIR
make install install-config \
    DESTDIR=%{buildroot} \
    LIBEXEC_DIR="%{nagios_plugindir}" \
    MANDIR=%{_mandir} \
    INSTALL_OPTS=""
# move samples to the docdir
mkdir -p %{buildroot}/%{_defaultdocdir}/%{name}/sample-config
find sample-config/ -name "*-sample*" -exec cp -v {} %{buildroot}/%{_defaultdocdir}/%{name}/sample-config/ \;
# install config files
mv %{buildroot}/%{_sysconfdir}/%{name}/rra.cfg-sample %{buildroot}/%{pnp4nagios_sysconfdir}/rra.cfg
sed -e 's@\^hna0@localhost@' \
    -e 's@\^traffic@traffic@' \
    %{buildroot}/%{pnp4nagios_sysconfdir}/pages/web_traffic.cfg-sample > %{buildroot}/%{pnp4nagios_sysconfdir}/pages/web_traffic.cfg
mv %{buildroot}/%{pnp4nagios_sysconfdir}/check_commands/check_nwstat.cfg-sample %{buildroot}/%{pnp4nagios_sysconfdir}/check_commands/check_nwstat.cfg
install -m644 %{SOURCE6} %{buildroot}/%{pnp4nagios_sysconfdir}/check_commands/check_nrpe.cfg
find %{buildroot}/%{pnp4nagios_sysconfdir} -name "*-sample" -exec rm {} \;
%if 0%{?suse_version} < 1500
install -Dm644 %{SOURCE7} %{buildroot}/%{_sysconfdir}/xinetd.d/%{name}
%endif
rm %{buildroot}/%{pnp4nagios_datarootdir}/install.php
# install own nagios command file
install -m644 %{SOURCE5}  %{buildroot}/%{pnp4nagios_sysconfdir}/%{name}.cfg
# install the documentation
mkdir -p %{buildroot}/%{_defaultdocdir}/%{name}
install -m0644 AUTHORS ChangeLog COPYING README* THANKS %{buildroot}/%{_defaultdocdir}/%{name}/
install -m0644 %{SOURCE2} %{buildroot}/%{_defaultdocdir}/%{name}/README.SUSE
install -m0644 %{SOURCE11} %{buildroot}/%{_defaultdocdir}/%{name}/README.SUSE-Icinga
# install contrib scripts into docdir
mkdir -p %{buildroot}/%{_defaultdocdir}/%{name}/example
cp -r contrib/* %{buildroot}/%{_defaultdocdir}/%{name}/example/
# verify script
install -Dm0755 scripts/verify_pnp_config_v2.pl  %{buildroot}%{_bindir}/verify_pnp_config.pl
# install template placeholder
mkdir -p %{buildroot}/%{pnp4nagios_datadir}/templates/
touch %{buildroot}/%{pnp4nagios_datadir}/templates/PUT-YOUR-TEMPLATES-HERE
# install directories
install -d -m0775 %{buildroot}/%{pnp4nagios_perfdata_spooldir}
install -d -m0775 %{buildroot}%{pnp4nagios_logdir}/stats
# install apache configuration
install -Dm0644 %{SOURCE1} %{buildroot}/%{apache2_sysconfdir}/conf.d/%{name}.conf
# lighttpd config goes as example to /etc/pnp4nagios
install -m0644 %{SOURCE8} %buildroot/etc/pnp4nagios/pnp4nagios-lighttpd.conf
# install init-scripts
mkdir -p %{buildroot}/%{_sbindir}
%if %{with systemd}
install -Dm0644 %{SOURCE14} %{buildroot}/%{_unitdir}/npcd.service
sed -e "s|NAGIOS_PLUGINDIR|%{nagios_plugindir}|g; \
        s|PNP4NAGIOS_SYSCONFDIR|%{pnp4nagios_sysconfdir}|g" %{SOURCE15} > %{buildroot}/%{_unitdir}/pnp_gearman_worker.service
install -Dm0644 %{SOURCE16} %{buildroot}%{_prefix}/lib/tmpfiles.d/%{name}-nagios.conf
install  -m0644 %{SOURCE17} %{buildroot}%{_prefix}/lib/tmpfiles.d/%{name}-icinga.conf
ln -sf /usr/sbin/service %{buildroot}%{_sbindir}/rcnpcd
ln -sf /usr/sbin/service %{buildroot}%{_sbindir}/rcpnp_gearman_worker
%else
install -Dm0755 %{SOURCE3} %{buildroot}/%{_sysconfdir}/init.d/npcd
ln -s %{_sysconfdir}/init.d/npcd %{buildroot}/%{_sbindir}/rcnpcd
sed -e "s|NAGIOS_PLUGINDIR|%{nagios_plugindir}|g; \
		s|PNP4NAGIOS_SYSCONFDIR|%{pnp4nagios_sysconfdir}|g" %{SOURCE12} > %{buildroot}/%{_sysconfdir}/init.d/pnp_gearman_worker
chmod +x %{buildroot}/%{_sysconfdir}/init.d/pnp_gearman_worker
ln -s %{_sysconfdir}/init.d/pnp_gearman_worker %{buildroot}/%{_sbindir}/rcpnp_gearman_worker
%endif
test -d %{buildroot}%{pnp4nagios_datadir}/application/logs || mkdir -p %{buildroot}%{pnp4nagios_datadir}/application/logs
test -d %{buildroot}%{pnp4nagios_datadir}/application/cache || mkdir -p %{buildroot}%{pnp4nagios_datadir}/application/cache
test -d %{buildroot}%{pnp4nagios_perfdata_dir} || mkdir -p %{buildroot}%{pnp4nagios_perfdata_dir}
# install sysconfig file
install -Dm 644 %{SOURCE13} %{buildroot}%{_fillupdir}/sysconfig.%{name}
# install ssi files in datadir
install -Dm644 contrib/ssi/status-header.ssi %{buildroot}%{pnp4nagios_datadir}/media/ssi/status-header.ssi
# install permissions file for nagios users
install -Dm644 %{SOURCE9} %{buildroot}/%{_sysconfdir}/permissions.d/%{name}-nagios
# install permissions file for icinga users
install -Dm644 %{SOURCE10} %{buildroot}/%{_sysconfdir}/permissions.d/%{name}-icinga
# install logrotate snipplet
install -d -m 755 %{buildroot}/%{_sysconfdir}/logrotate.d
cat >> %{buildroot}/%{_sysconfdir}/logrotate.d/%{name}-nagios << EOF
%{pnp4nagios_logdir}/perfdata.log {
        missingok
        notifempty
        dateext
        copytruncate
        compress
        size +4096k
        rotate 99
        maxage 365
        su %{nagios_user} %{nagios_command_group}
}
EOF

cat >> %{buildroot}/%{_sysconfdir}/logrotate.d/%{name}-icinga << EOF
%{pnp4nagios_logdir}/perfdata.log {
        missingok
        notifempty
        dateext
        copytruncate
        compress
        size +4096k
        rotate 99
        maxage 365
        su %{icinga_user} %{icinga_command_group}
}
EOF
# fix path to livestatus socket
sed -i "s|/usr/local/nagios/var/rw/live|%{livestatus_socketdir}/livestatus.cmd|g" %{buildroot}/%{pnp4nagios_sysconfdir}/config*.php
# save discspace, create links
%if 0%{?suse_version} > 1020
%fdupes -s %{buildroot}/%{_datadir}/%{name}
%endif
# use internal kohana and move kohana to %%{pnp4nagios_datadir}/kohana 
# because kohana is deprecated and dead.
# And pnp4nagios install internal kohana to /usr/%%{_libdir}/kohana
mv %{buildroot}%{_libdir}/kohana %{buildroot}/%{pnp4nagios_datadir}

%clean
rm -rf %{buildroot}

%post
if [ x"$1" == x"1" ]; then
    # this is the initial installation: enable pnp4nagios
    test -x %{_sbindir}/a2enflag && %{_sbindir}/a2enflag PNP4NAGIOS >/dev/null
else 
  if [ -d etc/nagios/pnp ]; then
    echo "WARNING:" >&2
    echo "The directory locations of %{name} have changed." >&2
    echo "Please adapt your NEW configuration in %{pnp4nagios_sysconfdir}" >&2
    echo >&2
  fi
  if [ -f %apache2_sysconfdir/conf.d/nagios-pnp.conf ]; then
	echo "WARNING:" >&2
	echo "The file %apache2_sysconfdir/conf.d/nagios-pnp.conf has been renamed to" >&2
	echo "%apache2_sysconfdir/conf.d/%{name}.conf - please check and adapt the" >&2
	echo "new configuration and remove your the old file afterward." >&2
	echo >&2
  fi
fi
%if %{with systemd}
%service_add_post npcd.service
%endif
%fillup_only

%post nagios
%if 0%{?suse_version} < 01140
%run_permissions
%else
%set_permissions %{pnp4nagios_libdir}/
%set_permissions %{pnp4nagios_perfdata_dir}/
%set_permissions %{pnp4nagios_perfdata_spooldir}/
%set_permissions %{pnp4nagios_logdir}/
%set_permissions %{pnp4nagios_logdir}/stats/
%endif
%if %{with systemd}
systemd-tmpfiles --create %{_prefix}/lib/tmpfiles.d/%{name}-nagios.conf
%endif
if [ -w %{_sysconfdir}/sysconfig/pnp4nagios ]; then
	sed -i "s|^PNP_GEARMAN_WORKER_USER=.*|PNP_GEARMAN_WORKER_USER=\"%{nagios_user}\"|g" %{_sysconfdir}/sysconfig/pnp4nagios
fi

%post icinga
%if 0%{?suse_version} < 01140
%run_permissions
%else
%set_permissions %{pnp4nagios_libdir}/
%set_permissions %{pnp4nagios_perfdata_dir}/
%set_permissions %{pnp4nagios_perfdata_spooldir}/
%set_permissions %{pnp4nagios_logdir}/
%set_permissions %{pnp4nagios_logdir}/stats/
%endif
%if %{with systemd}
systemd-tmpfiles --create %{_prefix}/lib/tmpfiles.d/%{name}-icinga.conf
%endif
if [ -w %{_sysconfdir}/sysconfig/pnp4nagios ]; then
	sed -i "s|^PNP_GEARMAN_WORKER_USER=.*|PNP_GEARMAN_WORKER_USER=\"%{icinga_user}\"|g" %{_sysconfdir}/sysconfig/pnp4nagios
fi

%post gearman-worker
%if %{with systemd}
%service_add_post pnp_gearman_worker.service
%endif

%verifyscript nagios
%verify_permissions -e %{pnp4nagios_libdir}/
%verify_permissions -e %{pnp4nagios_perfdata_dir}/
%verify_permissions -e %{pnp4nagios_perfdata_spooldir}/
%verify_permissions -e %{pnp4nagios_logdir}/
%verify_permissions -e %{pnp4nagios_logdir}/stats/

%verifyscript icinga
%verify_permissions -e %{pnp4nagios_libdir}/
%verify_permissions -e %{pnp4nagios_perfdata_dir}/
%verify_permissions -e %{pnp4nagios_perfdata_spooldir}/
%verify_permissions -e %{pnp4nagios_logdir}/
%verify_permissions -e %{pnp4nagios_logdir}/stats/

%preun
%if %{with systemd}
%service_del_preun npcd.service
%else
%stop_on_removal npcd
%endif

%postun
%if %{with systemd}
%service_del_postun npcd.service
%else
%{insserv_cleanup npcd}
%endif
if [ x"$1" == x"0" ]; then
    # deinstallation of the package - remove the apache flag
    test -x %{_sbindir}/a2disflag && %{_sbindir}/a2disflag PNP4NAGIOS >/dev/null
    %if %{with systemd}
      %service_del_postun apache2.service
    %else
      %restart_on_update apache2
    %endif
fi

%preun gearman-worker
%if %{with systemd}
%service_del_preun pnp_gearman_worker.service
%else
%stop_on_removal pnp_gearman_worker
%endif

%postun gearman-worker
%if %{with systemd}
%service_del_postun pnp_gearman_worker.service
%else
%restart_on_update pnp_gearman_worker
%insserv_cleanup
%endif

%files
%defattr(-,root,root)
%doc %{_defaultdocdir}/%{name}/
%dir %{pnp4nagios_datadir}
%dir %{pnp4nagios_sysconfdir}
%if 0%{?suse_version} < 1500
%config(noreplace) %{_sysconfdir}/xinetd.d/%{name}
%endif
%config(noreplace) %{pnp4nagios_sysconfdir}/*
%config(noreplace) %apache2_sysconfdir/conf.d/%{name}.conf
%{pnp4nagios_templatedir}/*
%{_libdir}/npcdmod.o
%{_bindir}/*
%{_sbindir}/rcnpcd
%if %{with systemd}
%{_unitdir}/npcd.service
%else
%{_sysconfdir}/init.d/npcd
%endif
%{pnp4nagios_datadir}/*
%{nagios_plugindir}/*
%exclude %{nagios_plugindir}/check_pnp_rrds.pl
%attr(0644,root,root) %{_mandir}/man8/*
%defattr(775,wwwrun,www)
%{pnp4nagios_datadir}/application/cache
%{pnp4nagios_datadir}/application/logs

%files nagios
%defattr(-,root,root)
%config(noreplace) %{_sysconfdir}/permissions.d/%{name}-nagios
%config(noreplace) %{_sysconfdir}/logrotate.d/%{name}-nagios
%if %{with systemd}
%{_prefix}/lib/tmpfiles.d/%{name}-nagios.conf
%endif
%defattr(-,%{nagios_user},%{nagios_command_group},0775)
%dir %{pnp4nagios_libdir}
%dir %{pnp4nagios_perfdata_spooldir}
%dir %{pnp4nagios_perfdata_dir}
%dir %{pnp4nagios_logdir}
%dir %{pnp4nagios_logdir}/stats

%files icinga
%defattr(-,root,root)
%config(noreplace) %{_sysconfdir}/permissions.d/%{name}-icinga
%config(noreplace) %{_sysconfdir}/logrotate.d/%{name}-icinga
%if %{with systemd}
%{_prefix}/lib/tmpfiles.d/%{name}-icinga.conf
%endif
%defattr(-,%{icinga_user},%{icinga_command_group},0775)
%dir %{pnp4nagios_libdir}
%dir %{pnp4nagios_perfdata_spooldir}
%dir %{pnp4nagios_perfdata_dir}
%dir %{pnp4nagios_logdir}
%dir %{pnp4nagios_logdir}/stats

%files -n monitoring-plugins-pnp_rrds
%defattr(-,root,root)
%dir %{nagios_libdir}
%dir %{nagios_plugindir}
%{nagios_plugindir}/check_pnp_rrds.pl

%files gearman-worker
%defattr(-,root,root)
%if %{with systemd}
%{_unitdir}/pnp_gearman_worker.service
%else
%{_sysconfdir}/init.d/pnp_gearman_worker
%endif
%{_sbindir}/rcpnp_gearman_worker
%{_fillupdir}/sysconfig.%{name}

%changelog