File naemon.spec of Package naemon

#
# spec file for package naemon
#
# 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/
#


%define logmsg logger -t %{name}/rpm
%if %{defined suse_version}
%define apacheuser wwwrun
%define apachegroup www
%define apachedir apache2
%else
%define apacheuser apache
%define apachegroup apache
%define apachedir httpd
%endif

%if 0%{?el7}%{?fc20}%{?fc21}%{?fc22}
%global use_systemd 1
%endif
%if 0%{?suse_version} >= 1315
%global use_systemd 1
%endif

# Setup some debugging options in case we build with --with debug
%if %{defined _with_debug}
  %define mycflags -O0 -pg -ggdb3
%else
  %define mycflags %{nil}
%endif

Name:           naemon
Version:        1.0.5
Release:        1
License:        GPL-2.0
Summary:        Open Source Host, Service And Network Monitoring Program
Url:            http://www.naemon.org/
%if 0%{?suse_version}
Group:          System/Monitoring
%else
Group:          Applications/System
%endif
Source0:        http://www.naemon.org/download/naemon/naemon-%{version}.tar.gz
BuildRequires:  autoconf
BuildRequires:  automake
BuildRequires:  cron
BuildRequires:  doxygen
BuildRequires:  gcc-c++
BuildRequires:  gd
BuildRequires:  gd-devel > 1.8
BuildRequires:  gperf
BuildRequires:  glib2-devel
BuildRequires:  help2man
BuildRequires:  libjpeg-devel
BuildRequires:  libpng-devel
BuildRequires:  libtool
BuildRequires:  libicu-devel
BuildRequires:  logrotate
BuildRequires:  mysql-devel
BuildRequires:  perl
BuildRequires:  rsync
BuildRequires:  freetype-devel
BuildRequires:  fontconfig-devel
BuildRequires:  gd-devel
BuildRequires:  libXpm-devel
BuildRequires:  xorg-x11-devel
BuildRequires:  zlib-devel
BuildRoot:      %{_tmppath}/%{name}-%{version}-build
# sles / rhel specific requirements
%if %{defined suse_version}
%if 0%{?use_systemd}
BuildRequires: systemd
%endif
Requires(pre):  %fillup_prereq
Requires(pre):  %insserv_prereq
BuildRequires:  libexpat-devel
BuildRequires:  apache2
Requires(pre):  pwdutils
%else
BuildRequires:  expat-devel
Requires(pre):  shadow
%endif
# rhel6 specific requirements
%if 0%{?el6}
BuildRequires:  perl-ExtUtils-MakeMaker
%endif
%if 0%{?el7}
BuildRequires:  perl-autodie
%endif
Requires(pre):  cron
Requires:       %{name}-core            = %{version}
Requires:       %{name}-livestatus      = %{version}
Requires:       %{name}-thruk           = %{version}
Requires:       %{name}-thruk-reporting = %{version}
# https://fedoraproject.org/wiki/Packaging:DistTag
# http://stackoverflow.com/questions/5135502/rpmbuild-dist-not-defined-on-centos-5-5

%description
Naemon is an application, system and network monitoring application.
It can escalate problems by email, pager or any other medium. It is
also useful for incident or SLA reporting. It is originally a fork
of Nagios, but with extended functionality, stability and performance.

It is written in C and is designed as a background process,
intermittently running checks on various services that you specify.

The actual service checks are performed by separate "plugin" programs
which return the status of the checks to Naemon. The plugins are
compatible with Nagios, and can be found in the monitoring-plugins package.

Naemon ships the Thruk gui with extended reporting and dashboard features.

# disable binary striping
##%global __os_install_post %{nil}

%package core
Summary:        Naemon Monitoring Core
Group:          Applications/System
Requires:       logrotate
%if 0%{?suse_version}
Requires(pre):  %insserv_prereq
%endif

%description core
Contains the %{name} core.

%package tools
Summary:   Naemon Monitoring Tools
Group:     Applications/System
Requires:  libnaemon = %{version}

