CM監(jiān)控及短信告警

需要安裝cm-api這個(gè)python依賴:https://cloudera.github.io/cm_api/docs/python-client/
cm-api相關(guān)api文檔:https://cloudera.github.io/cm_api/apidocs/v5.15.0/index.html
請(qǐng)注意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)#短信接口請(qǐng)自行實(shí)現(xiàn)
else:
    logger.info('safe!')
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • # Awesome Python [![Awesome](https://cdn.rawgit.com/sindr...
    emily_007閱讀 2,347評(píng)論 0 3
  • # Python 資源大全中文版 我想很多程序員應(yīng)該記得 GitHub 上有一個(gè) Awesome - XXX 系列...
    小邁克閱讀 3,130評(píng)論 1 3
  • # Python 資源大全中文版 我想很多程序員應(yīng)該記得 GitHub 上有一個(gè) Awesome - XXX 系列...
    aimaile閱讀 26,846評(píng)論 6 427
  • 星期五了,想想明天是休息,一定要帶帥子檢測(cè)一下眼睛,咨詢一下預(yù)防近視的方法。 下午下班去接銘帥,托付老...
    指揮官閱讀 401評(píng)論 0 4
  • 補(bǔ)間動(dòng)畫有四種,分別是淡入淡出(Alpha),旋轉(zhuǎn)(Rotate),移動(dòng)(Translate),縮放(Scale)...
    瘋子一般的勇士閱讀 452評(píng)論 0 0

友情鏈接更多精彩內(nèi)容