stats.c 32 KB
Newer Older
1 2 3 4 5
/* Icecast
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
6
 * Copyright 2000-2004, Jack Moffitt <jack@xiph.org,
7 8 9 10
 *                      Michael Smith <msmith@xiph.org>,
 *                      oddsock <oddsock@xiph.org>,
 *                      Karl Heyes <karl@xiph.org>
 *                      and others (see AUTHORS for details).
11
 * Copyright 2012-2014, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
12 13
 */

14 15 16 17
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Jack Moffitt's avatar
Jack Moffitt committed
18 19 20 21
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
22
#include <ctype.h>
Jack Moffitt's avatar
Jack Moffitt committed
23

Michael Smith's avatar
Michael Smith committed
24 25 26
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
Jack Moffitt's avatar
Jack Moffitt committed
27

Marvin Scholz's avatar
Marvin Scholz committed
28 29 30 31
#include "common/thread/thread.h"
#include "common/avl/avl.h"
#include "common/httpp/httpp.h"
#include "common/net/sock.h"
Jack Moffitt's avatar
Jack Moffitt committed
32 33 34

#include "connection.h"

35
#include "source.h"
Jack Moffitt's avatar
Jack Moffitt committed
36 37 38
#include "global.h"
#include "refbuf.h"
#include "client.h"
39
#include "admin.h"
Jack Moffitt's avatar
Jack Moffitt committed
40
#include "stats.h"
41
#include "xslt.h"
42
#include "util.h"
43
#include "auth.h"
44 45
#define CATMODULE "stats"
#include "logging.h"
Jack Moffitt's avatar
Jack Moffitt committed
46

47
#ifdef _WIN32
48
#define atoll _atoi64
49
#define vsnprintf _vsnprintf
50
#define snprintf _snprintf
51
#endif
Jack Moffitt's avatar
Jack Moffitt committed
52

53 54 55 56
#define STATS_EVENT_SET     0
#define STATS_EVENT_INC     1
#define STATS_EVENT_DEC     2
#define STATS_EVENT_ADD     3
Karl Heyes's avatar
Karl Heyes committed
57 58 59
#define STATS_EVENT_SUB     4
#define STATS_EVENT_REMOVE  5
#define STATS_EVENT_HIDDEN  6
60

61 62 63 64 65 66 67 68
typedef struct _event_queue_tag
{
    volatile stats_event_t *head;
    volatile stats_event_t **tail;
} event_queue_t;

#define event_queue_init(qp)    { (qp)->head = NULL; (qp)->tail = &(qp)->head; }

Jack Moffitt's avatar
Jack Moffitt committed
69 70
typedef struct _event_listener_tag
{
71 72
    event_queue_t queue;
    mutex_t mutex;
Jack Moffitt's avatar
Jack Moffitt committed
73

74
    struct _event_listener_tag *next;
Jack Moffitt's avatar
Jack Moffitt committed
75 76
} event_listener_t;

77
static volatile int _stats_running = 0;
78
static thread_type *_stats_thread_id;
79
static volatile int _stats_threads = 0;
Jack Moffitt's avatar
Jack Moffitt committed
80

81 82
static stats_t _stats;
static mutex_t _stats_mutex;
Jack Moffitt's avatar
Jack Moffitt committed
83

84
static event_queue_t _global_event_queue;
Jack Moffitt's avatar
Jack Moffitt committed
85 86
mutex_t _global_event_mutex;

87
static volatile event_listener_t *_event_listeners;
Jack Moffitt's avatar
Jack Moffitt committed
88 89 90 91 92 93 94


static void *_stats_thread(void *arg);
static int _compare_stats(void *a, void *b, void *arg);
static int _compare_source_stats(void *a, void *b, void *arg);
static int _free_stats(void *key);
static int _free_source_stats(void *key);
95
static void _add_event_to_queue(stats_event_t *event, event_queue_t *queue);
96 97
static stats_node_t *_find_node(avl_tree *tree, const char *name);
static stats_source_t *_find_source(avl_tree *tree, const char *source);
Jack Moffitt's avatar
Jack Moffitt committed
98
static void _free_event(stats_event_t *event);
99
static stats_event_t *_get_event_from_queue(event_queue_t *queue);
100
static void __add_metadata(xmlNodePtr node, const char *tag);
Jack Moffitt's avatar
Jack Moffitt committed
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

/* simple helper function for creating an event */
static stats_event_t *build_event (const char *source, const char *name, const char *value)
{
    stats_event_t *event;

    event = (stats_event_t *)calloc(1, sizeof(stats_event_t));
    if (event)
    {
        if (source)
            event->source = (char *)strdup(source);
        if (name)
            event->name = (char *)strdup(name);
        if (value)
            event->value = (char *)strdup(value);
        else
            event->action = STATS_EVENT_REMOVE;
    }
    return event;
}

static void queue_global_event (stats_event_t *event)
{
    thread_mutex_lock(&_global_event_mutex);
126
    _add_event_to_queue (event, &_global_event_queue);
127 128 129
    thread_mutex_unlock(&_global_event_mutex);
}

