def get(self, cachecluster_id):
        identification_id = cachecluster_id
        ec_monitor_item = g.args.get("elasticache_monitor_item")
        end_time = g.args.get("end_time")
        polymerization = g.args.get("polymerization")
        period = g.args.get("period")

        dvalue = 0
        if re.search("h", end_time, flags=re.I):
            dvalue = int(end_time.split("h")[0])
        elif re.search("d", end_time, flags=re.I):
            dvalue = int(end_time.split("d")[0]) * 24
        else:
            dvalue = int(end_time.split("w")[0]) * 168

        ec_instance = aws_model.DBsession.query(aws_model.Elasticache).filter(
            aws_model.Elasticache.data_status is not False,
            aws_model.Elasticache.cachecluster_id ==
            identification_id).first()
        if not ec_instance:
            abort('elasticache_not_found')
        id = "'" + identification_id + "'"

        now = datetime.datetime.now() + datetime.timedelta(hours=-8)
        end = datetime.datetime.now() + datetime.timedelta(hours=-(8 + dvalue))

        start_time = "'" + datetime.datetime.strptime(
            datetime.datetime.strftime(now, "%Y.%m.%d %H:%M:%S"),
            "%Y.%m.%d %H:%M:%S").isoformat() + "Z" + "'"
        end_time = "'" + datetime.datetime.strptime(
            datetime.datetime.strftime(end, "%Y.%m.%d %H:%M:%S"),
            "%Y.%m.%d %H:%M:%S").isoformat() + "Z" + "'"

        res_data = []
        res_influx = []
        if re.search("free", ec_monitor_item, flags=re.I):
            res_influx = influxdb_client.query(
                "select %s from cloudwatch_redis_freememory where id=%s and time > %s and time < %s "
                % (ec_monitor_item, id, end_time, start_time))
        elif re.search("hit", ec_monitor_item, flags=re.I):
            res_influx = influxdb_client.query(
                "select %s from cloudwatch_redis_cachehit where id=%s and time > %s and time < %s "
                % (ec_monitor_item, id, end_time, start_time))
        res_time = []
        res_monitor = []
        data = []
        for res in res_influx:
            data = res
        for item in data:
            time = datetime.datetime.strftime(
                dateutil.parser.parse(item["time"]), "%Y-%m-%d %H:%M:%S")
            res_time.append(time)
            res_monitor.append(item[ec_monitor_item])
        res_data.append(res_time)
        res_data.append(res_monitor)
        return res_data, 200, None
コード例 #2
0
    def get(self, instance_ip):
        ip = "'" + instance_ip + "'"
        dvalue = 24
        now = datetime.datetime.now() + datetime.timedelta(hours=-8)
        end = datetime.datetime.now() + datetime.timedelta(hours=-(8 + dvalue))

        start_time = "'" + datetime.datetime.strptime(
            datetime.datetime.strftime(now, "%Y.%m.%d %H:%M:%S"),
            "%Y.%m.%d %H:%M:%S").isoformat() + "Z" + "'"
        end_time = "'" + datetime.datetime.strptime(
            datetime.datetime.strftime(end, "%Y.%m.%d %H:%M:%S"),
            "%Y.%m.%d %H:%M:%S").isoformat() + "Z" + "'"

        res_influx = influxdb_client.query(
            "select %s,%s,%s,%s,%s,%s from ec2_disk where host=%s and time > %s and time < %s "
            % ("all_size", "avail_size", "device", "mount_dir", "used_percent",
               "used_size", ip, end_time, start_time))
        res_data = []

        for data in res_influx:
            for item in data:
                del item["time"]
                res_data.append(item)

        return res_data, 200, None
