示例#1
0
/* process node resource report */
static int __node_resource_update(xmlDoc * doc, xmlNode * node, int ent_id)
{
    logdebug(_("%s called\n"), __func__);

    NodeInfo *nf = ly_entity_data(ent_id);
    if (nf == NULL) {
        logerror(_("error in %s(%d)\n"), __func__, __LINE__);
        goto failed;
    }

    /* Create xpath evaluation context */
    xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
    if (xpathCtx == NULL) {
        logerror(_("unable to create new XPath context %s, %d\n"),
                 __func__, __LINE__);
        goto failed;
    }

    char *str;
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/report/resource/cpu/commit");
    if (str == NULL)
        goto failed;
    nf->cpu_commit = atoi(str);
    free(str);

    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/report/resource/memory/free");
    if (str == NULL)
        goto failed;
    nf->mem_free = atoi(str);
    free(str);

    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/report/resource/memory/commit");
    if (str == NULL)
        goto failed;
    nf->mem_commit = atoi(str);
    free(str);

    str = xml_xpath_text_from_ctx(xpathCtx,
                          "/" LYXML_ROOT "/report/resource/load/average");
    if (str == NULL)
        goto failed;
    nf->load_average = atoi(str);
    free(str);

    logdebug(_("update info for node %d: %d %d %d %d\n"),
                ly_entity_db_id(ent_id),
                nf->cpu_commit, nf->mem_free, nf->mem_commit,
                nf->load_average);

    return 0;

failed:
    return -1;
}
示例#2
0
/*
** process instance info query reply
**
** the is the reply of instance info query through node request
*/
static int __instance_info_update(xmlDoc * doc, xmlNode * node,
                                  int ent_id, int * j_status)
{
    logdebug(_("%s called\n"), __func__);

    /* Create xpath evaluation context */
    xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
    if (xpathCtx == NULL) {
        logerror(_("unable to create new XPath context %s, %d\n"),
                 __func__, __LINE__);
        goto failed;
    }

    InstanceInfo ii;
    char *str;
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/response/data/id");
    if (str == NULL)
        goto failed;
    ii.id = atoi(str);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/response/data/status");
    if (str == NULL)
        goto failed;
    ii.status = atoi(str);
    free(str);
    ii.ip = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/response/data/ip");

    logdebug(_("update info for instance %d: status %d, ip %s\n"),
                ii.id, ii.status, ii.ip);

    /* the ent_id passed to the routine is for node, not usefult */
    ent_id = ly_entity_find_by_db(LY_ENTITY_OSM, ii.id);
    if (!ly_entity_is_registered(ent_id) &&
        db_instance_update_status(ii.id, &ii, -1) < 0) {
        logerror(_("error in %s(%d)\n"), __func__, __LINE__);
        if (ii.ip)
            free(ii.ip);
        goto failed;
    }

    if (ii.ip)
        free(ii.ip);
    return 0;

failed:
    *j_status = LY_S_FINISHED_FAILURE;
    return 0;
}
示例#3
0
/* process node register request */
static int __node_xml_register(xmlDoc * doc, xmlNode * node, int ent_id)
{
    if (ly_entity_is_registered(ent_id)) {
        logwarn(_("received node register request again, ignored\n"));
        return -1;
    }

    LYNodeData * nd = ly_entity_data(ent_id);
    if (nd == NULL ) {
        logerror(_("error in %s(%d)\n"), __func__, __LINE__);
        return -1;
    }
    NodeInfo * nf = &nd->node;

    /* Create xpath evaluation context */
    xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
    if (xpathCtx == NULL) {
        logerror(_("unable to create new XPath context %s, %d\n"),
                 __func__, __LINE__);
        return -1;
    }
    int ret = -1;

    char *str;
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/status");
    if (str == NULL)
        goto xml_err;
    nf->status = atoi(str);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/hypervisor");
    if (str == NULL)
        goto xml_err;
    nf->hypervisor = atoi(str);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/host/tag");
    int tag = -1;
    if (str) {
        tag = atoi(str); /* NULL str is allowed for new node */
        free(str);
    }
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/host/name");
    if (str == NULL)
        goto xml_err;
    nf->host_name = str;
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/host/ip");
    if (str == NULL)
        goto xml_err;
    nf->host_ip = str;
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/cpu/arch");
    if (str == NULL)
        goto xml_err;
    nf->cpu_arch = atoi(str);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/cpu/model");
    if (str == NULL)
        goto xml_err;
    nf->cpu_model = str;
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/cpu/mhz");
    if (str == NULL)
        goto xml_err;
    nf->cpu_mhz = atoi(str);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/cpu/max");
    if (str == NULL)
        goto xml_err;
    nf->cpu_max = atoi(str);
    nf->cpu_vlimit = NODE_SCHEDULE_CPU_LIMIT(nf->cpu_max);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/cpu/commit");
    if (str == NULL)
        goto xml_err;
    nf->cpu_commit = atoi(str);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/memory/total");
    if (str == NULL)
        goto xml_err;
    nf->mem_max = atoi(str);
    nf->mem_vlimit = NODE_SCHEDULE_MEM_LIMIT(nf->mem_max);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/memory/free");
    if (str == NULL)
        goto xml_err;
    nf->mem_free = atoi(str);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/memory/commit");
    if (str == NULL)
        goto xml_err;
    nf->mem_commit = atoi(str);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/storage/total");
    if (str == NULL)
        goto xml_err;
    nf->storage_total = atoi(str);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/storage/free");
    if (str == NULL)
        goto xml_err;
    nf->storage_free = atoi(str);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                          "/" LYXML_ROOT "/request/parameters/load/average");
    if (str == NULL)
        goto xml_err;
    nf->load_average = atoi(str);
    free(str);

    if (nf->status >= NODE_STATUS_REGISTERED) {
        logwarn(_("node(%d, %s) tries to register from wrong status(%d)\n"),
                 nf->host_tag, nf->host_ip, nf->status);
        ly_entity_release(ent_id);
        ret = 0; /* no need to continue node registration */
        goto done;
    }
        
    if (ly_entity_node_active(nf->host_ip)) {
        logwarn(_("duplicate node ip received. something is wrong...\n"));
        ly_entity_release(ent_id);
        ret = 0; /* no need to continue node registration */
        goto done;
    }
   
    if (tag > 0) {
        /* check authentication result */
        if (nf->host_tag > 0 && tag != nf->host_tag) {
            logerror(_("node tag changed, %d -> %d. something is wrong.\n"),
                       nf->host_tag, tag);
            goto done;
        }
        nf->host_tag = tag;
        ret = __node_register_auth(nf, ent_id);
        if (ret == LY_S_REGISTERING_DONE_SUCCESS) {
            AuthConfig * ac = ly_entity_auth(ent_id);
            if (db_node_update_secret(DB_NODE_FIND_BY_ID, &tag,
                                      ac->secret) < 0 ||
                db_node_update_status(DB_NODE_FIND_BY_ID, &tag,
                                      NODE_STATUS_REGISTERED) < 0) {
                logerror(_("error in %s(%d)\n"), __func__, __LINE__);
                ret = -1;
                goto done;
            }
            loginfo(_("node(tag:%d) registered\n"), tag);
            ly_entity_update(ent_id, tag, LY_ENTITY_FLAG_STATUS_REGISTERED);
        }
        goto done;
    }

    /* new node */
    DBNodeRegInfo db_nf;
    bzero(&db_nf, sizeof(DBNodeRegInfo));
    ret = db_node_find(DB_NODE_FIND_BY_IP, nf->host_ip, &db_nf);
    if (ret < 0 || ret > 1) {
        logerror(_("error in %s(%d)\n"), __func__, __LINE__);
        ret = -1;
        goto done;
    }

    if (ret == 0 || db_nf.secret) {
        /* new node */
        logdebug(_("new node\n"));
        if (nf->status != NODE_STATUS_UNINITIALIZED) {
            logwarn(_("node(%d, %s) tries to register from unexpected status(%d)\n"),
                       nf->host_tag, nf->host_ip, nf->status);
            nf->status = NODE_STATUS_UNINITIALIZED;
        }
        if (db_nf.secret) {
            logwarn(_("new node takes ip(%s) used by tagged node\n"), nf->host_ip);
            db_node_reginfo_free(&db_nf);
            bzero(&db_nf, sizeof(DBNodeRegInfo));
        }
            
        ret = db_node_insert(nf);
        if (ret < 0) {
            logerror(_("error in %s(%d)\n"), __func__, __LINE__);
            goto new_done;
        }
        db_nf.id = ret;
        loginfo(_("new node %s added in db(%d)\n"), nf->host_ip, ret);

        /* enable node if node is control server */
        if (ly_is_clc_ip(nf->host_ip)) {
            if (db_node_enable(ret, 1) != 0 ||
                db_node_find(DB_NODE_FIND_BY_ID, &ret, &db_nf) != 1) {
                logerror(_("error in %s(%d)\n"), __func__, __LINE__);
                ret = -1;
                goto new_done;
            }
        }
    }
    else
        logdebug(_("untagged node for ip(%s) found in db\n"), nf->host_ip);

    ly_entity_update(ent_id, db_nf.id, LY_ENTITY_FLAG_STATUS_ONLINE);
    if (db_nf.enabled) {
        AuthConfig * ac = ly_entity_auth(ent_id);
        ret = -1;
        if (ac->secret) {
            logerror(_("error in %s(%d)\n"), __func__, __LINE__);
            goto new_done;
        }
        ac->secret = lyauth_secret();
        if (ac->secret == NULL) {
            logerror(_("error in %s(%d)\n"), __func__, __LINE__);
            goto new_done;
        }
        nf->host_tag = db_nf.id;
        ret = LY_S_REGISTERING_CONFIG;
    }
    else {
        logdebug(_("register request done, node %s not enabled\n"), nf->host_ip);
        ret = LY_S_REGISTERING_INIT;
    }
    nf->status = NODE_STATUS_ONLINE;
    if (db_node_update(DB_NODE_FIND_BY_ID, &db_nf.id, nf) < 0) {
        logerror(_("error in %s(%d)\n"), __func__, __LINE__);
        ret = -1;
    }

