File chrony-graph.spec of Package chrony-graph

#
# spec file for package chrony-graph
#
# Copyright (c) 2020 SUSE LINUX GmbH, Nuernberg, Germany.
#
# 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 local_user     chrony
%define script_dir     %{_libexecdir}/%{name}
%define script_wrapper %{name}
%define script_work    %{name}.sh
%define chrony_logdir  %{_localstatedir}/log/chrony
%define chrony_rundir  %{_rundir}/%{name}
%define chrony_htmldir %{chrony_rundir}/.%{name}

Name:           chrony-graph
Version:        20200204T184000.f395b49
Release:        0
Summary:        graphing logs from chrony
License:        MIT
Url:            https://github.com/ddrown/chrony-graph
Source0:        %{name}-%{version}.tar.xz
Patch0:         chrony-graph.patch
BuildArch:      noarch
BuildRequires:  systemd-rpm-macros
Requires:       bc
Requires:       chrony
Requires:       gawk
Requires:       gnuplot
Requires:       liberation-fonts
Requires:       perl(Cwd)
Requires:       perl(DateTime)
Requires:       perl(File::Basename)
Requires:       sed

%description
Continuously create colorful graphs from chronyd statistics file for entertainment.
They can be accessed via http://localhost/chrony-graph/ if a webserver is running.

%prep
%autosetup -p1

%build

%install
# /usr/lib/rpm/brp-suse.d/brp-25-symlink
export NO_BRP_STALE_LINK_ERROR='yes'
# follows rmt-server.spec, because nginx is crap
mkdir -vp %{buildroot}%{_datadir}/rmt/public
ln -sfv %{chrony_htmldir} %{buildroot}%{_datadir}/rmt/public/%{name}
#
d='%{_libexecdir}/%{name}'
br='%{buildroot}%{_libexecdir}/%{name}'
mkdir -vp "${br}"
pushd bin
for i in \
	copy-to-website \
	diff-freq-graph \
	foreach-stat \
	graph-header \
	local-clock-graph \
	offset-graph \
	plot \
	run \
	skew-graph \
; \
do
	o="${br}/${i}"
	sed "
	s@^#./bin/sh@#!/bin/bash@
	s@\.\./bin/@${d}/@g
	s@\.\./aliases@~/.%{name}.aliases.conf@g
	" "${i}" > "${o}"
	diff -u "${i}" "${o}" || :
	chmod -v 755 "${o}"
done
for perl in \
	index \
	histogram \
	mul \
	percentile \
	split \
	timestamps \
; \
do
	o="${br}/${perl}"
	sed '
	s@index.html.tmpl@%{_datadir}/%{name}/index.html.tmpl@
	' "${perl}" > "${o}"
	diff -u "${perl}" "${o}" || :
	chmod -v 755 "${o}"
