Skip to content

Instantly share code, notes, and snippets.

@OnkelDom
Last active November 23, 2021 21:52
Show Gist options
  • Save OnkelDom/157e8022ed3dfb7d480167baa484f10b to your computer and use it in GitHub Desktop.
Save OnkelDom/157e8022ed3dfb7d480167baa484f10b to your computer and use it in GitHub Desktop.
Script to Update Prometheus and Alertmanager straight forward
#!/bin/bash
bindir="/usr/local/bin"
binuser="root"
bingroup="root"
unamearch=$(uname -m)
case $unamearch in
(i386) arch=386;;
(x86_64) arch=amd64;;
(aarch64) arch=arm64;;
(armv7l) arch=armv7;;
(armv6l) arch=armv6;;
(*) arch=amd64;;
esac
echo "System architecure: $arch"
if [[ -f "$bindir/prometheus" ]]; then
prometheus_version_installed=v$($bindir/prometheus --version | head -n1 | awk '{print $3}')
prometheus_version_github=$(curl -s https://api.github.com/repos/prometheus/prometheus/releases/latest | jq -r .tag_name)
echo "Prometheus version installed: $prometheus_version_installed"
echo "Prometheus version Github: $prometheus_version_github"
if [ $prometheus_version_installed != $prometheus_version_github ]; then
echo "# Update Prometheus and Promtool"
tempdir="/tmp/prometheus"
promuser="prometheus"
promgroup="prometheus"
promconfigfolder="/etc/prometheus"
mkdir -p $tempdir
wget --quiet https://github.com/prometheus/prometheus/releases/download/$prometheus_version_github/prometheus-${prometheus_version_github:1}.linux-$arch.tar.gz -O $tempdir/prometheus_${prometheus_version_github:1}.tar.gz
tar xvf $tempdir/prometheus_${prometheus_version_github:1}.tar.gz -C $tempdir
systemctl stop prometheus.service
cp $tempdir/prometheus-${prometheus_version_github:1}.linux-$arch/prometheus $bindir/
cp $tempdir/prometheus-${prometheus_version_github:1}.linux-$arch/promtool $bindir/
chown $binuser:$bingroup $bindir/prometheus
chown $binuser:$bingroup $bindir/promtool
cp -r $tempdir/prometheus-${prometheus_version_github:1}.linux-$arch/consoles $promconfigfolder/
cp -r $tempdir/prometheus-${prometheus_version_github:1}.linux-$arch/console_libraries $promconfigfolder/
chown -R $promuser:$promgroup $promconfigfolder/consoles
chown -R $promuser:$promgroup $promconfigfolder/console_libraries
systemctl start prometheus.service
rm -rf $tempdir
prometheus_newversion_installed=v$($bindir/prometheus --version | head -n1 | awk '{print $3}')
if [ $prometheus_newversion_installed == $prometheus_version_github ]; then
echo "Prometheus updated to version $prometheus_newversion_installed"
systemctl status prometheus.service | tail -n1
else
echo "!!! Update failed !!!"
fi
else
echo "Current version $prometheus_version_installed up to date."
fi
else
echo "Prometheus not installed on $bindir/prometheus"
fi
if [[ -f "$bindir/alertmanager" ]]; then
alertmanager_version_installed=v$($bindir/alertmanager --version | head -n1 | awk '{print $3}')
alertmanager_version_github=$(curl -s https://api.github.com/repos/prometheus/alertmanager/releases/latest | jq -r .tag_name)
echo "Alertmanager version installed: $alertmanager_version_installed"
echo "Alertmanager version GitHub: $alertmanager_version_github"
if [ $alertmanager_version_installed != $alertmanager_version_github ]; then
echo "# Update Alertmanager and Amtool"
amtempdir="/tmp/alertmanager"
mkdir -p $amtempdir
wget --quiet https://github.com/prometheus/alertmanager/releases/download/$alertmanager_version_github/alertmanager-${alertmanager_version_github:1}.linux-$arch.tar.gz -O $amtempdir/alertmanager_${alertmanager_version_github:1}.tar.gz
tar xvf $amtempdir/alertmanager_${alertmanager_version_github:1}.tar.gz -C $amtempdir
systemctl stop alertmanager.service
cp $amtempdir/alertmanager-${alertmanager_version_github:1}.linux-$arch/alertmanager $bindir/
cp $amtempdir/alertmanager-${alertmanager_version_github:1}.linux-$arch/amtool $bindir/
chown $binuser:$bingroup $bindir/alertmanager
chown $binuser:$bingroup $bindir/amtool
systemctl start alertmanager.service
rm -rf $amtempdir
alertmanager_newversion_installed=v$($bindir/alertmanager --version | head -n1 | awk '{print $3}')
if [ $alertmanager_newversion_installed == $alertmanager_version_github ]; then
echo "Alertmanager updated to version $alertmanager_newversion_installed"
systemctl status alertmanager.service | tail -n1
else
echo "!!! Update failed !!!"
fi
else
echo "Current version $alertmanager_version_github up to date."
fi
else
echo "Alertmanager not installed on $bindir/alertmanager"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment