1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
|
/*
* Copyright (c) 2002-2013 Balabit
* Copyright (c) 1998-2013 Balázs Scheidler
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "stats/stats-registry.h"
#include "stats/stats-query.h"
#include "cfg.h"
#include <string.h>
typedef struct _StatsClusterContainer
{
GHashTable *static_clusters;
GHashTable *dynamic_clusters;
} StatsClusterContainer;
static StatsClusterContainer stats_cluster_container;
static guint
_number_of_dynamic_clusters(void)
{
return g_hash_table_size(stats_cluster_container.dynamic_clusters);
}
static GMutex stats_mutex;
gboolean stats_locked;
static void
_insert_cluster(StatsCluster *sc)
{
if (sc->dynamic)
g_hash_table_insert(stats_cluster_container.dynamic_clusters, &sc->key, sc);
else
g_hash_table_insert(stats_cluster_container.static_clusters, &sc->key, sc);
}
void
stats_lock(void)
{
g_mutex_lock(&stats_mutex);
stats_locked = TRUE;
}
void
stats_unlock(void)
{
stats_locked = FALSE;
g_mutex_unlock(&stats_mutex);
}
static StatsCluster *
_grab_dynamic_cluster(const StatsClusterKey *sc_key)
{
StatsCluster *sc;
sc = g_hash_table_lookup(stats_cluster_container.dynamic_clusters, sc_key);
if (!sc)
{
if (!stats_check_dynamic_clusters_limit(_number_of_dynamic_clusters()))
return NULL;
sc = stats_cluster_dynamic_new(sc_key);
_insert_cluster(sc);
if ( !stats_check_dynamic_clusters_limit(_number_of_dynamic_clusters()))
{
msg_warning("Number of dynamic cluster limit has been reached.",
evt_tag_int("allowed_clusters", stats_number_of_dynamic_clusters_limit()));
}
}
return sc;
}
static StatsCluster *
_grab_static_cluster(const StatsClusterKey *sc_key)
{
StatsCluster *sc;
sc = g_hash_table_lookup(stats_cluster_container.static_clusters, sc_key);
if (!sc)
{
sc = stats_cluster_new(sc_key);
_insert_cluster(sc);
}
return sc;
}
static StatsCluster *
_grab_cluster(gint stats_level, const StatsClusterKey *sc_key, gboolean dynamic)
{
if (!stats_check_level(stats_level))
return NULL;
StatsCluster *sc = NULL;
if (dynamic)
sc = _grab_dynamic_cluster(sc_key);
else
sc = _grab_static_cluster(sc_key);
if (!sc)
return NULL;
/* check that we are not overwriting a dynamic counter with a
* non-dynamic one or vica versa. This could only happen if the same
* key is used for both a dynamic counter and a non-dynamic one, which
* is a programming error */
g_assert(sc->dynamic == dynamic);
return sc;
}
static gchar *
_construct_counter_item_name(StatsCluster *sc, gint type)
{
return g_strdup_printf("%s.%s", sc->query_key, stats_cluster_get_type_name(sc, type));
}
static void
_update_counter_name_if_needed(StatsCounterItem *counter, StatsCluster *sc, gint type)
{
if (counter->name == NULL)
counter->name = _construct_counter_item_name(sc, type);
}
static StatsCluster *
_register_counter(gint stats_level, const StatsClusterKey *sc_key, gint type,
gboolean dynamic, StatsCounterItem **counter)
{
StatsCluster *sc;
g_assert(stats_locked);
sc = _grab_cluster(stats_level, sc_key, dynamic);
if (sc)
{
StatsCounterItem *ctr = stats_cluster_get_counter(sc, type);
*counter = stats_cluster_track_counter(sc, type);
if (ctr && ctr->external)
return sc;
(*counter)->external = FALSE;
(*counter)->type = type;
_update_counter_name_if_needed(*counter, sc, type);
}
else
{
*counter = NULL;
}
return sc;
}
static void
_assert_when_internal_or_stores_different_ref(StatsCluster *sc, gint type, atomic_gssize *external_counter)
{
StatsCounterItem *counter = stats_cluster_get_counter(sc, type);
if (counter)
{
g_assert(counter->external && counter->value_ref == external_counter);
}
}
static StatsCluster *
_register_external_counter(gint stats_level, const StatsClusterKey *sc_key, gint type,
gboolean dynamic, atomic_gssize *external_counter)
{
StatsCluster *sc;
if (!external_counter)
return NULL;
g_assert(stats_locked);
sc = _grab_cluster(stats_level, sc_key, dynamic);
if (sc)
{
_assert_when_internal_or_stores_different_ref(sc, type, external_counter);
StatsCounterItem *ctr = stats_cluster_track_counter(sc, type);
ctr->external = TRUE;
ctr->value_ref = external_counter;
ctr->type = type;
_update_counter_name_if_needed(ctr, sc, type);
}
return sc;
}
/**
* stats_register_counter:
* @stats_level: the required statistics level to make this counter available
* @component: a reference to the syslog-ng component that this counter belongs to (SCS_*)
* @id: the unique identifier of the configuration item that this counter belongs to
* @instance: if a given configuration item manages multiple similar counters
* this makes those unique (like destination filename in case macros are used)
* @type: the counter type (processed, dropped, etc)
* @counter: returned pointer to the counter
*
* This function registers a general purpose counter. Whenever multiple
* objects touch the same counter all of these should register the counter
* with the same name. Internally the stats subsystem counts the number of
* users of the same counter in this case, thus the counter will only be
* freed when all of these uses are unregistered.
**/
StatsCluster *
stats_register_counter(gint stats_level, const StatsClusterKey *sc_key, gint type,
StatsCounterItem **counter)
{
return _register_counter(stats_level, sc_key, type, FALSE, counter);
}
StatsCluster *
stats_register_external_counter(gint stats_level, const StatsClusterKey *sc_key, gint type,
atomic_gssize *external_counter)
{
return _register_external_counter(stats_level, sc_key, type, FALSE, external_counter);
}
StatsCluster *
stats_register_alias_counter(gint level, const StatsClusterKey *sc_key, gint type, StatsCounterItem *aliased_counter)
{
return stats_register_external_counter(level, sc_key, type, &aliased_counter->value);
}
StatsCluster *
stats_register_dynamic_counter(gint stats_level, const StatsClusterKey *sc_key,
gint type, StatsCounterItem **counter)
{
return _register_counter(stats_level, sc_key, type, TRUE, counter);
}
/*
* stats_instant_inc_dynamic_counter
* @timestamp: if non-negative, an associated timestamp will be created and set
*
* Instantly create (if not exists) and increment a dynamic counter.
*/
void
stats_register_and_increment_dynamic_counter(gint stats_level, const StatsClusterKey *sc_key,
time_t timestamp)
{
StatsCounterItem *counter, *stamp;
StatsCluster *handle;
g_assert(stats_locked);
handle = stats_register_dynamic_counter(stats_level, sc_key, SC_TYPE_PROCESSED, &counter);
if (!handle)
return;
stats_counter_inc(counter);
if (timestamp >= 0)
{
stats_register_associated_counter(handle, SC_TYPE_STAMP, &stamp);
stats_counter_set(stamp, timestamp);
stats_unregister_dynamic_counter(handle, SC_TYPE_STAMP, &stamp);
}
stats_unregister_dynamic_counter(handle, SC_TYPE_PROCESSED, &counter);
}
/**
* stats_register_associated_counter:
* @sc: the dynamic counter that was registered with stats_register_dynamic_counter
* @type: the type that we want to use in the same StatsCluster instance
* @counter: the returned pointer to the counter itself
*
* This function registers another counter type in the same StatsCounter
* instance in order to avoid an unnecessary lookup.
**/
void
stats_register_associated_counter(StatsCluster *sc, gint type, StatsCounterItem **counter)
{
g_assert(stats_locked);
*counter = NULL;
if (!sc)
return;
g_assert(sc->dynamic);
*counter = stats_cluster_track_counter(sc, type);
_update_counter_name_if_needed(*counter, sc, type);
}
void
stats_unregister_counter(const StatsClusterKey *sc_key, gint type,
StatsCounterItem **counter)
{
StatsCluster *sc;
g_assert(stats_locked);
if (*counter == NULL)
return;
sc = g_hash_table_lookup(stats_cluster_container.static_clusters, sc_key);
stats_cluster_untrack_counter(sc, type, counter);
}
void
stats_unregister_external_counter(const StatsClusterKey *sc_key, gint type,
atomic_gssize *external_counter)
{
StatsCluster *sc;
if (!external_counter)
return;
g_assert(stats_locked);
sc = g_hash_table_lookup(stats_cluster_container.static_clusters, sc_key);
StatsCounterItem *ctr = stats_cluster_get_counter(sc, type);
g_assert(ctr->value_ref == external_counter);
stats_cluster_untrack_counter(sc, type, &ctr);
}
void
stats_unregister_alias_counter(const StatsClusterKey *sc_key, gint type, StatsCounterItem *aliased_counter)
{
stats_unregister_external_counter(sc_key, type, &aliased_counter->value);
}
void
stats_unregister_dynamic_counter(StatsCluster *sc, gint type, StatsCounterItem **counter)
{
g_assert(stats_locked);
if (!sc)
return;
stats_cluster_untrack_counter(sc, type, counter);
}
StatsCluster *
stats_get_cluster(const StatsClusterKey *sc_key)
{
g_assert(stats_locked);
StatsCluster *sc = g_hash_table_lookup(stats_cluster_container.static_clusters, sc_key);
if (!sc)
sc = g_hash_table_lookup(stats_cluster_container.dynamic_clusters, sc_key);
return sc;
}
gboolean
stats_remove_cluster(const StatsClusterKey *sc_key)
{
g_assert(stats_locked);
StatsCluster *sc;
sc = g_hash_table_lookup(stats_cluster_container.dynamic_clusters, sc_key);
if (sc)
{
if (stats_cluster_is_orphaned(sc))
return g_hash_table_remove(stats_cluster_container.dynamic_clusters, sc_key);
return FALSE;
}
sc = g_hash_table_lookup(stats_cluster_container.static_clusters, sc_key);
if (sc)
{
if (stats_cluster_is_orphaned(sc))
return g_hash_table_remove(stats_cluster_container.static_clusters, sc_key);
return FALSE;
}
return FALSE;
}
gboolean
stats_contains_counter(const StatsClusterKey *sc_key, gint type)
{
g_assert(stats_locked);
StatsCluster *sc = stats_get_cluster(sc_key);
if (!sc)
{
return FALSE;
}
return stats_cluster_is_alive(sc, type);
}
StatsCounterItem *
stats_get_counter(const StatsClusterKey *sc_key, gint type)
{
g_assert(stats_locked);
StatsCluster *sc = stats_get_cluster(sc_key);
if (!sc)
return NULL;
return stats_cluster_get_counter(sc, type);
}
static void
_foreach_cluster_helper(gpointer key, gpointer value, gpointer user_data)
{
gpointer *args = (gpointer *) user_data;
StatsForeachClusterFunc func = args[0];
gpointer func_data = args[1];
StatsCluster *sc = (StatsCluster *) value;
func(sc, func_data);
}
static void
_foreach_cluster(GHashTable *clusters, gpointer *args, gboolean *cancelled)
{
GHashTableIter iter;
g_hash_table_iter_init(&iter, clusters);
gpointer key, value;
while (g_hash_table_iter_next(&iter, &key, &value))
{
if (cancelled && *cancelled)
break;
_foreach_cluster_helper(key, value, args);
}
}
void
stats_foreach_cluster(StatsForeachClusterFunc func, gpointer user_data, gboolean *cancelled)
{
gpointer args[] = { func, user_data };
g_assert(stats_locked);
_foreach_cluster(stats_cluster_container.static_clusters, args, cancelled);
_foreach_cluster(stats_cluster_container.dynamic_clusters, args, cancelled);
}
static gboolean
_foreach_cluster_remove_helper(gpointer key, gpointer value, gpointer user_data)
{
gpointer *args = (gpointer *) user_data;
StatsForeachClusterRemoveFunc func = args[0];
gpointer func_data = args[1];
StatsCluster *sc = (StatsCluster *) value;
gboolean should_be_removed = func(sc, func_data) && stats_cluster_is_orphaned(sc);
return should_be_removed;
}
void
stats_foreach_cluster_remove(StatsForeachClusterRemoveFunc func, gpointer user_data)
{
gpointer args[] = { func, user_data };
g_hash_table_foreach_remove(stats_cluster_container.static_clusters, _foreach_cluster_remove_helper, args);
g_hash_table_foreach_remove(stats_cluster_container.dynamic_clusters, _foreach_cluster_remove_helper, args);
}
static void
_foreach_counter_helper(StatsCluster *sc, gpointer user_data)
{
gpointer *args = (gpointer *) user_data;
StatsForeachCounterFunc func = args[0];
gpointer func_data = args[1];
stats_cluster_foreach_counter(sc, func, func_data);
}
static void
_foreach_legacy_counter_helper(StatsCluster *sc, gpointer user_data)
{
if (stats_cluster_key_is_legacy(&sc->key))
_foreach_counter_helper(sc, user_data);
}
void
stats_foreach_counter(StatsForeachCounterFunc func, gpointer user_data, gboolean *cancelled)
{
gpointer args[] = { func, user_data };
g_assert(stats_locked);
stats_foreach_cluster(_foreach_counter_helper, args, cancelled);
}
void
stats_foreach_legacy_counter(StatsForeachCounterFunc func, gpointer user_data, gboolean *cancelled)
{
gpointer args[] = { func, user_data };
g_assert(stats_locked);
stats_foreach_cluster(_foreach_legacy_counter_helper, args, cancelled);
}
void
stats_registry_init(void)
{
stats_cluster_container.static_clusters = g_hash_table_new_full((GHashFunc) stats_cluster_key_hash,
(GEqualFunc) stats_cluster_key_equal, NULL,
(GDestroyNotify) stats_cluster_free);
stats_cluster_container.dynamic_clusters = g_hash_table_new_full((GHashFunc) stats_cluster_key_hash,
(GEqualFunc) stats_cluster_key_equal, NULL,
(GDestroyNotify) stats_cluster_free);
g_mutex_init(&stats_mutex);
}
void
stats_registry_deinit(void)
{
g_hash_table_destroy(stats_cluster_container.static_clusters);
g_hash_table_destroy(stats_cluster_container.dynamic_clusters);
stats_cluster_container.static_clusters = NULL;
stats_cluster_container.dynamic_clusters = NULL;
g_mutex_clear(&stats_mutex);
}
|