%description tools
contains tools for %{name}.

%package livestatus
Summary:        Naemon Livestatus Eventbroker Module
Group:          Applications/System
Requires:       %{name}-core = %{version}
Requires(post): %{name}-core = %{version}

%description livestatus
Contains the %{name} livestatus eventbroker module.

%package thruk
Summary:        Thruk Gui For Naemon
Group:          Applications/System
Requires:       thruk
Requires(pre):  naemon-core = %{version}
AutoReqProv: no
%if %{defined suse_version}
Requires:       apache2
Requires(pre):  apache2-mod_fcgid
Requires(pre):  cron
Requires(pre):  %insserv_prereq
%else
Requires:       httpd
Requires:       mod_fcgid
%endif

%description thruk
This package contains the thruk gui for %{name}.

%package devel
Summary:        Development Files For Naemon
Group:          Development/Libraries
Requires:       libnaemon = %version
Requires:       glib2-devel

%description devel
This package contains the header files, static libraries and development
documentation for %{name}. If you are a NEB-module author or wish to
write addons for Naemon using Naemons own APIs, you should install
this package.

%package -n libnaemon
Summary: Shared Library for Naemon and NEB modules
Group: Development/Libraries

%description -n libnaemon
libnaemon contains the shared library for building NEB modules or addons for
Naemon.

%prep
%setup -q

%build
CFLAGS="%{mycflags}" LDFLAGS="$CFLAGS" %configure \
    --datadir="%{_datadir}/%{name}" \
    --libdir="%{_libdir}/%{name}" \
    --localstatedir="%{_localstatedir}/lib/%{name}" \
    --sysconfdir="%{_sysconfdir}/%{name}" \
    --enable-event-broker \
    --without-tests \
    --with-pluginsdir="%{_libdir}/naemon/plugins" \
    --with-tempdir="%{_localstatedir}/cache/naemon" \
    --with-checkresultdir="%{_localstatedir}/cache/naemon/checkresults" \
    --with-logdir="%{_localstatedir}/log/naemon" \
    --with-initdir="%{_initddir}" \
    --with-logrotatedir="%{_sysconfdir}/logrotate.d" \
    --with-naemon-user="naemon" \
    --with-naemon-group="naemon" \
    --with-naemon-config-dir="%{_sysconfdir}/%{name}/module-conf.d" \
    --with-lockfile="%{_localstatedir}/run/%{name}/%{name}.pid" \
    --with-pkgconfdir="%{_sysconfdir}/%{name}" \
    --with-pluginsdir="%{_libdir}/%{name}/plugins" \
    --with-tempdir="%{_localstatedir}/cache/%{name}" \
    --with-logdir="%{_localstatedir}/log/%{name}" \
    --with-logrotatedir="%{_sysconfdir}/logrotate.d" \
    --with-thruk-user="%{apacheuser}" \
    --with-thruk-group="naemon" \
    --with-thruk-libs="%{_libdir}/naemon/perl5" \
    --with-thruk-tempdir="%{_localstatedir}/cache/naemon/thruk" \
    --with-thruk-vardir="%{_localstatedir}/lib/naemon/thruk" \
    --with-httpd-conf="%{_sysconfdir}/%{apachedir}/conf.d" \
    --with-htmlurl="/naemon"
%{__make} %{?_smp_mflags} all

### Build our documentaiton
# cd naemon-core
# %{__make} dox

%install
%{__make} install \
    DESTDIR=%{buildroot} \
    INSTALL_OPTS="" \
    COMMAND_OPTS="" \
    INIT_OPTS=""

# because we globally disabled binary striping, we have to do this manually for some files
#%{__cp} -p %{buildroot}%{_bindir}/naemon %{buildroot}%{_bindir}/naemon-dbg
#%{__strip} %{buildroot}%{_bindir}/naemon
#%{__strip} %{buildroot}%{_bindir}/naemonstats
#%{__strip} %{buildroot}%{_bindir}/oconfsplit
#%{__strip} %{buildroot}%{_bindir}/naemon-unixcat
#%{__strip} %{buildroot}%{_libdir}/naemon/libnaemon.so.0.0.0
#%{__mv} %{buildroot}%{_sysconfdir}/logrotate.d/thruk %{buildroot}%{_sysconfdir}/logrotate.d/naemon-thruk
#%{__mv} %{buildroot}%{_sysconfdir}/logrotate.d/naemon %{buildroot}%{_sysconfdir}/logrotate.d/naemon-core

