#needsrootforbuild
%define __os_install_post %{nil}

%define without_baseinfo      0
%define without_virt          0
%define without_flamegraph    0
%define without_l7            0
%define without_tcp           0
%define without_socket        0
%define without_io            0
%define without_proc          0
%define without_jvm           0
%define without_postgre_sli   0
%define without_opengauss_sli 0
%define without_nginx         1
%define without_tprofiling    0
%define without_kafka         1
%define without_hw            1
%define without_ksli          0
%define without_container     0
%define without_sermant       1

%define disable_report_event   0
%define disable_kafka_channel  0
%define disable_flamegraph_svg 0


Summary:       Intelligent ops toolkit for openEuler
Name:          gala-gopher
Version:       2.0.1
Release:       2
License:       Mulan PSL v2
URL:           https://gitee.com/openeuler/gala-gopher
Source:        %{name}-%{version}.tar.gz
BuildRoot:     %{_builddir}/%{name}-%{version}
BuildRequires: systemd cmake gcc-c++ elfutils-devel clang llvm bpftool >= 6.8
BuildRequires: libconfig-devel libevent-devel openssl-devel libbpf-devel >= 2:0.8 uthash-devel
BuildRequires: jsoncpp-devel git libstdc++-devel
# for DT
#BuildRequires: CUnit-devel

%if !0%{?disable_kafka_channel}
BuildRequires: librdkafka-devel
%endif
%if !0%{?without_flamegraph}
BuildRequires: libcurl-devel
%endif
%if !0%{?without_jvm}
BuildRequires: java-1.8.0-openjdk-devel
%endif
%if !0%{?without_l7}
BuildRequires: jsoncpp-devel java-1.8.0-openjdk-devel
%endif

Requires:      bash gawk procps-ng glibc elfutils libbpf >= 2:0.8
Requires:      libconfig libevent iproute jsoncpp libstdc++

%if !0%{?disable_kafka_channel}
Requires:      librdkafka
%endif

%if !0%{?without_baseinfo}
Requires:      ethtool systemd iproute
%endif

%if !0%{?without_virt}
Requires:      systemd
%endif
%if !0%{?without_tcp}
Requires:      iproute conntrack-tools
%endif
%if !0%{?without_proc}
Requires:      kmod
%endif
%if !0%{?without_flamegraph}
%if !0%{?disable_flamegraph_svg}
Requires:      flamegraph
%endif
Requires:      libcurl
%endif
%if !0%{?without_opengauss_sli}
Requires:      python3-psycopg2 python3-yaml net-tools
%endif
%if !0%{?without_container}
Requires:      cadvisor python3-requests net-tools util-linux
%endif
%if !0%{?without_postgre_sli}
Requires:      iproute
%endif
%if !0%{?without_l7}
Requires:      jsoncpp conntrack-tools
%endif
%if !0%{?without_tprofiling}
Requires:      lsof
%endif

Patch1:	       avoid-use-ato.patch
Patch2:	       refactor-cadvisor-remove-the-dependency-on-the-pytho.patch
Patch3:	       fix-writing-metadata-to-log-file-properly.patch
Patch4:	       fix-segmentation-fault-when-setting-out_channel-of-m.patch
Patch5:	       fix-compile-warnings-when-disable-KAFKA_CHANNEL.patch
Patch6:	       fix-buffer-overflow-caused-by-strcpy.patch
Patch7:	       fix-delete-unnecessary-judgments.patch
Patch8:	       cadvisor_probe-fix-int-to-str-warnings.patch
Patch9:	       bugfix-probe_define-access-out-of-bounds.patch

%description
gala-gopher is a low-overhead eBPF-based probes framework

%prep
%autosetup -n %{name}-%{version} -p1


