%global gangver     3.7.2
%global webver      3.7.5
%global systemd         1
%global _hardened_build 1
%global py2             0
Summary:             Distributed Monitoring System
Name:                ganglia
Version:             %{gangver}
Release:             3
License:             BSD
URL:                 http://ganglia.sourceforge.net/
Source0:             http://downloads.sourceforge.net/sourceforge/ganglia/ganglia-%{version}.tar.gz
Source1:             https://github.com/ganglia/ganglia-web/archive/%{webver}/ganglia-web-%{webver}.tar.gz
Source2:             gmond.service
Source3:             gmetad.service
Source4:             ganglia-httpd24.conf.d
Source5:             ganglia-httpd.conf.d
Source6:             conf.php
Patch0:              ganglia-web-3.7.2-path.patch
Patch1:              ganglia-3.7.2-apache.patch
Patch2:              ganglia-3.7.2-sflow.patch
Patch3:              ganglia-3.7.2-tirpc-hack.patch
Patch4:              ganglia-web-5ee6b7.patch
Patch5:		     CVE-2019-20378_CVE-2019-20379.patch
%if 0%{?systemd}
BuildRequires:       systemd
%endif
BuildRequires:       rpcgen libtirpc-devel autoconf automake libtool
BuildRequires:       apr-devel >= 1 expat-devel freetype-devel gcc libart_lgpl-devel
BuildRequires:       libconfuse-devel libmemcached-devel libpng-devel
%if 0%{?py2}
BuildRequires:       python2-devel}
%endif
BuildRequires:       rrdtool-devel rsync /usr/bin/pod2man /usr/bin/pod2html make
BuildRequires:       php php-gd
%description
Ganglia is a scalable, real-time monitoring and execution environment
with all execution requests and statistics expressed in an open
well-defined XML format.

%package            web
Summary:             Ganglia Web Frontend
Version:             %{webver}
Requires:            rrdtool php php-gd %{name}-gmetad = %{gangver}-%{release}
%description        web
This package provides a web frontend to display the XML tree published
by ganglia, and to provide historical graphs of collected
metrics. This website is written in the PHP.

%package            gmetad
Summary:             Ganglia Metadata collection daemon
Requires:            %{name} = %{gangver}-%{release}
%if 0%{?systemd}
Requires(post):     systemd
Requires(preun):    systemd
Requires(postun):   systemd
%else
Requires(post):     /sbin/chkconfig
Requires(preun):    /sbin/chkconfig
Requires(preun):    /sbin/service
%endif
%description        gmetad
Ganglia is a scalable, real-time monitoring and execution environment
with all execution requests and statistics expressed in an open
well-defined XML format.
This gmetad daemon aggregates monitoring data from several clusters to
form a monitoring grid. It also keeps metric history using rrdtool.

%package            gmond
Summary:             Ganglia Monitoring daemon
Requires:            %{name} = %{gangver}-%{release}
%if 0%{?systemd}
Requires(post):     systemd
Requires(preun):    systemd
Requires(postun):   systemd
%else
Requires(post):     /sbin/chkconfig
Requires(preun):    /sbin/chkconfig
Requires(preun):    /sbin/service
%endif

%description        gmond
Ganglia is a scalable, real-time monitoring and execution environment
with all execution requests and statistics expressed in an open
well-defined XML format.
This gmond daemon provides the ganglia service within a single cluster
or Multicast domain.

%if 0%{?py2}
%package         -n python2-ganglia-gmond
Summary:             Ganglia Monitor daemon python DSO and metric modules
Requires:            ganglia-gmond python2
%{?python_provide:%python_provide python2-ganglia-gmond}
Provides:            ganglia-gmond-python = %{version}-%{release}
%description     -n python2-ganglia-gmond
Ganglia is a scalable, real-time monitoring and execution environment
with all execution requests and statistics expressed in an open
well-defined XML format.
This package provides the gmond python DSO and python gmond modules,
which can be loaded via the DSO at gmond daemon start time.
%endif

%package            devel
Summary:             Ganglia Library
Requires:            %{name} = %{gangver}-%{release} apr-devel libconfuse-devel
%description        devel
The Ganglia Monitoring Core library provides a set of functions that
programmers can use to build scalable cluster or grid applications

%prep
%setup -q
install -m 0644 %{SOURCE2} gmond/gmond.service.in
install -m 0644 %{SOURCE3} gmetad/gmetad.service.in
%patch1 -p0
%patch2 -p0
%patch3 -p1
%setup -q -T -D -a 1
mv ganglia-web-%{webver} web
pushd web
%patch0 -p1
%patch4 -p1
%patch5 -p1
popd

