CM监控及短信告警

需要安装cm-api这个python依赖:https://cloudera.github.io/cm_api/docs/python-client/
cm-api相关api文档:https://cloudera.github.io/cm_api/apidocs/v5.15.0/index.html
请注意CM的版本和使用的api版本

# -*- coding:utf-8 -*-
import logging
import os

from cm_api.api_client import ApiResource

from Sms import *

cm_host = "{cm_host}"
username = 'admin'
password = 'admin'
version = 12

'''修改编码字符集为utf-8'''
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)

logger = logging.getLogger()
logger.setLevel(logging.INFO)
BASIC_FORMAT = "%(asctime)s:%(levelname)s:%(message)s"
DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
formatter = logging.Formatter(BASIC_FORMAT, DATE_FORMAT)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
console.setFormatter(formatter)
script_name = sys.exc_info()[-1]
file_handler = logging.FileHandler("%s.log" % os.path.abspath(__file__))
file_handler.setFormatter(formatter)
file_handler.setLevel(logging.INFO)
logger.addHandler(file_handler)
logger.addHandler(console)



def check():
    api = ApiResource(cm_host, username=username, password=password, version=version)
    host_dict = get_host_dict(api)
    messages = service_check(host_dict, api.get_all_clusters()[0].get_all_services())
    if messages:
        return 'CDH集群告警\r\n' + '\r\n'.join(messages)
    else:
        return None


def get_host_dict(api):
    hosts = api.get_all_hosts()
    host_dict = {}
    for h in hosts:
        host_dict[h.hostId] = h.hostname
    return host_dict


def service_check(host_dict, services):
    service_num = 1
    messages = []
    for service in services:
        role_msgs = role_check(host_dict, service_num, service)
        if role_msgs:
            messages.append(str(service_num) + '.' + service.type + '\r\n' + '\r\n'.join(role_msgs))
            service_num += 1
    return messages


def role_check(host_dict, service_num, service):
    role_msgs = []
    role_num = 1
    for role in service.get_all_roles():
        health_check_msg = health_check(service_num, role_num, role)
        if health_check_msg:
            role_msgs.append(
                str(service_num) + '.' + str(role_num) + ' ' + role.type + '/' + host_dict[
                    role.hostRef.hostId] + ':\r\n' + '\r\n'.join(
                    health_check_msg))
            role_num += 1
    return role_msgs


def health_check(service_num, role_num, role):
    health_check_msg = []
    if role.healthSummary == 'BAD' and not role.maintenanceMode:
        health_num = 1
        for healthCheck in role.healthChecks:
            if not healthCheck['suppressed'] and healthCheck['summary'] == 'BAD':
                health_check_msg.append(
                    str(service_num) + '.' + str(role_num) + '.' + str(health_num) + ' ' + healthCheck['name'] + ':' +
                    healthCheck[
                        'summary'])
                health_num += 1
    return health_check_msg


res = check()

if res:
    logger.info(res)
    send_sms(res)#短信接口请自行实现
else:
    logger.info('safe!')
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • # Awesome Python [![Awesome](https://cdn.rawgit.com/sindr...
    emily_007阅读 6,641评论 0 3
  • # Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列...
    小迈克阅读 8,175评论 1 3
  • # Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列...
    aimaile阅读 26,747评论 6 427
  • 星期五了,想想明天是休息,一定要带帅子检测一下眼睛,咨询一下预防近视的方法。 下午下班去接铭帅,托付老...
    指挥官阅读 2,795评论 0 4
  • 补间动画有四种,分别是淡入淡出(Alpha),旋转(Rotate),移动(Translate),缩放(Scale)...
    疯子一般的勇士阅读 2,999评论 0 0

友情链接更多精彩内容