##
##  nagios.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package versions
%define       V_nagios                   3.5.0
%define       V_nagios_plugins           1.4.16
%define       V_perl_nagios_plugin       0.36
%define       V_perl_nagios_object       0.20
%define       V_perl_nagios_cmd          0.05

#   package information
Name:         nagios
Summary:      System and Network Monitoring
URL:          http://www.nagios.org/
Vendor:       Ethan Galstad et al.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Monitoring
License:      GPL
Version:      %{V_nagios}
Release:      20130316

#   package options
%option       with_perl   yes
%option       with_fsl    yes

#   list of sources
Source0:      http://download.sourceforge.net/nagios/nagios-%{V_nagios}.tar.gz
Source1:      http://download.sourceforge.net/nagiosplug/nagios-plugins-%{V_nagios_plugins}.tar.gz
Source2:      http://www.cpan.org/authors/id/T/TO/TONVOON/Nagios-Plugin-%{V_perl_nagios_plugin}.tar.gz
Source3:      http://www.cpan.org/authors/id/T/TO/TOBEYA/Nagios-Object-%{V_perl_nagios_object}.tar.gz
Source4:      http://www.cpan.org/authors/id/T/TO/TOBEYA/Nagios-Cmd-%{V_perl_nagios_cmd}.tar.gz
Source5:      nagios.txt
Source6:      rc.nagios
Source7:      fsl.nagios
Patch0:       nagios.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc
PreReq:       OpenPKG, openpkg >= 20160101, apache, MTA
%if "%{with_perl}" == "yes"
BuildPreReq:  perl-openpkg
BuildPreReq:  perl, perl-net, perl-util
PreReq:       perl, perl-net, perl-util
%endif
BuildPreReq:  zlib, jpeg, png, gd
PreReq:       zlib, jpeg, png, gd
BuildPreReq:  traceroute, openssl, openssh, coreutils, fping, nail, snmp
PreReq:       traceroute, openssl, openssh, coreutils, fping, nail, snmp
%if "%{with_fsl}" == "yes"
BuildPreReq:  fsl
PreReq:       fsl
%endif

%description
    Nagios is a system and network monitoring application. It watches
    hosts and services that you specify, alerting you when things go bad
    and when they get better.

%track
    prog nagios = {
        version   = %{V_nagios}
        url       = http://sourceforge.net/projects/nagios/files/
        regex     = nagios-(\d+(\.\d+)+)\.tar\.gz
    }
    prog nagios:plugins = {
        version   = %{V_nagios_plugins}
        url       = http://sourceforge.net/projects/nagiosplug/files/
        regex     = nagios-plugins-(\d+(\.\d+)+)\.tar\.gz
    }
    prog nagios:Nagios-Plugin = {
        version   = %{V_perl_nagios_plugin}
        url       = http://www.cpan.org/authors/id/T/TO/TONVOON/
        regex     = Nagios-Plugin-(__VER__)\.tar\.gz
    }
    prog nagios:Nagios-Object = {
        version   = %{V_perl_nagios_object}
        url       = http://www.cpan.org/authors/id/T/TO/TOBEYA/
        regex     = Nagios-Object-(__VER__)\.tar\.gz
    }
    prog nagios:Nagios-Cmd = {
        version   = %{V_perl_nagios_cmd}
        url       = http://www.cpan.org/authors/id/T/TO/TOBEYA/
        regex     = Nagios-Cmd-(__VER__)\.tar\.gz
    }

%prep
    %setup -q
    %setup -q -T -D -a 1
    %setup -q -T -D -a 2
    %setup -q -T -D -a 3
    %setup -q -T -D -a 4
    %patch -p0
    %{l_shtool} subst \
        -e 's;/usr/local/nagios/var/;%{l_prefix}/var/nagios/log;' \
        p1.pl

%build
    #   configure and build Nagios
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    LDFLAGS="%{l_ldflags} %{l_fsl_ldflags}" \
    LIBS="%{l_fsl_libs} -lpng -lz -ljpeg" \
    PATH_TO_TRACEROUTE="%{l_prefix}/sbin/traceroute" \
    ./configure \
        --prefix=%{l_prefix} \
        --sbindir=%{l_prefix}/cgi/nagios \
        --sysconfdir=%{l_prefix}/etc/nagios \
        --libexecdir=%{l_prefix}/libexec/nagios \
        --localstatedir=%{l_prefix}/var/nagios \
        --datadir=%{l_prefix}/share/nagios \
        --with-cgiurl=/nagios/cgi \
        --with-htmurl=/nagios \
        --with-nagios-user=%{l_rusr} \
        --with-nagios-group=%{l_rgrp} \
        --with-command-user=%{l_nusr} \
        --with-command-group=%{l_ngrp} \
        --with-gd-lib=%{l_prefix}/lib \
        --with-gd-inc=%{l_prefix}/include \
        --with-lockfile=%{l_prefix}/var/nagios/run/nagios.pid \
%if "%{with_perl}" == "yes"
        --enable-embedded-perl \
        --with-perlcache \