130
void stats_initialize(void)
Jack Moffitt's avatar
Jack Moffitt committed
131
{
132
    _event_listeners = NULL;
133

134 135 136
    /* set up global struct */
    _stats.global_tree = avl_tree_new(_compare_stats, NULL);
    _stats.source_tree = avl_tree_new(_compare_source_stats, NULL);
Jack Moffitt's avatar
Jack Moffitt committed
137

138 139
    /* set up global mutex */
    thread_mutex_create(&_stats_mutex);
Jack Moffitt's avatar
Jack Moffitt committed
140

141
    /* set up stats queues */
142
    event_queue_init(&_global_event_queue);
143
    thread_mutex_create(&_global_event_mutex);
Jack Moffitt's avatar
Jack Moffitt committed
144

145 146 147
    /* fire off the stats thread */
    _stats_running = 1;
    _stats_thread_id = thread_create("Stats Thread", _stats_thread, NULL, THREAD_ATTACHED);
Jack Moffitt's avatar
Jack Moffitt committed
148 149
}

150
void stats_shutdown(void)
Jack Moffitt's avatar
Jack Moffitt committed
151
{
152
    int n;
Jack Moffitt's avatar
Jack Moffitt committed
153

154
    if (!_stats_running) /* We can't shutdown if we're not running. */
155 156
        return;

157 158 159
    /* wait for thread to exit */
    _stats_running = 0;
    thread_join(_stats_thread_id);
Jack Moffitt's avatar
Jack Moffitt committed
160

161 162 163 164 165 166 167
    /* wait for other threads to shut down */
    do {
        thread_sleep(300000);
        thread_mutex_lock(&_stats_mutex);
        n = _stats_threads;
        thread_mutex_unlock(&_stats_mutex);
    } while (n > 0);
168
    ICECAST_LOG_INFO("stats thread finished");
169

170
    /* free the queues */
Jack Moffitt's avatar
Jack Moffitt committed
171

172 173
    /* destroy the queue mutexes */
    thread_mutex_destroy(&_global_event_mutex);
Jack Moffitt's avatar
Jack Moffitt committed
174

175 176 177
    thread_mutex_destroy(&_stats_mutex);
    avl_tree_free(_stats.source_tree, _free_source_stats);
    avl_tree_free(_stats.global_tree, _free_stats);
178

179 180 181 182
    while (1)
    {
        stats_event_t *event = _get_event_from_queue (&_global_event_queue);
        if (event == NULL) break;
183 184 185 186
        if(event->source)
            free(event->source);
        if(event->value)
            free(event->value);
187 188
        if(event->name)
            free(event->name);
189 190
        free(event);
    }
Jack Moffitt's avatar
Jack Moffitt committed
191 192
}

193
stats_t *stats_get_stats(void)
Jack Moffitt's avatar
Jack Moffitt committed
194
{
195
    /* lock global stats
196

197
     copy stats
Jack Moffitt's avatar
Jack Moffitt committed
198

199
     unlock global stats
Jack Moffitt's avatar
Jack Moffitt committed
200

201
     return copied stats */
Jack Moffitt's avatar
Jack Moffitt committed
202

203
    return NULL;
Jack Moffitt's avatar
Jack Moffitt committed
204 205
}

206 207
/* simple name=tag stat create/update */
void stats_event(const char *source, const char *name, const char *value)
Jack Moffitt's avatar
Jack Moffitt committed
208
{
209 210
    stats_event_t *event;

211 212
    if (value && xmlCheckUTF8 ((unsigned char *)value) == 0)
    {
213
        ICECAST_LOG_WARN("seen non-UTF8 data, probably incorrect metadata (%s, %s)", name, value);
214 215
        return;
    }
216
    event = build_event(source, name, value);
217
    if (event)
218
        queue_global_event(event);
Jack Moffitt's avatar
Jack Moffitt committed
219 220
}

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

/* wrapper for stats_event, this takes a charset to convert from */
void stats_event_conv(const char *mount, const char *name, const char *value, const char *charset)
{
    const char *metadata = value;
    xmlBufferPtr conv = xmlBufferCreate ();

    if (charset)
    {
        xmlCharEncodingHandlerPtr handle = xmlFindCharEncodingHandler (charset);

        if (handle)
        {
            xmlBufferPtr raw = xmlBufferCreate ();
            xmlBufferAdd (raw, (const xmlChar *)value, strlen (value));
            if (xmlCharEncInFunc (handle, conv, raw) > 0)
                metadata = (char *)xmlBufferContent (conv);
            xmlBufferFree (raw);
            xmlCharEncCloseFunc (handle);
        }
        else
242
            ICECAST_LOG_WARN("No charset found for \"%s\"", charset);
243 244 245 246 247 248
    }

    stats_event (mount, name, metadata);
    xmlBufferFree (conv);
}

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
/* make stat hidden (non-zero). name can be NULL if it applies to a whole
 * source stats tree. */
void stats_event_hidden (const char *source, const char *name, int hidden)
{
    stats_event_t *event;
    const char *str = NULL;

    if (hidden)
        str = "";
    event = build_event (source, name, str);
    if (event)
    {
        event->action = STATS_EVENT_HIDDEN;
        queue_global_event (event);
    }
}
265 266 267

