File prometheus-snmp_exporter.spec of Package prometheus-snmp_exporter

#
# spec file for package prometheus-snmp_exporter
#
# Copyright (c) 2021 SUSE LLC
#
# 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 https://bugs.opensuse.org/
#


%global provider        github
%global provider_tld    com
%global project         prometheus
%global repo            snmp_exporter
%global provider_prefix %{provider}.%{provider_tld}/%{project}/%{repo}
%global import_path     %{provider_prefix}
%global generator_exec  prometheus-snmp_generator
%global _prometheusconfdir %{_sysconfdir}/prometheus-snmp_exporter
Name:           prometheus-snmp_exporter
Version:        0.20.0
Release:        0
Summary:        Prometheus exporter for SNMP-enabled devices
License:        Apache-2.0
Group:          System/Monitoring
URL:            https://github.com/%{project}/%{repo}
Source0:        %{repo}-%{version}.tar.gz
Source1:        vendor.tar.gz
Source2:        %{name}.service
Source3:        %{name}.sysconfig
BuildRequires:  golang-packaging
BuildRequires:  net-snmp-devel
%{go_provides}

%description
An exporter for Prometheus which exposes information gathered from Simple
Network Management Protocol (SNMP) for use by the Prometheus monitoring
system.

There are two components - an exporter which does the actual scraping, and a
generator which creates the configuration for use by the exporter.

%prep

%setup -q -n %{repo}-%{version} # unpack project sources
%setup -q -n %{repo}-%{version} -T -D -a 1 # unpack go dependencies in vendor.tar.gz, which was prepared by the source services

%build
#build prometheus-snmp_exporter
%{goprep} github.com/prometheus/snmp_exporter
go build -mod=vendor -buildmode=pie -o %{name}

#build prometheus-snmp_generator
cd generator
cp -a ../vendor ./
%{goprep} github.com/prometheus/snmp_exporter/generator
go build -mod=vendor -buildmode=pie  -o %{generator_exec}
cd -

%pre
%service_add_pre %{name}.service

%install
#install prometheus-snmp_exporter
install -m 0755 -d  %{buildroot}%{_bindir}
install -m 0755 %{name} %{buildroot}%{_bindir}/%{name}
install -m 0755 -d  %{buildroot}%{_prometheusconfdir}
install -m 0644 -vp snmp.yml %{buildroot}%{_prometheusconfdir}/snmp.yml

#install prometheus-snmp_generator
install -m 0755 generator/%{generator_exec} %{buildroot}%{_bindir}/%{generator_exec}

#install unit configuration file (systemd)
install -D -m 0644 %{SOURCE2} %{buildroot}%{_unitdir}/%{name}.service

#install configuration file
install -D -m 0644 %{SOURCE3} %{buildroot}%{_sysconfdir}/default/%{name}

#rclink
install -m 0755 -d  %{buildroot}%{_sbindir}
ln -sf %{_sbindir}/service %{buildroot}%{_sbindir}/rc%{name}

#copy and rename md files for prometheus-snmp_generator
for file in generator\/*\.md; do
  mv "${file}" "generator.$(basename ${file})"
done

%check
#removing vendor folders, cause they are used just for compiling
rm -rf vendor
rm -rf generator/vendor
#now check can be run
%make_build style
%make_build check_license

%post
%service_add_post %{name}.service

%preun
%service_del_preun %{name}.service

%postun
%service_del_postun %{name}.service

%files
%license LICENSE
%doc *\.md

%{_prometheusconfdir}
%{_bindir}/%{name}
%{_bindir}/%{generator_exec}
%{_unitdir}/%{name}.service
%{_sbindir}/rc%{name}

%config(noreplace) %{_prometheusconfdir}/snmp.yml
%config(noreplace) %{_sysconfdir}/default/%{name}

%changelog
openSUSE Build Service is sponsored by