Skip to content

Commit e74e84e

Browse files
prometheus config function updated
1 parent 7383c8c commit e74e84e

3 files changed

Lines changed: 58 additions & 37 deletions

File tree

src/routes/helper/prometheus_helper.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ def update_prometheus_config():
4343

4444
# Get the machine's IP address
4545
try:
46-
ipv4_address = subprocess.run(['hostname', '-I'], capture_output=True, text=True, check=True).stdout.split()[0]
46+
ipv4_address = subprocess.run(
47+
['hostname', '-I'], capture_output=True, text=True, check=True
48+
).stdout.split()[0]
4749
except subprocess.CalledProcessError as e:
4850
print(f"Error getting IP address: {e}")
4951
return False
@@ -54,29 +56,28 @@ def update_prometheus_config():
5456
except Exception as e:
5557
print(f"Error loading YAML config: {e}")
5658
return False
57-
58-
# Fetch the 'localhost' job
59-
localhost_job = next((job for job in config.get('scrape_configs', []) if job.get('job_name') == 'localhost'), None)
6059

61-
if localhost_job:
62-
# Update the IP address for the 'localhost' job target
63-
localhost_job['static_configs'][0]['targets'][0] = f'{ipv4_address}:5050'
64-
65-
# Create a new OrderedDict to maintain the correct order
66-
updated_job = OrderedDict()
67-
updated_job['job_name'] = localhost_job['job_name']
68-
updated_job['scrape_interval'] = localhost_job.get('scrape_interval', '10s')
69-
updated_job['static_configs'] = localhost_job['static_configs']
70-
71-
# Add basic_auth last to maintain order
72-
if 'basic_auth' in localhost_job:
73-
updated_job['basic_auth'] = localhost_job['basic_auth']
60+
for job in config.get('scrape_configs', []):
61+
if job['job_name'] == 'localhost':
62+
# Update the target for 'localhost' job
63+
job['static_configs'][0]['targets'][0] = f'{ipv4_address}:5050'
64+
65+
# Create a new OrderedDict to ensure the correct order
66+
updated_job = OrderedDict()
67+
updated_job['job_name'] = job['job_name']
68+
updated_job['scrape_interval'] = job.get('scrape_interval', '10s')
69+
updated_job['static_configs'] = job['static_configs']
70+
71+
# Add basic_auth at the end if it exists
72+
if 'basic_auth' in job:
73+
updated_job['basic_auth'] = job['basic_auth']
7474

7575
# Replace the old job with the updated one
76-
for index, job in enumerate(config['scrape_configs']):
77-
if job['job_name'] == 'localhost':
76+
for index, j in enumerate(config['scrape_configs']):
77+
if j['job_name'] == 'localhost':
7878
config['scrape_configs'][index] = updated_job
79-
break
79+
else:
80+
config['scrape_configs'][index] = OrderedDict(j)
8081

8182
# Save the updated config
8283
try:

src/routes/prometheus.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,21 +124,24 @@ def add_target():
124124

125125
break
126126

127-
# if not job_found:
128-
# # Create new job entry
129-
# new_job = OrderedDict()
130-
# new_job['job_name'] = job_name
131-
# new_job['static_configs'] = [{'targets': [new_target]}]
132-
# new_job['scrape_interval'] = scrape_interval
127+
if not job_found:
128+
# Create new job entry
129+
new_job = OrderedDict()
130+
new_job['job_name'] = job_name
131+
new_job['static_configs'] = [{'targets': [new_target]}]
132+
new_job['scrape_interval'] = scrape_interval
133133

134-
# # Add basic_auth if provided
135-
# if username and password:
136-
# new_job['basic_auth'] = {
137-
# 'username': username,
138-
# 'password': password
139-
# }
140-
# # Append the new job to scrape_configs
141-
# config['scrape_configs'].append(new_job)
134+
# Add basic_auth if provided
135+
if username and password:
136+
new_job['basic_auth'] = {
137+
'username': username,
138+
'password': password
139+
}
140+
# Append the new job to scrape_configs
141+
config['scrape_configs'].append(new_job)
142+
143+
for index, j in enumerate(config['scrape_configs']):
144+
config['scrape_configs'][index] = OrderedDict(j)
142145

143146
# Save the updated config
144147
save_yaml(config, prometheus_yml_path)
@@ -167,6 +170,10 @@ def remove_target():
167170
else:
168171
flash(f'Target {target_to_remove} not found in job {job_name}.', 'warning')
169172
break
173+
174+
for index, j in enumerate(config['scrape_configs']):
175+
config['scrape_configs'][index] = OrderedDict(j)
176+
170177
else:
171178
flash(f'Job {job_name} not found.', 'warning')
172179

src/templates/other/targets.html

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ <h2>Restart Prometheus Docker Service</h2>
6868
<div class="section">
6969
<h2>Add New Target</h2>
7070
<form action="{{ url_for('add_target') }}" method="post" class="add-target-form">
71-
<input type="text" name="job_name" value="localhost" class="input-field" required>
71+
<input type="text" name="job_name" placeholder="localhost" class="input-field" required>
7272
<input type="text" name="new_target" placeholder="New Target" class="input-field" required>
7373
<input type="text" name="scrape_interval" placeholder="Scrape Interval" class="input-field" required>
7474
<input type="text" name="username" placeholder="Username" class="input-field">
@@ -79,10 +79,23 @@ <h2>Add New Target</h2>
7979
</button>
8080
</form>
8181
</div>
82-
8382
</div>
84-
{% endblock %}
83+
<script>
84+
// if user enter localhost in job_name, then hide the username and password fields
85+
document.querySelector('.add-target-form input[name="job_name"]').addEventListener('input', function () {
86+
const usernameField = document.querySelector('.add-target-form input[name="username"]');
87+
const passwordField = document.querySelector('.add-target-form input[name="password"]');
88+
if (this.value === 'localhost') {
89+
usernameField.style.display = 'none';
90+
passwordField.style.display = 'none';
91+
} else {
92+
usernameField.style.display = 'block';
93+
passwordField.style.display = 'block';
94+
}
95+
});
8596

97+
</script>
98+
{% endblock %}
8699
{% block extra_scripts %}
87100
<script src="{{ url_for('static', filename='js/targets.js') }}"></script>
88101
{% endblock %}

0 commit comments

Comments
 (0)