### Install documentation
#%{__mkdir_p} -m 0755 %{buildroot}%{_datadir}/naemon/documentation/search
#%{__cp} -a naemon-core/Documentation/html/* %{buildroot}%{_datadir}/naemon/documentation
#%{__chmod} 0644 %{buildroot}%{_datadir}/naemon/documentation/*
#%{__chmod} 0755 %{buildroot}%{_datadir}/naemon/documentation/search
#%{__rm} -f %{buildroot}%{_datadir}/naemon/documentation/installdox

# Put the new RC script in place
%if 0%{?suse_version}
install -D -m 0644 naemon-core/sample-config/naemon.sysconfig %{buildroot}%{_localstatedir}/adm/fillup-templates/sysconfig.%{name}
%else
%{__install} -d -m 0755 %{buildroot}%{_sysconfdir}/sysconfig/
%{__install} -m 0644 naemon-core/sample-config/naemon.sysconfig %{buildroot}%{_sysconfdir}/sysconfig/naemon
%endif

%{__mkdir_p} -m 0755 %{buildroot}%{_libdir}/naemon/
ln -s %{_libdir}/nagios/plugins %{buildroot}%{_libdir}/naemon/plugins

%clean
%{__rm} -rf %{buildroot}



%pre core
if ! /usr/bin/id naemon &>/dev/null; then
    /usr/sbin/useradd -r -d %{_localstatedir}/lib/naemon -s /bin/sh -c "naemon" naemon || \
        %logmsg "Unexpected error adding user \"naemon\". Aborting installation."
fi
if ! /usr/bin/getent group naemon &>/dev/null; then
    /usr/sbin/groupadd naemon &>/dev/null || \
        %logmsg "Unexpected error adding group \"naemon\". Aborting installation."
fi