%build
BUILD_OPTS=(
  -DENABLE_BASEINFO=%[0%{?without_baseinfo}?0:1]
  -DENABLE_VIRT=%[0%{?without_virt}?0:1]

  -DENABLE_FLAMEGRAPH=%[0%{?without_flamegraph}?0:1]
  -DENABLE_L7=%[0%{?without_l7}?0:1]
  -DENABLE_TCP=%[0%{?without_tcp}?0:1]
  -DENABLE_SOCKET=%[0%{?without_tcp}?0:1]
  -DENABLE_IO=%[0%{?without_io}?0:1]
  -DENABLE_PROC=%[0%{?without_proc}?0:1]
  -DENABLE_JVM=%[0%{?without_jvm}?0:1]
  -DENABLE_POSTGRE_SLI=%[0%{?without_postgre_sli}?0:1]
  -DENABLE_OPENGAUSS_SLI=%[0%{?without_opengauss_sli}?0:1]
  -DENABLE_NGINX=%[0%{?without_nginx}?0:1]
  -DENABLE_TPROFILING=%[0%{?without_tprofiling}?0:1]
  -DENABLE_KAFKA=%[0%{?without_kafka}?0:1]
  -DENABLE_HW=%[0%{?without_hw}?0:1]
  -DENABLE_KSLI=%[0%{?without_ksli}?0:1]
  -DENABLE_CONTAINER=%[0%{?without_cadvisor}?0:1]
  -DENABLE_SERMANT=%[0%{?without_sermant}?0:1]

  -DENABLE_REPORT_EVENT=%[0%{?disable_report_event}?0:1]
  -DKAFKA_CHANNEL=%[0%{?disable_kafka_channel}?0:1]
  -DFLAMEGRAPH_SVG=%[0%{?disable_flamegraph_svg}?0:1]
)

pushd build
export PATH=$PATH:/usr/lib64/llvm12/bin
sh build.sh --debug "${BUILD_OPTS[@]}"
popd

%check
# pushd test
# sh test_modules.sh "${BUILD_OPTS[@]}"
# popd

%install
install -d %{buildroot}/etc/gala-gopher
install -d %{buildroot}/opt/gala-gopher
install -d %{buildroot}%{_bindir}
install -d %{buildroot}/usr/libexec/gala-gopher/
mkdir -p  %{buildroot}/usr/lib/systemd/system
install -m 0600 service/gala-gopher.service %{buildroot}/usr/lib/systemd/system/gala-gopher.service
pushd build
sh install.sh %{buildroot}%{_bindir} %{buildroot}/opt/gala-gopher %{buildroot}/etc/gala-gopher %{buildroot}/usr/libexec/gala-gopher/ %{buildroot}/opt/gala-gopher
popd

%post
%systemd_post gala-gopher.service
if [ -d /var/log/gala-gopher ]; then
  othermode=$(expr $(stat -L -c "%a" /var/log/gala-gopher) % 10)
  if [ $othermode -ne 0 ]; then
    chmod 750 /var/log/gala-gopher
    chmod 750 /var/log/gala-gopher/debug
    chmod 640 /var/log/gala-gopher/debug/gopher.log
  fi
fi

%preun
%systemd_preun gala-gopher.service

%postun
if [ $1 -eq 0 ]; then
  rm -rf /sys/fs/bpf/gala-gopher > /dev/null
fi
%systemd_postun_with_restart gala-gopher.service

