File: stats-aggregator-registry.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (246 lines) | stat: -rw-r--r-- 5,712 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (c) 2021 One Identity
 *
 * 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/aggregator/stats-aggregator-registry.h"
#include "stats/stats-registry.h"
#include "stats/stats-cluster.h"
#include "stats/stats-query.h"
#include "cfg.h"
#include "iv.h"
#include "timeutils/cache.h"
#include "mainloop.h"
#include "messages.h"

#define FREQUENCY_OF_UPDATE 60

typedef struct
{
  GHashTable *aggregators;
} StatsAggregatorContainer;

static StatsAggregatorContainer stats_container;

static GMutex stats_aggregator_mutex;
static gboolean stats_aggregator_locked;

void
stats_aggregator_lock(void)
{
  g_mutex_lock(&stats_aggregator_mutex);
  stats_aggregator_locked = TRUE;
}

void
stats_aggregator_unlock(void)
{
  stats_aggregator_locked = FALSE;
  g_mutex_unlock(&stats_aggregator_mutex);
}

/* time based aggregation */



/* handle orphaned of aggregators */

static gboolean
_remove_orphaned_helper(gpointer _key, gpointer _value, gpointer _user_data)
{
  StatsAggregator *aggr = (StatsAggregator *) _value;
  if (stats_aggregator_is_orphaned(aggr))
    {
      stats_aggregator_free(aggr);
      return TRUE;
    }
  return FALSE;
}

void
stats_aggregator_remove_orphaned_stats(void)
{
  g_assert(stats_aggregator_locked);

  g_hash_table_foreach_remove(stats_container.aggregators, _remove_orphaned_helper, NULL);
}

/* reset aggregators, i.e. syslog-ng-ctl stats --reset */

static void
_reset_func (gpointer _key, gpointer _value, gpointer _user_data)
{
  StatsAggregator *aggr = (StatsAggregator *) _value;
  stats_aggregator_reset(aggr);
}

void
stats_aggregator_registry_reset(void)
{
  g_assert(stats_aggregator_locked);
  main_loop_assert_main_thread();

  g_hash_table_foreach(stats_container.aggregators, _reset_func, NULL);
}

/* aggregator cleanup */

static gboolean
_cleanup_aggregator(gpointer _key, gpointer _value, gpointer _user_data)
{
  StatsAggregator *aggr = (StatsAggregator *) _value;
  if (!stats_aggregator_is_orphaned(aggr))
    stats_aggregator_unregister(aggr);

  stats_aggregator_free(aggr);
  return TRUE;
}

static void
stats_aggregator_cleanup(void)
{
  g_assert(stats_aggregator_locked);

  g_hash_table_foreach_remove(stats_container.aggregators, _cleanup_aggregator, NULL);
}

/* module init/deinit */

void
stats_aggregator_registry_init(void)
{
  g_mutex_init(&stats_aggregator_mutex);

  stats_container.aggregators = g_hash_table_new_full((GHashFunc) stats_cluster_key_hash,
                                                      (GEqualFunc) stats_cluster_key_equal, NULL, NULL);
}

void
stats_aggregator_registry_deinit(void)
{
  stats_aggregator_lock();
  stats_aggregator_cleanup();
  stats_aggregator_unlock();
  g_hash_table_destroy(stats_container.aggregators);
  stats_container.aggregators = NULL;
  g_mutex_clear(&stats_aggregator_mutex);
}

/* type specific registration helpers */

static void
_insert_to_table(StatsAggregator *aggr)
{
  g_hash_table_insert(stats_container.aggregators, &aggr->key, aggr);
}

static gboolean
_is_in_table(StatsClusterKey *sc_key)
{
  return g_hash_table_lookup(stats_container.aggregators, sc_key) != NULL;
}

static StatsAggregator *
_get_from_table(StatsClusterKey *sc_key)
{
  return g_hash_table_lookup(stats_container.aggregators, sc_key);
}

void
stats_register_aggregator_maximum(gint level, StatsClusterKey *sc_key, StatsAggregator **aggr)
{
  g_assert(stats_aggregator_locked);

  if (!stats_check_level(level))
    {
      *aggr = NULL;
      return;
    }

  if (!_is_in_table(sc_key))
    {
      *aggr = stats_aggregator_maximum_new(level, sc_key);
      _insert_to_table(*aggr);
    }
  else
    {
      *aggr = _get_from_table(sc_key);
    }

  stats_aggregator_start(*aggr);
}

void
stats_register_aggregator_average(gint level, StatsClusterKey *sc_key, StatsAggregator **aggr)
{
  g_assert(stats_aggregator_locked);

  if (!stats_check_level(level))
    {
      *aggr = NULL;
      return;
    }

  if (!_is_in_table(sc_key))
    {
      *aggr = stats_aggregator_average_new(level, sc_key);
      _insert_to_table(*aggr);
    }
  else
    {
      *aggr = _get_from_table(sc_key);
    }

  stats_aggregator_start(*aggr);
}

void
stats_register_aggregator_cps(gint level, StatsClusterKey *sc_key, StatsClusterKey *sc_key_input, gint stats_type,
                              StatsAggregator **aggr)
{
  g_assert(stats_aggregator_locked);

  if (!stats_check_level(level))
    {
      *aggr = NULL;
      return;
    }

  if (!_is_in_table(sc_key))
    {
      *aggr = stats_aggregator_cps_new(level, sc_key, sc_key_input, stats_type);
      _insert_to_table(*aggr);
    }
  else
    {
      *aggr = _get_from_table(sc_key);
    }

  stats_aggregator_start(*aggr);
}

void
stats_unregister_aggregator(StatsAggregator **aggr)
{
  g_assert(stats_aggregator_locked);
  stats_aggregator_stop(*aggr);
  *aggr = NULL;
}