Exemplo n.º 1
0
def PublicIsNull(token=None, dbconn=None, group=None):
    print('###############################################')
    if not token or not dbconn or not group:
        raise "token or dbconn or group is None"

    try:
        digital_manager = digital(token)
    except Exception as msg:
        raise msg

    public_is_null = dbconn.select(table='hosts',
                                   key=['`hostid`', '`group`'],
                                   where={
                                       'public': 'null',
                                       'tags': '%s_autoscaling' % group
                                   })

    if public_is_null:
        droplets_info = []
        for hostid, group in public_is_null:
            digital_droplets = digital_manager.id_droplet(hostid)

            droplets_info.append((
                digital_droplets.name,
                hostid,
                digital_droplets.ip_address,
                digital_droplets.private_ip_address,
                digital_droplets.size_slug,
                digital_droplets.region.get('slug'),
                digital_droplets.tags[0],
                digital_droplets.snapshot_ids[0]
                if digital_droplets.snapshot_ids else -1,
                group,
            ))
        insert_status = dbconn.insert(
            table="hosts",
            key=[
                "hostname", "hostid", "public", "private", "size", "region",
                "tags", "snapshotid", "group"
            ],
            values=droplets_info,
        )

        if insert_status:
            insert_status = (insert_status, "%s %s" % (
                "hostname, hostid, public, private, size, region, tags, snapshotid, group",
                droplets_info))
        else:
            insert_status = (False, "%s %s" % (
                "hostname, hostid, public, private, size, region, tags, snapshotid, group",
                droplets_info))
    else:
        insert_status = (True, "droplets info is full")

    return insert_status
Exemplo n.º 2
0
def InDroletNotDb(token=None, dbconn=None, group=None):
    if not token or not dbconn or not group:
        raise ValueEmpty("token or dbconn or group is None")
    try:
        digital_manager = digital(token)
    except Exception as msg:
        raise msg

    digital_droplets = digital_manager.tag_droplets("%s_autoscaling" % group)
    if digital_droplets:
        db_droplets = dbconn.select(table='hosts',
                                    key=[
                                        'hostid',
                                    ],
                                    where={'tags': "%s_autoscaling" % group})
        db_droplet_ids = [j[0] for j in db_droplets]

        droplet_not_in_db = []
        for hostid in digital_droplets:
            if hostid not in db_droplet_ids:
                droplet_not_in_db.append((
                    digital_droplets[hostid]["hostname"],
                    hostid,
                    digital_droplets[hostid]["public"],
                    digital_droplets[hostid]["private"],
                    digital_droplets[hostid]["size_slug"],
                    digital_droplets[hostid]["region"],
                    digital_droplets[hostid]["tags"],
                    digital_droplets[hostid]["snapshot_ids"]
                    if digital_droplets[hostid]["snapshot_ids"] else -1,
                    group,
                ))

        if droplet_not_in_db:
            insert_status = dbconn.insert(
                table="hosts",
                key=[
                    "hostname", "hostid", "public", "private", "size",
                    "region", "tags", "snapshotid", "group"
                ],
                values=droplet_not_in_db,
            )
        else:
            return (True, "%s droplets all in group" % group)
        return (insert_status,
                droplet_not_in_db) if insert_status else (False,
                                                          droplet_not_in_db)
    else:
        return (True, "group %s no living droplets" % group)
Exemplo n.º 3
0
def InDbNotDroplet(token=None, dbconn=None, group=None):
    if not token or not dbconn or not group:
        raise ValueEmpty("args null token: %s dbconn: %s group: %s" %
                         (token, dbconn, group))

    try:
        digital_manager = digital(token)
    except Exception as msg:
        raise msg

    digital_droplets = digital_manager.tag_droplets("%s_autoscaling" % group)
    digital_droplet_ids = [j for j in digital_droplets]
    if digital_droplet_ids:
        db_droplets = dbconn.select(table='hosts',
                                    key=[
                                        'hostid',
                                    ],
                                    where={'tags': "%s_autoscaling" % group})

        sucess = ()
        faild = ()
        for db_droplet_id in db_droplets:
            if db_droplet_id[0] not in digital_droplet_ids:
                delete_status = dbconn.delete(
                    table='hosts', where={'hostid': db_droplet_id[0]})
                if delete_status:
                    sucess.append(db_droplet_id[0])
                else:
                    faild.append(db_droplet_id[0])
            else:
                pass
        if faild:
            return (False, faild)
        else:
            return (True, sucess)

    else:
        if dbconn.delete(table='hosts',
                         where={'tags': '%s_autoscaling' % group}):
            return (True, "group %s no living droplets" % group)
        else:
            return (False, "group %s droplet delete faild" % group)
Exemplo n.º 4
0
from pyzabbix import ZabbixAPI
from setting import zabbix_CONF
from digital import digital
from setting import digital_CONF, qps_CONF
from setting import logging
from BaseZabbix import exist_template, exist_group, create_host, get_hostid, delete_host
import sys

if __name__ == "__main__":
    zabi = ZabbixAPI(zabbix_CONF.url)
    zabi.login(zabbix_CONF.user, zabbix_CONF.passwd)
    digital_obj = digital(digital_CONF.token)

    for program in qps_CONF.interface:
        group_id = exist_group(zabi, '%s_autoscaling' % program)
        template_id = exist_template(zabi, zabbix_CONF.template[program])
        droplets = digital_obj.tag_droplets("%s_autoscaling" % program)
        digital_host_names = [droplets[j].get("hostname") for j in droplets]

        for droplet_id in droplets:
            public = droplets.get(droplet_id).get('public')
            if public:
                digital_hostname = droplets.get(droplet_id).get("hostname")
                zabbix_host = zabi.host.get(filter={'host': digital_hostname})
                if not zabbix_host:
                    create_host_info = create_host(zabi, digital_hostname,
                                                   group_id, public,
                                                   template_id)
                    logging.info("create zabbix monitor host_name: %s sucess" %
                                 (digital_hostname))
                else:
Exemplo n.º 5
0
                RequestsPerSecs / qps_CONF.normalimit -
                (len(droplets_in_group) + qps_CONF.retain[program_name]))
        elif AvgRequestsPerSecs < qps_CONF.minlimit:
            change_droplet_num = math.ceil(
                RequestsPerSecs / qps_CONF.normalimit -
                (len(droplets_in_group) + qps_CONF.retain[program_name]))
        else:
            change_droplet_num = 0
        ##################################################################################################
        #print(RequestsPerSecs)
        #print(change_droplet_num)
        logging.debug("program %s change droplet number %s" %
                      (program_name, change_droplet_num))

        ##添加或删除droplet##########################################################################
        DIGITAL_OBJ = digital(digital_CONF.token)
        if change_droplet_num > 0:
            add_droplets = DIGITAL_OBJ.create_mul_droplets(
                tag=program_name, number=change_droplet_num)

            dbconn.insert(
                table='hosts',
                key=[
                    'hostname',
                    'region',
                    'size',
                    'hostid',
                    'public',
                    'group',
                    'tags',
                ],