|
| 1 | +#!/usr/bin/env python |
| 2 | +''' collect info of zabbix and compare with info on master ''' |
| 3 | + |
| 4 | +# TODO: change to use arguments for podname, and run once per podname |
| 5 | + |
| 6 | +#pylint: disable=import-error |
| 7 | +#pylint: disable=invalid-name |
| 8 | +#pylint: disable=broad-except |
| 9 | + |
| 10 | +import argparse |
| 11 | +import logging |
| 12 | +import json |
| 13 | +import urllib2 |
| 14 | +import sys |
| 15 | +from urllib2 import Request, urlopen, URLError, HTTPError |
| 16 | +from openshift_tools.monitoring.ocutil import OCUtil |
| 17 | +from openshift_tools.monitoring.metric_sender import MetricSender |
| 18 | +sys.path.insert(0, '/container_setup') |
| 19 | +from zabbix_data_sync import * |
| 20 | + |
| 21 | +logging.basicConfig( |
| 22 | + format='%(asctime)s - %(relativeCreated)6d - %(levelname)-8s - %(message)s', |
| 23 | +) |
| 24 | +logging.getLogger().setLevel(logging.WARN) |
| 25 | + |
| 26 | +class ZabbixInfo(object): |
| 27 | + ''' |
| 28 | + this will check the zabbix data and compare it with the real world |
| 29 | + ''' |
| 30 | + def __init__(self, args=None, ): |
| 31 | + '''initial for the InfraNodePodStatus''' |
| 32 | + self.args = args |
| 33 | + self.kubeconfig = '/tmp/admin.kubeconfig' |
| 34 | + self.oc = OCUtil(namespace=self.args.namespace, config_file=self.kubeconfig) |
| 35 | + |
| 36 | + def check_all_hosts(self, zabbix_data_sync_inventory_hosts, clusterid): |
| 37 | + ''' check the situation ''' |
| 38 | + result = 1 |
| 39 | + zabbix_data_sync_inventory_hosts_names = [] |
| 40 | + for host in zabbix_data_sync_inventory_hosts: |
| 41 | + zabbix_data_sync_inventory_hosts_names.append(host['name']) |
| 42 | + |
| 43 | + desire_number_cluster = cluster_desired_compute_size + cluster_desired_infra_size + cluster_desired_master_size |
| 44 | + logging.getLogger().info("the requested number of instance is :" + str(desire_number_cluster)) |
| 45 | + hosts = self.oc.get_nodes() |
| 46 | + #print hosts |
| 47 | + for host in hosts['items']: |
| 48 | + hostnameincluster = "" |
| 49 | + if host['metadata']['labels']['type'] == 'master': |
| 50 | + hostnameincluster = host['metadata']['labels']['hostname'] |
| 51 | + elif host['metadata']['labels']['type'] == 'infra': |
| 52 | + hostnameincluster = clusterid + "-infra-" + host['metadata']['labels']['kubernetes.io/hostname'] |
| 53 | + else: |
| 54 | + hostnameincluster = clusterid + "-compute-" + host['metadata']['labels']['kubernetes.io/hostname'] |
| 55 | + |
| 56 | + if hostnameincluster in zabbix_data_sync_inventory_hosts_names: |
| 57 | + logging.getLogger().info("found host in zabbix :" + str(hostnameincluster)) |
| 58 | + else: |
| 59 | + result = 0 |
| 60 | + logging.getLogger().info("host not in zabbix:" + str(hostnameincluster)) |
| 61 | + |
| 62 | + if result == 1: |
| 63 | + if len(hosts['items']) == desire_number_cluster: |
| 64 | + logging.getLogger().info("currrently cluster have :" + str(len(hosts['items']))) |
| 65 | + logging.getLogger().info("all the node under monitoring and the number is the same as requested:" + str(desire_number_cluster)) |
| 66 | + else: |
| 67 | + result = 2 |
| 68 | + logging.getLogger().info("cluster node number is different with requested") |
| 69 | + |
| 70 | + return result |
| 71 | + |
| 72 | + def send_metrics(self, status): |
| 73 | + """send_metrics""" |
| 74 | + ms = MetricSender(verbose=self.args.verbose) |
| 75 | + ms.add_metric({'openshift.master.zabbix.inventory.status': status}) |
| 76 | + ms.send_metrics() |
| 77 | + |
| 78 | +def parse_args(): |
| 79 | + """ parse the args from the cli """ |
| 80 | + logging.getLogger().debug("parse_args()") |
| 81 | + parser = argparse.ArgumentParser(description='AWS instance health') |
| 82 | + parser.add_argument('-v', '--verbose', action='count', default=0, |
| 83 | + help='verbosity level, specify multiple') |
| 84 | + parser.add_argument('--clusterid', default="default", |
| 85 | + help='clusterid') |
| 86 | + parser.add_argument('--namespace', default="default", |
| 87 | + help='Project namespace') |
| 88 | + args = parser.parse_args() |
| 89 | + if args.verbose > 0: |
| 90 | + logging.getLogger().setLevel(logging.DEBUG) |
| 91 | + return args |
| 92 | + |
| 93 | +if __name__ == '__main__': |
| 94 | + status = 1 |
| 95 | + ZABBIX = ZabbixInfo(args=parse_args(), ) |
| 96 | + status = ZABBIX.check_all_hosts(zabbix_data_sync_inventory_hosts, ZABBIX.args.clusterid) |
| 97 | + ZABBIX.send_metrics(status) |
0 commit comments