示例#1
0
文件: sampler.c 项目: mojaves/vmon
static gint
collect_success(VmonRequest *req)
{
    int j = 0;
    VmonResponse res;
    response_init(&res);

    VmChecks checks;
    checks.disk_usage_perc = req->ctx->conf.disk_usage_perc;

    for (j = 0; j < req->records_num; j++) {
        VmInfo vm;
        vminfo_init(&vm);

        vminfo_parse(&vm, req->records[j]); /* FIXME */

        response_open(&res);
        vminfo_send_events(&vm, &checks, res.out);
        if (!req->ctx->conf.events_only) {
            response_begin(&res, req->sr.uuid);
            vminfo_print_json(&vm, res.out);
            response_finish(&res);
        }
        response_close(&res, req->ctx->out);

        vminfo_free(&vm);
    }

    virDomainStatsRecordListFree(req->records);

    return 0;
}
示例#2
0
static void cmd_server_status(struct http_channel *c)
{
    int sessions   = sessions_count();
    int clients    = clients_count();
    int resultsets = resultsets_count();

    response_open(c, "server-status");
    wrbuf_printf(c->wrbuf, "\n  <sessions>%u</sessions>\n", sessions);
    wrbuf_printf(c->wrbuf, "  <clients>%u</clients>\n",   clients);
    /* Only works if yaz has been compiled with enabling of this */
    wrbuf_printf(c->wrbuf, "  <resultsets>%u</resultsets>\n",resultsets);
    print_meminfo(c->wrbuf);

    /* TODO add all sessions status                         */
    /*    http_sessions_t http_sessions = c->http_sessions; */
    /*    struct http_session *p;                           */
    /*
        yaz_mutex_enter(http_sessions->mutex);
        for (p = http_sessions->session_list; p; p = p->next)
        {
            p->activity_counter++;
            wrbuf_puts(c->wrbuf, "<session-status>\n");
            wrbuf_printf(c->wrbuf, "<id>%s</id>\n", p->session_id);
            yaz_mutex_leave(http_sessions->mutex);
            session_status(c, p);
            wrbuf_puts(c->wrbuf, "</session-status>\n");
            yaz_mutex_enter(http_sessions->mutex);
            p->activity_counter--;
        }
        yaz_mutex_leave(http_sessions->mutex);
    */
    response_close(c, "server-status");
    xmalloc_trav(0);
}
示例#3
0
static void cmd_exit(struct http_channel *c)
{
    yaz_log(YLOG_WARN, "exit");

    response_open(c, "exit");
    response_close(c, "exit");
    if (global_parameters.debug_mode)
        http_close_server(c->server);
}
示例#4
0
static void cmd_ping(struct http_channel *c)
{
    struct http_session *s = locate_session(c);
    if (!s)
        return;
    response_open(c, "ping");
    response_close(c, "ping");
    release_session(c, s);
}
示例#5
0
static void cmd_session_status(struct http_channel *c)
{
    struct http_session *s = locate_session(c);
    if (!s)
        return;

    response_open(c, "session-status");
    session_status(c, s);
    response_close(c, "session-status");
    release_session(c, s);
}
示例#6
0
static void cmd_search(struct http_channel *c)
{
    struct http_request *rq = c->request;
    struct http_response *rs = c->response;
    struct http_session *s = locate_session(c);
    const char *query = http_argbyname(rq, "query");
    const char *filter = http_argbyname(rq, "filter");
    const char *maxrecs = http_argbyname(rq, "maxrecs");
    const char *startrecs = http_argbyname(rq, "startrecs");
    const char *limit = http_argbyname(rq, "limit");
    const char *sort = http_argbyname(rq, "sort");
    enum pazpar2_error_code code;
    const char *addinfo = 0;
    struct reclist_sortparms *sp;
    struct conf_service *service = 0;

    if (!s)
        return;

    if (!query)
    {
        error(rs, PAZPAR2_MISSING_PARAMETER, "query");
        release_session(c, s);
        return;
    }
    if (!yaz_utf8_check(query))
    {
        error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query");
        release_session(c, s);
        return;
    }
    service = s->psession->service;
    if (!sort) {
        sort = service->default_sort;
    }
    if (!(sp = reclist_parse_sortparms(c->nmem, sort, s->psession->service)))
    {
        error(c->response, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
        release_session(c, s);
        return;
    }

    code = session_search(s->psession, query, startrecs, maxrecs, filter, limit,
                          &addinfo, sp);
    if (code)
    {
        error(rs, code, addinfo);
        release_session(c, s);
        return;
    }
    response_open(c, "search");
    response_close(c, "search");
    release_session(c, s);
}
示例#7
0
static void cmd_settings(struct http_channel *c)
{
    struct http_response *rs = c->response;
    struct http_request *rq = c->request;
    struct http_session *s = locate_session(c);
    const char *content_type = http_lookup_header(rq->headers, "Content-Type");

    if (!s)
        return;

    if (rq->content_len && content_type &&
            !yaz_strcmp_del("text/xml", content_type, "; "))
    {
        xmlDoc *doc = xmlParseMemory(rq->content_buf, rq->content_len);
        xmlNode *root_n;
        int ret;
        if (!doc)
        {
            error(rs, PAZPAR2_MALFORMED_SETTING, 0);
            release_session(c,s);
            return;
        }
        root_n = xmlDocGetRootElement(doc);
        ret = settings_read_node_x(root_n, s->psession, apply_local_setting);
        xmlFreeDoc(doc);
        if (ret)
        {
            error(rs, PAZPAR2_MALFORMED_SETTING, 0);
            release_session(c,s);
            return;
        }
    }
    if (process_settings(s->psession, rq, rs) < 0)
    {
        release_session(c, s);
        return;
    }
    response_open(c, "settings");
    response_close(c, "settings");
    release_session(c, s);
}
示例#8
0
static void bytarget_response(struct http_channel *c, struct http_session *s, const char *cmd_status) {
    int count, i;
    struct hitsbytarget *ht;
    struct http_request *rq = c->request;
    const char *settings = http_argbyname(rq, "settings");
    int version = get_version(rq);
    ht = get_hitsbytarget(s->psession, &count, c->nmem);
    if (!cmd_status)
        /* Old protocol, always ok */
        response_open(c, "bytarget");
    else {
        /* New protocol, OK or WARNING (...)*/
        response_open_no_status(c, "bytarget");
        wrbuf_printf(c->wrbuf, "<status>%s</status>", cmd_status);
    }

    if (count == 0)
        yaz_log(YLOG_WARN, "Empty bytarget Response. No targets found!");
    for (i = 0; i < count; i++)
    {
        wrbuf_puts(c->wrbuf, "\n<target>");

        wrbuf_puts(c->wrbuf, "<id>");
        wrbuf_xmlputs(c->wrbuf, ht[i].id);
        wrbuf_puts(c->wrbuf, "</id>\n");

        if (ht[i].name && ht[i].name[0])
        {
            wrbuf_puts(c->wrbuf, "<name>");
            wrbuf_xmlputs(c->wrbuf, ht[i].name);
            wrbuf_puts(c->wrbuf, "</name>\n");
        }

        wrbuf_printf(c->wrbuf, "<hits>" ODR_INT_PRINTF "</hits>\n", ht[i].hits);
        wrbuf_printf(c->wrbuf, "<diagnostic>%d</diagnostic>\n", ht[i].diagnostic);
        if (ht[i].diagnostic)
        {
            wrbuf_puts(c->wrbuf, "<message>");
            wrbuf_xmlputs(c->wrbuf, ht[i].message);
            wrbuf_puts(c->wrbuf, "</message>\n");
            wrbuf_puts(c->wrbuf, "<addinfo>");
            if (ht[i].addinfo)
                wrbuf_xmlputs(c->wrbuf, ht[i].addinfo);
            wrbuf_puts(c->wrbuf, "</addinfo>\n");
        }

        wrbuf_printf(c->wrbuf, "<records>%d</records>\n", ht[i].records - ht[i].filtered);
        if (version >= 2) {
            wrbuf_printf(c->wrbuf, "<filtered>%d</filtered>\n", ht[i].filtered);
            wrbuf_printf(c->wrbuf, "<approximation>" ODR_INT_PRINTF "</approximation>\n", ht[i].approximation);
        }
        wrbuf_puts(c->wrbuf, "<state>");
        wrbuf_xmlputs(c->wrbuf, ht[i].state);
        wrbuf_puts(c->wrbuf, "</state>\n");
        if (settings && *settings == '1')
        {
            wrbuf_puts(c->wrbuf, "<settings>\n");
            wrbuf_puts(c->wrbuf, ht[i].settings_xml);
            wrbuf_puts(c->wrbuf, "</settings>\n");
        }
        if (ht[i].suggestions_xml && ht[i].suggestions_xml[0]) {
            wrbuf_puts(c->wrbuf, "<suggestions>");
            wrbuf_puts(c->wrbuf, ht[i].suggestions_xml);
            wrbuf_puts(c->wrbuf, "</suggestions>");
        }
        wrbuf_puts(c->wrbuf, "</target>");
    }
    response_close(c, "bytarget");
}
示例#9
0
static void cmd_init(struct http_channel *c)
{
    struct http_request *r = c->request;
    const char *clear = http_argbyname(r, "clear");
    const char *content_type = http_lookup_header(r->headers, "Content-Type");
    unsigned int sesid;
    struct http_session *s;
    struct http_response *rs = c->response;
    struct conf_service *service = 0; /* no service (yet) */

    if (r->content_len && content_type &&
            !yaz_strcmp_del("text/xml", content_type, "; "))
    {
        xmlDoc *doc = xmlParseMemory(r->content_buf, r->content_len);
        xmlNode *root_n;
        if (!doc)
        {
            error(rs, PAZPAR2_MALFORMED_SETTING, 0);
            return;
        }
        root_n = xmlDocGetRootElement(doc);
        service = service_create(c->server, root_n);
        xmlFreeDoc(doc);
        if (!service)
        {
            error(rs, PAZPAR2_MALFORMED_SETTING, 0);
            return;
        }
    }

    if (!service)
    {
        const char *service_name = http_argbyname(c->request, "service");
        service = locate_service(c->server, service_name);
        if (!service)
        {
            error(rs, PAZPAR2_NO_SERVICE, service_name ? service_name : "unnamed");
            return;
        }
    }
    sesid = make_sessionid();
    s = http_session_create(service, c->http_sessions, sesid);

    yaz_log(c->http_sessions->log_level, "Session init %u ", sesid);
    if (!clear || *clear == '0')
        session_init_databases(s->psession);
    else
        yaz_log(YLOG_LOG, "Session %u init: No databases preloaded", sesid);

    if (process_settings(s->psession, c->request, c->response) < 0)
        return;

    response_open(c, "init");
    wrbuf_printf(c->wrbuf, "<session>%d", sesid);
    if (c->server->server_id)
    {
        wrbuf_puts(c->wrbuf, ".");
        wrbuf_puts(c->wrbuf, c->server->server_id);
    }
    wrbuf_puts(c->wrbuf, "</session>"
               "<protocol>" PAZPAR2_PROTOCOL_VERSION "</protocol>");

    wrbuf_printf(c->wrbuf, "<keepAlive>%d</keepAlive>\n", 1000 * ((s->psession->service->session_timeout >= 20) ?
                 (s->psession->service->session_timeout - 10) : 50));
    response_close(c, "init");
}
示例#10
0
static void show_records(struct http_channel *c, struct http_session *s, int active)
{
    struct http_request *rq = c->request;
    struct http_response *rs = c->response;
    struct record_cluster **rl;
    struct reclist_sortparms *sp;
    const char *start = http_argbyname(rq, "start");
    const char *num = http_argbyname(rq, "num");
    const char *sort = http_argbyname(rq, "sort");
    int version = get_version(rq);

    int startn = 0;
    int numn = 20;
    int total;
    Odr_int total_hits;
    Odr_int approx_hits;
    int i;
    struct conf_service *service = 0;
    if (!s)
        return;

    // We haven't counted clients yet if we're called on a block release
    if (active < 0)
        active = session_active_clients(s->psession);

    if (start)
        startn = atoi(start);
    if (num)
        numn = atoi(num);

    service = s->psession->service;
    if (!sort) {
        sort = service->default_sort;
    }
    if (!(sp = reclist_parse_sortparms(c->nmem, sort, service)))
    {
        error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "sort");
        return;

    }

    rl = show_range_start(s->psession, sp, startn, &numn, &total, &total_hits, &approx_hits);

    response_open(c, "show");
    wrbuf_printf(c->wrbuf, "\n<activeclients>%d</activeclients>\n", active);
    wrbuf_printf(c->wrbuf, "<merged>%d</merged>\n", total);
    wrbuf_printf(c->wrbuf, "<total>" ODR_INT_PRINTF "</total>\n", total_hits);
    if (version >= 2) {
        wrbuf_printf(c->wrbuf, "<approximation>" ODR_INT_PRINTF "</approximation>\n", approx_hits);
    }
    wrbuf_printf(c->wrbuf, "<start>%d</start>\n", startn);
    wrbuf_printf(c->wrbuf, "<num>%d</num>\n", numn);

    for (i = 0; i < numn; i++)
    {
        int ccount;
        struct record *p;
        struct record_cluster *rec = rl[i];
        struct conf_service *service = s->psession->service;

        wrbuf_puts(c->wrbuf, "<hit>\n");
        write_metadata(c->wrbuf, service, rec->metadata, 0, 1);
        for (ccount = 0, p = rl[i]->records; p;  p = p->next, ccount++)
            write_subrecord(p, c->wrbuf, service, 0); // subrecs w/o details
        wrbuf_printf(c->wrbuf, " <count>%d</count>\n", ccount);
        if (strstr(sort, "relevance"))
        {
            wrbuf_printf(c->wrbuf, " <relevance>%d</relevance>\n",
                         rec->relevance_score);
            if (service->rank_debug)
            {
                wrbuf_printf(c->wrbuf, " <relevance_info>\n");
                wrbuf_xmlputs(c->wrbuf, wrbuf_cstr(rec->relevance_explain1));
                wrbuf_xmlputs(c->wrbuf, wrbuf_cstr(rec->relevance_explain2));
                wrbuf_printf(c->wrbuf, " </relevance_info>\n");
            }
        }
        wrbuf_puts(c->wrbuf, " <recid>");
        wrbuf_xmlputs(c->wrbuf, rec->recid);
        wrbuf_puts(c->wrbuf, "</recid>\n");
        wrbuf_puts(c->wrbuf, "</hit>\n");
    }

    show_range_stop(s->psession, rl);

    response_close(c, "show");
}