Last active
March 5, 2024 06:21
-
-
Save krzysztofantczak/c11c9f8e289ea477aa2dcefcdd24b62c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# HELP deployment_status Status of deployments | |
# TYPE deployment_status gauge | |
deployment_status{deployment="lib",namespace="arc"} 1.0 | |
deployment_status{deployment="studio",namespace="arc"} 1.0 | |
deployment_status{deployment="harmony",namespace="arc"} 1.0 | |
deployment_status{deployment="prose",namespace="arc"} 1.0 | |
deployment_status{deployment="nginx-deployment",namespace="arc"} 1.0 | |
deployment_status{deployment="studio-dashboard",namespace="arc"} 1.0 | |
deployment_status{deployment="melody",namespace="arc"} 1.0 | |
# HELP available_cpu Available CPU in the namespace | |
# TYPE available_cpu gauge | |
available_cpu{namespace="arc"} +Inf | |
# HELP available_memory Available memory in the namespace | |
# TYPE available_memory gauge | |
available_memory{namespace="arc"} +Inf |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import time | |
from kubernetes import client, config | |
from prometheus_client import start_http_server, Gauge | |
NAMESPACE = os.getenv("NAMESPACE", "default") # Get namespace from environment variable or default to "default" | |
KUBE_CONFIG_PATH = os.getenv("KUBE_CONFIG_PATH", "~/.kube/config") | |
def get_kubernetes_client(): | |
try: | |
# Use kube config for authentication | |
config.load_kube_config(config_file=KUBE_CONFIG_PATH) | |
return client.ApiClient() | |
except Exception as e: | |
print("Failed to load kube config:", e) | |
return None | |
def main(): | |
k8s_client = get_kubernetes_client() | |
if k8s_client is None: | |
print("Exiting...") | |
return | |
v1 = client.AppsV1Api(k8s_client) | |
# Prometheus metrics | |
deployment_status = Gauge('deployment_status', 'Status of deployments', ['deployment', 'namespace']) | |
while True: | |
deployments = v1.list_namespaced_deployment(namespace=NAMESPACE).items | |
for deployment in deployments: | |
deployment_name = deployment.metadata.name | |
desired_replicas = deployment.spec.replicas | |
# Get current number of pods for the deployment | |
current_replicas = deployment.status.replicas if deployment.status.replicas else 0 | |
# Check if the deployment is up or down | |
status = 1 if current_replicas == desired_replicas else 0 | |
# Set Prometheus metric | |
deployment_status.labels(deployment=deployment_name, namespace=NAMESPACE).set(status) | |
# Sleep for a minute before checking again | |
time.sleep(60) | |
if __name__ == '__main__': | |
start_http_server(8000) # Start Prometheus client on port 8000 | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment