概要
Prometheus Server を systemd 管理下に自動構築する Bash スクリプトの例です。同じスクリプトでインストールと削除の両方を行えます。
スクリプト
#!/bin/bash
set -euo pipefail
PROM_VER=2.53.3
PROM_ARCH=amd64
PROM_TGZ="prometheus-${PROM_VER}.linux-${PROM_ARCH}.tar.gz"
PROM_URL="https://github.com/prometheus/prometheus/releases/download/v${PROM_VER}/${PROM_TGZ}"
DL_DIR="${PWD}/downloads"
APP_DIR="/opt/prometheus"
BASE_DIR="${APP_DIR}/prometheus-${PROM_VER}.linux-${PROM_ARCH}"
DATA_DIR="/var/lib/prometheus"
LOG_DIR="/var/log/prometheus"
LISTEN_IP="0.0.0.0"
WEB_PORT=9090
UNIT_NAME="prometheus.service"
HOST_NM="$(hostname -s)"
function usage() {
cat <<EOF
Usage: $0 {deploy|remove}
deploy - Prometheus をダウンロード・展開・起動する
remove - Prometheus を停止・削除する
EOF
}
function setup_env() {
[[ -d "$DL_DIR" ]] || mkdir -p "$DL_DIR"
[[ -d "$APP_DIR" ]] || mkdir -p "$APP_DIR"
[[ -d "$DATA_DIR" ]] || mkdir -p "$DATA_DIR"
[[ -d "$LOG_DIR" ]] || mkdir -p "$LOG_DIR"
if command -v wget &>/dev/null; then
GET="wget -qO"
elif command -v curl &>/dev/null; then
GET="curl -fsSL -o"
else
echo "Error: wget または curl をインストールしてください" >&2
exit 1
fi
}
function fetch_package() {
if [[ ! -s "${DL_DIR}/${PROM_TGZ}" ]]; then
$GET "${DL_DIR}/${PROM_TGZ}" "$PROM_URL"
fi
}
function extract_package() {
tar -xzf "${DL_DIR}/${PROM_TGZ}" -C "$APP_DIR"
}
function create_service() {
cat > "/etc/systemd/system/${UNIT_NAME}" <<EOF
[Unit]
Description=Prometheus Monitoring Server
Documentation=https://prometheus.io/docs/introduction/overview/
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
Restart=on-failure
User=root
Group=root
ExecStart=/bin/bash -c "${BASE_DIR}/prometheus \
--config.file=${BASE_DIR}/prometheus.yml \
--storage.tsdb.path=${DATA_DIR} \
--storage.tsdb.retention.time=60d \
--storage.tsdb.retention.size=512MB \
--web.listen-address=${LISTEN_IP}:${WEB_PORT} \
--web.enable-lifecycle \
--web.max-connections=65535 \
--web.read-timeout=5m \
--query.timeout=10s \
--query.max-concurrency=20 \
--log.level=info \
--log.format=json >> ${LOG_DIR}/prometheus.log 2>&1"
ExecReload=/bin/kill -HUP \\$MAINPID
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now "$UNIT_NAME"
}
function deploy() {
setup_env
fetch_package
extract_package
create_service
systemctl status "$UNIT_NAME" --no-pager
if ss -ntl | grep -q ":${WEB_PORT}[^0-9]"; then
echo "[${HOST_NM}] Prometheus が http://${LISTEN_IP}:${WEB_PORT} で起動しました"
fi
}
function remove() {
systemctl disable --now "$UNIT_NAME" 2>/dev/null || true
rm -f "/etc/systemd/system/${UNIT_NAME}"
rm -rf "$BASE_DIR" "$DATA_DIR" "$LOG_DIR"
systemctl daemon-reload
echo "[${HOST_NM}] Prometheus を停止・削除しました"
}
case "${1:-}" in
deploy|install) deploy ;;
remove|uninstall) remove ;;
*) usage; exit 1 ;;
esac
実行手順
root 権限で実行します。
chmod +x install-prometheus.sh
./install-prometheus.sh deploy
動作確認
サービス状態とポートを確認します。
systemctl status prometheus.service
ss -ntl | grep 9090
Web UI へは以下の URL からアクセスします。
http://<サーバIP>:9090/
アンインストール
./install-prometheus.sh remove
本スクリプトは実行ファイル、データディレクトリ、ログディレクトリ、systemd ユニットファイルを削除します。