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.
|
|
|
|
*
|
|
|
|
* Copyright 2000-2004, Jack Moffitt <jack@xiph.org,
|
|
|
|
* Michael Smith <msmith@xiph.org>,
|
|
|
|
* oddsock <oddsock@xiph.org>,
|
|
|
|
* Karl Heyes <karl@xiph.org>
|
|
|
|
* and others (see AUTHORS for details).
|
|
|
|
*/
|
|
|
|
|
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>
|
|
|
|
|
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
|
|
|
|
2007-11-20 21:55:11 -05:00
|
|
|
#include "thread/thread.h"
|
|
|
|
#include "avl/avl.h"
|
|
|
|
#include "httpp/httpp.h"
|
|
|
|
#include "net/sock.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"
|
|
|
|
#include "stats.h"
|
2002-08-09 02:52:07 -04:00
|
|
|
#include "xslt.h"
|
2005-06-18 06:54:53 -04:00
|
|
|
#include "util.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);
|
|
|
|
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);
|
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);
|
2005-11-14 19:36:34 -05:00
|
|
|
static stats_event_t *_get_event_from_queue (event_queue_t *queue);
|
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 */
|
2005-11-14 19:36:34 -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
|
|
|
|
2002-06-15 00:54:43 -04:00
|
|
|
if(!_stats_running) /* We can't shutdown if we're not running. */
|
|
|
|
return;
|
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* wait for thread to exit */
|
|
|
|
_stats_running = 0;
|
|
|
|
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);
|
2004-10-25 10:03:42 -04:00
|
|
|
INFO0("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
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
WARN2 ("seen non-UTF8 data, probably incorrect metadata (%s, %s)", name, value);
|
|
|
|
return;
|
|
|
|
}
|
2004-10-25 10:03:42 -04:00
|
|
|
event = build_event (source, name, value);
|
|
|
|
if (event)
|
|
|
|
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
|
|
|
|
WARN1 ("No charset found for \"%s\"", charset);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2004-10-25 10:03:42 -04:00
|
|
|
ret = vsnprintf(buf, 1024, 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))
|
|
|
|
{
|
|
|
|
WARN2 ("problem with formatting %s stat %s",
|
|
|
|
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);
|
|
|
|
/* DEBUG2("%s on %s", name, source==NULL?"global":source); */
|
|
|
|
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);
|
|
|
|
/* DEBUG2("%s on %s", name, source==NULL?"global":source); */
|
|
|
|
if (event)
|
|
|
|
{
|
|
|
|
event->value = malloc (16);
|
|
|
|
snprintf (event->value, 16, "%ld", value);
|
|
|
|
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);
|
|
|
|
snprintf (event->value, 16, "%ld", value);
|
|
|
|
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
|
|
|
{
|
2004-10-25 10:03:42 -04:00
|
|
|
/* DEBUG2("%s on %s", name, source==NULL?"global":source); */
|
|
|
|
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;
|
|
|
|
|
|
|
|
while (node) {
|
|
|
|
stats = (stats_node_t *)node->key;
|
|
|
|
cmp = strcmp(name, stats->name);
|
|
|
|
if (cmp < 0)
|
|
|
|
node = node->left;
|
|
|
|
else if (cmp > 0)
|
|
|
|
node = node->right;
|
|
|
|
else
|
|
|
|
return stats;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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) {
|
|
|
|
stats = (stats_source_t *)node->key;
|
|
|
|
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));
|
2003-03-14 21:10:19 -05:00
|
|
|
if (event->source)
|
|
|
|
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 */
|
|
|
|
static void modify_node_event (stats_node_t *node, stats_event_t *event)
|
|
|
|
{
|
|
|
|
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:
|
2009-07-10 11:08:30 -04:00
|
|
|
WARN2 ("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)
|
|
|
|
DEBUG3 ("update \"%s\" %s (%s)", event->source, node->name, node->value);
|
|
|
|
else
|
|
|
|
DEBUG2 ("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;
|
|
|
|
|
|
|
|
/* DEBUG3("global event %s %s %d", event->name, event->value, event->action); */
|
|
|
|
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;
|
|
|
|
DEBUG1 ("new source stat %s", event->source);
|
|
|
|
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
|
|
|
|
|
|
|
avl_insert(_stats.source_tree, (void *)snode);
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
DEBUG2 ("new node %s (%s)", event->name, event->value);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
DEBUG1 ("delete node %s", event->name);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
DEBUG1 ("delete source node %s", event->source);
|
|
|
|
avl_delete(_stats.source_tree, (void *)snode, _free_source_stats);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-10 21:24:58 -04:00
|
|
|
void stats_event_time (const char *mount, const char *name)
|
|
|
|
{
|
|
|
|
time_t now = time(NULL);
|
2005-11-14 19:36:34 -05:00
|
|
|
struct tm local;
|
2005-06-10 21:24:58 -04:00
|
|
|
char buffer[100];
|
|
|
|
|
|
|
|
localtime_r (&now, &local);
|
|
|
|
strftime (buffer, sizeof (buffer), "%a, %d %b %Y %H:%M:%S %z", &local);
|
|
|
|
stats_event (mount, name, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-04 03:14:54 -04:00
|
|
|
void stats_event_time_iso8601 (const char *mount, const char *name)
|
|
|
|
{
|
|
|
|
time_t now = time(NULL);
|
|
|
|
struct tm local;
|
|
|
|
char buffer[100];
|
|
|
|
|
|
|
|
localtime_r (&now, &local);
|
|
|
|
strftime (buffer, sizeof (buffer), "%FT%T%z", &local);
|
|
|
|
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;
|
|
|
|
|
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
|
|
|
|
|
|
|
INFO0 ("stats thread started");
|
2003-03-14 21:10:19 -05:00
|
|
|
while (_stats_running) {
|
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);
|
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));
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-10-28 22:47:41 -04:00
|
|
|
|
|
|
|
static xmlNodePtr _dump_stats_to_doc (xmlNodePtr root, const char *show_mount, int hidden)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
2008-10-28 22:47:41 -04:00
|
|
|
avl_node *avlnode;
|
|
|
|
xmlNodePtr ret = NULL;
|
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);
|
|
|
|
while (avlnode)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
while (avlnode)
|
|
|
|
{
|
|
|
|
stats_source_t *source = (stats_source_t *)avlnode->key;
|
|
|
|
if (source->hidden <= hidden &&
|
|
|
|
(show_mount == NULL || strcmp (show_mount, source->source) == 0))
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
xmlNewTextChild (xmlnode, NULL, XMLSTR(stat->name), XMLSTR(stat->value));
|
|
|
|
avlnode2 = avl_get_next (avlnode2);
|
|
|
|
}
|
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
|
|
|
|
** this function copies all stats to queue, and registers
|
|
|
|
** 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 */
|
|
|
|
|
|
|
|
/* start with the global stats */
|
|
|
|
node = avl_get_first(_stats.global_tree);
|
|
|
|
while (node) {
|
|
|
|
event = _make_event_from_node((stats_node_t *)node->key, NULL);
|
2005-11-14 19:36:34 -05:00
|
|
|
_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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2005-04-30 22:04:11 -04:00
|
|
|
INFO0 ("stats client starting");
|
|
|
|
|
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
|
|
|
|
|
|
|
while (_stats_running) {
|
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);
|
2005-04-30 22:04:11 -04:00
|
|
|
INFO0 ("stats client finished");
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
2005-06-18 06:54:53 -04:00
|
|
|
void stats_transform_xslt(client_t *client, const char *uri)
|
2002-08-09 02:52:07 -04:00
|
|
|
{
|
|
|
|
xmlDocPtr doc;
|
2005-06-18 06:54:53 -04:00
|
|
|
char *xslpath = util_get_path_from_normalised_uri (uri);
|
2007-09-04 22:16:45 -04:00
|
|
|
const char *mount = httpp_get_query_param (client->parser, "mount");
|
2002-08-09 02:52:07 -04:00
|
|
|
|
2008-10-28 22:47:41 -04:00
|
|
|
doc = stats_get_xml (0, mount);
|
2002-08-09 02:52:07 -04:00
|
|
|
|
2002-08-09 10:38:42 -04:00
|
|
|
xslt_transform(doc, xslpath, client);
|
2002-08-09 02:52:07 -04:00
|
|
|
|
|
|
|
xmlFreeDoc(doc);
|
2005-07-04 16:11:15 -04:00
|
|
|
free (xslpath);
|
2002-08-09 02:52:07 -04:00
|
|
|
}
|
|
|
|
|
2008-10-28 22:47:41 -04:00
|
|
|
xmlDocPtr stats_get_xml(int show_hidden, const char *show_mount)
|
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;
|
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);
|
|
|
|
|
2008-10-28 22:47:41 -04:00
|
|
|
node = _dump_stats_to_doc (node, show_mount, show_hidden);
|
2003-03-14 21:10:19 -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
|
|
|
|
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
|
|
|
|
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);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
DEBUG1 ("releasing %s stats", src->source);
|
|
|
|
avl_delete (_stats.source_tree, src, _free_source_stats);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
snode = avl_get_next (snode);
|
|
|
|
}
|
|
|
|
thread_mutex_unlock (&_stats_mutex);
|
|
|
|
}
|
|
|
|
|