@@ -43,22 +43,39 @@ def show_targets():
4343
4444def update_prometheus_config ():
4545 """
46- update the first target whenever the function called
47- as the network change the ip address will also change
48- so 1st ip address in the list is the ip adddress of the machine
49- with the job localhost
46+ Update the first target whenever the function is called.
47+ If the network changes, the IP address will also change.
48+ This updates the first IP address in the list, assuming it's the machine with the 'localhost' job.
5049 """
5150 print ("Updating Prometheus config..." )
51+
52+ # Get the machine's IP address
5253 ipv4_address = subprocess .run (['hostname' , '-I' ], capture_output = True , text = True , check = True ).stdout .split ()[0 ]
54+
55+ # Define the path to Prometheus config file
5356 prometheus_config_path = os .path .join (ROOT_DIR , 'prometheus_config' , 'prometheus.yml' )
57+
58+ # Load the existing config
5459 config = load_yaml (prometheus_config_path )
55- # fetch the localhost job
60+ # Fetch the ' localhost' job
5661 localhost_job = next ((job for job in config ['scrape_configs' ] if job ['job_name' ] == 'localhost' ), None )
62+
5763 if localhost_job :
64+ # Update the IP address for the 'localhost' job target
5865 localhost_job ['static_configs' ][0 ]['targets' ][0 ] = f'{ ipv4_address } :5050'
66+
67+ # localhost_job['basic_auth'] = {
68+ # 'username': "username",
69+ # 'password': "password"
70+ # }
71+
72+ # Save the updated config
5973 save_yaml (config , prometheus_config_path )
74+
75+ print ("Prometheus config updated successfully." )
6076 return True
6177
78+ print ("No 'localhost' job found in Prometheus config." )
6279 return False
6380
6481def update_prometheus_container ():
0 commit comments