/* printf style formatting for stat create/update */
void stats_event_args(const char *source, char *name, char *format, ...)
Jack Moffitt's avatar
Jack Moffitt committed
268
{
269 270
    char buf[1024];
    va_list val;
271 272 273 274
    int ret;

    if (name == NULL)
        return;
275
    va_start(val, format);
276
    ret = vsnprintf(buf, sizeof(buf), format, val);
277
    va_end(val);
Jack Moffitt's avatar
Jack Moffitt committed
278

279 280
    if (ret < 0 || (unsigned int)ret >= sizeof (buf))
    {
281
        ICECAST_LOG_WARN("problem with formatting %s stat %s",
282 283 284
                source==NULL ? "global" : source, name);
        return;
    }
285
    stats_event(source, name, buf);
Jack Moffitt's avatar
Jack Moffitt committed
286 287
}

288
static char *_get_stats(const char *source, const char *name)
Jack Moffitt's avatar
Jack Moffitt committed
289
{
290 291 292
    stats_node_t *stats = NULL;
    stats_source_t *src = NULL;
    char *value = NULL;
Jack Moffitt's avatar
Jack Moffitt committed
293

294
    thread_mutex_lock(&_stats_mutex);
Jack Moffitt's avatar
Jack Moffitt committed
295

296 297 298 299 300 301 302 303
    if (source == NULL) {
        stats = _find_node(_stats.global_tree, name);
    } else {
        src = _find_source(_stats.source_tree, source);
        if (src) {
            stats = _find_node(src->stats_tree, name);
        }
    }
Jack Moffitt's avatar
Jack Moffitt committed
304

305
    if (stats) value = (char *)strdup(stats->value);
Jack Moffitt's avatar
Jack Moffitt committed
306

307
    thread_mutex_unlock(&_stats_mutex);
Jack Moffitt's avatar
Jack Moffitt committed
308

309
    return value;
Jack Moffitt's avatar
Jack Moffitt committed
310 311
}

312
char *stats_get_value(const char *source, const char *name)
313
{
314
    return(_get_stats(source, name));
315
}
316 317 318

/* increase the value in the provided stat by 1 */
void stats_event_inc(const char *source, const char *name)
Jack Moffitt's avatar
Jack Moffitt committed
319
{
320
    stats_event_t *event = build_event (source, name, NULL);
321
    /* ICECAST_LOG_DEBUG("%s on %s", name, source==NULL?"global":source); */
322 323 324 325
    if (event)
    {
        event->action = STATS_EVENT_INC;
        queue_global_event (event);
326
    }
Jack Moffitt's avatar
Jack Moffitt committed
327 328
}

329
void stats_event_add(const char *source, const char *name, unsigned long value)
Jack Moffitt's avatar
Jack Moffitt committed
330
{
331
    stats_event_t *event = build_event (source, name, NULL);
332
    /* ICECAST_LOG_DEBUG("%s on %s", name, source==NULL?"global":source); */
333 334 335
    if (event)
    {
        event->value = malloc (16);
336
        snprintf(event->value, 16, "%lu", value);
337 338
        event->action = STATS_EVENT_ADD;
        queue_global_event (event);
339
    }
Jack Moffitt's avatar
Jack Moffitt committed
340 341
}

Karl Heyes's avatar
Karl Heyes committed
342 343 344 345 346 347
void stats_event_sub(const char *source, const char *name, unsigned long value)
{
    stats_event_t *event = build_event (source, name, NULL);
    if (event)
    {
        event->value = malloc (16);
348
        snprintf(event->value, 16, "%lu", value);
Karl Heyes's avatar
Karl Heyes committed
349 350 351 352 353
        event->action = STATS_EVENT_SUB;
        queue_global_event (event);
    }
}

354 355
/* decrease the value in the provided stat by 1 */
void stats_event_dec(const char *source, const char *name)
Jack Moffitt's avatar
Jack Moffitt committed
356
{
357
    /* ICECAST_LOG_DEBUG("%s on %s", name, source==NULL?"global":source); */
358 359 360 361 362
    stats_event_t *event = build_event (source, name, NULL);
    if (event)
    {
        event->action = STATS_EVENT_DEC;
        queue_global_event (event);
363
    }
Jack Moffitt's avatar
Jack Moffitt committed
364 365 366 367 368
}

/* note: you must call this function only when you have exclusive access
** to the avl_tree
*/
369
static stats_node_t *_find_node(avl_tree *stats_tree, const char *name)
Jack Moffitt's avatar
Jack Moffitt committed
370
{
371 372 373 374 375 376
    stats_node_t *stats;
    avl_node *node;
    int cmp;

    /* get the root node */
    node = stats_tree->root->right;
377

378
    while (node) {
379
        stats = (stats_node_t *) node->key;
380
        cmp = strcmp(name, stats->name);
381
        if (cmp < 0)
382 383 384 385 386 387
            node = node->left;
        else if (cmp > 0)
            node = node->right;
        else
            return stats;
    }
388

389 390
    /* didn't find it */
    return NULL;
Jack Moffitt's avatar
Jack Moffitt committed
391 392 393 394 395
}

