Installation and Configuration Script

Installation Script for Grafana, Prometheus, SNMP-Exporter and Nginx

# Define variables
SNMP_EXPORTER_FILE="snmp_exporter-0.26.0.linux-amd64.tar.gz"
SNMP_EXPORTER_FOLDER="snmp_exporter-0.26.0.linux-amd64"
PROMETHEUS_FILE="prometheus-3.0.0.linux-amd64.tar.gz"
PROMETHEUS_FOLDER="prometheus-3.0.0.linux-amd64"

echo "========== Installing Grafana =========="
dnf install -y grafana
if [ $? -eq 0 ]; then
systemctl enable grafana-server.service
echo "Grafana installation was successful"
else
echo "Grafana failed to install"
exit 1
fi

echo "========== Starting Grafana =========="
systemctl start grafana-server.service
if [ $? -eq 0 ]; then
systemctl is-active grafana-server.service
else
echo "Grafana failed to start"
exit 1
fi



echo "========== Installing Prometheus =========="
#useradd --no-create-home --shell /bin/false prometheus
useradd --system prometheus
mkdir /etc/prometheus /var/lib/prometheus
pwd
chown prometheus:prometheus /etc/prometheus
chown prometheus:prometheus /var/lib/prometheus

echo "Check if prometheus installer exist and install"
if [ -f "$PROMETHEUS_FILE" ]; then 
    tar -xvzf $PROMETHEUS_FILE
    cp $PROMETHEUS_FOLDER/prometheus /usr/local/bin/
    cp $PROMETHEUS_FOLDER/promtool /usr/local/bin/
    chown prometheus:prometheus /usr/local/bin/prometheus
    chown prometheus:prometheus /usr/local/bin/promtool
    cp -r $PROMETHEUS_FOLDER/consoles /etc/prometheus
    cp -r $PROMETHEUS_FOLDER/console_libraries /etc/prometheus
    rm -rf $PROMETHEUS_FOLDER
    chown -R prometheus:prometheus /etc/prometheus/consoles
    chown -R prometheus:prometheus /etc/prometheus/console_libraries
else
    echo "$PROMETHEUS_FILE doesn't exist"
    exit 1
fi

echo "========== Adding Prometheus Configuration =========="
cat <<EOT > /etc/prometheus/prometheus.yml
global:
  scrape_interval: 10s

scrape_configs:
  - job_name: 'prometheus_master'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9090']
EOT
chown prometheus:prometheus /etc/prometheus/prometheus.yml

echo "========== Configure Prometheus Service =========="
cat <<EOT > /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
--config.file /etc/prometheus/prometheus.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
EOT

systemctl daemon-reload
systemctl enable --now prometheus.service

echo "========== Starting Prometheus Service =========="
systemctl start prometheus.service
if [ $? -eq 0 ]; then
systemctl is-active prometheus
else
echo "Prometheus failed to start"
exit 1
fi



echo "Installing SNMP-Exporter"
if [ -f "$SNMP_EXPORTER_FILE" ]; then 
tar xzf $SNMP_EXPORTER_FILE
cd $SNMP_EXPORTER_FOLDER
cp ./snmp_exporter /usr/local/bin/snmp_exporter
cp ./snmp.yml /usr/local/bin/snmp.yml
chown prometheus:prometheus /usr/local/bin/snmp_exporter
chown prometheus:prometheus /usr/local/bin/snmp.yml
rm -rf $SNMP_EXPORTER_FOLDER
else
echo "$SNMP_EXPORTER_FILE doesn't exist"
exit 1
fi

#sudo useradd --system prometheus
echo "========== Configuring SNMP-Exporter Service =========="
cat <<EOT > /etc/systemd/system/snmp-exporter.service
[Unit]
Description=Prometheus SNMP Exporter Service
After=network.target

[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/bin/snmp_exporter --config.file="/usr/local/bin/snmp.yml"

[Install]
WantedBy=multi-user.target
EOT
systemctl daemon-reload
systemctl enable snmp-exporter.service

echo "========== Starting SNMP-Exporter Service =========="
systemctl start snmp-exporter.service
if [ $? -eq 0 ]; then 
systemctl is-active snmp-exporter.service
else
echo "SNMP-Exporter failed to start"
exit 1
fi

echo "========== Configure Prometheus to scrape using snmp-exporter =========="
cat <<EOT >> /etc/prometheus/prometheus.yml
 - job_name: snmp
    metrics_path: /snmp
    params:
      module: [if_mib]
    static_configs:
      - targets:
        - 127.0.0.1
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 127.0.0.1:9116 # URL as shown on the UI
EOT

echo "========== Restart Prometheus Service =========="
systemctl restart prometheus.service
if [ $? -eq 0 ]; then
systemctl is-active prometheus
else
echo "Prometheus failed to start"
exit 1
fi

echo "========== Installing Nginx =========="
dnf install -y nginx
if [ $? -eq 0 ]; then
systemctl enable nginx.service
echo "Nginx installation was successful"
else
echo "Nginx failed to install"
exit 1
fi

echo "========== Starting Nginx =========="
systemctl start nginx.service
if [ $? -eq 0 ]; then
systemctl is-active nginx.service
else
echo "nginx failed to start"
exit 1
fi

Configuration Script for SNMP-Exporter, Prometheus and Nginx

Installation Script for Blackbox-Exporter

Last updated