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
|
/*
* Copyright (c) 2021 One Identity
*
* 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 "mqtt-source.h"
#include <MQTTClient.h>
#include "messages.h"
#define RECEIVE_TIMEOUT 1000
static NVHandle handle_mqtt_topic = 0;
void
mqtt_sd_set_topic(LogDriver *s, const gchar *topic)
{
MQTTSourceDriver *self = (MQTTSourceDriver *) s;
g_free(self->topic);
self->topic = g_strdup(topic);
}
const gchar *
_format_persist_name(const LogPipe *s)
{
MQTTSourceDriver *self = (MQTTSourceDriver *) s;
static gchar stats_instance[1024];
if (((LogPipe *)s)->persist_name)
g_snprintf(stats_instance, sizeof(stats_instance), "%s", ((LogPipe *)s)->persist_name);
else
g_snprintf(stats_instance, sizeof(stats_instance), "mqtt,source,%s,%s", mqtt_client_options_get_address(&self->options),
self->topic);
return stats_instance;
}
static void
_format_stats_key(LogThreadedSourceDriver *s, StatsClusterKeyBuilder *kb)
{
MQTTSourceDriver *self = (MQTTSourceDriver *) s;
stats_cluster_key_builder_add_legacy_label(kb, stats_cluster_label("driver", "mqtt-source"));
stats_cluster_key_builder_add_legacy_label(kb, stats_cluster_label("address",
mqtt_client_options_get_address(&self->options)));
stats_cluster_key_builder_add_legacy_label(kb, stats_cluster_label("topic", self->topic));
}
static gboolean
_client_init(MQTTSourceDriver *self)
{
gint rc;
if ((rc = MQTTClient_create(&self->client, mqtt_client_options_get_address(&self->options),
mqtt_client_options_get_client_id(&self->options),
MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTCLIENT_SUCCESS)
{
msg_error("Error creating mqtt client",
evt_tag_str("address", mqtt_client_options_get_address(&self->options)),
evt_tag_str("error_code", MQTTClient_strerror(rc)),
evt_tag_str("client_id", mqtt_client_options_get_client_id(&self->options)),
log_pipe_location_tag(&self->super.super.super.super.super));
return FALSE;
}
return TRUE;
}
static gint
_log_ssl_errors(const gchar *str, gsize len, gpointer u)
{
MQTTSourceDriver *self = (MQTTSourceDriver *) u;
msg_error("MQTT TLS error", evt_tag_mem("line", str, len),
log_pipe_location_tag(&self->super.super.super.super.super));
return TRUE;
}
static gboolean
_subscribe_topic(MQTTSourceDriver *self)
{
gint rc;
if ((rc = MQTTClient_subscribe(self->client, self->topic,
mqtt_client_options_get_qos(&self->options))) != MQTTCLIENT_SUCCESS)
{
msg_error("mqtt: Error while subscribing to topic",
evt_tag_str("topic", self->topic),
evt_tag_int("qos", mqtt_client_options_get_qos(&self->options)),
evt_tag_str("error_code", MQTTClient_strerror(rc)),
evt_tag_str("driver", self->super.super.super.super.id),
log_pipe_location_tag(&self->super.super.super.super.super));
return FALSE;
}
return TRUE;
}
static void
_thread_init(LogThreadedFetcherDriver *s)
{
MQTTSourceDriver *self = (MQTTSourceDriver *)s;
_client_init(self);
}
static void
_thread_deinit(LogThreadedFetcherDriver *s)
{
MQTTSourceDriver *self = (MQTTSourceDriver *)s;
MQTTClient_destroy(&self->client);
}
static gboolean
_connect(LogThreadedFetcherDriver *s)
{
MQTTSourceDriver *self = (MQTTSourceDriver *)s;
gint rc;
MQTTClient_connectOptions conn_opts;
MQTTClient_SSLOptions ssl_opts;
mqtt_client_options_to_mqtt_client_connection_option(&self->options, &conn_opts, &ssl_opts);
if ((rc = MQTTClient_connect(self->client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
msg_error("Error connecting to mqtt server",
evt_tag_str("error_code", MQTTClient_strerror(rc)),
evt_tag_str("client_id", mqtt_client_options_get_client_id(&self->options)),
log_pipe_location_tag(&self->super.super.super.super.super));
return FALSE;
}
if (!_subscribe_topic(self))
return FALSE;
return TRUE;
}
static void
_disconnect(LogThreadedFetcherDriver *s)
{
MQTTSourceDriver *self = (MQTTSourceDriver *)s;
MQTTClient_disconnect(self->client, MQTT_DISCONNECT_TIMEOUT);
}
static ThreadedFetchResult
_receive_result_evaluation(gint rc, MQTTClient_message *message)
{
if (rc == MQTTCLIENT_SUCCESS && message != NULL)
return THREADED_FETCH_SUCCESS;
if (rc == MQTTCLIENT_TOPICNAME_TRUNCATED && message != NULL)
return THREADED_FETCH_SUCCESS;
if (rc == MQTTCLIENT_SUCCESS && message == NULL)
// timeout
return THREADED_FETCH_NO_DATA;
return THREADED_FETCH_ERROR;
}
static LogThreadedFetchResult
_fetch(LogThreadedFetcherDriver *s)
{
MQTTSourceDriver *self = (MQTTSourceDriver *)s;
ThreadedFetchResult result = THREADED_FETCH_ERROR;
char *topicName = NULL;
int topicLen;
MQTTClient_message *message = NULL;
LogMessage *msg = NULL;
gint rc = MQTTClient_receive(self->client, &topicName, &topicLen, &message, RECEIVE_TIMEOUT);
result = _receive_result_evaluation(rc, message);
if (result == THREADED_FETCH_SUCCESS)
{
msg = log_msg_new_empty();
log_msg_set_value(msg, LM_V_MESSAGE, (gchar *)message->payload, message->payloadlen);
log_msg_set_value(msg, handle_mqtt_topic, topicName, topicLen);
log_msg_set_value_to_string(msg, LM_V_TRANSPORT, "mqtt");
MQTTClient_freeMessage(&message);
MQTTClient_free(topicName);
}
if (result == THREADED_FETCH_ERROR)
{
msg_error("Error while receiving msg",
evt_tag_str("error_code", MQTTClient_strerror(rc)),
evt_tag_str("client_id", mqtt_client_options_get_client_id(&self->options)),
log_pipe_location_tag(&self->super.super.super.super.super));
}
return (LogThreadedFetchResult)
{
result, msg
};
}
static gboolean
_init(LogPipe *s)
{
MQTTSourceDriver *self = (MQTTSourceDriver *)s;
handle_mqtt_topic = log_msg_get_value_handle("MQTT_TOPIC");
if (!self->topic)
{
msg_error("mqtt: the topic() argument is required for mqtt source",
log_pipe_location_tag(&self->super.super.super.super.super));
return FALSE;
}
if(!mqtt_client_options_checker(&self->options))
return FALSE;
if(!log_threaded_fetcher_driver_init_method(s))
return FALSE;
if (mqtt_client_options_get_client_id(&self->options) == NULL)
{
gchar *tmp_client_id;
tmp_client_id = g_strdup_printf("syslog-ng-source-%s", self->topic);
mqtt_client_options_set_client_id(&self->options, tmp_client_id);
g_free(tmp_client_id);
}
return TRUE;
}
static gboolean
_deinit(LogPipe *s)
{
return log_threaded_fetcher_driver_deinit_method(s);
}
static void
_free(LogPipe *s)
{
MQTTSourceDriver *self = (MQTTSourceDriver *)s;
mqtt_client_options_destroy(&self->options);
g_free(self->topic);
log_threaded_fetcher_driver_free_method(s);
}
LogDriver *
mqtt_sd_new(GlobalConfig *cfg)
{
MQTTSourceDriver *self = g_new0(MQTTSourceDriver, 1);
log_threaded_fetcher_driver_init_instance(&self->super, cfg);
log_threaded_source_driver_set_transport_name(&self->super.super, "mqtt");
mqtt_client_options_defaults(&self->options);
mqtt_client_options_set_log_ssl_error_fn(&self->options, self, _log_ssl_errors);
self->super.super.super.super.super.init = _init;
self->super.super.super.super.super.deinit = _deinit;
self->super.super.super.super.super.free_fn = _free;
self->super.connect = _connect;
self->super.disconnect = _disconnect;
self->super.fetch = _fetch;
self->super.thread_init = _thread_init;
self->super.thread_deinit = _thread_deinit;
self->super.super.super.super.super.generate_persist_name = _format_persist_name;
self->super.super.format_stats_key = _format_stats_key;
return &self->super.super.super.super;
}
MQTTClientOptions *
mqtt_sd_get_options(LogDriver *s)
{
MQTTSourceDriver *self = (MQTTSourceDriver *)s;
return &self->options;
}
|