%build
touch Makefile.am
aclocal -I m4
autoheader
automake --add-missing --copy --foreign 2>/dev/null
libtoolize --automake --copy
automake --add-missing --copy --foreign
autoconf -f || exit 1
export CFLAGS="%{optflags} -fcommon"
%configure \
    --enable-setuid=ganglia \
    --enable-setgid=ganglia \
    --with-gmetad \
    --with-memcached \
    --disable-static \
    --enable-shared \
    --with-libpcre=no \
%if 0%{?py2}
    --with-python=%{__python2} \
%else
    --disable-python \
%endif
    --sysconfdir=%{_sysconfdir}/ganglia
%{__sed} -i 's|^hardcode_libdir_flag_spec=.*|hardcode_libdir_flag_spec=""|g' libtool
%{__sed} -i 's|^runpath_var=LD_RUN_PATH|runpath_var=DIE_RPATH_DIE|g' libtool
%{__perl} -pi.orig -e 's|nobody|ganglia|g' \
    gmond/gmond.conf.html ganglia.html gmond/conf.pod
%{__perl} -pi.orig -e 's|.*setuid_username.*|setuid_username ganglia|' \
    gmetad/gmetad.conf.in
%{__perl} -pi.orig -e 's|2345|-|g' gmond/gmond.init gmetad/gmetad.init
make %{?_smp_mflags}

%install
make install DESTDIR=%{buildroot}
%if 0%{?py2}
mkdir -p %{buildroot}%{_libdir}/ganglia/python_modules}
%endif
mkdir -p %{buildroot}%{_localstatedir}/lib/%{name}/rrds
%if 0%{?systemd}
install -Dp -m 0644 %{SOURCE2} %{buildroot}%{_unitdir}/gmond.service
install -Dp -m 0644 %{SOURCE3} %{buildroot}%{_unitdir}/gmetad.service
%else
install -Dp -m 0755 gmond/gmond.init %{buildroot}%{_sysconfdir}/init.d/gmond
install -Dp -m 0755 gmetad/gmetad.init %{buildroot}%{_sysconfdir}/init.d/gmetad
%endif
LD_LIBRARY_PATH=lib/.libs gmond/gmond -t | %{__perl} -pe 's|nobody|ganglia|g' \
    > %{buildroot}%{_sysconfdir}/ganglia/gmond.conf
%if 0%{?py2}
cp -p gmond/python_modules/conf.d/*.pyconf %{buildroot}%{_sysconfdir}/ganglia/conf.d/
cp -p gmond/modules/conf.d/*.conf %{buildroot}%{_sysconfdir}/ganglia/conf.d/
cp -p gmond/python_modules/*/*.py %{buildroot}%{_libdir}/ganglia/python_modules/
%endif
pushd web
make install DESTDIR=%{buildroot}
install -p -m 0644 %{SOURCE6} %{buildroot}%{_sysconfdir}/ganglia/conf.php
ln -s ../../..%{_sysconfdir}/%{name}/conf.php \
    %{buildroot}%{_datadir}/%{name}/conf.php