/* note: you must call this function only when you have exclusive access
** to the avl_tree
*/
396
static stats_source_t *_find_source(avl_tree *source_tree, const char *source)
Jack Moffitt's avatar
Jack Moffitt committed
397
{
398 399 400 401 402 403 404
    stats_source_t *stats;
    avl_node *node;
    int cmp;

    /* get the root node */
    node = source_tree->root->right;
    while (node) {
405
        stats = (stats_source_t *) node->key;
406 407 408 409 410 411 412 413
        cmp = strcmp(source, stats->source);
        if (cmp < 0)
            node = node->left;
        else if (cmp > 0)
            node = node->right;
        else
            return stats;
    }
Jack Moffitt's avatar
Jack Moffitt committed
414

415 416
    /* didn't find it */
    return NULL;
Jack Moffitt's avatar
Jack Moffitt committed
417 418 419 420
}

static stats_event_t *_copy_event(stats_event_t *event)
{
421
    stats_event_t *copy = (stats_event_t *)calloc(1, sizeof(stats_event_t));
422
    if (event->source)
423 424 425
        copy->source = (char *)strdup(event->source);
    else
        copy->source = NULL;
426 427
    if (event->name)
        copy->name = (char *)strdup(event->name);
428 429 430 431
    if (event->value)
        copy->value = (char *)strdup(event->value);
    else
        copy->value = NULL;
432
    copy->hidden = event->hidden;
433
    copy->next = NULL;
Jack Moffitt's avatar
Jack Moffitt committed
434

435
    return copy;
Jack Moffitt's avatar
Jack Moffitt committed
436 437
}

438 439

/* helper to apply specialised changes to a stats node */
440
static void modify_node_event(stats_node_t *node, stats_event_t *event)
441 442 443
{
    char *str;

444 445 446 447 448 449 450 451
    if (event->action == STATS_EVENT_HIDDEN)
    {
        if (event->value)
            node->hidden = 1;
        else
            node->hidden = 0;
        return;
    }
452 453
    if (event->action != STATS_EVENT_SET)
    {
454
        int64_t value = 0;
455 456 457 458 459 460 461 462 463 464 465 466

        switch (event->action)
        {
            case STATS_EVENT_INC:
                value = atoi (node->value)+1;
                break;
            case STATS_EVENT_DEC:
                value = atoi (node->value)-1;
                break;
            case STATS_EVENT_ADD:
                value = atoi (node->value)+atoi (event->value);
                break;
467 468 469
            case STATS_EVENT_SUB:
                value = atoll (node->value) - atoll (event->value);
                break;
470
            default:
471
                ICECAST_LOG_WARN("unhandled event (%d) for %s", event->action, event->source);
472 473 474
                break;
        }
        str = malloc (16);
475
        snprintf (str, 16, "%" PRId64, value);
476 477
        if (event->value == NULL)
            event->value = strdup (str);
478 479 480 481 482
    }
    else
        str = (char *)strdup (event->value);
    free (node->value);
    node->value = str;
483
    if (event->source)
484
        ICECAST_LOG_DEBUG("update \"%s\" %s (%s)", event->source, node->name, node->value);
485
    else
486
        ICECAST_LOG_DEBUG("update global %s (%s)", node->name, node->value);
487 488 489 490 491 492 493
}


static void process_global_event (stats_event_t *event)
{
    stats_node_t *node;

494
    /* ICECAST_LOG_DEBUG("global event %s %s %d", event->name, event->value, event->action); */
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
    if (event->action == STATS_EVENT_REMOVE)
    {
        /* we're deleting */
        node = _find_node(_stats.global_tree, event->name);
        if (node != NULL)
            avl_delete(_stats.global_tree, (void *)node, _free_stats);
        return;
    }
    node = _find_node(_stats.global_tree, event->name);
    if (node)
    {
        modify_node_event (node, event);
    }
    else
    {
        /* add node */
        node = (stats_node_t *)calloc(1, sizeof(stats_node_t));
        node->name = (char *)strdup(event->name);
        node->value = (char *)strdup(event->value);

        avl_insert(_stats.global_tree, (void *)node);
    }
}


static void process_source_event (stats_event_t *event)
{
    stats_source_t *snode = _find_source(_stats.source_tree, event->source);
    if (snode == NULL)
    {
        if (event->action == STATS_EVENT_REMOVE)
            return;
        snode = (stats_source_t *)calloc(1,sizeof(stats_source_t));
        if (snode == NULL)
            return;
530
        ICECAST_LOG_DEBUG("new source stat %s", event->source);
531 532
        snode->source = (char *)strdup(event->source);
        snode->stats_tree = avl_tree_new(_compare_stats, NULL);
533 534 535 536
        if (event->action == STATS_EVENT_HIDDEN)
            snode->hidden = 1;
        else
            snode->hidden = 0;
537

538
        avl_insert(_stats.source_tree, (void *) snode);
539 540 541 542 543 544 545 546 547 548 549
    }
    if (event->name)
    {
        stats_node_t *node = _find_node(snode->stats_tree, event->name);
        if (node == NULL)
        {
            if (event->action == STATS_EVENT_REMOVE)
                return;
            /* adding node */
            if (event->value)
            {
550
                ICECAST_LOG_DEBUG("new node %s (%s)", event->name, event->value);
551 552 553
                node = (stats_node_t *)calloc(1,sizeof(stats_node_t));
                node->name = (char *)strdup(event->name);
                node->value = (char *)strdup(event->value);
554
                node->hidden = snode->hidden;
555 556 557 558 559 560 561

                avl_insert(snode->stats_tree, (void *)node);
            }
            return;
        }
        if (event->action == STATS_EVENT_REMOVE)
        {
562
            ICECAST_LOG_DEBUG("delete node %s", event->name);
563 564 565 566 567 568
            avl_delete(snode->stats_tree, (void *)node, _free_stats);
            return;
        }
        modify_node_event (node, event);
        return;
    }
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
    if (event->action == STATS_EVENT_HIDDEN)
    {
        avl_node *node = avl_get_first (snode->stats_tree);

        if (event->value)
            snode->hidden = 1;
        else
            snode->hidden = 0;
        while (node)
        {
            stats_node_t *stats = (stats_node_t*)node->key;
            stats->hidden = snode->hidden;
            node = avl_get_next (node);
        }
        return;
    }
585 586
    if (event->action == STATS_EVENT_REMOVE)
    {
587
        ICECAST_LOG_DEBUG("delete source node %s", event->source);
588 589 590 591
        avl_delete(_stats.source_tree, (void *)snode, _free_source_stats);
    }
}

