Project

General

Profile

Actions

Feature #2432

closed

Adding JSON Output support to mod_status (patch)

Added by Xaos over 12 years ago. Updated over 8 years ago.

Status:
Fixed
Priority:
Normal
Category:
mod_status
Target version:
ASK QUESTIONS IN Forums:

Description

I think a much simpler parseable format for the mod_status text based output should be used. It adds the ability to easily monitor remote lighttpd instances. Currently the 5s average traffic values are missing also.
It would be nice if you can add this to the next release :)
Thank you in advance

I've just replaced mod_status_handle_server_status_text in mod_status.c with the following code:

/* JSON TEXT */
static handler_t mod_status_handle_server_status_text(server *srv, connection *con, void *p_d) {
    plugin_data *p = p_d;
    buffer *b;
    double avg;
    time_t ts;
    char buf[32];
    size_t j;

    b = chunkqueue_get_append_buffer(con->write_queue);

    /* output total number of requests */
    buffer_append_string_len(b, CONST_STR_LEN("{\n\tRequests: "));
    avg = p->abs_requests;
    snprintf(buf, sizeof(buf) - 1, "%.0f", avg);
    buffer_append_string(b, buf);
    buffer_append_string_len(b, CONST_STR_LEN(",\n"));

    /* output total traffic out in kbytes */
    buffer_append_string_len(b, CONST_STR_LEN("\tTraffic: "));
    avg = p->abs_traffic_out / 1024;
    snprintf(buf, sizeof(buf) - 1, "%.0f", avg);
    buffer_append_string(b, buf);
    buffer_append_string_len(b, CONST_STR_LEN(",\n"));

    /* output uptime */
    buffer_append_string_len(b, CONST_STR_LEN("\tUptime: "));
    ts = srv->cur_ts - srv->startup_ts;
    buffer_append_long(b, ts);
    buffer_append_string_len(b, CONST_STR_LEN(",\n"));

    /* output busy servers */
    buffer_append_string_len(b, CONST_STR_LEN("\tBusyServers: "));
    buffer_append_long(b, srv->conns->used);
    buffer_append_string_len(b, CONST_STR_LEN(",\n"));

    buffer_append_string_len(b, CONST_STR_LEN("\tIdleServers: "));
    buffer_append_long(b, srv->conns->size - srv->conns->used);
    buffer_append_string_len(b, CONST_STR_LEN(",\n"));

    /* average request last 5s */
       for (j = 0, avg = 0; j < 5; j++) {
           avg += p->mod_5s_requests[j];
       }
       avg /= 5;

       buffer_append_string_len(b, CONST_STR_LEN("\tRequestsAVG5s: "));
       buffer_append_long(b, avg);
       buffer_append_string_len(b, CONST_STR_LEN(",\n"));

    /* average traffic last 5s */
       for (j = 0, avg = 0; j < 5; j++) {
           avg += p->mod_5s_traffic_out[j] / 1024;
       }
       avg /= 5;

       buffer_append_string_len(b, CONST_STR_LEN("\tTrafficAVG5s: "));
       sprintf(buf, "%.2f", avg);
       buffer_append_string(b, buf);
       buffer_append_string_len(b, CONST_STR_LEN("\n}"));

    /* set text/plain output */
    response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/javascript"));

    return 0;
}

It will produce an output like:

{
    Requests: 83,
    Traffic: 23,
    Uptime: 254,
    BusyServers: 6,
    IdleServers: 122,
    RequestsAVG5s: 7,
    TrafficAVG5s: 1.83
}


Files

mod_status.patch (3.75 KB) mod_status.patch Xaos, 2012-08-07 17:57
mod_status-json.patch (3.61 KB) mod_status-json.patch gstrauss, 2016-04-21 09:19

Related issues 1 (0 open1 closed)

Related to Feature #1689: Enhanced server-status page with xml outputMissing FeedbackActions
Actions #1

Updated by spaam over 12 years ago

you know, you need to have double quotes on text strings..

Actions #2

Updated by stbuehler over 12 years ago

  • Target version changed from 1.4.32 to 1.4.x

JSON is NOT simpler to parse than what we have now. And JSON parsers (which of course are easy to use if present) are usually not available in shell scripts.

And we certainly are not going to change the current format, at least you should provide a patch which provides a new mode.
Also what spaam said :)

Actions #3

Updated by Xaos over 12 years ago

stbuehler wrote:

JSON is NOT simpler to parse than what we have now. And JSON parsers (which of course are easy to use if present) are usually not available in shell scripts.

And we certainly are not going to change the current format, at least you should provide a patch which provides a new mode.
Also what spaam said :)

I'm sorry..it was to early in the morning ;) I've added a seperate format, double quotes, as well as jsonp support.
JSON/JSONP is simpler to parse by web-applications written in javascript/php/python, you can fetch the "object" directly and easily build monitoring apps e.g. with html+jquery/mootools. And of course it is not made for "oldschool" shell scripting.

Currently im not sure if the string handling is "corretly" in lighttpd style - i am using C only for lowlevel hardware development, so please be forgiving.

Attached the patch file - i hope it is correctly created..

Usage
http://localhost/server-status?json
http://localhost/server-status?jsonp=callbackFunction

Actions #4

Updated by gstrauss over 8 years ago

Xaos, still interested in this patch?

The jsonp callback needs to be url-decoded from the query string, and then xml-encoded into the response (or checked to contain only alphanumeric or underscore chars)

Actions #5

Updated by gstrauss over 8 years ago

  • Related to Feature #1689: Enhanced server-status page with xml output added
Actions #6

Updated by Xaos over 8 years ago

gstrauss wrote:

Xaos, still interested in this patch?

I'm still interested but currently it has a very low priority for my part.
Generally i'm not familiar with the lighttpd core functions/framework - hence it will took too much time to modify the patch and apply the required url encoding/regex matching.

Actions #7

Updated by gstrauss over 8 years ago

Xaos, please try the attached patch, which is based on yours, but updated to work off 'master' and to protect against cross-site scripting attacks (by checking/validating the jsonp parameter value from the query string). I have not tested this new patch besides compiling it.

Actions #8

Updated by gstrauss over 8 years ago

Xaos, still interested in this patch?

Please give my patch a try. Thanks.

Actions #9

Updated by gstrauss over 8 years ago

  • Status changed from New to Patch Pending
  • Target version changed from 1.4.x to 1.4.40
Actions #10

Updated by gstrauss over 8 years ago

  • Status changed from Patch Pending to Fixed
Actions

Also available in: Atom