示例#1
0
    def metric():
        metrics = []
        su = SqlUtil()
        # 获取监控表
        dt_now = datetime.now()
        dt = DateTools.update_time(dt_now,
                                   minutes=conf.monitor_frequency,
                                   add=False)
        monitors, err = su.get_monitor_data_gt_time(Monitor, time=dt)
        if err:
            print "%s Error: %s" % (DateTools.date_format(dt), err)
        else:
            for monitor in monitors:
                metrics.append(
                    'private_monitor_env{cluster="%s",type="%s", time="%s", detail="%s"} %d'
                    % (monitor.cluster, monitor.type, monitor.time,
                       monitor.detail, monitor.status))

        # 获取服务主动监控表
        clusters, err = su.get_cluster()
        if err:
            print "%s Error: %s" % (DateTools.date_format(dt), err)
        else:
            for cluster in clusters:
                metrics.append(
                    'private_monitor_env{cluster="%s",type="connect", time="%s", detail="%s"} %d'
                    % (cluster.name, cluster.last_update, cluster.detail,
                       cluster.status))
        return metrics
示例#2
0
 def check_cluster(cluster_name):
     su = SqlUtil()
     clusters, _ = su.get_cluster()
     cluster_names = [cluster.name for cluster in clusters]
     if cluster_name not in cluster_names:
         raise Exception("此集群不存在, 请手动添加!")
     return u'可以使用'
示例#3
0
def test_delete_monitor_data():
    su = SqlUtil()
    ex_time = datetime.now()
    ex_time_s = DateTools.update_time(ex_time, minutes=1, add=False)
    res, err = su.delete_expire_monitor_data(ex_time_s)
    if err:
        print err
    print res
示例#4
0
def test_insert_monitor():
    sql = SqlUtil()
    monitor = Monitor()
    monitor.time = DateTools.date_format(datetime.now())
    monitor.cluster = "test"
    monitor.type = "port"
    monitor.status = True
    monitor.detail = "测试"
    print sql.insert_one_sql(monitor)
示例#5
0
def test_get():
    obj = Cluster
    sql = SqlUtil()
    sql.db_session()
    result, reason = sql.get(obj, 0, 1)
    print result, reason
    assert result
    for obj1 in result:
        print str(obj1)
示例#6
0
def test_get_gt_date():
    dt = datetime.now()
    dt1 = DateTools.update_time(dt, days=2, add=False)
    su = SqlUtil()
    r1, _ = su.get_monitor_data_gt_time(Monitor, time=dt)
    r2, _ = su.get_monitor_data_gt_time(Monitor, time=dt1)
    assert not r1
    assert r2
    print r2
示例#7
0
def test_update():
    su = SqlUtil()
    obj, err = su.get_cluster(8)
    if err:
        print err
        return
    print obj
    obj.status = False
    obj.last_update = DateTools.date_format(datetime.now())
    su.update_cluster_sql(obj)
示例#8
0
def test_insert_cluster():
    sql = SqlUtil()
    cluster = Cluster()
    cluster.name = "HuaweiCloudera"
    cluster.detail = "华为数融大数据集群"
    cluster.alias = "华为数融大数据集群"
    cluster.ip = "116.63.229.215"
    cluster.port = "7180"
    cluster.normal_ports = "ALL"
    cluster.create_time = DateTools.date_format(datetime.now())
    cluster.last_update = DateTools.date_format(datetime.now())
    print sql.insert_one_sql(cluster)
示例#9
0
class CronMonitor(object):

    queue = multiprocessing.Queue()
    su = SqlUtil()

    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, "_init"):
            cls._init = object.__new__(cls)
        return cls._init

    def insert_monitor(self, monitor):
        if not isinstance(monitor, Monitor):
            print "Error: monitor 必须是Monitor"
        self.queue.put(monitor)

    # cron进程执行方法
    def cron_exec(self):
        while True:
            obj = self.queue.get()
            if not isinstance(obj, Monitor):
                print "Error, %s" % obj
            self.su.insert_one_sql(obj)

    # 启动进程
    @staticmethod
    def start():
        ct = CronMonitor()
        multiprocessing.Process(target=ct.cron_exec).start()
示例#10
0
class CronDataExpire(object):
    su = SqlUtil()

    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, "_init"):
            cls._init = object.__new__(cls)
        return cls._init

    # 执行检查端口
    @staticmethod
    def data_expire_delete():
        data_keep = conf.data_keep
        expire_time = DateTools.update_time(datetime.now(),
                                            days=data_keep,
                                            add=False)
        CronDataExpire.su.delete_expire_monitor_data(expire_time)

    # cron进程执行方法
    @staticmethod
    def cron_exec():
        while True:
            CronDataExpire.data_expire_delete()
            time.sleep(60 * 60)

    # 启动进程
    @staticmethod
    def start():
        multiprocessing.Process(target=CronDataExpire.cron_exec).start()
示例#11
0
def get_status(cluster):
    try:
        data = request.get_json()
        return StatusHandler.get_status(cluster, data)
    except Exception:
        return u'参数错误'