592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
/* NOTE: implicit %z is added to format string. */
static inline void __format_time(char * buffer, size_t len, const char * format) {
    time_t now = time(NULL);
    struct tm local;
    char tzbuffer[32];
    char timebuffer[128];
#ifdef _WIN32
    struct tm *thetime;
    int time_days, time_hours, time_tz;
    int tempnum1, tempnum2;
    char sign;
#endif

    localtime_r (&now, &local);
#ifndef _WIN32
    strftime (tzbuffer, sizeof(tzbuffer), "%z", &local);
#else
    thetime = gmtime (&now);
    time_days = local.tm_yday - thetime->tm_yday;

    if (time_days < -1) {
        tempnum1 = 24;
    } else {
        tempnum1 = 1;
    }

    if (tempnum1 < time_days) {
        tempnum2 = -24;
    } else {
        tempnum2 = time_days*24;
    }

    time_hours = (tempnum2 + local.tm_hour - thetime->tm_hour);
    time_tz = time_hours * 60 + local.tm_min - thetime->tm_min;

    if (time_tz < 0) {
        sign = '-';
        time_tz = -time_tz;
    } else {
        sign = '+';
    }

    snprintf(tzbuffer, sizeof(tzbuffer), "%c%.2d%.2d", sign, time_tz / 60, time_tz % 60);
#endif
    strftime (timebuffer, sizeof(timebuffer), format, &local);

    snprintf(buffer, len, "%s%s", timebuffer, tzbuffer);
}
640

Karl Heyes's avatar
Karl Heyes committed
641 642 643 644
void stats_event_time (const char *mount, const char *name)
{
    char buffer[100];

645
    __format_time(buffer, sizeof(buffer), "%a, %d %b %Y %H:%M:%S ");
Karl Heyes's avatar
Karl Heyes committed
646 647 648 649
    stats_event (mount, name, buffer);
}