popd
install -Dp -m 0644 %{SOURCE4} %{buildroot}%{_sysconfdir}/httpd/conf.d/%{name}.conf
rm -f %{buildroot}%{_sysconfdir}/ganglia/conf.d/{modgstatus,example}.conf
rm -rf %{buildroot}%{_datadir}/%{name}/{Makefile*,debian,ganglia-web.spec*,ganglia-web}
rm -rf %{buildroot}%{_datadir}/%{name}/{conf_default.php.in,version.php.in}
rm -rf %{buildroot}%{_localstatedir}/lib/%{name}-web/conf/sql
rm -rf %{buildroot}%{_datadir}/%{name}/{README,TODO,AUTHORS,COPYING}
rm -f %{buildroot}%{_libdir}/*.la
chmod 0644 %{buildroot}%{_datadir}/%{name}/header.php
%if 0%{?py2}
chmod 0644 %{buildroot}%{_libdir}/%{name}/python_modules/*.py}
%endif
chmod 0644 %{buildroot}%{_datadir}/%{name}/css/smoothness/jquery-ui-1.10.2.custom.css
chmod 0644 %{buildroot}%{_datadir}/%{name}/css/smoothness/jquery-ui-1.10.2.custom.min.css
%if 0%{?py2}
sed -i '1{\@^#!@d}' %{buildroot}%{_libdir}/%{name}/python_modules/*.py}
%endif

%pre
/usr/sbin/useradd -c "Ganglia Monitoring System" \
        -s /sbin/nologin -r -d %{_localstatedir}/lib/%{name} ganglia 2> /dev/null || :
%if 0%{?systemd}

%post gmond
%systemd_post gmond.service

%preun gmond
%systemd_preun gmond.service

%postun gmond
%systemd_postun_with_restart gmond.service

%post gmetad
%systemd_post gmetad.service

%preun gmetad
%systemd_preun gmetad.service

%postun gmetad
%systemd_postun_with_restart gmetad.service
%else

%post gmond
/sbin/chkconfig --add gmond

%post gmetad
/sbin/chkconfig --add gmetad

%preun gmetad
if [ "$1" = 0 ]; then
  /sbin/service gmetad stop >/dev/null 2>&1 || :
  /sbin/chkconfig --del gmetad
fi

%preun gmond
if [ "$1" = 0 ]; then
  /sbin/service gmond stop >/dev/null 2>&1 || :
  /sbin/chkconfig --del gmond
fi
%endif

%pretrans web -p <lua>
path = "/usr/share/ganglia/lib/Zend"
st = posix.stat(path)
if st and st.type == "link" then
  os.remove(path)
end

%files
%license COPYING
%doc AUTHORS NEWS README ChangeLog
%{_libdir}/libganglia*.so.*
%dir %{_libdir}/ganglia
%{_libdir}/ganglia/*.so
%{?py2:%exclude %{_libdir}/ganglia/modpython.so}

%files gmetad
%dir %{_localstatedir}/lib/%{name}
%attr(0755,ganglia,ganglia) %{_localstatedir}/lib/%{name}/rrds
%{_sbindir}/gmetad
%if 0%{?systemd}
%{_unitdir}/gmetad.service
%else
%{_sysconfdir}/init.d/gmetad
%endif
%{_mandir}/man1/gmetad.1*
%{_mandir}/man1/gmetad.py.1*
%dir %{_sysconfdir}/ganglia
%config(noreplace) %{_sysconfdir}/ganglia/gmetad.conf

%files gmond
%{_bindir}/gmetric
%{_bindir}/gstat
%{_sbindir}/gmond
%if 0%{?systemd}
%{_unitdir}/gmond.service
%else
%{_sysconfdir}/init.d/gmond
%endif
%{_mandir}/man5/gmond.conf.5*
%{_mandir}/man1/gmond.1*
%{_mandir}/man1/gstat.1*
%{_mandir}/man1/gmetric.1*
%dir %{_sysconfdir}/ganglia
%if 0%{?py2}
%dir %{_sysconfdir}/ganglia/conf.d}
%endif
%config(noreplace) %{_sysconfdir}/ganglia/gmond.conf
%if 0%{?py2}
%config(noreplace) %{_sysconfdir}/ganglia/conf.d/*.conf}
%exclude %{_sysconfdir}/ganglia/conf.d/modpython.conf}
%endif

%if 0%{?py2}
%files -n python2-ganglia-gmond
%dir %{_libdir}/ganglia/python_modules/
%{_libdir}/ganglia/python_modules/*.py*
%{_libdir}/ganglia/modpython.so*
%config(noreplace) %{_sysconfdir}/ganglia/conf.d/*.pyconf*
%config(noreplace) %{_sysconfdir}/ganglia/conf.d/modpython.conf
%endif

%files devel
%{_bindir}/ganglia-config
%{_includedir}/*.h
%{_libdir}/libganglia*.so

%files web
%license web/COPYING
%doc web/AUTHORS web/README web/TODO
%config(noreplace) %{_sysconfdir}/%{name}/conf.php
%config(noreplace) %{_sysconfdir}/httpd/conf.d/%{name}.conf
%{_datadir}/%{name}
%dir %attr(0755,apache,apache) %{_localstatedir}/lib/%{name}-web/conf
%config(noreplace) %attr(0644,apache,apache) %{_localstatedir}/lib/%{name}-web/conf/*.json
%dir %attr(0755,apache,apache) %{_localstatedir}/lib/%{name}-web/dwoo
%dir %attr(0755,apache,apache) %{_localstatedir}/lib/%{name}-web/dwoo/cache
%dir %attr(0755,apache,apache) %{_localstatedir}/lib/%{name}-web/dwoo/compiled

%changelog
* Tue Mar 12 2024 yaoxin <yao_xin001@hoperun.com> - 3.7.2-3
- Disable pcre

* Wed Mar 02 2022 houyingchao <houyingchao@huawei.com> - 3.7.2-2
- Fix CVE-2019-20378 CVE-2019-20379

* Wed Apr 14 2021 chengzihan <chengzihan2@huawei.com> - 3.7.2-1
- package init