%files
%attr(0750,root,root) %dir /opt/gala-gopher
%attr(0550,root,root) %dir /opt/gala-gopher/extend_probes
%attr(0750,root,root) %dir /opt/gala-gopher/meta
%attr(0750,root,root) %dir /opt/gala-gopher/btf
%attr(0550,root,root) %dir /opt/gala-gopher/lib
%attr(0550,root,root) %{_bindir}/*
%attr(0550,root,root) /opt/gala-gopher/extend_probes/*
%attr(0640,root,root) /opt/gala-gopher/meta/*
#%attr(0640,root,root) /opt/gala-gopher/btf/*
%attr(0550,root,root) /opt/gala-gopher/lib/*
%attr(0640,root,root) %config(noreplace) /etc/gala-gopher/probes.init
%attr(0640,root,root) %config(noreplace) /etc/gala-gopher/*.conf
%attr(0640,root,root) %config(noreplace) /etc/gala-gopher/extend_probes/*.conf
%attr(0600,root,root) /usr/lib/systemd/system/gala-gopher.service
%attr(0550,root,root) /usr/libexec/gala-gopher/init_probes.sh

%changelog
* Wed May 8 2024 Liping Hu <huliping10@huawei.com> - 2.0.1-2
- fix delete unnecessary judgments
  fix buffer overflow caused by strcpy()
  cadvisor_probe: fix int to str warnings
  fix compile warnings when disable KAFKA_CHANNEL
  fix segmentation fault when setting out_channel of metrics to logs
  fix writing metadata to log file properly
  bugfix: probe_define access out of bounds
  refactor(cadvisor): remove the dependency on the python-libconf
  avoid use ato*

* Wed Apr 24 2024 Tangxin Xie <xietangxin@huawei.com> - 2.0.1-1
- Update to 2.0.1

* Fri Mar 8 2024 Zhen Chen <chenzhen126@huawei.com> - 1.0.2-4
- fix command injection in ioprobe

* Fri Jun 9 2023 Tangxin Xie <xietangxin@huawei.com> - 1.0.2-3
- fix httpprobe find libssl path
  Fix segmentation fault of gala-gopher cmd
  fix prepare_dependence
  bugfix:The log time is not the local time
  fix: fix fd leakage problem
  cadvisor_probe:convert container_id type to bytes
  adapt block_rq_issue tracepoint args in kernel-5.10
  modify desc from chinese to english
  bugfix: fix gala-gopher -h print err
  fix: change license software name
  fix: fix install error
  bugfix: zombie task.
  fix:add default data of event conf
  fix access violation

* Fri Apr 7 2023 Tangxin Xie <xietangxin@huawei.com> - 1.0.2-2
- bugfix add check whether cadvisor is installed
  bugfix fix system_proc collect data err
  change return type of uprobe from void to int
  Fix for popen cannot get stderr
  fix modify unit of some metrics to second
  refactor modify jvmprobe to support pod
  stackprobe fix inaccurate call stack count add samp
  add python3-libconf and python3-request deps

* Fri Mar 3 2023 Tangxin Xie <xietangxin@huawei.com> - 1.0.2-1
- update to 1.0.2

* Wed Feb 22 2023 Tangxin Xie <xietangxin@huawei.com> - 1.0.1-5
- fix testcase compile and run errs

* Tue Jan 31 2023 Zhen Chen <chenzhen126@huawei.com> - 1.0.1-4
- add net-tools and ethtool to Requires

* Sun Jan 29 2023 Zhen Chen <chenzhen126@huawei.com> - 1.0.1-3
- specify libbpf version in BuildRequires and add some Requires

* Sat Jan 14 2023 Zhen Chen <chenzhen126@huawei.com> - 1.0.1-2
- fix ksliprobe get invalid args occasionally at startup
- fix error print when starting gala-gopher
- add system_uuid field to distinguish client when post to pyroscope server
- repair stackprobe caused cpu rush
- add support to pyroscope
- bugfix: add check if thread is 0
- fix stackprobe memory allocation and deallocation errors.
- normalize time format in flamegraph svg filename

* Mon Dec 12 2022 Zhen Chen <chenzhen126@huawei.com> - 1.0.1-1
- Update to 1.0.1

* Mon Dec 12 2022 Zhen Chen <chenzhen126@huawei.com> - 1.0.0-12
- remove debug_package %{nil} to strip files and provide debug packages

* Tue Dec 6 2022 Zhen Chen <chenzhen126@huawei.com> - 1.0.0-11
- add %config(noreplace) to conf files of extend_probes

* Tue Dec 6 2022 Zhen Chen <chenzhen126@huawei.com> - 1.0.0-10
- simplify vmlinux.h management with different kernel versions

* Wed Nov 30 2022 Zhen Chen <chenzhen126@huawei.com> - 1.0.0-9
- fix java.probe build error caused by a typo in build.sh
  Only check the installation of dep when building ebpf.probes
  Remove unused BuildRequires and add java-1.8.0-openjdk-devel

* Mon Nov 28 2022 wo_cow <niuqianqian@huawei.com> - 1.0.0-8
- fix issues raised during 22.03 test

* Thu Nov 17 2022 wo_cow <niuqianqian@huawei.com> - 1.0.0-7
- resolve patch conflict

* Thu Nov 17 2022 wo_cow <niuqianqian@huawei.com> - 1.0.0-6
- adapt libbpf v0.8

* Thu Nov 17 2022 Zhen Chen <chenzhen126@huawei.com> - 1.0.0-5
- add flamegraph-stackcollapse to Requires

* Tue Nov 15 2022 Zhen Chen <chenzhen126@huawei.com> - 1.0.0-4
- add systemd to BuildRequires to fix install/uninstall errors
  simplify patch application in %prep

* Mon Nov 14 2022 Zhen Chen <chenzhen126@huawei.com> - 1.0.0-3
- fix pgsliprobe

* Mon Nov 14 2022 Zhen Chen <chenzhen126@huawei.com> - 1.0.0-2
- add vmlinux for 22.03-LTS & 22.03-LTS-SP1

* Mon Nov 14 2022 Zhen Chen <chenzhen126@huawei.com> - 1.0.0-1
- Package init

