Last active 1750263007

Revision 9b552b16f715dc390617e3500bc0baf73b00fd17

install-promtail.sh Raw
1#!/bin/bash
2
3# Promtail Installation and Configuration Script for Debian
4# Usage: bash -c "$(curl -fsSL <your-gist-url>/install-promtail.sh)"
5
6set -e
7
8# Configuration
9LOKI_ENDPOINT="loki.pfotenballen.de"
10LOKI_PORT="3100"
11PROMTAIL_VERSION="2.9.2"
12PROMTAIL_USER="promtail"
13PROMTAIL_DIR="/opt/promtail"
14CONFIG_DIR="/etc/promtail"
15LOG_DIR="/var/log/promtail"
16
17# Colors for output
18RED='\033[0;31m'
19GREEN='\033[0;32m'
20YELLOW='\033[1;33m'
21BLUE='\033[0;34m'
22NC='\033[0m' # No Color
23
24# Logging functions
25log_info() {
26 echo -e "${BLUE}[INFO]${NC} $1"
27}
28
29log_success() {
30 echo -e "${GREEN}[SUCCESS]${NC} $1"
31}
32
33log_warning() {
34 echo -e "${YELLOW}[WARNING]${NC} $1"
35}
36
37log_error() {
38 echo -e "${RED}[ERROR]${NC} $1"
39}
40
41# Check if running as root
42check_root() {
43 if [[ $EUID -ne 0 ]]; then
44 log_error "This script must be run as root"
45 exit 1
46 fi
47}
48
49# Check if promtail is already installed
50check_promtail_installed() {
51 if command -v promtail &> /dev/null || [[ -f "/usr/local/bin/promtail" ]] || [[ -f "$PROMTAIL_DIR/promtail" ]]; then
52 return 0
53 else
54 return 1
55 fi
56}
57
58# Test Loki endpoint connectivity
59test_loki_connectivity() {
60 log_info "Testing connectivity to Loki endpoint: $LOKI_ENDPOINT:$LOKI_PORT"
61
62 if timeout 10 bash -c "</dev/tcp/$LOKI_ENDPOINT/$LOKI_PORT" 2>/dev/null; then
63 log_success "Successfully connected to $LOKI_ENDPOINT:$LOKI_PORT"
64 return 0
65 else
66 log_error "Cannot reach $LOKI_ENDPOINT:$LOKI_PORT"
67 log_error "Please check your network connection and Loki server status"
68 return 1
69 fi
70}
71
72# Install dependencies
73install_dependencies() {
74 log_info "Installing dependencies..."
75 apt-get update -qq
76 apt-get install -y wget curl unzip systemd
77 log_success "Dependencies installed"
78}
79
80# Create promtail user
81create_promtail_user() {
82 if ! id "$PROMTAIL_USER" &>/dev/null; then
83 log_info "Creating promtail user..."
84 useradd --system --no-create-home --shell /bin/false $PROMTAIL_USER
85 log_success "Promtail user created"
86 else
87 log_info "Promtail user already exists"
88 fi
89}
90
91# Download and install promtail
92install_promtail() {
93 log_info "Downloading Promtail v$PROMTAIL_VERSION..."
94
95 # Determine architecture
96 ARCH=$(uname -m)
97 case $ARCH in
98 x86_64)
99 ARCH_SUFFIX="amd64"
100 ;;
101 aarch64)
102 ARCH_SUFFIX="arm64"
103 ;;
104 armv7l)
105 ARCH_SUFFIX="armv7"
106 ;;
107 *)
108 log_error "Unsupported architecture: $ARCH"
109 exit 1
110 ;;
111 esac
112
113 # Create directories
114 mkdir -p $PROMTAIL_DIR
115 mkdir -p $CONFIG_DIR
116 mkdir -p $LOG_DIR
117
118 # Download promtail binary
119 DOWNLOAD_URL="https://github.com/grafana/loki/releases/download/v$PROMTAIL_VERSION/promtail-linux-$ARCH_SUFFIX.zip"
120
121 cd /tmp
122 wget -q "$DOWNLOAD_URL" -O promtail.zip
123 unzip -q promtail.zip
124
125 # Install binary
126 chmod +x promtail-linux-$ARCH_SUFFIX
127 mv promtail-linux-$ARCH_SUFFIX /usr/local/bin/promtail
128
129 # Set ownership
130 chown root:root /usr/local/bin/promtail
131 chown -R $PROMTAIL_USER:$PROMTAIL_USER $CONFIG_DIR $LOG_DIR
132
133 # Cleanup
134 rm -f promtail.zip
135
136 log_success "Promtail installed successfully"
137}
138
139# Create promtail configuration
140create_config() {
141 log_info "Creating Promtail configuration..."
142
143 cat > $CONFIG_DIR/promtail.yml << EOF
144server:
145 http_listen_port: 9080
146 grpc_listen_port: 0
147
148positions:
149 filename: /var/lib/promtail/positions.yaml
150
151clients:
152 - url: http://$LOKI_ENDPOINT:$LOKI_PORT/loki/api/v1/push
153
154scrape_configs:
155 # System logs
156 - job_name: system-logs
157 static_configs:
158 - targets:
159 - localhost
160 labels:
161 job: system-logs
162 host: \$(hostname)
163 __path__: /var/log/*.log
164
165 # Syslog
166 - job_name: syslog
167 static_configs:
168 - targets:
169 - localhost
170 labels:
171 job: syslog
172 host: \$(hostname)
173 __path__: /var/log/syslog
174
175 # Auth logs
176 - job_name: auth-logs
177 static_configs:
178 - targets:
179 - localhost
180 labels:
181 job: auth-logs
182 host: \$(hostname)
183 __path__: /var/log/auth.log
184
185 # Kernel logs
186 - job_name: kernel-logs
187 static_configs:
188 - targets:
189 - localhost
190 labels:
191 job: kernel-logs
192 host: \$(hostname)
193 __path__: /var/log/kern.log
194
195 # Apache logs (if exists)
196 - job_name: apache-access
197 static_configs:
198 - targets:
199 - localhost
200 labels:
201 job: apache-access
202 host: \$(hostname)
203 __path__: /var/log/apache2/access.log
204 pipeline_stages:
205 - match:
206 selector: '{job="apache-access"}'
207 stages:
208 - regex:
209 expression: '^(?P<remote_addr>\S+) \S+ \S+ \[(?P<time_local>[^\]]+)\] "(?P<method>\S+) (?P<request>\S+) \S+" (?P<status>\d+) (?P<body_bytes_sent>\d+)'
210
211 # Apache error logs (if exists)
212 - job_name: apache-error
213 static_configs:
214 - targets:
215 - localhost
216 labels:
217 job: apache-error
218 host: \$(hostname)
219 __path__: /var/log/apache2/error.log
220
221 # Nginx logs (if exists)
222 - job_name: nginx-access
223 static_configs:
224 - targets:
225 - localhost
226 labels:
227 job: nginx-access
228 host: \$(hostname)
229 __path__: /var/log/nginx/access.log
230
231 - job_name: nginx-error
232 static_configs:
233 - targets:
234 - localhost
235 labels:
236 job: nginx-error
237 host: \$(hostname)
238 __path__: /var/log/nginx/error.log
239EOF
240
241 # Create positions directory
242 mkdir -p /var/lib/promtail
243 chown $PROMTAIL_USER:$PROMTAIL_USER /var/lib/promtail
244
245 # Set proper permissions
246 chown $PROMTAIL_USER:$PROMTAIL_USER $CONFIG_DIR/promtail.yml
247 chmod 640 $CONFIG_DIR/promtail.yml
248
249 log_success "Configuration created"
250}
251
252# Create systemd service
253create_systemd_service() {
254 log_info "Creating systemd service..."
255
256 cat > /etc/systemd/system/promtail.service << EOF
257[Unit]
258Description=Promtail service
259Documentation=https://grafana.com/docs/loki/latest/clients/promtail/
260After=network.target
261
262[Service]
263Type=simple
264User=$PROMTAIL_USER
265ExecStart=/usr/local/bin/promtail -config.file=$CONFIG_DIR/promtail.yml
266Restart=always
267RestartSec=10
268StandardOutput=journal
269StandardError=journal
270SyslogIdentifier=promtail
271
272[Install]
273WantedBy=multi-user.target
274EOF
275
276 systemctl daemon-reload
277 log_success "Systemd service created"
278}
279
280# Add promtail user to adm group for log access
281configure_log_access() {
282 log_info "Configuring log file access..."
283 usermod -a -G adm $PROMTAIL_USER
284 log_success "Log access configured"
285}
286
287# Start and enable service
288start_service() {
289 log_info "Starting Promtail service..."
290
291 systemctl enable promtail
292 systemctl start promtail
293
294 # Wait a moment and check status
295 sleep 2
296
297 if systemctl is-active --quiet promtail; then
298 log_success "Promtail service is running"
299 log_info "Service status:"
300 systemctl status promtail --no-pager -l
301 else
302 log_error "Failed to start Promtail service"
303 log_error "Check logs with: journalctl -u promtail -f"
304 exit 1
305 fi
306}
307
308# Main installation process
309main() {
310 echo "=================================="
311 echo " Promtail Installation Script "
312 echo "=================================="
313 echo
314
315 check_root
316
317 if check_promtail_installed; then
318 log_warning "Promtail appears to be already installed"
319 echo "Existing installation found. Do you want to continue and reconfigure? (y/N)"
320 read -r response
321 if [[ ! "$response" =~ ^[Yy]$ ]]; then
322 log_info "Installation cancelled"
323 exit 0
324 fi
325 fi
326
327 # Test Loki connectivity first
328 if ! test_loki_connectivity; then
329 echo "Do you want to continue anyway? (y/N)"
330 read -r response
331 if [[ ! "$response" =~ ^[Yy]$ ]]; then
332 log_info "Installation cancelled"
333 exit 1
334 fi
335 fi
336
337 install_dependencies
338 create_promtail_user
339
340 if ! check_promtail_installed; then
341 install_promtail
342 else
343 log_info "Promtail binary already exists, skipping download"
344 fi
345
346 create_config
347 create_systemd_service
348 configure_log_access
349 start_service
350
351 echo
352 echo "=================================="
353 log_success "Promtail installation completed!"
354 echo "=================================="
355 echo
356 echo "Configuration file: $CONFIG_DIR/promtail.yml"
357 echo "Service status: systemctl status promtail"
358 echo "Service logs: journalctl -u promtail -f"
359 echo "Loki endpoint: http://$LOKI_ENDPOINT:$LOKI_PORT"
360 echo
361 echo "To check if logs are being sent to Loki:"
362 echo "curl -G -s \"http://$LOKI_ENDPOINT:$LOKI_PORT/loki/api/v1/query\" --data-urlencode 'query={job=\"system-logs\"}'"
363}
364
365# Execute main function
366main "$@"