#!/bin/bash
# Define constants
PROMETHEUS_USER="prometheus"
PROMETHEUS_GROUP="prometheus"
PROMETHEUS_BIN_PATH="/usr/local/bin"
PROMETHEUS_CONF_DIR="/etc/prometheus"
PROMETHEUS_DATA_DIR="/var/lib/prometheus"
PROMETHEUS_CONFIG_FILE="$PROMETHEUS_CONF_DIR/exporter-config.yml"
PROMETHEUS_SERVICE_FILE="/etc/systemd/system/prometheus.service"
# Check if user exists and create if not
if ! getent passwd $PROMETHEUS_USER > /dev/null 2>&1; then
sudo useradd --no-create-home --shell /bin/false $PROMETHEUS_USER
echo "User $PROMETHEUS_USER created"
else
echo "User $PROMETHEUS_USER already exists"
fi
# Check if group exists and create if not
if ! getent group $PROMETHEUS_GROUP > /dev/null 2>&1; then
sudo groupadd $PROMETHEUS_GROUP
echo "Group $PROMETHEUS_GROUP created"
else
echo "Group $PROMETHEUS_GROUP already exists"
fi
# Create required directories and assign ownership
for DIR in $PROMETHEUS_CONF_DIR $PROMETHEUS_DATA_DIR; do
if [ -d "$DIR" ]; then
echo "Directory $DIR already exists"
else
sudo mkdir -p "$DIR"
echo "Directory $DIR created"
fi
sudo chown -R $PROMETHEUS_USER:$PROMETHEUS_GROUP "$DIR"
echo "Ownership set for $DIR"
done
# Copy Prometheus binaries to the specified location and assign ownership
for FILE in prometheus promtool; do
sudo cp "$FILE" "$PROMETHEUS_BIN_PATH/"
sudo chown $PROMETHEUS_USER:$PROMETHEUS_GROUP "$PROMETHEUS_BIN_PATH/$FILE"
done
echo "Files copied to $PROMETHEUS_BIN_PATH"
# Copy console and console library files to the Prometheus configuration directory and assign ownership
for DIR in consoles console_libraries; do
sudo cp -r "$DIR" "$PROMETHEUS_CONF_DIR/"
sudo chown -R $PROMETHEUS_USER:$PROMETHEUS_GROUP "$PROMETHEUS_CONF_DIR/$DIR"
done
echo "Consoles and console libraries copied to $PROMETHEUS_CONF_DIR"
# Create Prometheus configuration file and assign ownership
cat <<EOF > "$PROMETHEUS_CONFIG_FILE"
global:
scrape_interval: 15s
scrape_configs:
- job_name: Docker Servers
static_configs:
- targets: ['localhost:8080']
- job_name: OES Servers
static_configs:
- targets: ['localhost:9100']
EOF
sudo chown $PROMETHEUS_USER:$PROMETHEUS_GROUP "$PROMETHEUS_CONFIG_FILE"
echo "Configuration file created at $PROMETHEUS_CONFIG_FILE"
# Create Prometheus service file
cat <<EOF > "$PROMETHEUS_SERVICE_FILE"
[Unit]
Description=Prometheus
Wants=network-online.target ndsd.service
After=network-online.target ndsd.service
[Service]
User=$PROMETHEUS_USER
Group=$PROMETHEUS_GROUP
Type=simple
ExecStart=/usr/local/bin/prometheus \
--config.file /etc/prometheus/exporter-config.yml \
--storage.tsdb.path /var/lib/prometheus/ \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries
[Install]
WantedBy=multi-user.target
EOF
echo "Prometheus service is created"
#Adding prometheus port to firewall
cat /etc/novell-release | grep "VERSION = 2023"
if [ $? -eq 0 ]; then
DEFAULT_ZONE=`firewall-cmd --get-default-zone`
for port in 9090; 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="9090 /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 prometheus
sleep 5
#Checking the status of prometheus service
systemctl is-active --quiet prometheus.service
if [ $? -eq 0 ]; then
echo Pormetheus service is running
else
echo Pormetheus service is not running
fi
exit