-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_yml.py
More file actions
75 lines (60 loc) · 1.68 KB
/
auto_yml.py
File metadata and controls
75 lines (60 loc) · 1.68 KB
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
import os
from dotenv import load_dotenv
# 載入 .env 檔案(如需預設 IP)
load_dotenv()
# Prometheus 基本設定(含預設 jobs)
base_config = """# my global config
global:
scrape_interval: 15s
evaluation_interval: 15s
# scrape_timeout is set to the global default (10s).
external_labels:
monitor: 'my-project'
rule_files:
- 'alert.rules'
alerting:
alertmanagers:
- scheme: http
static_configs:
- targets:
- "alertmanager:9093"
scrape_configs:
- job_name: 'prometheus'
scrape_interval: 15s
static_configs:
- targets: ['localhost:9090']
- job_name: 'cadvisor'
scrape_interval: 15s
static_configs:
- targets: ['cadvisor:8080']
- job_name: 'node-exporter'
scrape_interval: 15s
static_configs:
- targets: ['node-exporter:9100']
"""
# 加入 agent list
agent_jobs = ""
agent_file = "agent_name_ip.txt"
if os.path.exists(agent_file):
with open(agent_file, "r") as f:
for line in f:
if not line.strip():
continue
try:
name, ip = line.strip().split()
agent_jobs += f"""
- job_name: '{name}'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 15s
static_configs:
- targets: ['{ip}']"""
except ValueError:
print(f"⚠️ 格式錯誤:{line.strip()}")
else:
print("❌ 找不到 agent_name_ip.txt")
# 合併內容
final_yml = base_config + agent_jobs
# 寫入 prometheus.yml
with open("./prometheus/prometheus/prometheus.yml", "w") as f:
f.write(final_yml)
print("✅ 已成功生成 prometheus.yml(含格式與註解)")