2004-01-28 20:02:12 -05:00
|
|
|
/* Icecast
|
|
|
|
*
|
|
|
|
* This program is distributed under the GNU General Public License, version 2.
|
|
|
|
* A copy of this license is included with this source.
|
|
|
|
*
|
2015-01-10 13:53:44 -05:00
|
|
|
* Copyright 2000-2004, Jack Moffitt <jack@xiph.org,
|
2004-01-28 20:02:12 -05:00
|
|
|
* Michael Smith <msmith@xiph.org>,
|
|
|
|
* oddsock <oddsock@xiph.org>,
|
|
|
|
* Karl Heyes <karl@xiph.org>
|
|
|
|
* and others (see AUTHORS for details).
|
2020-10-03 04:44:37 -04:00
|
|
|
* Copyright 2012-2020, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
|
2004-01-28 20:02:12 -05:00
|
|
|
*/
|
|
|
|
|
2003-07-20 21:58:54 -04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2001-09-09 22:21:46 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
2014-12-09 11:08:27 -05:00
|
|
|
#include <ctype.h>
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2002-06-29 00:29:37 -04:00
|
|
|
#include <libxml/xmlmemory.h>
|
|
|
|
#include <libxml/parser.h>
|
|
|
|
#include <libxml/tree.h>
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2014-12-02 16:50:57 -05:00
|
|
|
#include "common/thread/thread.h"
|
|
|
|
#include "common/avl/avl.h"
|
|
|
|
#include "common/httpp/httpp.h"
|
|
|
|
#include "common/net/sock.h"
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2018-06-17 08:47:35 -04:00
|
|
|
#include "stats.h"
|
2001-09-09 22:21:46 -04:00
|
|
|
#include "connection.h"
|
2007-08-11 13:44:45 -04:00
|
|
|
#include "source.h"
|
2001-09-09 22:21:46 -04:00
|
|
|
#include "global.h"
|
|
|
|
#include "refbuf.h"
|
|
|
|
#include "client.h"
|
2014-11-21 10:37:50 -05:00
|
|
|
#include "admin.h"
|
2002-08-09 02:52:07 -04:00
|
|
|
#include "xslt.h"
|
2005-06-18 06:54:53 -04:00
|
|
|
#include "util.h"
|
2014-12-19 05:44:24 -05:00
|
|
|
#include "auth.h"
|
2004-10-25 10:03:42 -04:00
|
|
|
#define CATMODULE "stats"
|
|
|
|
#include "logging.h"
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2001-10-20 02:51:29 -04:00
|
|
|
#ifdef _WIN32
|
2009-08-05 18:32:47 -04:00
|
|
|
#define atoll _atoi64
|
2001-10-20 02:43:04 -04:00
|
|
|
#define vsnprintf _vsnprintf
|
2004-10-25 10:03:42 -04:00
|
|
|
#define snprintf _snprintf
|
2001-10-20 02:51:29 -04:00
|
|
|
#endif
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2004-10-25 10:03:42 -04:00
|
|
|
#define STATS_EVENT_SET 0
|
|
|
|
#define STATS_EVENT_INC 1
|
|
|
|
#define STATS_EVENT_DEC 2
|
|
|
|
#define STATS_EVENT_ADD 3
|
2007-10-19 20:58:05 -04:00
|
|
|
#define STATS_EVENT_SUB 4
|
|
|
|
#define STATS_EVENT_REMOVE 5
|
|
|
|
#define STATS_EVENT_HIDDEN 6
|
2004-10-25 10:03:42 -04:00
|
|
|
|
2005-11-14 19:36:34 -05:00
|
|
|
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; }
|
|
|
|
|
2001-09-09 22:21:46 -04:00
|
|
|
typedef struct _event_listener_tag
|
|
|
|
{
|
2005-11-14 19:36:34 -05:00
|
|
|
event_queue_t queue;
|
|
|
|
mutex_t mutex;
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
struct _event_listener_tag *next;
|
2001-09-09 22:21:46 -04:00
|
|
|
} event_listener_t;
|
|
|
|
|
2005-08-12 11:27:32 -04:00
|
|
|
static volatile int _stats_running = 0;
|
2004-10-25 10:03:42 -04:00
|
|
|
static thread_type *_stats_thread_id;
|
2005-08-12 11:27:32 -04:00
|
|
|
static volatile int _stats_threads = 0;
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2004-10-25 10:03:42 -04:00
|
|
|
static stats_t _stats;
|
|
|
|
static mutex_t _stats_mutex;
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2005-11-14 19:36:34 -05:00
|
|
|
static event_queue_t _global_event_queue;
|
2001-09-09 22:21:46 -04:00
|
|
|
mutex_t _global_event_mutex;
|
|
|
|
|
2005-08-12 11:27:32 -04:00
|
|
|
static volatile event_listener_t *_event_listeners;
|
2001-09-09 22:21:46 -04:00
|
|
|
|
|
|
|
|
|
|
|
static void *_stats_thread(void *arg);
|
2020-04-18 22:05:24 -04:00
|
|
|
static int _compare_stats(void *arg, void *a, void *b);
|
|
|
|
static int _compare_source_stats(void *arg, void *a, void *b);
|
2001-09-09 22:21:46 -04:00
|
|
|
static int _free_stats(void *key);
|
|
|
|
static int _free_source_stats(void *key);
|
2005-11-14 19:36:34 -05:00
|
|
|
static void _add_event_to_queue(stats_event_t *event, event_queue_t *queue);
|
2007-08-28 22:01:28 -04:00
|
|
|
static stats_node_t *_find_node(avl_tree *tree, const char *name);
|
|
|
|
static stats_source_t *_find_source(avl_tree *tree, const char *source);
|
2001-09-09 22:21:46 -04:00
|
|
|
static void _free_event(stats_event_t *event);
|
2014-11-30 15:32:30 -05:00
|
|
|
static stats_event_t *_get_event_from_queue(event_queue_t *queue);
|
2014-12-18 05:24:15 -05:00
|
|
|
static void __add_metadata(xmlNodePtr node, const char *tag);
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2004-10-25 10:03:42 -04:00
|
|
|
|
|
|
|
/* 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);
|
2005-11-14 19:36:34 -05:00
|
|
|
_add_event_to_queue (event, &_global_event_queue);
|
2004-10-25 10:03:42 -04:00
|
|
|
thread_mutex_unlock(&_global_event_mutex);
|
|
|
|
}
|
|
|
|
|
2005-12-17 07:23:09 -05:00
|
|
|
void stats_initialize(void)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
_event_listeners = NULL;
|
2002-04-05 11:33:35 -05:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* set up global struct */
|
|
|
|
_stats.global_tree = avl_tree_new(_compare_stats, NULL);
|
|
|
|
_stats.source_tree = avl_tree_new(_compare_source_stats, NULL);
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* set up global mutex */
|
|
|
|
thread_mutex_create(&_stats_mutex);
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* set up stats queues */
|
2014-11-30 15:32:30 -05:00
|
|
|
event_queue_init(&_global_event_queue);
|
2003-03-14 21:10:19 -05:00
|
|
|
thread_mutex_create(&_global_event_mutex);
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* fire off the stats thread */
|
|
|
|
_stats_running = 1;
|
|
|
|
_stats_thread_id = thread_create("Stats Thread", _stats_thread, NULL, THREAD_ATTACHED);
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
2005-12-17 07:23:09 -05:00
|
|
|
void stats_shutdown(void)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
int n;
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2014-11-30 15:32:30 -05:00
|
|
|
if (!_stats_running) /* We can't shutdown if we're not running. */
|
2002-06-15 00:54:43 -04:00
|
|
|
return;
|
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* wait for thread to exit */
|
2018-09-20 08:12:59 -04:00
|
|
|
thread_mutex_lock(&_stats_mutex);
|
2003-03-14 21:10:19 -05:00
|
|
|
_stats_running = 0;
|
2018-09-20 08:12:59 -04:00
|
|
|
thread_mutex_unlock(&_stats_mutex);
|
2003-03-14 21:10:19 -05:00
|
|
|
thread_join(_stats_thread_id);
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* 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);
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_INFO("stats thread finished");
|
2002-02-13 19:41:57 -05:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* free the queues */
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* destroy the queue mutexes */
|
|
|
|
thread_mutex_destroy(&_global_event_mutex);
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
thread_mutex_destroy(&_stats_mutex);
|
|
|
|
avl_tree_free(_stats.source_tree, _free_source_stats);
|
|
|
|
avl_tree_free(_stats.global_tree, _free_stats);
|
2002-12-29 09:06:20 -05:00
|
|
|
|
2005-11-14 19:36:34 -05:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
stats_event_t *event = _get_event_from_queue (&_global_event_queue);
|
|
|
|
if (event == NULL) break;
|
2002-12-29 09:06:20 -05:00
|
|
|
if(event->source)
|
|
|
|
free(event->source);
|
|
|
|
if(event->value)
|
|
|
|
free(event->value);
|
2003-06-26 09:32:34 -04:00
|
|
|
if(event->name)
|
|
|
|
free(event->name);
|
2002-12-29 09:06:20 -05:00
|
|
|
free(event);
|
|
|
|
}
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
2005-12-17 07:23:09 -05:00
|
|
|
stats_t *stats_get_stats(void)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
/* lock global stats
|
2015-01-10 13:53:44 -05:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
copy stats
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
unlock global stats
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return copied stats */
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return NULL;
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
2004-10-25 10:03:42 -04:00
|
|
|
/* simple name=tag stat create/update */
|
|
|
|
void stats_event(const char *source, const char *name, const char *value)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
stats_event_t *event;
|
|
|
|
|
2007-08-24 11:44:37 -04:00
|
|
|
if (value && xmlCheckUTF8 ((unsigned char *)value) == 0)
|
|
|
|
{
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_WARN("seen non-UTF8 data, probably incorrect metadata (%s, %s)", name, value);
|
2007-08-24 11:44:37 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-11-30 15:32:30 -05:00
|
|
|
event = build_event(source, name, value);
|
2004-10-25 10:03:42 -04:00
|
|
|
if (event)
|
2014-11-30 15:32:30 -05:00
|
|
|
queue_global_event(event);
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
2007-08-24 11:44:37 -04:00
|
|
|
|
|
|
|
/* 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
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_WARN("No charset found for \"%s\"", charset);
|
2007-08-24 11:44:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
stats_event (mount, name, metadata);
|
|
|
|
xmlBufferFree (conv);
|
|
|
|
}
|
|
|
|
|
2004-11-22 13:21:48 -05:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
}
|
2004-10-25 10:03:42 -04:00
|
|
|
|
|
|
|
/* printf style formatting for stat create/update */
|
|
|
|
void stats_event_args(const char *source, char *name, char *format, ...)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
char buf[1024];
|
|
|
|
va_list val;
|
2004-10-25 10:03:42 -04:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (name == NULL)
|
|
|
|
return;
|
2003-03-14 21:10:19 -05:00
|
|
|
va_start(val, format);
|
2014-11-08 07:28:17 -05:00
|
|
|
ret = vsnprintf(buf, sizeof(buf), format, val);
|
2003-03-14 21:10:19 -05:00
|
|
|
va_end(val);
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2004-10-25 10:03:42 -04:00
|
|
|
if (ret < 0 || (unsigned int)ret >= sizeof (buf))
|
|
|
|
{
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_WARN("problem with formatting %s stat %s",
|
2004-10-25 10:03:42 -04:00
|
|
|
source==NULL ? "global" : source, name);
|
|
|
|
return;
|
|
|
|
}
|
2003-03-14 21:10:19 -05:00
|
|
|
stats_event(source, name, buf);
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
2007-08-28 22:01:28 -04:00
|
|
|
static char *_get_stats(const char *source, const char *name)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
stats_node_t *stats = NULL;
|
|
|
|
stats_source_t *src = NULL;
|
|
|
|
char *value = NULL;
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
thread_mutex_lock(&_stats_mutex);
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
if (stats) value = (char *)strdup(stats->value);
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
thread_mutex_unlock(&_stats_mutex);
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return value;
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
2007-08-28 22:01:28 -04:00
|
|
|
char *stats_get_value(const char *source, const char *name)
|
2003-02-02 09:25:53 -05:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
return(_get_stats(source, name));
|
2003-02-02 09:25:53 -05:00
|
|
|
}
|
2004-10-25 10:03:42 -04:00
|
|
|
|
|
|
|
/* increase the value in the provided stat by 1 */
|
|
|
|
void stats_event_inc(const char *source, const char *name)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
2004-10-25 10:03:42 -04:00
|
|
|
stats_event_t *event = build_event (source, name, NULL);
|
2014-10-31 04:46:58 -04:00
|
|
|
/* ICECAST_LOG_DEBUG("%s on %s", name, source==NULL?"global":source); */
|
2004-10-25 10:03:42 -04:00
|
|
|
if (event)
|
|
|
|
{
|
|
|
|
event->action = STATS_EVENT_INC;
|
|
|
|
queue_global_event (event);
|
2003-03-14 21:10:19 -05:00
|
|
|
}
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
2004-10-25 10:03:42 -04:00
|
|
|
void stats_event_add(const char *source, const char *name, unsigned long value)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
2004-10-25 10:03:42 -04:00
|
|
|
stats_event_t *event = build_event (source, name, NULL);
|
2014-10-31 04:46:58 -04:00
|
|
|
/* ICECAST_LOG_DEBUG("%s on %s", name, source==NULL?"global":source); */
|
2004-10-25 10:03:42 -04:00
|
|
|
if (event)
|
|
|
|
{
|
|
|
|
event->value = malloc (16);
|
2015-04-08 05:36:59 -04:00
|
|
|
snprintf(event->value, 16, "%lu", value);
|
2004-10-25 10:03:42 -04:00
|
|
|
event->action = STATS_EVENT_ADD;
|
|
|
|
queue_global_event (event);
|
2003-03-14 21:10:19 -05:00
|
|
|
}
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
2007-10-19 20:58:05 -04:00
|
|
|
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);
|
2015-04-08 05:36:59 -04:00
|
|
|
snprintf(event->value, 16, "%lu", value);
|
2007-10-19 20:58:05 -04:00
|
|
|
event->action = STATS_EVENT_SUB;
|
|
|
|
queue_global_event (event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-25 10:03:42 -04:00
|
|
|
/* decrease the value in the provided stat by 1 */
|
|
|
|
void stats_event_dec(const char *source, const char *name)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
2014-10-31 04:46:58 -04:00
|
|
|
/* ICECAST_LOG_DEBUG("%s on %s", name, source==NULL?"global":source); */
|
2004-10-25 10:03:42 -04:00
|
|
|
stats_event_t *event = build_event (source, name, NULL);
|
|
|
|
if (event)
|
|
|
|
{
|
|
|
|
event->action = STATS_EVENT_DEC;
|
|
|
|
queue_global_event (event);
|
2003-03-14 21:10:19 -05:00
|
|
|
}
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* note: you must call this function only when you have exclusive access
|
|
|
|
** to the avl_tree
|
|
|
|
*/
|
2007-08-28 22:01:28 -04:00
|
|
|
static stats_node_t *_find_node(avl_tree *stats_tree, const char *name)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
stats_node_t *stats;
|
|
|
|
avl_node *node;
|
|
|
|
int cmp;
|
|
|
|
|
|
|
|
/* get the root node */
|
|
|
|
node = stats_tree->root->right;
|
2014-11-30 15:32:30 -05:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
while (node) {
|
2014-11-30 15:32:30 -05:00
|
|
|
stats = (stats_node_t *) node->key;
|
2003-03-14 21:10:19 -05:00
|
|
|
cmp = strcmp(name, stats->name);
|
2015-01-10 13:53:44 -05:00
|
|
|
if (cmp < 0)
|
2003-03-14 21:10:19 -05:00
|
|
|
node = node->left;
|
|
|
|
else if (cmp > 0)
|
|
|
|
node = node->right;
|
|
|
|
else
|
|
|
|
return stats;
|
|
|
|
}
|
2015-01-10 13:53:44 -05:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* didn't find it */
|
|
|
|
return NULL;
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* note: you must call this function only when you have exclusive access
|
|
|
|
** to the avl_tree
|
|
|
|
*/
|
2007-08-28 22:01:28 -04:00
|
|
|
static stats_source_t *_find_source(avl_tree *source_tree, const char *source)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
stats_source_t *stats;
|
|
|
|
avl_node *node;
|
|
|
|
int cmp;
|
|
|
|
|
|
|
|
/* get the root node */
|
|
|
|
node = source_tree->root->right;
|
|
|
|
while (node) {
|
2014-11-30 15:32:30 -05:00
|
|
|
stats = (stats_source_t *) node->key;
|
2003-03-14 21:10:19 -05:00
|
|
|
cmp = strcmp(source, stats->source);
|
|
|
|
if (cmp < 0)
|
|
|
|
node = node->left;
|
|
|
|
else if (cmp > 0)
|
|
|
|
node = node->right;
|
|
|
|
else
|
|
|
|
return stats;
|
|
|
|
}
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* didn't find it */
|
|
|
|
return NULL;
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static stats_event_t *_copy_event(stats_event_t *event)
|
|
|
|
{
|
2004-10-25 10:03:42 -04:00
|
|
|
stats_event_t *copy = (stats_event_t *)calloc(1, sizeof(stats_event_t));
|
2015-01-10 13:53:44 -05:00
|
|
|
if (event->source)
|
2003-03-14 21:10:19 -05:00
|
|
|
copy->source = (char *)strdup(event->source);
|
|
|
|
else
|
|
|
|
copy->source = NULL;
|
2004-10-25 10:03:42 -04:00
|
|
|
if (event->name)
|
|
|
|
copy->name = (char *)strdup(event->name);
|
2003-03-14 21:10:19 -05:00
|
|
|
if (event->value)
|
|
|
|
copy->value = (char *)strdup(event->value);
|
|
|
|
else
|
|
|
|
copy->value = NULL;
|
2004-11-22 13:21:48 -05:00
|
|
|
copy->hidden = event->hidden;
|
2003-03-14 21:10:19 -05:00
|
|
|
copy->next = NULL;
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return copy;
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
2004-10-25 10:03:42 -04:00
|
|
|
|
|
|
|
/* helper to apply specialised changes to a stats node */
|
2014-11-30 15:32:30 -05:00
|
|
|
static void modify_node_event(stats_node_t *node, stats_event_t *event)
|
2004-10-25 10:03:42 -04:00
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
|
2004-11-22 13:21:48 -05:00
|
|
|
if (event->action == STATS_EVENT_HIDDEN)
|
|
|
|
{
|
|
|
|
if (event->value)
|
|
|
|
node->hidden = 1;
|
|
|
|
else
|
|
|
|
node->hidden = 0;
|
|
|
|
return;
|
|
|
|
}
|
2004-10-25 10:03:42 -04:00
|
|
|
if (event->action != STATS_EVENT_SET)
|
|
|
|
{
|
2009-08-05 18:32:47 -04:00
|
|
|
int64_t value = 0;
|
2004-10-25 10:03:42 -04:00
|
|
|
|
|
|
|
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;
|
2009-07-10 11:08:30 -04:00
|
|
|
case STATS_EVENT_SUB:
|
|
|
|
value = atoll (node->value) - atoll (event->value);
|
|
|
|
break;
|
2004-10-25 10:03:42 -04:00
|
|
|
default:
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_WARN("unhandled event (%d) for %s", event->action, event->source);
|
2004-10-25 10:03:42 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
str = malloc (16);
|
2009-08-05 18:32:47 -04:00
|
|
|
snprintf (str, 16, "%" PRId64, value);
|
2005-04-18 10:32:26 -04:00
|
|
|
if (event->value == NULL)
|
|
|
|
event->value = strdup (str);
|
2004-10-25 10:03:42 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
str = (char *)strdup (event->value);
|
|
|
|
free (node->value);
|
|
|
|
node->value = str;
|
2008-07-18 21:36:16 -04:00
|
|
|
if (event->source)
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("update \"%s\" %s (%s)", event->source, node->name, node->value);
|
2008-07-18 21:36:16 -04:00
|
|
|
else
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("update global %s (%s)", node->name, node->value);
|
2004-10-25 10:03:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void process_global_event (stats_event_t *event)
|
|
|
|
{
|
|
|
|
stats_node_t *node;
|
|
|
|
|
2014-10-31 04:46:58 -04:00
|
|
|
/* ICECAST_LOG_DEBUG("global event %s %s %d", event->name, event->value, event->action); */
|
2004-10-25 10:03:42 -04:00
|
|
|
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;
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("new source stat %s", event->source);
|
2004-10-25 10:03:42 -04:00
|
|
|
snode->source = (char *)strdup(event->source);
|
|
|
|
snode->stats_tree = avl_tree_new(_compare_stats, NULL);
|
2004-11-22 13:21:48 -05:00
|
|
|
if (event->action == STATS_EVENT_HIDDEN)
|
|
|
|
snode->hidden = 1;
|
|
|
|
else
|
|
|
|
snode->hidden = 0;
|
2004-10-25 10:03:42 -04:00
|
|
|
|
2014-11-30 15:32:30 -05:00
|
|
|
avl_insert(_stats.source_tree, (void *) snode);
|
2004-10-25 10:03:42 -04:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("new node %s (%s)", event->name, event->value);
|
2004-10-25 10:03:42 -04:00
|
|
|
node = (stats_node_t *)calloc(1,sizeof(stats_node_t));
|
|
|
|
node->name = (char *)strdup(event->name);
|
|
|
|
node->value = (char *)strdup(event->value);
|
2004-11-22 13:21:48 -05:00
|
|
|
node->hidden = snode->hidden;
|
2004-10-25 10:03:42 -04:00
|
|
|
|
|
|
|
avl_insert(snode->stats_tree, (void *)node);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (event->action == STATS_EVENT_REMOVE)
|
|
|
|
{
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("delete node %s", event->name);
|
2004-10-25 10:03:42 -04:00
|
|
|
avl_delete(snode->stats_tree, (void *)node, _free_stats);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
modify_node_event (node, event);
|
|
|
|
return;
|
|
|
|
}
|
2004-11-22 13:21:48 -05:00
|
|
|
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;
|
|
|
|
}
|
2004-10-25 10:03:42 -04:00
|
|
|
if (event->action == STATS_EVENT_REMOVE)
|
|
|
|
{
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("delete source node %s", event->source);
|
2004-10-25 10:03:42 -04:00
|
|
|
avl_delete(_stats.source_tree, (void *)snode, _free_source_stats);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-07 15:56:04 -05:00
|
|
|
/* 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);
|
|
|
|
}
|
2004-10-25 10:03:42 -04:00
|
|
|
|
2005-06-10 21:24:58 -04:00
|
|
|
void stats_event_time (const char *mount, const char *name)
|
|
|
|
{
|
|
|
|
char buffer[100];
|
|
|
|
|
2014-11-07 15:56:04 -05:00
|
|
|
__format_time(buffer, sizeof(buffer), "%a, %d %b %Y %H:%M:%S ");
|
2005-06-10 21:24:58 -04:00
|
|
|
stats_event (mount, name, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-04 03:14:54 -04:00
|
|
|
void stats_event_time_iso8601 (const char *mount, const char *name)
|
|
|
|
{
|
|
|
|
char buffer[100];
|
|
|
|
|
2014-11-07 15:56:04 -05:00
|
|
|
__format_time(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S");
|
2014-05-04 03:14:54 -04:00
|
|
|
stats_event (mount, name, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-15 22:13:18 -04:00
|
|
|
void stats_global (ice_config_t *config)
|
|
|
|
{
|
2007-09-02 20:55:27 -04:00
|
|
|
stats_event (NULL, "server_id", config->server_id);
|
2007-08-15 22:13:18 -04:00
|
|
|
stats_event (NULL, "host", config->hostname);
|
|
|
|
stats_event (NULL, "location", config->location);
|
|
|
|
stats_event (NULL, "admin", config->admin);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-09 22:21:46 -04:00
|
|
|
static void *_stats_thread(void *arg)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
stats_event_t *event;
|
|
|
|
stats_event_t *copy;
|
|
|
|
event_listener_t *listener;
|
|
|
|
|
2015-11-28 06:30:34 -05:00
|
|
|
(void)arg;
|
|
|
|
|
2005-06-10 21:24:58 -04:00
|
|
|
stats_event_time (NULL, "server_start");
|
2014-05-04 03:14:54 -04:00
|
|
|
stats_event_time_iso8601 (NULL, "server_start_iso8601");
|
2004-10-25 10:03:42 -04:00
|
|
|
|
|
|
|
/* global currently active stats */
|
|
|
|
stats_event (NULL, "clients", "0");
|
|
|
|
stats_event (NULL, "connections", "0");
|
|
|
|
stats_event (NULL, "sources", "0");
|
|
|
|
stats_event (NULL, "stats", "0");
|
2007-10-19 20:58:05 -04:00
|
|
|
stats_event (NULL, "listeners", "0");
|
2004-10-25 10:03:42 -04:00
|
|
|
|
|
|
|
/* 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");
|
2005-06-09 16:54:08 -04:00
|
|
|
stats_event (NULL, "listener_connections", "0");
|
2004-10-25 10:03:42 -04:00
|
|
|
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_INFO("stats thread started");
|
2018-09-20 08:12:59 -04:00
|
|
|
while (1) {
|
|
|
|
thread_mutex_lock(&_stats_mutex);
|
|
|
|
if (!_stats_running) {
|
|
|
|
thread_mutex_unlock(&_stats_mutex);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
thread_mutex_unlock(&_stats_mutex);
|
|
|
|
|
2012-07-17 10:03:37 -04:00
|
|
|
thread_mutex_lock(&_global_event_mutex);
|
2005-11-14 19:36:34 -05:00
|
|
|
if (_global_event_queue.head != NULL) {
|
2003-03-14 21:10:19 -05:00
|
|
|
/* grab the next event from the queue */
|
2005-11-14 19:36:34 -05:00
|
|
|
event = _get_event_from_queue (&_global_event_queue);
|
2004-10-25 10:03:42 -04:00
|
|
|
thread_mutex_unlock(&_global_event_mutex);
|
|
|
|
|
2007-08-28 22:01:28 -04:00
|
|
|
if (event == NULL)
|
|
|
|
continue;
|
2003-03-14 21:10:19 -05:00
|
|
|
event->next = NULL;
|
|
|
|
|
|
|
|
thread_mutex_lock(&_stats_mutex);
|
2004-10-25 10:03:42 -04:00
|
|
|
|
|
|
|
/* check if we are dealing with a global or source event */
|
|
|
|
if (event->source == NULL)
|
|
|
|
process_global_event (event);
|
|
|
|
else
|
|
|
|
process_source_event (event);
|
2015-01-10 13:53:44 -05:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* now we have an event that's been processed into the running stats */
|
|
|
|
/* this event should get copied to event listeners' queues */
|
2004-10-25 10:03:42 -04:00
|
|
|
listener = (event_listener_t *)_event_listeners;
|
2003-03-14 21:10:19 -05:00
|
|
|
while (listener) {
|
|
|
|
copy = _copy_event(event);
|
2005-11-14 19:36:34 -05:00
|
|
|
thread_mutex_lock (&listener->mutex);
|
|
|
|
_add_event_to_queue (copy, &listener->queue);
|
|
|
|
thread_mutex_unlock (&listener->mutex);
|
2003-03-14 21:10:19 -05:00
|
|
|
|
|
|
|
listener = listener->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now we need to destroy the event */
|
|
|
|
_free_event(event);
|
|
|
|
|
|
|
|
thread_mutex_unlock(&_stats_mutex);
|
2003-10-31 14:21:00 -05:00
|
|
|
continue;
|
2003-03-14 21:10:19 -05:00
|
|
|
}
|
2012-07-17 10:03:37 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
thread_mutex_unlock(&_global_event_mutex);
|
|
|
|
}
|
2003-03-14 21:10:19 -05:00
|
|
|
|
2004-02-19 21:09:00 -05:00
|
|
|
thread_sleep(300000);
|
2003-03-14 21:10:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
2005-04-30 22:04:11 -04:00
|
|
|
/* you must have the _stats_mutex locked here */
|
2005-11-14 19:36:34 -05:00
|
|
|
static void _unregister_listener(event_listener_t *listener)
|
2005-04-30 22:04:11 -04:00
|
|
|
{
|
|
|
|
event_listener_t **prev = (event_listener_t **)&_event_listeners,
|
|
|
|
*current = *prev;
|
|
|
|
while (current)
|
|
|
|
{
|
2005-11-14 19:36:34 -05:00
|
|
|
if (current == listener)
|
2005-04-30 22:04:11 -04:00
|
|
|
{
|
|
|
|
*prev = current->next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
prev = ¤t->next;
|
|
|
|
current = *prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-09 22:21:46 -04:00
|
|
|
static stats_event_t *_make_event_from_node(stats_node_t *node, char *source)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
stats_event_t *event = (stats_event_t *)malloc(sizeof(stats_event_t));
|
2015-01-10 13:53:44 -05:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
if (source != NULL)
|
|
|
|
event->source = (char *)strdup(source);
|
|
|
|
else
|
|
|
|
event->source = NULL;
|
|
|
|
event->name = (char *)strdup(node->name);
|
|
|
|
event->value = (char *)strdup(node->value);
|
2004-11-22 13:21:48 -05:00
|
|
|
event->hidden = node->hidden;
|
2004-10-25 10:03:42 -04:00
|
|
|
event->action = STATS_EVENT_SET;
|
2003-03-14 21:10:19 -05:00
|
|
|
event->next = NULL;
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return event;
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-14 19:36:34 -05:00
|
|
|
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;
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-14 19:36:34 -05:00
|
|
|
static stats_event_t *_get_event_from_queue (event_queue_t *queue)
|
|
|
|
{
|
|
|
|
stats_event_t *event = NULL;
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2005-11-14 19:36:34 -05:00
|
|
|
if (queue && queue->head)
|
|
|
|
{
|
|
|
|
event = (stats_event_t *)queue->head;
|
|
|
|
queue->head = event->next;
|
|
|
|
if (queue->head == NULL)
|
|
|
|
queue->tail = &queue->head;
|
|
|
|
}
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return event;
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
2005-05-06 11:57:15 -04:00
|
|
|
static int _send_event_to_client(stats_event_t *event, client_t *client)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
2005-11-14 19:36:34 -05:00
|
|
|
int len;
|
2005-05-06 11:57:15 -04:00
|
|
|
char buf [200];
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* send data to the client!!!! */
|
2005-05-06 11:57:15 -04:00
|
|
|
len = snprintf (buf, sizeof (buf), "EVENT %s %s %s\n",
|
2004-10-25 10:03:42 -04:00
|
|
|
(event->source != NULL) ? event->source : "global",
|
|
|
|
event->name ? event->name : "null",
|
|
|
|
event->value ? event->value : "null");
|
2005-06-18 06:54:53 -04:00
|
|
|
if (len > 0 && len < (int)sizeof (buf))
|
2005-11-14 19:36:34 -05:00
|
|
|
{
|
|
|
|
client_send_bytes (client, buf, len);
|
|
|
|
if (client->con->error)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
2014-12-26 16:57:50 -05:00
|
|
|
static inline void __add_authstack (auth_stack_t *stack, xmlNodePtr parent) {
|
|
|
|
xmlNodePtr authentication;
|
|
|
|
authentication = xmlNewTextChild(parent, NULL, XMLSTR("authentication"), NULL);
|
2008-10-28 22:47:41 -04:00
|
|
|
|
2014-12-26 16:57:50 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2020-10-09 12:01:24 -04:00
|
|
|
static xmlNodePtr _dump_stats_to_doc (xmlNodePtr root, unsigned int flags, const char *show_mount, client_t *client) {
|
|
|
|
int hidden = flags & STATS_XML_FLAG_SHOW_HIDDEN ? 1 : 0;
|
2008-10-28 22:47:41 -04:00
|
|
|
avl_node *avlnode;
|
|
|
|
xmlNodePtr ret = NULL;
|
2014-12-26 16:57:50 -05:00
|
|
|
ice_config_t *config;
|
2003-03-14 21:10:19 -05:00
|
|
|
|
2018-09-20 09:03:05 -04:00
|
|
|
config = config_get_config();
|
|
|
|
__add_authstack(config->authstack, root);
|
|
|
|
config_release_config();
|
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
thread_mutex_lock(&_stats_mutex);
|
2008-10-28 22:47:41 -04:00
|
|
|
/* general stats first */
|
|
|
|
avlnode = avl_get_first(_stats.global_tree);
|
2015-01-10 13:53:44 -05:00
|
|
|
|
2015-01-08 18:39:59 -05:00
|
|
|
while (avlnode) {
|
2008-10-28 22:47:41 -04:00
|
|
|
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);
|
2014-12-26 16:57:50 -05:00
|
|
|
|
2015-01-08 18:39:59 -05:00
|
|
|
while (avlnode) {
|
2008-10-28 22:47:41 -04:00
|
|
|
stats_source_t *source = (stats_source_t *)avlnode->key;
|
2014-12-18 05:24:15 -05:00
|
|
|
|
2008-10-28 22:47:41 -04:00
|
|
|
if (source->hidden <= hidden &&
|
|
|
|
(show_mount == NULL || strcmp (show_mount, source->source) == 0))
|
|
|
|
{
|
2015-03-28 12:15:49 -04:00
|
|
|
xmlNodePtr metadata, history;
|
2014-12-18 05:24:15 -05:00
|
|
|
source_t *source_real;
|
2014-12-19 05:44:24 -05:00
|
|
|
mount_proxy *mountproxy;
|
2014-12-18 05:24:15 -05:00
|
|
|
int i;
|
|
|
|
|
2008-10-28 22:47:41 -04:00
|
|
|
avl_node *avlnode2 = avl_get_first (source->stats_tree);
|
|
|
|
xmlNodePtr xmlnode = xmlNewTextChild (root, NULL, XMLSTR("source"), NULL);
|
2003-03-14 21:10:19 -05:00
|
|
|
|
2008-10-28 22:47:41 -04:00
|
|
|
xmlSetProp (xmlnode, XMLSTR("mount"), XMLSTR(source->source));
|
|
|
|
if (ret == NULL)
|
|
|
|
ret = xmlnode;
|
|
|
|
while (avlnode2)
|
|
|
|
{
|
|
|
|
stats_node_t *stat = avlnode2->key;
|
2018-05-19 03:41:45 -04:00
|
|
|
if (client && strcmp(stat->name, "listenurl") == 0) {
|
|
|
|
char buf[512];
|
|
|
|
client_get_baseurl(client, NULL, buf, sizeof(buf), NULL, NULL, NULL, source->source, NULL);
|
|
|
|
xmlNewTextChild (xmlnode, NULL, XMLSTR(stat->name), XMLSTR(buf));
|
|
|
|
} else {
|
|
|
|
xmlNewTextChild (xmlnode, NULL, XMLSTR(stat->name), XMLSTR(stat->value));
|
|
|
|
}
|
2008-10-28 22:47:41 -04:00
|
|
|
avlnode2 = avl_get_next (avlnode2);
|
|
|
|
}
|
2014-12-18 05:24:15 -05:00
|
|
|
|
|
|
|
|
|
|
|
avl_tree_rlock(global.source_tree);
|
|
|
|
source_real = source_find_mount_raw(source->source);
|
2018-11-04 04:28:41 -05:00
|
|
|
if (source_real) {
|
|
|
|
history = playlist_render_xspf(source_real->history);
|
|
|
|
if (history)
|
|
|
|
xmlAddChild(xmlnode, history);
|
|
|
|
|
|
|
|
metadata = xmlNewTextChild(xmlnode, NULL, XMLSTR("metadata"), NULL);
|
|
|
|
if (source_real->format) {
|
|
|
|
for (i = 0; i < source_real->format->vc.comments; i++)
|
|
|
|
__add_metadata(metadata, source_real->format->vc.user_comments[i]);
|
|
|
|
}
|
2020-10-01 10:08:18 -04:00
|
|
|
|
|
|
|
if (source_real->running)
|
|
|
|
xmlNewTextChild(xmlnode, NULL, XMLSTR("content-type"), XMLSTR(source_real->format->contenttype));
|
2020-10-09 12:01:24 -04:00
|
|
|
|
|
|
|
if (flags & STATS_XML_FLAG_SHOW_LISTENERS)
|
|
|
|
admin_add_listeners_to_mount(source_real, xmlnode, client->mode);
|
2014-12-20 13:06:42 -05:00
|
|
|
}
|
2014-12-18 05:24:15 -05:00
|
|
|
avl_tree_unlock(global.source_tree);
|
2014-12-19 05:44:24 -05:00
|
|
|
|
|
|
|
config = config_get_config();
|
|
|
|
mountproxy = config_find_mount(config, source->source, MOUNT_TYPE_NORMAL);
|
2014-12-26 16:57:50 -05:00
|
|
|
__add_authstack(mountproxy->authstack, xmlnode);
|
2014-12-19 05:44:24 -05:00
|
|
|
config_release_config();
|
2003-03-14 21:10:19 -05:00
|
|
|
}
|
2008-10-28 22:47:41 -04:00
|
|
|
avlnode = avl_get_next (avlnode);
|
2003-03-14 21:10:19 -05:00
|
|
|
}
|
|
|
|
thread_mutex_unlock(&_stats_mutex);
|
2008-10-28 22:47:41 -04:00
|
|
|
return ret;
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
2008-10-28 22:47:41 -04:00
|
|
|
|
2002-02-06 02:52:09 -05:00
|
|
|
/* factoring out code for stats loops
|
2015-01-10 13:53:44 -05:00
|
|
|
** this function copies all stats to queue, and registers
|
2002-02-06 02:52:09 -05:00
|
|
|
** the queue for all new events atomically.
|
|
|
|
** note: mutex must already be created!
|
|
|
|
*/
|
2005-11-14 19:36:34 -05:00
|
|
|
static void _register_listener (event_listener_t *listener)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
avl_node *node;
|
|
|
|
avl_node *node2;
|
|
|
|
stats_event_t *event;
|
|
|
|
stats_source_t *source;
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
thread_mutex_lock(&_stats_mutex);
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* first we fill our queue with the current stats */
|
2014-11-30 15:32:30 -05:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* start with the global stats */
|
|
|
|
node = avl_get_first(_stats.global_tree);
|
|
|
|
while (node) {
|
2014-11-30 15:32:30 -05:00
|
|
|
event = _make_event_from_node((stats_node_t *) node->key, NULL);
|
|
|
|
_add_event_to_queue(event, &listener->queue);
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
node = avl_get_next(node);
|
|
|
|
}
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* 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);
|
2005-11-14 19:36:34 -05:00
|
|
|
_add_event_to_queue (event, &listener->queue);
|
2003-03-14 21:10:19 -05:00
|
|
|
|
|
|
|
node2 = avl_get_next(node2);
|
|
|
|
}
|
2015-01-10 13:53:44 -05:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
node = avl_get_next(node);
|
|
|
|
}
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* now we register to receive future event notices */
|
2005-11-14 19:36:34 -05:00
|
|
|
listener->next = (event_listener_t *)_event_listeners;
|
|
|
|
_event_listeners = listener;
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
thread_mutex_unlock(&_stats_mutex);
|
2002-02-06 02:52:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void *stats_connection(void *arg)
|
|
|
|
{
|
2005-05-06 11:57:15 -04:00
|
|
|
client_t *client = (client_t *)arg;
|
2003-03-14 21:10:19 -05:00
|
|
|
stats_event_t *event;
|
2005-11-14 19:36:34 -05:00
|
|
|
event_listener_t listener;
|
2003-03-14 21:10:19 -05:00
|
|
|
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_INFO("stats client starting");
|
2005-04-30 22:04:11 -04:00
|
|
|
|
2005-11-14 19:36:34 -05:00
|
|
|
event_queue_init (&listener.queue);
|
2003-03-14 21:10:19 -05:00
|
|
|
/* increment the thread count */
|
|
|
|
thread_mutex_lock(&_stats_mutex);
|
|
|
|
_stats_threads++;
|
2005-05-06 11:57:15 -04:00
|
|
|
stats_event_args (NULL, "stats", "%d", _stats_threads);
|
2003-03-14 21:10:19 -05:00
|
|
|
thread_mutex_unlock(&_stats_mutex);
|
|
|
|
|
2005-11-14 19:36:34 -05:00
|
|
|
thread_mutex_create (&(listener.mutex));
|
2003-03-14 21:10:19 -05:00
|
|
|
|
2005-11-14 19:36:34 -05:00
|
|
|
_register_listener (&listener);
|
2003-03-14 21:10:19 -05:00
|
|
|
|
2018-09-20 08:12:59 -04:00
|
|
|
while (1) {
|
|
|
|
thread_mutex_lock(&_stats_mutex);
|
|
|
|
if (!_stats_running) {
|
|
|
|
thread_mutex_unlock(&_stats_mutex);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
thread_mutex_unlock(&_stats_mutex);
|
|
|
|
|
2005-11-14 19:36:34 -05:00
|
|
|
thread_mutex_lock (&listener.mutex);
|
|
|
|
event = _get_event_from_queue (&listener.queue);
|
|
|
|
thread_mutex_unlock (&listener.mutex);
|
2003-03-14 21:10:19 -05:00
|
|
|
if (event != NULL) {
|
2005-11-14 19:36:34 -05:00
|
|
|
if (_send_event_to_client(event, client) < 0) {
|
2003-03-14 21:10:19 -05:00
|
|
|
_free_event(event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_free_event(event);
|
|
|
|
continue;
|
|
|
|
}
|
2005-11-14 19:36:34 -05:00
|
|
|
thread_sleep (500000);
|
2003-03-14 21:10:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
thread_mutex_lock(&_stats_mutex);
|
2005-11-14 19:36:34 -05:00
|
|
|
_unregister_listener (&listener);
|
2003-03-14 21:10:19 -05:00
|
|
|
_stats_threads--;
|
2005-05-06 11:57:15 -04:00
|
|
|
stats_event_args (NULL, "stats", "%d", _stats_threads);
|
2003-03-14 21:10:19 -05:00
|
|
|
thread_mutex_unlock(&_stats_mutex);
|
|
|
|
|
2005-11-14 19:36:34 -05:00
|
|
|
thread_mutex_destroy (&listener.mutex);
|
2005-05-06 11:57:15 -04:00
|
|
|
client_destroy (client);
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_INFO("stats client finished");
|
2005-04-30 22:04:11 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return NULL;
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
2005-08-12 11:27:32 -04:00
|
|
|
|
|
|
|
void stats_callback (client_t *client, void *notused)
|
|
|
|
{
|
2015-11-28 06:30:34 -05:00
|
|
|
(void)notused;
|
|
|
|
|
2005-08-12 11:27:32 -04:00
|
|
|
if (client->con->error)
|
|
|
|
{
|
|
|
|
client_destroy (client);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
client_set_queue (client, NULL);
|
|
|
|
thread_create("Stats Connection", stats_connection, (void *)client, THREAD_DETACHED);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-09 22:21:46 -04:00
|
|
|
typedef struct _source_xml_tag {
|
2003-03-14 21:10:19 -05:00
|
|
|
char *mount;
|
|
|
|
xmlNodePtr node;
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
struct _source_xml_tag *next;
|
2001-09-09 22:21:46 -04:00
|
|
|
} source_xml_t;
|
|
|
|
|
|
|
|
|
2018-09-13 09:38:57 -04:00
|
|
|
void stats_transform_xslt(client_t *client)
|
2002-08-09 02:52:07 -04:00
|
|
|
{
|
|
|
|
xmlDocPtr doc;
|
2018-09-13 09:38:57 -04:00
|
|
|
char *xslpath = util_get_path_from_normalised_uri(client->uri);
|
2018-06-18 17:45:55 -04:00
|
|
|
const char *mount = httpp_get_param(client->parser, "mount");
|
2002-08-09 02:52:07 -04:00
|
|
|
|
2020-10-09 11:43:57 -04:00
|
|
|
doc = stats_get_xml(STATS_XML_FLAG_NONE, mount, client);
|
2002-08-09 02:52:07 -04:00
|
|
|
|
2020-10-01 13:34:31 -04:00
|
|
|
xslt_transform(doc, xslpath, client, 200, NULL, NULL);
|
2002-08-09 02:52:07 -04:00
|
|
|
|
|
|
|
xmlFreeDoc(doc);
|
2014-11-30 15:32:30 -05:00
|
|
|
free(xslpath);
|
2002-08-09 02:52:07 -04:00
|
|
|
}
|
|
|
|
|
2014-12-09 11:08:27 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-10-09 11:43:57 -04:00
|
|
|
xmlDocPtr stats_get_xml(unsigned int flags, const char *show_mount, client_t *client)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
xmlDocPtr doc;
|
2008-10-28 22:47:41 -04:00
|
|
|
xmlNodePtr node;
|
2018-08-03 05:13:49 -04:00
|
|
|
xmlNodePtr modules;
|
2003-03-14 21:10:19 -05:00
|
|
|
|
2007-10-04 12:48:38 -04:00
|
|
|
doc = xmlNewDoc (XMLSTR("1.0"));
|
|
|
|
node = xmlNewDocNode (doc, NULL, XMLSTR("icestats"), NULL);
|
2003-03-14 21:10:19 -05:00
|
|
|
xmlDocSetRootElement(doc, node);
|
|
|
|
|
2018-08-03 05:13:49 -04:00
|
|
|
modules = module_container_get_modulelist_as_xml(global.modulecontainer);
|
|
|
|
xmlAddChild(node, modules);
|
|
|
|
|
2020-10-09 12:01:24 -04:00
|
|
|
node = _dump_stats_to_doc(node, flags, show_mount, client);
|
2014-11-21 10:37:50 -05:00
|
|
|
|
2008-10-28 22:47:41 -04:00
|
|
|
return doc;
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
2008-10-28 22:47:41 -04:00
|
|
|
|
2001-09-09 22:21:46 -04:00
|
|
|
static int _compare_stats(void *arg, void *a, void *b)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
stats_node_t *nodea = (stats_node_t *)a;
|
|
|
|
stats_node_t *nodeb = (stats_node_t *)b;
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2015-11-28 06:30:34 -05:00
|
|
|
(void)arg;
|
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return strcmp(nodea->name, nodeb->name);
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int _compare_source_stats(void *arg, void *a, void *b)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
stats_source_t *nodea = (stats_source_t *)a;
|
|
|
|
stats_source_t *nodeb = (stats_source_t *)b;
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2015-11-28 06:30:34 -05:00
|
|
|
(void)arg;
|
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return strcmp(nodea->source, nodeb->source);
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int _free_stats(void *key)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
stats_node_t *node = (stats_node_t *)key;
|
|
|
|
free(node->value);
|
|
|
|
free(node->name);
|
|
|
|
free(node);
|
2015-01-10 13:53:44 -05:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return 1;
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int _free_source_stats(void *key)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
stats_source_t *node = (stats_source_t *)key;
|
|
|
|
avl_tree_free(node->stats_tree, _free_stats);
|
|
|
|
free(node->source);
|
2002-05-03 11:04:56 -04:00
|
|
|
free(node);
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return 1;
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void _free_event(stats_event_t *event)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
if (event->source) free(event->source);
|
|
|
|
if (event->name) free(event->name);
|
|
|
|
if (event->value) free(event->value);
|
|
|
|
free(event);
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
2007-08-09 19:36:29 -04:00
|
|
|
|
|
|
|
|
2008-04-29 00:50:23 -04:00
|
|
|
refbuf_t *stats_get_streams (void)
|
2007-08-09 19:36:29 -04:00
|
|
|
{
|
2008-04-29 00:50:23 -04:00
|
|
|
#define STREAMLIST_BLKSIZE 4096
|
2007-08-09 19:36:29 -04:00
|
|
|
avl_node *node;
|
2008-05-11 21:21:06 -04:00
|
|
|
unsigned int remaining = STREAMLIST_BLKSIZE;
|
2008-04-29 00:50:23 -04:00
|
|
|
refbuf_t *start = refbuf_new (remaining), *cur = start;
|
|
|
|
char *buffer = cur->data;
|
2007-08-09 19:36:29 -04:00
|
|
|
|
|
|
|
/* now the stats for each source */
|
|
|
|
thread_mutex_lock (&_stats_mutex);
|
|
|
|
node = avl_get_first(_stats.source_tree);
|
|
|
|
while (node)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
stats_source_t *source = (stats_source_t *)node->key;
|
|
|
|
|
|
|
|
if (source->hidden == 0)
|
|
|
|
{
|
2008-04-29 00:50:23 -04:00
|
|
|
if (remaining <= strlen (source->source) + 3)
|
2007-08-09 19:36:29 -04:00
|
|
|
{
|
2008-04-29 00:50:23 -04:00
|
|
|
cur->len = STREAMLIST_BLKSIZE - remaining;
|
|
|
|
cur->next = refbuf_new (STREAMLIST_BLKSIZE);
|
|
|
|
remaining = STREAMLIST_BLKSIZE;
|
|
|
|
cur = cur->next;
|
|
|
|
buffer = cur->data;
|
2007-08-09 19:36:29 -04:00
|
|
|
}
|
|
|
|
ret = snprintf (buffer, remaining, "%s\r\n", source->source);
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
buffer += ret;
|
|
|
|
remaining -= ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node = avl_get_next(node);
|
|
|
|
}
|
2014-11-30 15:32:30 -05:00
|
|
|
thread_mutex_unlock(&_stats_mutex);
|
2008-04-29 00:50:23 -04:00
|
|
|
cur->len = STREAMLIST_BLKSIZE - remaining;
|
|
|
|
return start;
|
2007-08-09 19:36:29 -04:00
|
|
|
}
|
|
|
|
|
2008-04-29 00:50:23 -04:00
|
|
|
|
|
|
|
|
2007-08-11 13:44:45 -04:00
|
|
|
/* This removes any source stats from virtual mountpoints, ie mountpoints
|
|
|
|
* where no source_t exists. This function requires the global sources lock
|
|
|
|
* to be held before calling.
|
|
|
|
*/
|
|
|
|
void stats_clear_virtual_mounts (void)
|
|
|
|
{
|
|
|
|
avl_node *snode;
|
|
|
|
|
|
|
|
thread_mutex_lock (&_stats_mutex);
|
|
|
|
snode = avl_get_first(_stats.source_tree);
|
|
|
|
while (snode)
|
|
|
|
{
|
|
|
|
stats_source_t *src = (stats_source_t *)snode->key;
|
|
|
|
source_t *source = source_find_mount_raw (src->source);
|
|
|
|
|
|
|
|
if (source == NULL)
|
|
|
|
{
|
|
|
|
/* no source_t is reserved so remove them now */
|
|
|
|
snode = avl_get_next (snode);
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("releasing %s stats", src->source);
|
2007-08-11 13:44:45 -04:00
|
|
|
avl_delete (_stats.source_tree, src, _free_source_stats);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
snode = avl_get_next (snode);
|
|
|
|
}
|
|
|
|
thread_mutex_unlock (&_stats_mutex);
|
|
|
|
}
|
|
|
|
|