コード例 #3
0
    def get(self, instance_ip):

        host_ip = instance_ip
        time_point = g.args.get("time_point")
        monitor_item = g.args.get("monitor_item")

        ip = "'" + host_ip + "'"

        monitor_point_time = datetime.datetime.strptime(time_point,"%Y-%m-%d %H:%M:%S")

        start = monitor_point_time + datetime.timedelta(minutes=-0.08)
        end = monitor_point_time + datetime.timedelta(minutes=+0.08)

        start_time = "'" + datetime.datetime.strptime(datetime.datetime.strftime(start,"%Y.%m.%d %H:%M:%S"),"%Y.%m.%d %H:%M:%S").isoformat() + "Z" + "'"
        end_time = "'" + datetime.datetime.strptime(datetime.datetime.strftime(end,"%Y.%m.%d %H:%M:%S"),"%Y.%m.%d %H:%M:%S").isoformat() + "Z" + "'"

        def inf_data(monitor, res_influx):
            res_data = []
            for res in res_influx:
                data = res
                pid = []
                process_name = []
                monitor_data = []
                for item in data:
                    pid.append(item["pid"])
                    process_name.append(item["process_name"])
                    monitor_data.append(float(item[monitor]))
                res_data.append(pid)
                res_data.append(process_name)
                res_data.append(monitor_data)
            return res_data

        res_data = []

        if re.search("cpu", monitor_item, flags=re.I):
            res_influx = influxdb_client.query("select %s,%s,%s from app_cpu_top where host=%s and time > %s and time < %s " %("cpu_percent","pid","process_name", ip, start_time, end_time))
            res_data = inf_data("cpu_percent",res_influx)
        elif re.search("memery", monitor_item, flags=re.I):
            res_influx = influxdb_client.query("select %s,%s,%s from app_memery_top where host=%s and time > %s and time < %s " %("memery_percent","pid","process_name", ip, start_time, end_time))
            res_data = inf_data("memery_percent",res_influx)


        return res_data, 200, None
コード例 #4
0
def ec2_utilizat(instance_id, ):
    identification_id = instance_id
    ec2_monitor_item = "CPUUtilization"
    end_time = "3d"

    dvalue = 0
    if re.search("h", end_time, flags=re.I):
        dvalue = int(end_time.split("h")[0])
    elif re.search("d", end_time, flags=re.I):
        dvalue = int(end_time.split("d")[0]) * 24
    else:
        dvalue = int(end_time.split("w")[0]) * 168

    now = datetime.datetime.now() + datetime.timedelta(hours=-8)
    end = datetime.datetime.now() + datetime.timedelta(hours=-(8 + dvalue))

    start_time = "'" + datetime.datetime.strptime(
        datetime.datetime.strftime(now, "%Y.%m.%d %H:%M:%S"),
        "%Y.%m.%d %H:%M:%S").isoformat() + "Z" + "'"
    end_time = "'" + datetime.datetime.strptime(
        datetime.datetime.strftime(end, "%Y.%m.%d %H:%M:%S"),
        "%Y.%m.%d %H:%M:%S").isoformat() + "Z" + "'"

    ec2_instance = aws_model.DBsession.query(aws_model.Ec2).filter(
        aws_model.Ec2.data_status is not False,
        aws_model.Ec2.instance_id == identification_id).first()
    if not ec2_instance:
        abort('ec2_not_found')
    id = "'" + identification_id + "'"

    res_data = []
    res_influx = []
    try:
        if re.search("cpu", ec2_monitor_item, flags=re.I):
            res_influx = influxdb_client.query(
                "select %s from cloudwatch_ec2_cpu where id=%s and time > %s and time < %s "
                % (ec2_monitor_item, id, end_time, start_time))
        elif re.search("net", ec2_monitor_item, flags=re.I):
            res_influx = influxdb_client.query(
                "select %s from cloudwatch_ec2_netwolrkIn where id=%s and time > %s and time < %s "
                % (ec2_monitor_item, id, end_time, start_time))
    except Exception:
        return "insufficient data"
    res_time = []
    res_monitor = []
    data = []
    if not res_influx:
        return "insufficient data"
    for res in res_influx:
        data = res
    for item in data:
        time = datetime.datetime.strftime(dateutil.parser.parse(item["time"]),
                                          "%Y-%m-%d %H:%M:%S")
        res_time.append(time)
        res_monitor.append(item[ec2_monitor_item])
    res_data.append(res_time)
    res_data.append(res_monitor)

    gt_argv_one = []
    gt_argv_two = []
    first_level = 0
    utilizations = ["overmuch", "equitable", "nervous", "insufficiency"]
    utilization = ""
    argv = sum(res_data[1]) // len(res_data[1])
    if argv < 35:
        first_level = 35
        utilization = "overmuch"
    elif 35 < argv < 50:
        first_level = 50
        utilization = "equitable"
    elif 50 < argv < 65:
        first_level = 65
        utilization = "nervous"
    else:
        utilization = "insufficiency"
        return utilization

    for cpu_value in res_data[1]:
        if (cpu_value - first_level) > 30:
            gt_argv_two.append(cpu_value)
        elif (cpu_value - first_level) > 15:
            gt_argv_one.append(cpu_value)

    gt_argvone_percent = len(gt_argv_one) / len(res_data[1])
    gt_argvtwo_percent = len(gt_argv_two) / len(res_data[1])

    if gt_argvtwo_percent > 0.3:
        try:
            utilization = utilizations[utilizations.index(utilization) + 2]
        except Exception:
            utilization = "insufficiency"
    elif gt_argvone_percent > 0.3:
        utilization = utilizations[utilizations.index(utilization) + 1]

    return utilization
