summaryrefslogtreecommitdiffstats
path: root/clover/monitoring/validate.py
blob: fafe5df797f6ba7b0ab8f9a17d03540bc2311f97 (plain)
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
# Copyright (c) Authors of Clover
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0

from monitoring import Monitoring
from kubernetes import client, config

PROMETHEUS_URL = "http://127.0.0.1:9090"
PROMETHEUS_DEPLOYMENT = "prometheus"
PROMETHEUS_LABELS = "app=prometheus"
ISTIO_NAMESPACE = "istio-system"


def validateDeploy():
    config.load_kube_config()
    appsv1 = client.AppsV1Api()
    corev1 = client.CoreV1Api()
    find_flag = False
    prom_pod_name = None

    # check prometheus deploytment
    ret = appsv1.list_deployment_for_all_namespaces(watch=False)
    for i in ret.items:
        if PROMETHEUS_DEPLOYMENT == i.metadata.name and \
           ISTIO_NAMESPACE == i.metadata.namespace:
           find_flag = True
           break
    if find_flag == False:
        print("ERROR: Deployment: {} doesn't present in {} namespace".format(
                        PROMETHEUS_DEPLOYMENT, ISTIO_NAMESPACE))
        return False

    # find prometheus pod by label selector
    ret = corev1.list_namespaced_pod(ISTIO_NAMESPACE, label_selector=PROMETHEUS_LABELS)
    for i in ret.items:
        prom_pod_name = i.metadata.name
    if prom_pod_name == None:
        print("ERROR: prometheus pod not found")
        return False

    # check prometheus pod status
    ret = corev1.read_namespaced_pod_status(prom_pod_name, ISTIO_NAMESPACE)
    if ret.status.phase != "Running":
        print("ERROR: prometheus pod %s is under %s state" % (prom_pod_name, ret.status.phase))
        return False

    return True


def validateService():
    m = Monitoring(PROMETHEUS_URL)

    return m.is_targets_healthy()


def main():
    if validateDeploy() and validateService():
        print"Prometheus monitoring validation has passed"
        return True
    else:
        print"ERROR: Prometheus monitoring validation has failed"
        return False


if __name__ == '__main__':
    main()