Пример #1
0
int main(void) {

    statsd_t *link, *link2;

    link = statsd_init("127.0.0.1", 8125);
    assert(statsd_count(link, "count1", 123, 1.0) == 0);

    link2 = statsd_init_with_namespace("127.0.0.1", 8125, "mynamespace");
    assert(statsd_count(link, "count2", 125, 1.0) == 0);
    assert(statsd_gauge(link, "speed", 10) == 0);
    assert(statsd_timing(link2, "request", 2400) == 0);

    sleep(1);

    assert(statsd_inc(link, "count1", 1.0) == 0);
    assert(statsd_dec(link2, "count2", 1.0) == 0);

    int i;

    for (i=0; i<10; i++) {
        assert(statsd_count(link2, "count3", i, 0.8) == 0);
    }

    statsd_finalize(link);
    statsd_finalize(link2);

    return 0;
}
Пример #2
0
/*
 *  Called when the LFS module is deactivated.
 */
int lfs_deactivate(void)
{
    globus_extension_registry_remove(GLOBUS_GFS_DSI_REGISTRY, "lfs");
    STATSD_COUNT("deactivate",1);
    if (lfs_statsd_link != NULL) {
        statsd_finalize(lfs_statsd_link);
        lfs_statsd_link = NULL;
    }
    return 0;
}
Пример #3
0
int main(void)
{
    statsd_init("127.0.0.1", 8125);
    statsd_count("count1", 123, 1.0);
    statsd_init_with_namespace("127.0.0.1", 8125, "mynamespace");
    statsd_count("count2", 125, 1.0);
    statsd_gauge("speed", 10);
    statsd_timing("request", 2400);
    sleep(1);
    statsd_inc("count1", 1.0);
    statsd_dec("count2", 1.0);
    int i;
    for(i=0; i<10; i++) {
        statsd_count("count3", i, 0.8);
    }
    statsd_finalize();

    return 0;
}
int main(int argc, char *argv[]) {


    char *metric_name = NULL;
    int c;

    char *statsd_host = STATSD_DEFAULT_HOST;
    int statsd_port = STATSD_DEFAULT_PORT;

    char *gearmand_host = GEARMAND_DEFAULT_HOST;
    int gearmand_port = GEARMAND_DEFAULT_PORT;

    int debug = 0;
    int foreground = 0;
    int polling_interval = DEFAULT_POLLING_INTERVAL;


    while ((c = getopt (argc, argv, "e:dvifH:t::P::h:p::N:")) != -1) {
        switch (c) {
            case '?':
                showHelp();
                return 0;
            break;
            case 'N':
                metric_name = malloc(strlen(optarg));
                strcpy(metric_name, optarg);
            break;
            case 'h': 
                statsd_host = malloc(strlen(optarg));
                strcpy(statsd_host, optarg);
            break;
            case 'p':
                statsd_port = atoi(optarg);
            break;
            case 'H':
                gearmand_host = malloc(strlen(optarg));
                strcpy(gearmand_host, optarg);
            break;
            case 'P':
                gearmand_port = atoi(optarg);
            break;
            case 't':
                polling_interval = atoi(optarg);
            break;            
            case 'f':
                foreground = 1;
            break;       
            case 'd':
                debug = 1;
            break;
            default:
                showHelp();
                return 1;
            break;
        }
    }

    if (metric_name == NULL) {
        fprintf(stderr, "%s: missing -N option\n", argv[0]);
        showHelp();
        return 1;
    }

    if (debug) {
        setlogmask(LOG_UPTO(LOG_INFO));
        openlog("statsd-gearmand-monitor",  LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);
        syslog(LOG_INFO, "statsd host: %s:%d", statsd_host, statsd_port);
        syslog(LOG_INFO, "metric name: %s", metric_name);
        syslog(LOG_INFO, "polling interval: %d secs", polling_interval);
        syslog(LOG_INFO, "gearmand host: %s:%d", gearmand_host, gearmand_port);
    }


    signal(SIGHUP, SIG_IGN);
    signal(SIGPIPE, SIG_IGN);
    signal(SIGCHLD, SIG_IGN);
    signal(SIGTERM, sigterm);

    int sockfd = 0, n = 0;
    char recvBuff[1024];
    struct sockaddr_in gearmand_serv_addr; 


    memset(recvBuff, '0',sizeof(recvBuff));
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("error: could not create socket \n");
        return 1;
    } 


    /* resolve hostname */
    struct hostent * he;
    if ((he = gethostbyname(gearmand_host)) == NULL) {
        perror("error: could not resolve host\n");
        return 1;
    }

    memset(&gearmand_serv_addr, '0', sizeof(gearmand_serv_addr)); 

    gearmand_serv_addr.sin_family = AF_INET;
    gearmand_serv_addr.sin_port = htons(gearmand_port); 

    if(memcpy(&gearmand_serv_addr.sin_addr, he->h_addr_list[0], he->h_length) <= 0) {
        perror("error: memcpy error occured\n");
        return 1;
    }

    if (!foreground) {
        if (debug) {
            syslog(LOG_INFO, "sending process to background");
        }
        daemon(0, 0);
    }

    statsd_link *_statsd_link;
    _statsd_link = statsd_init_with_namespace(statsd_host, statsd_port, metric_name);


    while(running) {

        if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
            perror("error : could not create socket \n");
            return 1;
        } 

        if( connect(sockfd, (struct sockaddr *)&gearmand_serv_addr, sizeof(gearmand_serv_addr)) < 0) {
            perror("error: connect Failed to gearmand \n");
            return 1;
        }

        write(sockfd, "status\n", strlen("status\n")); 

        while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0) {
            recvBuff[n] = 0;

            if (recvBuff[n - 2] == '.' && recvBuff[n - 1] == '\n') {
                recvBuff[n - 2] = 0;
                break;
            }
        }

        close(sockfd);

        char * pch;

        pch = strtok (recvBuff, "\n");
        while (pch != NULL) {
            char function_name[1024];
            char * _metric_name = NULL;

            int queued, running, connected;

            sscanf(pch, "%s\t%d\t%d\t%d", function_name, &queued, &running, &connected);

            _metric_name = _forge_metric_name(function_name, "connected");
            statsd_gauge(_statsd_link, _metric_name, connected);
            free(_metric_name);

            _metric_name = _forge_metric_name(function_name, "queued");
            statsd_gauge(_statsd_link, _metric_name , queued);
            free(_metric_name);

            _metric_name = _forge_metric_name(function_name, "running");
            statsd_gauge(_statsd_link, _metric_name, running);
            free(_metric_name);


            if (debug) {
                 syslog(LOG_INFO, "function %s has a total of %d queued jobs - %d workers running - %d workers connected\n", function_name, queued, running, connected);
            }

            pch = strtok (NULL, "\n");
        }

        sleep(polling_interval);
    }

    statsd_finalize(_statsd_link);

    return 0;
}