added script
This commit is contained in:
		
							parent
							
								
									c89aac328b
								
							
						
					
					
						commit
						276e157dcf
					
				
							
								
								
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @ -158,5 +158,6 @@ cython_debug/ | ||||
| #  be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore | ||||
| #  and can be added to the global gitignore or merged into this file.  For a more nuclear | ||||
| #  option (not recommended) you can uncomment the following to ignore the entire idea folder. | ||||
| #.idea/ | ||||
| .idea/ | ||||
| 
 | ||||
| checks.yaml | ||||
|  | ||||
							
								
								
									
										40
									
								
								docker-healthcheck.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								docker-healthcheck.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,40 @@ | ||||
| # /usr/bin/env python3 | ||||
| 
 | ||||
| import requests | ||||
| import yaml | ||||
| import docker | ||||
| import docker.errors | ||||
| 
 | ||||
| 
 | ||||
| def read_checks() -> dict: | ||||
|     with open("./checks.yaml") as f: | ||||
|         checks = yaml.safe_load(f) | ||||
|     return checks | ||||
| 
 | ||||
| 
 | ||||
| def main() -> None: | ||||
|     checks = read_checks() | ||||
|     if "self" in checks: | ||||
|         requests.get(checks["self"]) | ||||
| 
 | ||||
|     client = docker.from_env() | ||||
|     for container_id in checks: | ||||
|         if container_id == "self": | ||||
|             continue | ||||
|         try: | ||||
|             container = client.containers.get(container_id) | ||||
|             container_status = container.attrs["State"]["Status"] | ||||
|             if container_status in ["healthy", "running"]: | ||||
|                 requests.post(checks[container_id]) | ||||
|             else: | ||||
|                 requests.post( | ||||
|                     checks[container_id] + "/fail", | ||||
|                     f"Container has status '{container_status}' which is not one of 'healthy' or 'running'" | ||||
|                 ) | ||||
|         except docker.errors.NotFound: | ||||
|             if "self" in checks: | ||||
|                 requests.post(checks["self"] + "/fail", f"Container '{container_id}' not found.") | ||||
| 
 | ||||
| 
 | ||||
| if __name__ == "__main__": | ||||
|     main() | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user