Example #1
0
int
read_swift_health()
{
    char msg[LEN_512];
    char buf[1024*1024];
    sprintf(msg,
            "GET /status?SERVICE=swift HTTP/1.1\r\nConnection: close\r\n"
            "Host: cdn.hc.org\r\n\r\n");

    int len, conn, bytesWritten, fsize = 0;
    int port = mgrport - 1;

    if (my_swift_net_connect(HOSTNAME, port, &conn, "tcp") != 0) {
        close(conn);
        return 0;
    }

    int flags;

    /* set socket fd noblock */
    if ((flags = fcntl(conn, F_GETFL, 0)) < 0) {
        close(conn);
        return 0;
    }

    if (fcntl(conn, F_SETFL, flags & ~O_NONBLOCK) < 0) {
        close(conn);
        return 0;
    }

    struct timeval timeout = {10, 0};

    setsockopt(conn, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(struct timeval));
    setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval));

    bytesWritten = mywrite_swift(conn, msg, strlen(msg));
    if (bytesWritten < 0) {
        close(conn);
        return 0;

    } else if (bytesWritten != strlen(msg)) {
        close(conn);
        return 0;
    }

    while ((len = myread_swift(conn, buf + fsize, sizeof(buf) - fsize) - 1) > 0) {
        fsize += len;
    }

    buf[fsize] = '\0';

    char *p = strstr(buf, "\r\n");
    if (p && memcmp(buf, "HTTP/1.1 200 OK", 15) == 0) {
        return 1;
    }

    return 0;
}
Example #2
0
int
read_swift_stat(char *cmd)
{
    char msg[LEN_512];
    char buf[LEN_4096];
    sprintf(msg,
            "GET cache_object://localhost/%s "
            "HTTP/1.1\r\n"
            "Host: localhost\r\n"
            "Accept:*/*\r\n"
            "Connection: close\r\n\r\n",
            cmd);

    int len, conn, bytesWritten, fsize = 0;

    if (my_swift_net_connect(HOSTNAME, mgrport, &conn, "tcp") != 0) {
        close(conn);
        return -1;
    }

    int flags;

    /* set socket fd noblock */
    if ((flags = fcntl(conn, F_GETFL, 0)) < 0) {
        close(conn);
        return -1;
    }

    if (fcntl(conn, F_SETFL, flags & ~O_NONBLOCK) < 0) {
        close(conn);
        return -1;
    }

    struct timeval timeout = {10, 0};

    setsockopt(conn, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(struct timeval));
    setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval));

    bytesWritten = mywrite_swift(conn, msg, strlen(msg));
    if (bytesWritten < 0) {
        close(conn);
        return -2;

    } else if (bytesWritten != strlen(msg)) {
        close(conn);
        return -3;
    }

    while ((len = myread_swift(conn, buf, sizeof(buf))) > 0) {
        fsize += len;
    }

    /* read error */
    if (fsize < 100) {
        close(conn);
        return -1;
    }

    if (parse_swift_info(buf) < 0) {
        close(conn);
        return -1;
    }

    close(conn);
    return 0;
}