-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_monitoring.sh
executable file
·167 lines (135 loc) · 4.59 KB
/
setup_monitoring.sh
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/bash
set -e
# Configuration
PROMETHEUS_VERSION="2.45.0"
GRAFANA_VERSION="10.0.3"
PROJECT_ROOT="$(pwd)"
MONITORING_DIR="${PROJECT_ROOT}/monitoring"
PROMETHEUS_DIR="${MONITORING_DIR}/prometheus"
GRAFANA_DIR="${MONITORING_DIR}/grafana"
# Function to check if a port is in use
check_port() {
local port=$1
if netstat -tln | grep -q ":${port} "; then
return 0
else
return 1
fi
}
# Function to cleanup existing processes
cleanup_processes() {
echo "Cleaning up existing processes..."
# Kill any existing Prometheus process
if pgrep prometheus > /dev/null; then
echo "Stopping existing Prometheus process..."
pkill prometheus || true
fi
# Kill any existing Grafana process
if pgrep grafana-server > /dev/null; then
echo "Stopping existing Grafana process..."
pkill grafana-server || true
fi
# Wait for processes to stop
sleep 2
# Clean up data directories
echo "Cleaning up data directories..."
rm -rf "${MONITORING_DIR}/data/prometheus/*"
rm -rf "${MONITORING_DIR}/data/grafana/*"
}
# Function to create necessary directories
create_directories() {
echo "Creating directories..."
mkdir -p "${PROMETHEUS_DIR}" "${GRAFANA_DIR}" \
"${MONITORING_DIR}/data/prometheus" "${MONITORING_DIR}/data/grafana" \
"${GRAFANA_DIR}/conf/provisioning/datasources" \
"${GRAFANA_DIR}/conf/provisioning/dashboards" \
"${GRAFANA_DIR}/dashboards"
}
# Function to setup Prometheus
setup_prometheus() {
echo "Setting up Prometheus..."
if [ ! -f "${PROMETHEUS_DIR}/prometheus" ]; then
wget "https://github.com/prometheus/prometheus/releases/download/v${PROMETHEUS_VERSION}/prometheus-${PROMETHEUS_VERSION}.linux-amd64.tar.gz"
tar xzf "prometheus-${PROMETHEUS_VERSION}.linux-amd64.tar.gz"
mv "prometheus-${PROMETHEUS_VERSION}.linux-amd64"/* "${PROMETHEUS_DIR}/"
rm -rf "prometheus-${PROMETHEUS_VERSION}.linux-amd64" "prometheus-${PROMETHEUS_VERSION}.linux-amd64.tar.gz"
fi
# Copy Prometheus config
cp "${PROJECT_ROOT}/configs/prometheus/prometheus.yml" "${PROMETHEUS_DIR}/prometheus.yml"
}
# Function to setup Grafana
setup_grafana() {
echo "Setting up Grafana..."
# Download and extract Grafana
wget "https://dl.grafana.com/oss/release/grafana-${GRAFANA_VERSION}.linux-amd64.tar.gz"
tar -zxf "grafana-${GRAFANA_VERSION}.linux-amd64.tar.gz"
# Create Grafana directories
mkdir -p "${GRAFANA_DIR}/conf/provisioning/datasources"
mkdir -p "${GRAFANA_DIR}/conf/provisioning/dashboards"
mkdir -p "${GRAFANA_DIR}/dashboards"
mkdir -p "${GRAFANA_DIR}/data"
# Copy Grafana files
cp -r "grafana-${GRAFANA_VERSION}"/* "${GRAFANA_DIR}/"
# Copy existing configurations
cp -r "${PROJECT_ROOT}/configs/grafana/provisioning/"* "${GRAFANA_DIR}/conf/provisioning/"
cp -r "${PROJECT_ROOT}/configs/grafana/dashboards/"* "${GRAFANA_DIR}/dashboards/"
# Create custom Grafana config
cat > "${GRAFANA_DIR}/conf/custom.ini" << EOL
[paths]
data = ${GRAFANA_DIR}/data
logs = ${GRAFANA_DIR}/data/log
plugins = ${GRAFANA_DIR}/data/plugins
provisioning = ${GRAFANA_DIR}/conf/provisioning
[server]
protocol = http
http_port = 3000
[security]
admin_user = admin
admin_password = admin
[auth.anonymous]
enabled = true
org_role = Viewer
EOL
# Cleanup
rm -f "grafana-${GRAFANA_VERSION}.linux-amd64.tar.gz"
rm -rf "grafana-${GRAFANA_VERSION}"
}
# Function to create start script
create_start_script() {
echo "Creating start script..."
cat > "${MONITORING_DIR}/start_monitoring.sh" << 'EOL'
#!/bin/bash
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "${SCRIPT_DIR}"
# Start Prometheus
./prometheus/prometheus \
--config.file=prometheus/prometheus.yml \
--storage.tsdb.path=data/prometheus \
--web.console.libraries=prometheus/console_libraries \
--web.console.templates=prometheus/consoles \
--web.listen-address=:9090 &
# Start Grafana
./grafana/bin/grafana server \
--homepath="${SCRIPT_DIR}/grafana" \
--config="${SCRIPT_DIR}/grafana/conf/custom.ini" \
&
echo "Monitoring stack started!"
echo "Prometheus: http://localhost:9090"
echo "Grafana: http://localhost:3000 (login: admin/admin)"
echo "Press Ctrl+C to stop..."
wait
EOL
}
# Main setup process
echo "Starting monitoring setup..."
# Run cleanup
cleanup_processes
# Create directories
create_directories
# Setup components
setup_prometheus
setup_grafana
# Create start script
create_start_script
echo "Setup complete! To start monitoring, run:"
echo "${MONITORING_DIR}/start_monitoring.sh"