650 651 652 653
void stats_event_time_iso8601 (const char *mount, const char *name)
{
    char buffer[100];

654
    __format_time(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S");
655 656 657 658
    stats_event (mount, name, buffer);
}


Karl Heyes's avatar
Karl Heyes committed
659 660
void stats_global (ice_config_t *config)
{
661
    stats_event (NULL, "server_id", config->server_id);
Karl Heyes's avatar
Karl Heyes committed
662 663 664 665 666 667
    stats_event (NULL, "host", config->hostname);
    stats_event (NULL, "location", config->location);
    stats_event (NULL, "admin", config->admin);
}


Jack Moffitt's avatar
Jack Moffitt committed
668 669
static void *_stats_thread(void *arg)
{
670 671 672 673
    stats_event_t *event;
    stats_event_t *copy;
    event_listener_t *listener;

674 675
    (void)arg;

Karl Heyes's avatar
Karl Heyes committed
676
    stats_event_time (NULL, "server_start");
677
    stats_event_time_iso8601 (NULL, "server_start_iso8601");
678 679 680 681 682 683

    /* global currently active stats */
    stats_event (NULL, "clients", "0");
    stats_event (NULL, "connections", "0");
    stats_event (NULL, "sources", "0");
    stats_event (NULL, "stats", "0");
Karl Heyes's avatar
Karl Heyes committed
684
    stats_event (NULL, "listeners", "0");
685 686 687 688 689 690 691

    /* global accumulating stats */
    stats_event (NULL, "client_connections", "0");
    stats_event (NULL, "source_client_connections", "0");
    stats_event (NULL, "source_relay_connections", "0");
    stats_event (NULL, "source_total_connections", "0");
    stats_event (NULL, "stats_connections", "0");
692
    stats_event (NULL, "listener_connections", "0");
693

694
    ICECAST_LOG_INFO("stats thread started");
695
    while (_stats_running) {
696
        thread_mutex_lock(&_global_event_mutex);
697
        if (_global_event_queue.head != NULL) {
698
            /* grab the next event from the queue */
699
            event = _get_event_from_queue (&_global_event_queue);
700 701
            thread_mutex_unlock(&_global_event_mutex);

702 703
            if (event == NULL)
                continue;
704 705 706
            event->next = NULL;

            thread_mutex_lock(&_stats_mutex);
707 708 709 710 711 712

            /* check if we are dealing with a global or source event */
            if (event->source == NULL)
                process_global_event (event);
            else
                process_source_event (event);
713

714 715
            /* now we have an event that's been processed into the running stats */
            /* this event should get copied to event listeners' queues */
716
            listener = (event_listener_t *)_event_listeners;
717 718
            while (listener) {
                copy = _copy_event(event);
719 720 721
                thread_mutex_lock (&listener->mutex);
                _add_event_to_queue (copy, &listener->queue);
                thread_mutex_unlock (&listener->mutex);
722 723 724 725 726 727 728 729

                listener = listener->next;
            }

            /* now we need to destroy the event */
            _free_event(event);

            thread_mutex_unlock(&_stats_mutex);
730
            continue;
731
        }
732 733 734 735
        else
        {
            thread_mutex_unlock(&_global_event_mutex);
        }
736

737
        thread_sleep(300000);
738 739 740
    }

    return NULL;
Jack Moffitt's avatar
Jack Moffitt committed
741 742
}

743
/* you must have the _stats_mutex locked here */
744
static void _unregister_listener(event_listener_t *listener)
745 746 747 748 749
{
    event_listener_t **prev = (event_listener_t **)&_event_listeners,
                     *current = *prev;
    while (current)
    {
750
        if (current == listener)
751 752 753 754 755 756 757 758 759 760
        {
            *prev = current->next;
            break;
        }
        prev = &current->next;
        current = *prev;
    }
}


Jack Moffitt's avatar
Jack Moffitt committed
761 762
static stats_event_t *_make_event_from_node(stats_node_t *node, char *source)
{
763
    stats_event_t *event = (stats_event_t *)malloc(sizeof(stats_event_t));
764

765 766 767 768 769 770
    if (source != NULL)
        event->source = (char *)strdup(source);
    else
        event->source = NULL;
    event->name = (char *)strdup(node->name);
    event->value = (char *)strdup(node->value);
771
    event->hidden = node->hidden;
772
    event->action = STATS_EVENT_SET;
773
    event->next = NULL;
Jack Moffitt's avatar
Jack Moffitt committed
774

775
    return event;
Jack Moffitt's avatar
Jack Moffitt committed
776 777 778
}


779 780 781 782
static void _add_event_to_queue(stats_event_t *event, event_queue_t *queue)
{
    *queue->tail = event;
    queue->tail = (volatile stats_event_t **)&event->next;
Jack Moffitt's avatar
Jack Moffitt committed
783 784 785
}


786 787 788
static stats_event_t *_get_event_from_queue (event_queue_t *queue)
{
    stats_event_t *event = NULL;
Jack Moffitt's avatar
Jack Moffitt committed
789

790 791 792 793 794 795 796
    if (queue && queue->head)
    {
        event = (stats_event_t *)queue->head;
        queue->head = event->next;
        if (queue->head == NULL)
            queue->tail = &queue->head;
    }
Jack Moffitt's avatar
Jack Moffitt committed
797

798
    return event;
Jack Moffitt's avatar
Jack Moffitt committed
799 800
}

801
static int _send_event_to_client(stats_event_t *event, client_t *client)
Jack Moffitt's avatar
Jack Moffitt committed
802
{
803
    int len;
804
    char buf [200];
Jack Moffitt's avatar
Jack Moffitt committed
805

806
    /* send data to the client!!!! */
807
    len = snprintf (buf, sizeof (buf), "EVENT %s %s %s\n",
808 809 810
            (event->source != NULL) ? event->source : "global",
            event->name ? event->name : "null",
            event->value ? event->value : "null");
811
    if (len > 0 && len < (int)sizeof (buf))
812 813 814 815 816 817
    {
        client_send_bytes (client, buf, len);
        if (client->con->error)
            return -1;
    }
    return 0;
Jack Moffitt's avatar
Jack Moffitt committed
818 819
}

820 821 822
static inline void __add_authstack (auth_stack_t *stack, xmlNodePtr parent) {
    xmlNodePtr authentication;
    authentication = xmlNewTextChild(parent, NULL, XMLSTR("authentication"), NULL);
823

824 825 826 827 828 829 830 831 832 833
    auth_stack_addref(stack);

    while (stack) {
        auth_t *auth = auth_stack_get(stack);
        admin_add_role_to_authentication(auth, authentication);
        auth_release(auth);
        auth_stack_next(&stack);
   }
}
static xmlNodePtr _dump_stats_to_doc (xmlNodePtr root, const char *show_mount, int hidden) {
834 835
    avl_node *avlnode;
    xmlNodePtr ret = NULL;
836
    ice_config_t *config;
837 838

    thread_mutex_lock(&_stats_mutex);
839 840
    /* general stats first */
    avlnode = avl_get_first(_stats.global_tree);
841

842
    while (avlnode) {
843 844 845 846 847 848 849
        stats_node_t *stat = avlnode->key;
        if (stat->hidden <=  hidden)
            xmlNewTextChild (root, NULL, XMLSTR(stat->name), XMLSTR(stat->value));
        avlnode = avl_get_next (avlnode);
    }
    /* now per mount stats */
    avlnode = avl_get_first(_stats.source_tree);
850 851 852 853
    config = config_get_config();
    __add_authstack(config->authstack, root);
    config_release_config();

854
    while (avlnode) {
855
        stats_source_t *source = (stats_source_t *)avlnode->key;
856

857 858 859
        if (source->hidden <= hidden &&
                (show_mount == NULL || strcmp (show_mount, source->source) == 0))
        {
860
            xmlNodePtr metadata, history;
861
            source_t *source_real;
862
            mount_proxy *mountproxy;
863 864
            int i;

865 866
            avl_node *avlnode2 = avl_get_first (source->stats_tree);
            xmlNodePtr xmlnode = xmlNewTextChild (root, NULL, XMLSTR("source"), NULL);
867

868 869 870 871 872 873 874 875 876
            xmlSetProp (xmlnode, XMLSTR("mount"), XMLSTR(source->source));
            if (ret == NULL)
                ret = xmlnode;
            while (avlnode2)
            {
                stats_node_t *stat = avlnode2->key;
                xmlNewTextChild (xmlnode, NULL, XMLSTR(stat->name), XMLSTR(stat->value));
                avlnode2 = avl_get_next (avlnode2);
            }
877 878 879 880


            avl_tree_rlock(global.source_tree);
            source_real = source_find_mount_raw(source->source);
881 882 883 884
            history = playlist_render_xspf(source_real->history);
            if (history)
                xmlAddChild(xmlnode, history);
            metadata = xmlNewTextChild(xmlnode, NULL, XMLSTR("metadata"), NULL);
885 886 887 888
            if (source_real->format) {
                for (i = 0; i < source_real->format->vc.comments; i++)
                    __add_metadata(metadata, source_real->format->vc.user_comments[i]);
            }
889
            avl_tree_unlock(global.source_tree);
890 891 892

            config = config_get_config();
            mountproxy = config_find_mount(config, source->source, MOUNT_TYPE_NORMAL);
893
            __add_authstack(mountproxy->authstack, xmlnode);
894
            config_release_config();
895
        }
896
        avlnode = avl_get_next (avlnode);
897 898
    }
    thread_mutex_unlock(&_stats_mutex);
899
    return ret;
Jack Moffitt's avatar
Jack Moffitt committed
900 901
}

902

903
/* factoring out code for stats loops
904
** this function copies all stats to queue, and registers
905 906 907
** the queue for all new events atomically.
** note: mutex must already be created!
*/
908
static void _register_listener (event_listener_t *listener)
Jack Moffitt's avatar
Jack Moffitt committed
909
{
910 911 912 913
    avl_node *node;
    avl_node *node2;
    stats_event_t *event;
    stats_source_t *source;
Jack Moffitt's avatar
Jack Moffitt committed
914

915
    thread_mutex_lock(&_stats_mutex);
Jack Moffitt's avatar
Jack Moffitt committed
916

917
    /* first we fill our queue with the current stats */
918

919 920 921
    /* start with the global stats */
    node = avl_get_first(_stats.global_tree);
    while (node) {
922 923
        event = _make_event_from_node((stats_node_t *) node->key, NULL);
        _add_event_to_queue(event, &listener->queue);
Jack Moffitt's avatar
Jack Moffitt committed
924

925 926
        node = avl_get_next(node);
    }
Jack Moffitt's avatar
Jack Moffitt committed
927

928 929 930 931 932 933 934
    /* now the stats for each source */
    node = avl_get_first(_stats.source_tree);
    while (node) {
        source = (stats_source_t *)node->key;
        node2 = avl_get_first(source->stats_tree);
        while (node2) {
            event = _make_event_from_node((stats_node_t *)node2->key, source->source);
935
            _add_event_to_queue (event, &listener->queue);
936 937 938

            node2 = avl_get_next(node2);
        }
939

940 941
        node = avl_get_next(node);
    }
Jack Moffitt's avatar
Jack Moffitt committed
942

943
    /* now we register to receive future event notices */
944 945
    listener->next = (event_listener_t *)_event_listeners;
    _event_listeners = listener;
Jack Moffitt's avatar
Jack Moffitt committed
946

947
    thread_mutex_unlock(&_stats_mutex);
948 949 950 951
}

void *stats_connection(void *arg)
{
952
    client_t *client = (client_t *)arg;
953
    stats_event_t *event;
954
    event_listener_t listener;
955

956
    ICECAST_LOG_INFO("stats client starting");
957

958
    event_queue_init (&listener.queue);
959 960 961
    /* increment the thread count */
    thread_mutex_lock(&_stats_mutex);
    _stats_threads++;
962
    stats_event_args (NULL, "stats", "%d", _stats_threads);
963 964
    thread_mutex_unlock(&_stats_mutex);

965
    thread_mutex_create (&(listener.mutex));
966

967
    _register_listener (&listener);
968 969

    while (_stats_running) {
970 971 972
        thread_mutex_lock (&listener.mutex);
        event = _get_event_from_queue (&listener.queue);
        thread_mutex_unlock (&listener.mutex);
973
        if (event != NULL) {
974
            if (_send_event_to_client(event, client) < 0) {
975 976 977 978 979 980
                _free_event(event);
                break;
            }
            _free_event(event);
            continue;
        }
981
        thread_sleep (500000);
982 983 984
    }

    thread_mutex_lock(&_stats_mutex);
985
    _unregister_listener (&listener);
986
    _stats_threads--;
987
    stats_event_args (NULL, "stats", "%d", _stats_threads);
988 989
    thread_mutex_unlock(&_stats_mutex);

990
    thread_mutex_destroy (&listener.mutex);
991
    client_destroy (client);
992
    ICECAST_LOG_INFO("stats client finished");
993

994
    return NULL;
Jack Moffitt's avatar
Jack Moffitt committed
995 996
}

997 998 999

void stats_callback (client_t *client, void *notused)
{
1000 1001
    (void)notused;

1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
    if (client->con->error)
    {
        client_destroy (client);
        return;
    }
    client_set_queue (client, NULL);
    thread_create("Stats Connection", stats_connection, (void *)client, THREAD_DETACHED);
}


Jack Moffitt's avatar
Jack Moffitt committed
1012
typedef struct _source_xml_tag {
1013 1014
    char *mount;
    xmlNodePtr node;
Jack Moffitt's avatar
Jack Moffitt committed
1015

1016
    struct _source_xml_tag *next;
Jack Moffitt's avatar
Jack Moffitt committed
1017 1018 1019
} source_xml_t;


1020
void stats_transform_xslt(client_t *client, const char *uri)
1021 1022
{
    xmlDocPtr doc;
1023 1024
    char *xslpath = util_get_path_from_normalised_uri(uri);
    const char *mount = httpp_get_query_param(client->parser, "mount");
1025

1026
    doc = stats_get_xml(0, mount, client->mode);
1027

1028
    xslt_transform(doc, xslpath, client);
1029 1030

    xmlFreeDoc(doc);
1031
    free(xslpath);
1032 1033
}

1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
static void __add_metadata(xmlNodePtr node, const char *tag) {
    const char *value = strstr(tag, "=");
    char *name = NULL;
    size_t namelen = value - tag + 1;
    size_t i;

    if (!value)
        return;

    name = malloc(namelen);
    if (!name)
        return;

    for (i = 0; i < (namelen - 1); i++)
        name[i] = tolower(tag[i]);

    name[namelen-1] = 0;

    xmlNewTextChild(node, NULL, XMLSTR(name), XMLSTR(value+1));

    free(name);
}

1057
xmlDocPtr stats_get_xml(int show_hidden, const char *show_mount, operation_mode mode)
Jack Moffitt's avatar
Jack Moffitt committed
1058
{
1059
    xmlDocPtr doc;
1060
    xmlNodePtr node;
1061
    source_t * source;
1062

1063 1064
    doc = xmlNewDoc (XMLSTR("1.0"));
    node = xmlNewDocNode (doc, NULL, XMLSTR("icestats"), NULL);
1065 1066
    xmlDocSetRootElement(doc, node);

1067
    node = _dump_stats_to_doc (node, show_mount, show_hidden);
1068

1069 1070 1071
    if (show_mount && node) {
        avl_tree_rlock(global.source_tree);
        source = source_find_mount_raw(show_mount);
1072
        admin_add_listeners_to_mount(source, node, mode);
1073 1074 1075
        avl_tree_unlock(global.source_tree);
    }

1076
    return doc;
Jack Moffitt's avatar
Jack Moffitt committed
1077 1078
}

1079

Jack Moffitt's avatar
Jack Moffitt committed
1080 1081
static int _compare_stats(void *arg, void *a, void *b)
{
1082 1083
    stats_node_t *nodea = (stats_node_t *)a;
    stats_node_t *nodeb = (stats_node_t *)b;
Jack Moffitt's avatar
Jack Moffitt committed
1084

1085 1086
    (void)arg;

1087
    return strcmp(nodea->name, nodeb->name);
Jack Moffitt's avatar
Jack Moffitt committed
1088 1089 1090 1091
}

static int _compare_source_stats(void *arg, void *a, void *b)
{
1092 1093
    stats_source_t *nodea = (stats_source_t *)a;
    stats_source_t *nodeb = (stats_source_t *)b;
Jack Moffitt's avatar
Jack Moffitt committed
1094

1095 1096
    (void)arg;

1097
    return strcmp(nodea->source, nodeb->source);
Jack Moffitt's avatar
Jack Moffitt committed
1098 1099 1100 1101
}

static int _free_stats(void *key)
{
1102 1103 1104 1105
    stats_node_t *node = (stats_node_t *)key;
    free(node->value);
    free(node->name);
    free(node);
1106

1107
    return 1;
Jack Moffitt's avatar
Jack Moffitt committed
1108 1109 1110 1111
}

static int _free_source_stats(void *key)
{
1112 1113 1114
    stats_source_t *node = (stats_source_t *)key;
    avl_tree_free(node->stats_tree, _free_stats);
    free(node->source);
1115
    free(node);
Jack Moffitt's avatar
Jack Moffitt committed
1116

1117
    return 1;
Jack Moffitt's avatar
Jack Moffitt committed
1118 1119 1120 1121
}

static void _free_event(stats_event_t *event)
{
1122 1123 1124 1125
    if (event->source) free(event->source);
    if (event->name) free(event->name);
    if (event->value) free(event->value);
    free(event);
Jack Moffitt's avatar
Jack Moffitt committed
1126
}
1127 1128


1129
refbuf_t *stats_get_streams (void)
1130
{