#!/bin/sh
#Creating the alertmanager directory to keep configuration files
DIR="/etc/alertmanager"
if [ -d "$DIR" ]; then
### Take action if $DIR exists ###
echo "Directory already exsists"
else
### Control will jump here if $DIR does NOT exists ###
sudo mkdir /etc/alertmanager
echo "${DIR} Created"
fi
DIR="/usr/local/bin/alertmanager"
if [ -d "$DIR" ]; then
### Take action if $DIR exists ###
echo "Directory already exsists"
else
### Control will jump here if $DIR does NOT exists ###
sudo mkdir /usr/local/bin/alertmanager
echo "${DIR} Created"
fi
#copy prometheus binaries to the following location and assign the perimission
sudo cp alertmanager /usr/local/bin/alertmanager
sudo cp amtool /usr/local/bin/alertmanager
echo "Files are copied to target location"
#Creating alertmanager configuration file
sudo touch /etc/alertmanager/alertmanager.yml
cat <<EOF >/etc/alertmanager/alertmanager.yml
route:
group_by: [Alertname]
group_interval: 30s
repeat_interval: 30s
# Send all notifications to me.
receiver: email-me
receivers:
- name: email-me
email_configs:
- send_resolved: true
to: test@example.com
from: alertmanager@example.com
smarthost: smtp.gmail.com:587
auth_username: "alertmanager@example.com"
auth_identity: "alertmanager@example.com"
auth_password: "**********"
EOF
echo "Configuration file is created"
#Creating the alertmanager service file
sudo touch /etc/systemd/system/alertmanager.service
cat <<EOF >/etc/systemd/system/alertmanager.service
[Unit]
Description=Prometheus Alert Manager Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/alertmanager/alertmanager --config.file=/etc/alertmanager/alertmanager.yml
[Install]
WantedBy=multi-user.target
EOF
echo "Alert manager service is created"
#Adding alertmanager port to firewall
cat /etc/novell-release | grep "VERSION = 2023"
if [ $? -eq 0 ]; then
DEFAULT_ZONE=`firewall-cmd --get-default-zone`
for port in 9093; do
firewall-cmd --permanent --zone=$DEFAULT_ZONE --add-port=${port}/tcp > /dev/null 2>&1
done
firewall-cmd --add-masquerade --permanent --zone=$DEFAULT_ZONE > /dev/null 2>&1
firewall-cmd --reload > /dev/null 2>&1
else
sed -i 's/FW_SERVICES_EXT_TCP="/FW_SERVICES_EXT_TCP="9093 /g' /etc/sysconfig/SuSEfirewall2
systemctl restart SuSEfirewall2.service
fi
sleep 5
#After creating and modifying service file need to restart daemom
sudo systemctl daemon-reload
sudo systemctl start alertmanager
sleep 5
#Checking the status of alertmanager service
systemctl is-active --quiet alertmanager.service
if [ $? -eq 0 ]; then
echo Alert Manager service is running
else
echo Alert Manager service is not running
fi
exit