%endif
        --without-init-dir
    %{l_make} %{l_mflags} all

    #   configure and build Nagios plugins
    ( cd nagios-plugins-%{V_nagios_plugins}/
      ( echo "ac_cv_ping_packets_first=yes"
        echo "ac_cv_ping_has_timeout=no"
      ) >config.cache
      CC="%{l_cc}" \
      CFLAGS="%{l_cflags -O}" \
      LDFLAGS="%{l_ldflags} %{l_fsl_ldflags}" \
      LIBS="-lssl -lcrypto" \
      ./configure \
          --cache-file=./config.cache \
          --prefix=%{l_prefix}/libexec/nagios \
          --libexecdir=%{l_prefix}/libexec/nagios \
          --with-cgiurl=/nagios/cgi \
          --with-openssl=%{l_prefix} \
          --with-perl=%{l_prefix}/bin/perl \
          --with-df-command="%{l_prefix}/bin/gdf" \
          --with-fping-command=%{l_prefix}/sbin/fping \
          --with-ping-command="%{l_prefix}/bin/gping -n -c %d %s" \
          --with-ssh-command="%{l_prefix}/bin/ssh" \
          --with-snmpget-command="%{l_prefix}/bin/snmpget" \
          --with-snmpgetnext-command="%{l_prefix}/bin/snmpgetnext"
      %{l_make} %{l_mflags}
    ) || exit $?

%install

    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/nagios \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/nagios \
        $RPM_BUILD_ROOT%{l_prefix}/var/nagios/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/nagios/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/nagios/cmd

    #   install Nagios
    %{l_make} install DESTDIR="$RPM_BUILD_ROOT" INSTALL_OPTS="" COMMAND_OPTS=""

    #   install Nagios plugins
    ( cd nagios-plugins-%{V_nagios_plugins}/
      %{l_make} install DESTDIR="$RPM_BUILD_ROOT" \
          MKINSTALLDIRS="%{l_shtool} install -d"
    ) || exit $?

%if "%{with_perl}" == "yes"
    #   install Perl modules
    %{l_prefix}/bin/perl-openpkg prepare
    %{l_prefix}/bin/perl-openpkg -d Nagios-Plugin-%{V_perl_nagios_plugin} configure build install
    %{l_prefix}/bin/perl-openpkg -d Nagios-Object-%{V_perl_nagios_object} configure build install
    %{l_prefix}/bin/perl-openpkg -d Nagios-Cmd-%{V_perl_nagios_cmd} configure build install
    %{l_prefix}/bin/perl-openpkg -F perl-openpkg-files fixate cleanup
%else
    >perl-openpkg-files
%endif

    #   post-adjust installation
%if "%{with_perl}" == "yes"
    mv  $RPM_BUILD_ROOT%{l_prefix}/bin/p1.pl \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/nagios/p1.pl
%endif
    chmod 0755 $RPM_BUILD_ROOT%{l_prefix}/libexec/nagios/utils.sh
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/var/nagios/archives
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/libexec/nagios/share
    strip  $RPM_BUILD_ROOT%{l_prefix}/libexec/nagios/* 2>/dev/null || true

    #   install default configuration
    for name in `grep "^<file" %{SOURCE nagios.txt} | sed -e 's;^.*name=";;' -e 's;".*$;;'`; do
        (echo ""; cat %{SOURCE nagios.txt}; echo "") |\
            sed -e "1,/^<file name=\"$name\">/d" -e "/<\/file>/,\$d" >$name
        %{l_shtool} install -c -m 644 %{l_value -s -a} \
            $name $RPM_BUILD_ROOT%{l_prefix}/etc/nagios/
    done

    #   install run-command script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.nagios} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   install OSSP fsl configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/fsl
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE fsl.nagios} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/fsl/

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} `cat perl-openpkg-files` \
        '%config %{l_prefix}/etc/fsl/fsl.nagios' \
        '%config %{l_prefix}/etc/nagios/*' \
        '%config %attr(600,%{l_rusr},%{l_rgrp}) %{l_prefix}/etc/nagios/resource.cfg' \
        '%attr(555,%{l_musr},%{l_mgrp}) %{l_prefix}/bin/nagios*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/nagios/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/nagios/*/*' \
        '%attr(2775,%{l_rusr},%{l_ngrp}) %{l_prefix}/var/nagios/cmd'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   add hook to Apache configuration
        apacheconf="$RPM_INSTALL_PREFIX/etc/apache/apache.conf"
        if [ -f $apacheconf ]; then
            ( echo "Include $RPM_INSTALL_PREFIX/etc/nagios/apache.conf"
            ) | $RPM_INSTALL_PREFIX/lib/openpkg/rpmtool config \
                -a -i "$RPM_INSTALL_PREFIX:nagios" $apacheconf
        fi

        #   display final hints on initial installation
        ( echo "To complete this installation of Nagios please start the"
          echo "Nagios daemon (backend) Apache webserver (frontend) with:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc nagios start"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc apache start"
          echo "After this you can connect to the Nagios frontend via URL"
          echo "    http://127.0.0.1/nagios/"
          echo "and login with username \"nagios\" and password \"nagios\"."
          echo "You can change the password with:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/htpasswd \\ "
          echo "      $RPM_INSTALL_PREFIX/etc/nagios/apache.passwd nagios"
        ) | %{l_rpmtool} msg -b -t notice
    fi
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} nagios status 2>/dev/null`
        [ ".$nagios_active" = .yes ] && %{l_rc} nagios restart
    fi
    exit 0

%preun
    if [ $1 -eq 0 ]; then
        #   remove hook from Apache configuration
        apacheconf="$RPM_INSTALL_PREFIX/etc/apache/apache.conf"
        if [ -f $apacheconf ]; then
            $RPM_INSTALL_PREFIX/lib/openpkg/rpmtool config \
                -r -i "$RPM_INSTALL_PREFIX:nagios" $apacheconf
        fi

        #   before erase, stop service and remove log files
        %{l_rc} nagios stop 2>/dev/null
        rm -f $RPM_INSTALL_PREFIX/var/nagios/log/* >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/var/nagios/run/* >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/var/nagios/cmd/* >/dev/null 2>&1 || true
    fi
    exit 0