# 程序入口
if __name__ == "__main__":
    # 加载配置
    conf.parse_from_config_ini()
    if not conf.check_config():
        print "Error: 参数配置不正确, 请查看config.ini"
        sys.exit(1)

    # 创建数据库
    su = SqlUtil()
    su.create_tables_all()

    # 启动报告cron任务
    CronMonitor.start()
    # 启动服务端cron任务
    CronCluster.start()
    # 启动数据自动清理
    CronDataExpire.start()
    # 启动用户超时检验
    Auth().cron_start()
    # 运行web
    app.run(host=web_host, port=web_port, debug=False)
示例#12
0
def test_create():
    # 创建数据库
    su = SqlUtil()
    su.create_tables_all()
示例#13
0
 def main():
     return SqlUtil().get(Cluster, 0, 20)
示例#14
0
# coding: utf-8

from src.orm.SqlStrunct import Cluster
from src.orm.SqlUtil import SqlUtil
from src.tools.DateTools import DateTools
from datetime import datetime
from Config import Config

config = Config()

sql = SqlUtil()


class MainImplement(object):

    # 主页面
    @staticmethod
    def main():
        return SqlUtil().get(Cluster, 0, 20)

    # 登录验证
    @staticmethod
    def login_verify(data):
        if data['username'] == config.username and data[
                'password'] == config.password:
            return True
        return False

    # 添加一个集群到私有化监控中
    @staticmethod
    def add_cluster(data):
示例#15
0
class CronCluster(object):

    su = SqlUtil()

    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, "_init"):
            cls._init = object.__new__(cls)
        return cls._init

    # 执行检查端口
    @staticmethod
    def exec_check_port(cluster):
        if not isinstance(cluster, Cluster):
            return
        try:
            telnetlib.Telnet(host=cluster.ip, port=cluster.port)
            cluster.status = True
        except Exception as e:
            cluster.status = False
            cluster.detail = e.message
        cluster.last_update = DateTools.date_format(datetime.now())
        su = CronCluster.su
        su.update_cluster_sql(cluster)

    # 检查端口
    @staticmethod
    def check_port():
        try:
            su = CronCluster.su
            clusters, err = su.get_cluster()
            if err:
                print err
                return
            for cluster in clusters:
                enable = False
                while not enable:
                    if threading.activeCount() < 20:
                        threading.Thread(target=CronCluster.exec_check_port,
                                         args=(cluster, )).start()
                        enable = True
                    else:
                        time.sleep(5)
        except Exception as e:
            print e.message

    # 执行防火墙检查端口
    @staticmethod
    def exec_check_firewalld(cluster):
        if not isinstance(cluster, Cluster):
            return
        # 如果是全开放,则退出
        if cluster.normal_ports == "ALL":
            return
        # 定义monitor类
        monitor = Monitor()
        monitor.status = True
        monitor.detail = "%s" % cluster.ip
        monitor.cluster = cluster.name
        monitor.time = DateTools.date_format(datetime.now())
        monitor.type = "firewalld"
        # 定义检查端口
        normal_ports = []
        out_ports = cluster.normal_ports.split(",")
        for port in out_ports:
            if '-' in port:
                start, end = port.split('-')
                normal_ports.append(range(int(start), int(end) + 1))
            else:
                normal_ports.append(int(port))
        ports = [
            22, 80, 443, 7180, 8088, 8888, 11000, 10000, 8998,
            random.randint(15000, 65535),
            random.randint(15000, 65535),
            random.randint(15000, 65535)
        ]
        [ports.remove(port) for port in normal_ports if port in ports]
        for port in ports:
            try:
                telnetlib.Telnet(host=cluster.ip, port=port)
                if monitor.status:
                    monitor.status = False
                    monitor.detail += u"%s 可以正常连接,与预期状态不符合" % port
            except Exception:
                pass
        monitor.detail += u"非法端口检查正常" if monitor.status else u"非法端口检查不正常"
        CronCluster.su.insert_one_sql(monitor)

    # 检查防火墙端口
    @staticmethod
    def check_firewalld():
        try:
            su = CronCluster.su
            clusters, err = su.get_cluster()
            if err:
                print err
                return
            for cluster in clusters:
                enable = False
                while not enable:
                    if threading.activeCount() < 20:
                        threading.Thread(
                            target=CronCluster.exec_check_firewalld,
                            args=(cluster, )).start()
                        enable = True
                    else:
                        time.sleep(5)
        except Exception as e:
            print e.message

    # cron进程执行方法
    @staticmethod
    def cron_exec():
        while True:
            threading.Thread(target=CronCluster.check_port).start()
            threading.Thread(target=CronCluster.check_firewalld).start()
            time.sleep(conf.monitor_frequency * 60)

    # 启动进程
    @staticmethod
    def start():
        multiprocessing.Process(target=CronCluster.cron_exec).start()
示例#16
0
def test_connect():
    sql = SqlUtil()
    conn = sql.connect()
    assert conn
    print conn
示例#17
0
def test_session():
    sql = SqlUtil()
    sql.db_session()
    session, reason = sql.session()
    print session, reason
    assert session