Exemplo n.º 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;
}
Exemplo n.º 2
0
/* process xml report */
static int __process_node_xml_report(xmlDoc * doc, xmlNode * node, int ent_id)
{
    loginfo(_("node report for entity %d\n"), ent_id);

    int node_id = ly_entity_db_id(ent_id);
    int status = -1;
    node = node->children;
    for (; node; node = node->next) {
        if (node->type == XML_ELEMENT_NODE) {
            if (strcmp((char *)node->name, "status") == 0) {
                if (node->children != NULL &&
                    node->children->type == XML_TEXT_NODE) {
                    status = atoi((char *)node->children->content);
                    logwarn(_("node %d report status %d\n"),
                               node_id, status);
                }
                else {
                    logerror(_("error in %s(%d)\n"), __func__, __LINE__);
                    return -1;
                }
            }
            else if (strcmp((char *)node->name, "message") == 0) {
                if (node->children != NULL && 
                    node->children->type == XML_TEXT_NODE) {
                    logwarn(_("node %d report message %s\n"),
                               node_id, node->children->content);
                }
            }
            else if (strcmp((char *)node->name, "resource") == 0) {
                 logdebug(_("node %d report resource\n"), node_id);
                 __node_resource_update(doc, node, ent_id);
            }
        }
    }

    NodeInfo *nf = ly_entity_data(ent_id);
    if (nf != NULL && status != -1) {
        nf->status = status;
        loginfo(_("update node status to %d from report\n"), status);
    }

    return 0;
}
Exemplo n.º 3
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;
}
Exemplo n.º 4
0
int ly_epoll_entity_recv(int ent_id)
{
    if (ly_entity_type(ent_id) == LY_ENTITY_CLC)
        return __epoll_work_recv(ent_id);

    int fd = ly_entity_fd(ent_id);
    if (fd < 0) {
        logerror(_("fd for entity %d was closed. ignore event.\n"), ent_id);
        return 1;
    }

    LYPacketRecv *pkt = ly_entity_pkt(ent_id);
    if (pkt == NULL)
        return -255;

    int size;
    void * buf = ly_packet_buf(pkt, &size);
    if (buf == NULL) {
        logerror(_("ly_packet_buf returns NULL buffer. close socket\n"));
        return 1;
    }
    if (size == 0) {
        logerror(_("ly_packet_buf returns 0 size buffer. close socket\n"));
        return 1;
    }

    int len = recv(fd, buf, size, 0);
    if (len <= 0) {
        loginfo(_("socket %d recv returns %d. close socket\n"), fd, len);
        if (len < 0)
            loginfo(_("socket %d recv, errno %d\n"), fd, errno);

        int type = ly_entity_type(ent_id);
        int db_id = ly_entity_db_id(ent_id);
        if (type == LY_ENTITY_NODE) {
            logdebug(_("update node %d status in db to offline\n"), db_id);
            db_node_update_status(DB_NODE_FIND_BY_ID, &db_id, NODE_STATUS_OFFLINE);
            return 1;
        }

        if (ly_entity_type(ent_id) != LY_ENTITY_OSM)
            return 1;

        logdebug(_("update instance %d status in db\n"), db_id);
        InstanceInfo ii;
        ii.ip = NULL;
        ii.status = DOMAIN_S_NEED_QUERY;
        db_instance_update_status(db_id, &ii, -1);
        job_internal_query_instance(db_id);
        return 1;
    }
    logdebug(_("socket %d recv %d bytes\n"), fd, len);

    while(1) {
        int ret = ly_packet_recv(pkt, len);
        if (ret < 0) {
            logerror(_("package recv error in %s\n"), __func__);
            __print_recv_buf(buf, len);
            break;
        }

        /* currenly we only support processing a complete packet */
        if (ret == 0) {
            if (pkt->pkt_buf_received > 0) {
                loginfo(_("socket %d recv partial packet(len %d)\n"),
                           fd, pkt->pkt_buf_received);
                __print_recv_buf(buf, len);
            }
            break;
        }

        int type = ly_packet_type(pkt);
        loginfo(_("socket %d recv packet, type %d\n"), fd, type);
        /*
        if (type == PKT_TYPE_UNKNOW)
            break;
        */

        buf = ly_packet_data(pkt, &size);
        if (type == PKT_TYPE_WEB_NEW_JOB_REQUEST) {
            ly_entity_init(ent_id, LY_ENTITY_WEB);
            ret = __process_web_job(buf, size, ent_id);
            if (ret < 0)
                logerror(_("web packet process error in %s.\n"), __func__);
        }
	else if (type == PKT_TYPE_NODE_REGISTER_REQUEST) {
            ly_entity_init(ent_id, LY_ENTITY_NODE);
            ret = eh_process_node_xml(buf, ent_id);
            if (ret < 0)
                logerror(_("node packet process error in %s.\n"), __func__);
        }
        else if (type == PKT_TYPE_NODE_AUTH_REQUEST ||
                 type == PKT_TYPE_NODE_AUTH_REPLY) {
            ly_entity_init(ent_id, LY_ENTITY_NODE);
            ret = eh_process_node_auth(type == PKT_TYPE_NODE_AUTH_REPLY ?
                                       1 : 0, buf, ent_id);
            if (ret < 0)
                logerror(_("node auth packet process error in %s.\n"), __func__);
        }
        else if (type == PKT_TYPE_OSM_AUTH_REQUEST ||
                 type == PKT_TYPE_OSM_AUTH_REPLY) {
            ly_entity_init(ent_id, LY_ENTITY_OSM);
            ret = eh_process_osm_auth(type == PKT_TYPE_OSM_AUTH_REPLY ?
                                       1 : 0, buf, ent_id);
            if (ret < 0)
                logerror(_("osm auth packet process error in %s.\n"), __func__);
        }
        else if (type == PKT_TYPE_CLC_OSM_QUERY_REPLY) {
            ret = eh_process_osm_query(ly_packet_data(pkt, NULL));
            if (ret < 0)
                logerror(_("osm packet process error in %s\n"), __func__);
        }
	else if (PKT_TYPE_ENTITY_GROUP_CLC(type) ||
                 PKT_TYPE_ENTITY_GROUP_NODE(type)) {
            ret = eh_process_node_xml(buf, ent_id);
            if (ret < 0)
                logerror(_("node packet process error in %s.\n"), __func__);
        }
        else if (type == PKT_TYPE_OSM_REGISTER_REQUEST) {
            ly_entity_init(ent_id, LY_ENTITY_OSM);
            ret = eh_process_osm_register(buf, size, ent_id);
            if (ret < 0)
                logerror(_("osm packet process error in %s.\n"), __func__);
        }
        else if (type == PKT_TYPE_OSM_REPORT) {
            ret = eh_process_osm_report(buf, size, ent_id);
            if (ret < 0)
                logerror(_("osm packet process error in %s.\n"), __func__);
        }
        else if (type == PKT_TYPE_TEST_ECHO_REQUEST) {
            ret = __process_test_echo(buf, size, ent_id);
            if (ret < 0)
                logerror(_("echo packet process error in %s.\n"), __func__);
        }
        else {
            logerror(_("unrecognized packet type.\n"));
        }

        if (ly_packet_recv_done(pkt) < 0 || ret < 0) {
            logerror(_("%s return error\n"), __func__);
            return -1;
        }

        if (ret > 0)
            return ret;

        len = 0; /* continue processing data in buffer */
    }

    return 0;
}