%post core
%if 0%{?suse_version}
%{fillup_and_insserv naemon}
%else
/sbin/chkconfig --add naemon
%endif
case "$*" in
  2)
    # Upgrading so try and restart if already running
    # For systemctl systems we need to reload the configs
    # becaues it'll complain if we just installed a new
    # init script
    if [ `which systemctl 2>/dev/null` ]; then
      systemctl daemon-reload
      systemctl condrestart %{name}.service
    fi
    /etc/init.d/naemon condrestart &>/dev/null || :
  ;;
  1)
    # install example conf.d only once on the first installation
    if [ ! -d %{_sysconfdir}/%{name}/conf.d/templates ]; then
        mkdir -p %{_sysconfdir}/%{name}/conf.d/
        cp -rp %{_datadir}/%{name}/examples/conf.d/* %{_sysconfdir}/%{name}/conf.d/
    fi
    chown naemon:naemon \
        /etc/naemon/conf.d \
        /etc/naemon/conf.d/*.cfg \
        /etc/naemon/conf.d/templates \
        /etc/naemon/conf.d/templates/*.cfg
    chown naemon:naemon \
        /etc/naemon/module-conf.d/*.cfg 2>/dev/null
    chmod 0664 /etc/naemon/conf.d/*.cfg /etc/naemon/conf.d/templates/*.cfg
    chmod 2775 /etc/naemon/conf.d /etc/naemon/conf.d/templates
    %if 0%{?use_systemd}
      %systemd_post %{name}.service
    %else
      chkconfig --add %{name}
    %endif
  ;;
  *) echo case "$*" not handled in post
esac

if /usr/bin/id %{apacheuser} &>/dev/null; then
    if ! /usr/bin/id -Gn %{apacheuser} 2>/dev/null | grep -q naemon ; then
%if %{defined suse_version} && 0%{?suse_version}< 1230
        /usr/sbin/groupmod -A %{apacheuser} naemon >/dev/null
%else
        /usr/sbin/usermod -a -G naemon %{apacheuser} >/dev/null
%endif
    fi
else
    %logmsg "User \"%{apacheuser}\" does not exist and is not added to group \"naemon\". Sending commands to naemon from the CGIs is not possible."
fi
touch /var/log/naemon/naemon.log
chmod 0664 /var/log/naemon/naemon.log
chown naemon:naemon /var/log/naemon/naemon.log

%preun core
%if 0%{?suse_version}
%stop_on_removal naemon
%endif
case "$*" in
  1)
    # Upgrade, don't do anything
  ;;
  0)
    # Uninstall, go ahead and stop before removing
    %if 0%{?use_systemd}
      %systemd_preun %{name}.service
    %else
      /etc/init.d/naemon stop >/dev/null 2>&1 || :
      service %{name} stop >/dev/null 2>&1 || :
      chkconfig --del %{name} || :
    %endif
  ;;
  *) echo case "$*" not handled in preun
esac

%postun core
%if 0%{?suse_version}
%restart_on_update naemon 
%insserv_cleanup
%else
case "$*" in
  0)
    # POSTUN
    rm -f /var/cache/naemon/naemon.configtest \
          /var/lib/naemon/objects.cache \
          /var/lib/naemon/objects.precache \
          /var/lib/naemon/retention.dat \
          /var/log/naemon/naemon.log \
          /var/log/naemon/archives \
          /var/lib/naemon/naemon.cmd
    ;;
  1)
    # POSTUPDATE
    ;;
  *) echo case "$*" not handled in postun
esac
%endif
exit 0


%post livestatus
case "$*" in
  2)
    # Upgrading so try and restart if already running
    /etc/init.d/naemon condrestart &>/dev/null || :
  ;;
  1)
    # New install, enable module
    if [ -e /etc/naemon/naemon.cfg ]; then
      sed -i /etc/naemon/naemon.cfg -e 's~#\(broker_module=/usr/lib[0-9]*/naemon/livestatus.o.*\)~\1~'
    fi
  ;;
  *) echo case "$*" not handled in postun
esac
exit 0

%preun livestatus
case "$*" in
  0)
    # POSTUN
    rm -f /var/log/%{name}/livestatus.log
    ;;
  1)
    # POSTUPDATE
    ;;
  *) echo case "$*" not handled in postun
esac

%postun livestatus
case "$*" in
  0)
    # POSTUN
    if [ -e %{_sysconfig}/%{name}/%{name}.cfg ]; then
      sed -i %{_sysconfig}/%{name}/%{name}.cfg -e 's~\(broker_module=/usr/lib[0-9]*/%{name}/livestatus.o.*\)~#\1~'
    fi
    ;;
  1)
    # POSTUPDATE
    ;;
  *) echo case "$*" not handled in postun
esac
exit 0

%pre thruk
if ! /usr/bin/id naemon &>/dev/null; then
    /usr/sbin/useradd -r -d %{_localstatedir}/lib/naemon -s /bin/sh -c "naemon" naemon || \
        %logmsg "Unexpected error adding user \"naemon\". Aborting installation."
fi
if ! /usr/bin/getent group naemon &>/dev/null; then
    /usr/sbin/groupadd naemon &>/dev/null || \
        %logmsg "Unexpected error adding group \"naemon\". Aborting installation."
fi