new_done:
    db_node_reginfo_free(&db_nf);
    goto done;
xml_err:
    logerror(_("invalid node xml register request\n"));
done:
    xmlXPathFreeContext(xpathCtx);
    logdebug(_("end of %s, node status %d\n"), __func__, nf->status);
    return ret;
}
示例#4
0
/* process node info query reply */
static int __node_info_update(xmlDoc * doc, xmlNode * node,
                              int ent_id, int * j_status)
{
    logdebug(_("%s called\n"), __func__);

    if (!ly_entity_is_online(ent_id)) {
        /* shouldn't come here */
        logerror(_("error in %s(%d)\n"), __func__, __LINE__);
        *j_status = LY_S_FINISHED_FAILURE_NODE_NOT_AVAIL;
        return 0;
    }

    LYNodeData * nd = ly_entity_data(ent_id);
    if (nd == NULL) {
        logerror(_("error in %s(%d)\n"), __func__, __LINE__);
        goto failed;
    }
    NodeInfo * nf = &nd->node;

    /* Create xpath evaluation context */
    xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
    if (xpathCtx == NULL) {
        logerror(_("unable to create new XPath context %s, %d\n"),
                 __func__, __LINE__);
        *j_status = LY_S_FINISHED_FAILURE;
        return 0;
    }

    char *str;
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/response/data/status");
    if (str == NULL)
        goto failed;
    nf->status = atoi(str);
    free(str);

    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/response/data/cpu/commit");
    if (str == NULL)
        goto failed;
    nf->cpu_commit = atoi(str);
    free(str);

    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/response/data/memory/free");
    if (str == NULL)
        goto failed;
    nf->mem_free = atoi(str);
    free(str);

    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/response/data/storage/free");
    if (str == NULL)
        goto failed;
    nf->storage_free = atoi(str);
    free(str);

    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/response/data/memory/commit");
    if (str == NULL)
        goto failed;
    nf->mem_commit = atoi(str);
    free(str);

    str = xml_xpath_text_from_ctx(xpathCtx,
                          "/" LYXML_ROOT "/response/data/load/average");
    if (str == NULL)
        goto failed;
    nf->load_average = atoi(str);
    free(str);

    int node_id = ly_entity_db_id(ent_id);
    logdebug(_("update info for node %d: %d %d %d %d %d\n"), node_id,
                nf->status, nf->cpu_commit, 
                nf->mem_free, nf->mem_commit, nf->load_average);

    if (db_node_update(DB_NODE_FIND_BY_ID, &node_id, nf) < 0) {
        logerror(_("error in %s(%d)\n"), __func__, __LINE__);
        goto failed;
    }

    /* no need to update j_status */
    goto done;

failed:
    *j_status = LY_S_FINISHED_FAILURE;
done:
    xmlXPathFreeContext(xpathCtx);
    return 0;
}
示例#5
0
/*
** process instance info data,
** either from internal query or from xml response
**
*/
static int __instance_info_update(xmlDoc * doc, xmlNode * node)
{
    int ret;
    logdebug(_("%s called\n"), __func__);

    /* Create xpath evaluation context */
    xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
    if (xpathCtx == NULL) {
        logerror(_("unable to create new XPath context %s, %d\n"),
                 __func__, __LINE__);
        return -1;
    }

    InstanceInfo ii;
    bzero(&ii, sizeof(InstanceInfo));
    char *str;
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/response/data/id");
    if (str == NULL)
        goto failed;
    ii.id = atoi(str);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/response/data/status");
    if (str == NULL)
        goto failed;
    ii.status = atoi(str);
    free(str);
    ii.ip = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/response/data/ip");
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/response/data/gport");
    if (str == NULL)
        goto failed;
    ii.gport = atoi(str);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/response/data/netstat0");
    if (str)
        sscanf(str, "%ld %ld %ld %ld", &ii.netstat[0].rx_bytes, &ii.netstat[0].rx_pkts,
                                       &ii.netstat[0].tx_bytes, &ii.netstat[0].tx_pkts);
    else
        goto failed;
    free(str);

    logdebug(_("update info for instance %d:"), ii.id);
    luoyun_instance_info_print(&ii);

    int ent_id = ly_entity_find_by_db(LY_ENTITY_OSM, ii.id);
    if (ly_entity_is_registered(ent_id))
        ii.status = DOMAIN_S_UNKNOWN; /* don't update status */
    if (db_instance_update_status(ii.id, &ii, -1) < 0) {
        logerror(_("error in %s(%d)\n"), __func__, __LINE__);
        if (ii.ip)
            free(ii.ip);
        goto failed;
    }

    if (ii.ip)
        free(ii.ip);
    ret = 0;
    goto done;

failed:
    ret = -1;
done:
    xmlXPathFreeContext(xpathCtx);
    return ret;
}