GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — SystemGuard/src/scripts/start_prometheus.sh at dev · codeperfectplus/SystemGuard

Expand file tree

/

start_prometheus.sh

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
123 lines (107 loc) · 4.14 KB
/

start_prometheus.sh

Copy path

File metadata and controls

123 lines (107 loc) · 4.14 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/bash
# Function to determine the username, handles root user case
get_user_name() {
if [ "$(whoami)" = "root" ]; then
local logname_user
logname_user=$(logname 2>/dev/null)
if [ $? -ne 0 ]; then
# Fallback: get the last user from /etc/passwd with a home directory
USER_NAME=$(grep '/home' /etc/passwd | cut -d: -f1 | tail -n 1)
else
USER_NAME=$logname_user
fi
else
USER_NAME=$(whoami)
fi
echo "$USER_NAME"
}
USER_NAME=$(get_user_name)
# Configuration variables
NETWORK_NAME="flask-prometheus-net"
PROMETHEUS_CONTAINER="prometheus"
PROMETHEUS_IMAGE="prom/prometheus:v2.55.0-rc.0"
PROMETHEUS_PORT="9090"
# Paths and directories
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
# 2 levels up from the script directory to configuration directory
PROMETHEUS_CONFIG_DIR="$(dirname "$(dirname "$SCRIPT_DIR")")/prometheus_config"
echo "$PROMETHEUS_CONFIG_DIR"
ALERT_RULES_FILE="$PROMETHEUS_CONFIG_DIR/alert_rules.yml"
PROMETHEUS_CONFIG_FILE="$PROMETHEUS_CONFIG_DIR/prometheus.yml"
PROMETHEUS_DATA_DIR="/home/$USER_NAME/.database/prometheus"
# App configuration
FLASK_APP_IP=$(hostname -I | cut -d' ' -f1)
FLASK_APP_PORT="5050"
SCRAPE_INTERVAL="10s"
SYSTEM_LABEL="systemguard-metrics"
ENVIRONMENT="production"
JOB_NAME="localhost"
PROMETHEUS_USERNAME="prometheus_admin"
PROMETHEUS_PASSWORD="prometheus_password"
# Logging function for information
log_info() {
echo "[INFO] $1"
}
# Error function for failed steps
log_error() {
echo "[ERROR] $1"
exit 1
}
# Ensure configuration and data directories exist
log_info "Creating necessary directories if they don't exist."
mkdir -p "$PROMETHEUS_CONFIG_DIR" || log_error "Failed to create directory: $PROMETHEUS_CONFIG_DIR"
mkdir -p "$PROMETHEUS_DATA_DIR" || log_error "Failed to create directory: $PROMETHEUS_DATA_DIR"
# Set correct permissions for the Prometheus data directory
log_info "Setting permissions for Prometheus data directory."
chmod 777 "$PROMETHEUS_DATA_DIR" || log_error "Failed to set permissions on $PROMETHEUS_DATA_DIR"
# Create Prometheus configuration file
log_info "Generating prometheus.yml configuration file."
cat > "$PROMETHEUS_CONFIG_FILE" <<EOL
global:
external_labels:
system: $SYSTEM_LABEL
rule_files:
- /etc/prometheus/alert_rules.yml
scrape_configs:
- job_name: $JOB_NAME
scrape_interval: $SCRAPE_INTERVAL
static_configs:
- targets:
- $FLASK_APP_IP:$FLASK_APP_PORT
basic_auth:
username: $PROMETHEUS_USERNAME
password: $PROMETHEUS_PASSWORD
EOL
# Check if Docker network exists, create it if not
if ! docker network ls | grep -q "$NETWORK_NAME"; then
log_info "Creating Docker network: $NETWORK_NAME"
docker network create "$NETWORK_NAME" || log_error "Failed to create Docker network."
else
log_info "Docker network $NETWORK_NAME already exists."
fi
# Check if Prometheus container is running, stop and remove it if necessary
if docker ps -a --format '{{.Names}}' | grep -q "$PROMETHEUS_CONTAINER"; then
log_info "Stopping and removing existing Prometheus container: $PROMETHEUS_CONTAINER."
docker stop "$PROMETHEUS_CONTAINER" &> /dev/null || log_error "Failed to stop Prometheus container."
docker rm "$PROMETHEUS_CONTAINER" &> /dev/null || log_error "Failed to remove Prometheus container."
else
log_info "No existing Prometheus container found. Proceeding to start a new one."
fi
# Start Prometheus container
log_info "Starting Prometheus container: $PROMETHEUS_CONTAINER"
run_output=$(docker run -d \
--name "$PROMETHEUS_CONTAINER" \
--network "$NETWORK_NAME" \
-p "$PROMETHEUS_PORT:$PROMETHEUS_PORT" \
--restart always \
-v "$PROMETHEUS_CONFIG_FILE:/etc/prometheus/prometheus.yml" \
-v "$ALERT_RULES_FILE:/etc/prometheus/alert_rules.yml" \
-v "$PROMETHEUS_DATA_DIR:/prometheus" \
"$PROMETHEUS_IMAGE" 2>&1)
# Verify if Prometheus container started successfully
if [ $? -eq 0 ]; then
log_info "Prometheus container $PROMETHEUS_CONTAINER started successfully on port $PROMETHEUS_PORT."
log_info "Prometheus config file located at $PROMETHEUS_CONFIG_FILE"
else
log_error "Failed to start Prometheus container: $run_output"
fi
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.