#!/bin/bash
sudo id
sudo mkdir /opt/nginx-exporter 
sudo useradd --system --no-create-home --shell /bin/false nginx-exporter
#sudo curl -L https://github.com/nginxinc/nginx-prometheus-exporter/releases/download/v1.4.0/nginx-prometheus-exporter_1.4.0_linux_amd64.tar.gz -o /opt/nginx-exporter/nginx-prometheus-exporter_1.4.0_linux_amd64.tar.gz
sudo curl -L https://din235.ru/scripts/nginx-prometheus-exporter_1.4.1_linux_amd64.tar.gz -o /opt/nginx-exporter/nginx-prometheus-exporter_1.4.1_linux_amd64.tar.gz
cat > nginx-exporter.service  <<EOF
[Unit]
Description=Nginx Exporter
Wants=network-online.target
After=network-online.target
 
StartLimitIntervalSec=0
 
[Service]
User=nginx-exporter
Group=nginx-exporter
Type=simple
Restart=on-failure
RestartSec=5s
 
ExecStart=/opt/nginx-exporter/nginx-prometheus-exporter --nginx.scrape-uri=http://127.0.0.1/nginx_status 
 
[Install]
WantedBy=multi-user.target
EOF
sudo mv nginx-exporter.service /etc/systemd/system/nginx-exporter.service
cd /opt/nginx-exporter
sudo tar -zxf nginx-prometheus-exporter_1.4.1_linux_amd64.tar.gz
sudo rm nginx-prometheus-exporter_1.4.1_linux_amd64.tar.gz
sudo chown -R nginx-exporter:nginx-exporter /opt/nginx-exporter
sudo systemctl enable --now nginx-exporter
curl localhost:9113/metrics
echo "sudo systemctl restart nginx-exporter && sleep 2 && sudo systemctl status nginx-exporter"
sudo systemctl restart nginx-exporter && sleep 2 && sudo systemctl status nginx-exporter