# save themes, plugins so we don't reenable them on every update
rm -rf /var/cache/naemon/thruk_update
if [ -d /etc/naemon/themes/themes-enabled/. ]; then
  mkdir -p /var/cache/naemon/thruk_update/themes
  cp -rp /etc/naemon/themes/themes-enabled/* /var/cache/naemon/thruk_update/themes/ 2>/dev/null
fi
if [ -d /etc/naemon/plugins/plugins-enabled/. ]; then
  mkdir -p /var/cache/naemon/thruk_update/plugins
  cp -rp /etc/naemon/plugins/plugins-enabled/* /var/cache/naemon/thruk_update/plugins/ 2>/dev/null
fi
exit 0

%post thruk
%if 0%{?suse_version}
%fillup_and_insserv thruk
a2enmod alias
a2enmod fcgid
a2enmod auth_basic
a2enmod rewrite
%else
chkconfig --add thruk
mkdir -p /var/lib/naemon/thruk /var/cache/naemon/thruk /etc/naemon/bp /var/log/naemon /etc/naemon/conf.d
touch /var/log/naemon/thruk.log
chown -R %{apacheuser}:%{apachegroup} /var/cache/naemon/thruk /var/log/naemon/thruk.log /etc/naemon/plugins/plugins-enabled /etc/naemon/thruk_local.conf /etc/naemon/bp /var/lib/naemon/thruk
/usr/bin/crontab -l -u %{apacheuser} 2>/dev/null | /usr/bin/crontab -u %{apacheuser} -
/etc/init.d/httpd condrestart
if [ "$(getenforce 2>/dev/null)" = "Enforcing" ]; then
  echo "******************************************";
  echo "Thruk will not work when SELinux is enabled";
  echo "SELinux: "$(getenforce);
  echo "******************************************";
fi
if [ -d %{_libdir}/naemon/perl5 ]; then
  /usr/bin/thruk -a clearcache,installcron --local > /dev/null
fi
%endif
if /usr/bin/id %{apacheuser} &>/dev/null; then
    if ! /usr/bin/id -Gn %{apacheuser} 2>/dev/null | grep -q naemon ; then
%if %{defined suse_version}
        /usr/sbin/groupmod -A %{apacheuser} naemon >/dev/null
%else
        /usr/sbin/usermod -a -G naemon %{apacheuser} >/dev/null
%endif
    fi
else
    %logmsg "User \"%{apacheuser}\" does not exist and is not added to group \"naemon\". Sending commands to naemon from the CGIs is not possible."
fi

echo "Naemon/Thruk have been configured for http://$(hostname)/naemon/."
echo "The default user is 'admin' with password 'admin'. You can usually change that by 'htpasswd /etc/naemon/htpasswd admin'. And you really should change that!"
exit 0

%posttrans thruk
# restore themes and plugins
if [ -d /var/cache/naemon/thruk_update/themes/. ]; then
  rm -f /etc/naemon/themes/themes-enabled/*
  cp -rp /var/cache/naemon/thruk_update/themes/* /etc/naemon/themes/themes-enabled/ 2>/dev/null  # might fail if no themes are enabled
fi
if [ -d /var/cache/naemon/thruk_update/plugins/. ]; then
  rm -f /etc/naemon/plugins/plugins-enabled/*
  cp -rp /var/cache/naemon/thruk_update/plugins/* /etc/naemon/plugins/plugins-enabled/ 2>/dev/null  # might fail if no plugins are enabled
fi
echo "thruk plugins enabled:" $(ls /etc/naemon/plugins/plugins-enabled/)
rm -rf /var/cache/naemon/thruk_update || :

%preun thruk
%if 0%{?suse_version}
%insserv_cleanup thruk
%else
if [ $1 = 0 ]; then
  # last version will be deinstalled
  if [ -d %{_libdir}/naemon/perl5 ]; then
    /usr/bin/thruk -a uninstallcron --local
  fi
fi
/etc/init.d/thruk stop
chkconfig --del thruk >/dev/null 2>&1
%endif
rmdir /etc/naemon/bp 2>/dev/null
rmdir /etc/naemon 2>/dev/null
exit 0

%postun thruk
case "$*" in
  0)
    # POSTUN
    rm -rf /var/cache/naemon/thruk \
           %{_datadir}/naemon/root/thruk/plugins \
           /var/lib/naemon/thruk
    # try to clean some empty folders
    rmdir /etc/naemon/plugins/plugins-available 2>/dev/null
    rmdir /etc/naemon/plugins/plugins-enabled 2>/dev/null
    rmdir /etc/naemon/plugins 2>/dev/null
    rmdir /etc/naemon 2>/dev/null
    %{insserv_cleanup}
    ;;
  1)
    # POSTUPDATE
    rm -rf /var/cache/naemon/thruk/*
    mkdir -p /var/cache/naemon/thruk/reports
    chown -R %{apacheuser}:%{apachegroup} /var/cache/naemon/thruk
    ;;
  *) echo case "$*" not handled in postun
esac
exit 0


%files
%defattr(-,root,root)

%files core
%defattr(-,root,root)
%dir %{_sysconfdir}/naemon
%dir %{_sysconfdir}/naemon/conf.d
%dir %{_sysconfdir}/naemon/conf.d/templates
%dir %{_libdir}/naemon
%dir %{_datadir}/naemon
%attr(0755,root,root) %{_bindir}/naemon
%attr(0755,root,root) %{_bindir}/naemonstats
%attr(0755,root,root) %{_bindir}/oconfsplit
%attr(0755,root,root) %{_initddir}/naemon
%config(noreplace) %{_sysconfdir}/logrotate.d/naemon-core
%attr(0755,root,root) %dir %{_sysconfdir}/naemon/
%attr(2775,naemon,naemon) %dir %{_sysconfdir}/naemon/conf.d
%attr(0644,root,root) %config(noreplace) %{_sysconfdir}/naemon/naemon.cfg
%attr(0644,root,root) %config(noreplace) %{_sysconfdir}/naemon/resource.cfg
%attr(0664,naemon,naemon) %config(noreplace) %{_sysconfdir}/naemon/conf.d/*.cfg
%attr(0664,naemon,naemon) %config(noreplace) %{_sysconfdir}/naemon/conf.d/templates/*.cfg
%attr(0644,root,root) %config(noreplace) %{_sysconfdir}/sysconfig/naemon
%attr(2775,naemon,%{apachegroup}) %dir %{_localstatedir}/cache/naemon/checkresults
%attr(2775,naemon,naemon) %dir %{_localstatedir}/cache/naemon
%attr(0755,naemon,naemon) %dir %{_localstatedir}/lib/naemon
%attr(0755,naemon,naemon) %dir %{_localstatedir}/log/naemon
%attr(0755,naemon,naemon) %dir %{_localstatedir}/log/naemon/archives
%attr(-,root,root) %{_datadir}/naemon/documentation
%dir %{_libdir}/naemon
%attr(-,root,root) %{_libdir}/naemon/libnaemon.so*
%attr(-,root,root) %{_libdir}/naemon/plugins

%files tools
%defattr(-,root,root)
%attr(0755,root,root) %{_bindir}/naemonstats
%attr(0755,root,root) %{_bindir}/shadownaemon
%{_mandir}/man8/naemonstats.8*
%{_mandir}/man8/shadownaemon.8*

%files -n libnaemon
%defattr(-,root,root)
%attr(-,root,root) %{_libdir}/%{name}/libnaemon.so*
%{_mandir}/man8/naemonstats.8*
%{_mandir}/man8/shadownaemon.8*


%files devel
%defattr(-,root,root)
%attr(-,root,root) %{_includedir}/naemon/
%attr(-,root,root) %{_libdir}/naemon/libnaemon.a
%attr(-,root,root) %{_libdir}/naemon/libnaemon.la

%files livestatus
%defattr(-,root,root)
%dir %{_libdir}/naemon
%attr(0755,root,root) %{_bindir}/naemon-unixcat
%attr(0644,root,root) %{_libdir}/naemon/livestatus.o
%attr(0755,naemon,naemon) %dir %{_localstatedir}/log/naemon

%files thruk
%defattr(-,root,root)
%config(noreplace) %{_sysconfdir}/%{apachedir}/conf.d/naemon.conf
%attr(-,root,root) %{_datadir}/%{name}/naemon-thruk.include
%config(noreplace) %{_sysconfdir}/thruk/thruk_local.d/naemon.conf

%changelog