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
def add_cluster(data): cluster = Cluster() cluster.name = data['name'] cluster.detail = data['detail'] cluster.alias = data['alias'] cluster.ip = data['ip'] cluster.port = data['port'] cluster.normal_ports = data['normal_ports'] cluster.create_time = DateTools.date_format(datetime.now()) cluster.last_update = DateTools.date_format(datetime.now()) return sql.insert_one_sql(cluster)
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)
def test_compute(): dt = datetime.now() dt1 = DateTools.update_time(dt) assert dt.minute == dt1.minute dt2 = DateTools.update_time(dt, minutes=2) if dt.minute < 57: assert dt.minute == dt2.minute - 2 else: assert dt.minute == dt2.minute + 60 - 2 dt3 = DateTools.update_time(dt, minutes=2, add=False) if dt.minute > 2: assert dt.minute == dt3.minute + 2 else: assert dt.minute == dt3.minute + 2 - 60
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
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
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)
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)
def update_cluster(cluster_id, data): cluster = Cluster() cluster.id = cluster_id cluster.name = data['name'] cluster.detail = data[ 'detail'] + "update Time: " + DateTools.date_format(datetime.now()) cluster.alias = data['alias'] cluster.ip = data['ip'] cluster.port = data['port'] cluster.normal_ports = data['normal_ports'] return sql.update_cluster_sql(cluster)
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)
def get_status(cluster, data): if data['cluster'] != cluster: raise Exception(u"传入参数有误,请重新传入") if not data['detail'] or not data['type']: raise Exception(u"传入参数有误,请重新传入") if data['status'] != "success" and data['status'] != "fail": raise Exception(u"传入参数有误,请重新传入") monitor = Monitor() monitor.cluster = cluster monitor.detail = data['detail'] monitor.status = True if data['status'] == "success" else False monitor.type = data['type'] monitor.time = DateTools.date_format(datetime.now()) ct = CronMonitor() ct.insert_monitor(monitor) return "Well"
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)
def test_date_format(): dt = datetime.now() print DateTools.date_format(dt)
def test_str_format(): s_time = "1982-02-5 12:25:32" dt = DateTools.str_format(s_time) print dt print type(dt)
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)