コード例 #5
0
    def get(self, app_id):

        app_name = g.args.get("app_name")
        sys_status = g.args.get("sys_status")
        end_time = g.args.get("end_time")
        polymerization = g.args.get("polymerization")
        period = g.args.get("period")

        dvalue = 0
        if re.search("h", end_time, flags=re.I):
            dvalue = int(end_time.split("h")[0])
        elif re.search("d", end_time, flags=re.I):
            dvalue = int(end_time.split("d")[0]) * 12
        else:
            dvalue = int(end_time.split("w")[0]) * 168

        now = datetime.datetime.now()
        end = datetime.datetime.now() + datetime.timedelta(hours=-dvalue)

        start_time = "'" + datetime.datetime.strptime(
            datetime.datetime.strftime(now, "%Y.%m.%d %H:%M:%S"),
            "%Y.%m.%d %H:%M:%S").isoformat() + "Z" + "'"
        end_time = "'" + datetime.datetime.strptime(
            datetime.datetime.strftime(end, "%Y.%m.%d %H:%M:%S"),
            "%Y.%m.%d %H:%M:%S").isoformat() + "Z" + "'"

        ec2_ins_ips = aws_model.DBsession.query(aws_model.Ec2.private_ip).\
            join(aws_model.App2Aws, aws_model.Ec2.id == aws_model.App2Aws.resource_id).\
            join(aws_model.App, aws_model.App.id == aws_model.App2Aws.app_id).\
            filter(aws_model.App.name == app_name, aws_model.App2Aws.resource_type == 'ec2').all()

        res_cpu_data = []
        res_mem_data = []
        id = 1
        if re.search("cpu", sys_status, flags=re.I):
            for ec2_ip in ec2_ins_ips:
                ip = "'" + ec2_ip[0] + "'"
                res_cpu_ec2 = []
                idle_cpu = []
                sys_cpu = []
                user_cpu = []
                res_time = []
                res_influx = influxdb_client.query(
                    "select %s,%s,%s from system_cpu where host=%s and time > %s and time < %s "
                    % ("idle_cpu", "sys_cpu", "user_cpu", ip, end_time,
                       start_time))
                for res in res_influx:
                    data = res
                    for item in data:
                        time = datetime.datetime.strftime(
                            dateutil.parser.parse(item["time"]),
                            "%Y-%m-%d %H:%M:%S")
                        idle_cpu.append(int(item['idle_cpu']))
                        sys_cpu.append(int(item['sys_cpu']))
                        user_cpu.append(int(item['user_cpu']))
                        res_time.append(time)
                res_cpu_ec2.append({"quota": "idle_cpu", "value": idle_cpu})
                res_cpu_ec2.append({"quota": "sys_cpu", "value": sys_cpu})
                res_cpu_ec2.append({"quota": "user_cpu", "value": user_cpu})
                res_cpu_ec2.append(res_time)
                res_cpu_data.append({
                    "id": id,
                    "host": ec2_ip[0],
                    "value": res_cpu_ec2,
                    "monitor_item": "cpu"
                })
                id += 1

            return res_cpu_data, 200, None

        elif re.search("mem", sys_status, flags=re.I):
            for ec2_ip in ec2_ins_ips:
                ip = "'" + ec2_ip[0] + "'"
                res_mem_ec2 = []
                free_mem_retio = []
                free_mem = []
                res_time = []
                res_influx = influxdb_client.query(
                    "select %s,%s from system_memery where host=%s and time > %s and time < %s "
                    % ("free_memery", "free_memery_retio", ip, end_time,
                       start_time))
                for res in res_influx:
                    data = res
                    for item in data:
                        time = datetime.datetime.strftime(
                            dateutil.parser.parse(item["time"]),
                            "%Y-%m-%d %H:%M:%S")
                        free_mem_retio.append(item['free_memery_retio'])
                        free_mem.append(item['free_memery'])
                        res_time.append(time)
                res_mem_ec2.append({
                    "quota": "free_memery_retio",
                    "value": free_mem_retio
                })
                res_mem_ec2.append({"quota": "free_memery", "value": free_mem})
                res_mem_ec2.append(res_time)
                res_mem_data.append({
                    "id": id,
                    "host": ec2_ip[0],
                    "value": res_mem_ec2,
                    "monitor_item": "memery"
                })
                id += 1
            return res_mem_data, 200, None