File: graphite-output.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 (187 lines) | stat: -rw-r--r-- 5,907 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
/*
 * Copyright (c) 2014 Balabit
 * Copyright (c) 2014 Viktor Tusa <tusa@balabit.hu>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; 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 "graphite-output.h"
#include "template/templates.h"
#include "logmsg/logmsg.h"
#include "value-pairs/value-pairs.h"
#include "value-pairs/cmdline.h"

typedef struct _TFGraphiteState
{
  ValuePairs *vp;
  LogTemplate *timestamp_template;
} TFGraphiteState;

typedef struct _TFGraphiteArgumentsUserData
{
  TFGraphiteState *state;
  GlobalConfig *cfg;
} TFGraphiteArgumentsUserData;

static gboolean
tf_graphite_set_timestamp(const gchar *option_name, const gchar *value,
                          gpointer data, GError **error)
{
  TFGraphiteArgumentsUserData *args = (TFGraphiteArgumentsUserData *) data;

  args->state->timestamp_template = log_template_new(args->cfg, NULL);

  if (!log_template_compile(args->state->timestamp_template, value, error))
    {
      log_template_unref(args->state->timestamp_template);
      args->state->timestamp_template = NULL;
      return FALSE;
    }

  return TRUE;
};

static gboolean
tf_graphite_parse_command_line_arguments(TFGraphiteState *self, gint *argc, gchar ***argv, LogTemplate *parent)
{
  GOptionContext *ctx;
  GOptionGroup *og;
  TFGraphiteArgumentsUserData userdata;
  gboolean success;
  GError *error = NULL;

  GOptionEntry graphite_options[] =
  {
    { "timestamp", 't', 0, G_OPTION_ARG_CALLBACK, tf_graphite_set_timestamp, NULL, NULL },
    { NULL },
  };

  userdata.state = self;
  userdata.cfg = parent->cfg;

  ctx = g_option_context_new ("graphite-options");
  og = g_option_group_new (NULL, NULL, NULL, &userdata, NULL);
  g_option_group_add_entries(og, graphite_options);
  g_option_context_set_main_group(ctx, og);
  g_option_context_set_ignore_unknown_options(ctx, TRUE);

  success = g_option_context_parse (ctx, argc, argv, &error);
  g_option_context_free (ctx);
  if (error)
    g_error_free(error);

  return success;
}

static gboolean
tf_graphite_prepare(LogTemplateFunction *self, gpointer s, LogTemplate *parent,
                    gint argc, gchar *argv[],
                    GError **error)
{
  TFGraphiteState *state = (TFGraphiteState *)s;
  ValuePairsTransformSet *vpts;

  if (!tf_graphite_parse_command_line_arguments(state, &argc, &argv, parent))
    return FALSE;

  if (!state->timestamp_template)
    {
      state->timestamp_template = log_template_new(parent->cfg, NULL);
      log_template_compile(state->timestamp_template, "$R_UNIXTIME", NULL);
    }

  state->vp = value_pairs_new_from_cmdline (parent->cfg, &argc, &argv, NULL, NULL, error);
  if (!state->vp)
    return FALSE;

  /* Always replace a leading dot with an underscore. */
  vpts = value_pairs_transform_set_new(".*");
  value_pairs_transform_set_add_func(vpts, value_pairs_new_transform_replace_prefix(".", "_"));
  value_pairs_add_transforms(state->vp, vpts);

  return TRUE;
}

typedef struct _TFGraphiteForeachUserData
{
  GString *formatted_unixtime;
  GString *result;
} TFGraphiteForeachUserData;

/* TODO escape '\0' when passing down the value */
static gboolean
tf_graphite_foreach_func(const gchar *name, LogMessageValueType type, const gchar *value,
                         gsize value_len, gpointer user_data)
{
  TFGraphiteForeachUserData *data = (TFGraphiteForeachUserData *) user_data;

  g_string_append(data->result, name);
  g_string_append_c(data->result, ' ');
  g_string_append(data->result, value);
  g_string_append_c(data->result, ' ');
  g_string_append(data->result, data->formatted_unixtime->str);
  g_string_append_c(data->result, '\n');

  return FALSE;
}

static gboolean
tf_graphite_format(GString *result, ValuePairs *vp, LogMessage *msg, LogTemplate *timestamp_template,
                   LogTemplateEvalOptions *options)
{
  TFGraphiteForeachUserData userdata;
  gboolean return_value;

  userdata.result = result;
  userdata.formatted_unixtime = g_string_new("");
  log_template_format(timestamp_template, msg, &DEFAULT_TEMPLATE_EVAL_OPTIONS, userdata.formatted_unixtime);

  return_value = value_pairs_foreach(vp, tf_graphite_foreach_func, msg, options, &userdata);

  g_string_free(userdata.formatted_unixtime, TRUE);
  return return_value;
}

static void
tf_graphite_call(LogTemplateFunction *self, gpointer s,
                 const LogTemplateInvokeArgs *args, GString *result, LogMessageValueType *type)
{
  TFGraphiteState *state = (TFGraphiteState *)s;
  gint i;
  gboolean r = TRUE;
  gsize orig_size = result->len;

  *type = LM_VT_STRING;
  for (i = 0; i < args->num_messages; i++)
    r &= tf_graphite_format(result, state->vp, args->messages[i], state->timestamp_template, args->options);

  if (!r && (args->options->opts->on_error & ON_ERROR_DROP_MESSAGE))
    g_string_set_size(result, orig_size);
}

static void
tf_graphite_free_state(gpointer s)
{
  TFGraphiteState *state = (TFGraphiteState *)s;

  value_pairs_unref(state->vp);
  log_template_unref(state->timestamp_template);
}

TEMPLATE_FUNCTION(TFGraphiteState, tf_graphite, tf_graphite_prepare, NULL, tf_graphite_call,
                  tf_graphite_free_state, NULL);