done
popd
o='%{buildroot}%{_datadir}/%{name}'
mkdir -vp "${o}"
cat runX/index.html.tmpl > %{buildroot}%{_datadir}/%{name}/index.html.tmpl
#
mkdir -vp %{buildroot}%{_unitdir}
pushd "$_"
tee '%{name}.timer' <<_EOT_
[Unit]
Description=%{name} timer
After=network-online.target
After=chronyd.service
[Timer]
OnBootSec=1m
OnUnitActiveSec=123m
OnActiveSec=3s
[Install]
WantedBy=timers.target
_EOT_
tee '%{name}.service' <<_EOS_
[Unit]
Description=%{name} service
ConditionPathExists=%{chrony_logdir}/measurements.log
ConditionPathExists=%{chrony_logdir}/statistics.log
ConditionPathExists=%{chrony_logdir}/tracking.log
After=network-online.target
After=chronyd.service
[Service]
WorkingDirectory=~
User=%{local_user}
Type=simple
ExecStart=%{script_dir}/%{script_wrapper}
Nice=9
PrivateTmp=yes
_EOS_
mkdir -vp 'chronyd.service.d'
tee 'chronyd.service.d/%{name}.conf' <<_EOS_
[Unit]
Description=chronyd, with override from %{name}
[Service]
Nice=-13
IOSchedulingClass=realtime
IOSchedulingPriority=3
_EOS_
popd
#
mkdir -vp %{buildroot}%{_tmpfilesdir}
pushd "$_"
tee '%{name}.conf' <<_EOS_
d %{chrony_rundir} 0755 %{local_user} %{local_user} -
_EOS_
popd
#
mkdir -vp %{buildroot}%{script_dir}
pushd "$_"
tee %{script_wrapper} <<'_EOS_'
#!/bin/bash
fn() {
	%{script_dir}/%{script_work}
}
fn
_EOS_
#
tee '%{script_work}' <<'_EOF_'
#!/bin/bash
set -e
td=`mktemp --directory --tmpdir=%{chrony_rundir} .%{name}.XXX`
OUTPUT_DIR=%{chrony_htmldir}
TITLES=~/titles
trap "rm -rf '${td}'" EXIT
td="${td}/%{name}"
mkdir "${td}"
if pushd "${td}" > /dev/null
then
	do_run='maybe'
	if ! test -d "${OUTPUT_DIR}"
	then
		mkdir -v "${OUTPUT_DIR}"
		touch -d '@1' "${OUTPUT_DIR}"
	fi
	for i in %{chrony_logdir}/*
	do
		if ! test -e "${i}"
		then
			do_run=
			echo "no logs"
			break
		fi
		if test -d "${OUTPUT_DIR}" && test "${i}" -nt "${OUTPUT_DIR}"
		then
			do_run='newer'
			break
		fi
	done
	if test -f "${TITLES}" && test "${TITLES}" -nt "${OUTPUT_DIR}"
	then
		do_run='newer'
	fi
	if test "${do_run}" = 'maybe'
	then
		do_run=
		echo "no news"
	fi
	if test -n "${do_run}"
	then
		export OUTPUT_DIR
		export TITLES
		export CHRONY_LOGDIR='%{chrony_logdir}'
		(
			if rpm -q --qf '%%{NAME} %%{VERSION}-%%{RELEASE} - %%{DISTRIBUTION}\n' %{name} 2>> 'stderr'
			then
				: good
			else
				echo 'rpm(1) failed'
			fi
			ccs='chronyc -n sources'
			if chronyc -n sources > "${ccs}" 2>> 'stderr'
			then
				sed -i -n '/^\^[+*]/p' "${ccs}"
			else
				echo 'chronyc(1) failed'
			fi
			if head -n -0 %{_sysconfdir}/chrony.conf %{_sysconfdir}/chrony.d/*.conf "${ccs}" /proc/uptime 'stderr' &> ".$$"
			then
				grep -Evh '^[[:blank:]]*([!#;%%]|$)' ".$$"
			else
				echo 'head(1) failed'
			fi
			if test -s 'stderr'
			then
				logger --id=$PPID.$$ --tag '%{name}' < 'stderr'
			fi
		) &> 'notes'
		if bash %{_libexecdir}/%{name}/run
		then
			touch "${OUTPUT_DIR}"
		fi
	fi
	popd > /dev/null
fi
_EOF_
chmod -v 00555 *
popd
#
DIR=%{buildroot}%{_sysconfdir}/apache2/conf.d/
mkdir -vp "${DIR}"
pushd "${DIR}"
tee %{name}.conf <<_EOF_
Alias /%{name} "%{chrony_htmldir}"
<Directory %{chrony_htmldir}>
  <IfModule !mod_access_compat.c>
    Require all granted
  </IfModule>
  <IfModule mod_access_compat.c>
    Order allow,deny
    Allow from all
  </IfModule>
  Options	+Indexes +Multiviews +FollowSymLinks
  IndexOptions	FancyIndexing \
	ScanHTMLTitles \
	NameWidth=* \
	DescriptionWidth=*
</Directory>
_EOF_
popd
#
DIR=%{buildroot}%{_sysconfdir}/chrony.d/
mkdir -vp "${DIR}"
pushd "${DIR}"
tee %{name}.conf <<_EOF_
logdir %{chrony_logdir}
log measurements statistics tracking
logbanner 0
logchange 0.01
_EOF_
popd
#

%pre
%service_add_pre %{name}.timer
%post
%tmpfiles_create %{_tmpfilesdir}/%{name}.conf
%service_add_post %{name}.timer
%preun
%service_del_preun %{name}.timer
%postun
%service_del_postun %{name}.timer
%posttrans
if test -x %{_bindir}/systemctl
then
	if %{_bindir}/systemctl --quiet --no-pager is-enabled %{name}.timer
	then
		: good
	else
		echo "Enable %{name}.timer" 
		%{_bindir}/systemctl --no-pager enable %{name}.timer
	fi
	if %{_bindir}/systemctl --quiet --no-pager is-active %{name}.timer
	then
		: good
	else
		echo "Starting %{name}.timer" 
		%{_bindir}/systemctl --no-pager start %{name}.timer
	fi
	if %{_bindir}/systemctl --quiet --no-pager is-failed %{name}.timer
	then
		%{_bindir}/systemctl --no-pager status %{name}.timer
		echo "Restarting %{name}.timer" 
		%{_bindir}/systemctl --no-pager restart %{name}.timer
	else
		: good
	fi
fi

%files
%config %{_datadir}/rmt
%config %{_sysconfdir}/apache2
%config %{_sysconfdir}/chrony.d
%config %{_unitdir}/*
%config %{_tmpfilesdir}/%{name}.conf
%config %{_libexecdir}/%{name}
%config %{_datadir}/%{name}

%changelog

openSUSE Build Service is sponsored by