| 1 |
/** |
| 2 |
* collectd - src/perl.c |
| 3 |
* Copyright (C) 2007-2009 Sebastian Harl |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 6 |
* copy of this software and associated documentation files (the "Software"), |
| 7 |
* to deal in the Software without restriction, including without limitation |
| 8 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following conditions: |
| 11 |
* |
| 12 |
* The above copyright notice and this permission notice shall be included in |
| 13 |
* all copies or substantial portions of the Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 |
* DEALINGS IN THE SOFTWARE. |
| 22 |
* |
| 23 |
* Authors: |
| 24 |
* Sebastian Harl <sh at tokkee.org> |
| 25 |
* Pavel Rochnyak <pavel2000 ngs.ru> |
| 26 |
**/ |
| 27 |
|
| 28 |
/* |
| 29 |
* This plugin embeds a Perl interpreter into collectd and provides an |
| 30 |
* interface for collectd plugins written in perl. |
| 31 |
*/ |
| 32 |
|
| 33 |
/* do not automatically get the thread specific Perl interpreter */ |
| 34 |
#define PERL_NO_GET_CONTEXT |
| 35 |
|
| 36 |
#include "collectd.h" |
| 37 |
#include <stdbool.h> |
| 38 |
|
| 39 |
#include <EXTERN.h> |
| 40 |
#include <perl.h> |
| 41 |
|
| 42 |
#include <XSUB.h> |
| 43 |
|
| 44 |
/* Some versions of Perl define their own version of DEBUG... :-/ */ |
| 45 |
#ifdef DEBUG |
| 46 |
#undef DEBUG |
| 47 |
#endif /* DEBUG */ |
| 48 |
|
| 49 |
/* ... while we want the definition found in plugin.h. */ |
| 50 |
#include "plugin.h" |
| 51 |
#include "utils/common/common.h" |
| 52 |
|
| 53 |
#include "filter_chain.h" |
| 54 |
|
| 55 |
#if !defined(USE_ITHREADS) |
| 56 |
#error "Perl does not support ithreads!" |
| 57 |
#endif /* !defined(USE_ITHREADS) */ |
| 58 |
|
| 59 |
/* clear the Perl sub's stack frame |
| 60 |
* (this should only be used inside an XSUB) */ |
| 61 |
#define CLEAR_STACK_FRAME PL_stack_sp = PL_stack_base + *PL_markstack_ptr |
| 62 |
|
| 63 |
#define PLUGIN_INIT 0 |
| 64 |
#define PLUGIN_READ 1 |
| 65 |
#define PLUGIN_WRITE 2 |
| 66 |
#define PLUGIN_SHUTDOWN 3 |
| 67 |
#define PLUGIN_LOG 4 |
| 68 |
#define PLUGIN_NOTIF 5 |
| 69 |
#define PLUGIN_FLUSH 6 |
| 70 |
#define PLUGIN_FLUSH_ALL 7 /* For collectd-5.6 only */ |
| 71 |
|
| 72 |
#define PLUGIN_TYPES 8 |
| 73 |
|
| 74 |
#define PLUGIN_CONFIG 254 |
| 75 |
#define PLUGIN_DATASET 255 |
| 76 |
|
| 77 |
#define FC_MATCH 0 |
| 78 |
#define FC_TARGET 1 |
| 79 |
|
| 80 |
#define FC_TYPES 2 |
| 81 |
|
| 82 |
#define FC_CB_CREATE 0 |
| 83 |
#define FC_CB_DESTROY 1 |
| 84 |
#define FC_CB_EXEC 2 |
| 85 |
|
| 86 |
#define FC_CB_TYPES 3 |
| 87 |
|
| 88 |
#define log_debug(...) DEBUG("perl: " __VA_ARGS__) |
| 89 |
#define log_info(...) INFO("perl: " __VA_ARGS__) |
| 90 |
#define log_warn(...) WARNING("perl: " __VA_ARGS__) |
| 91 |
#define log_err(...) ERROR("perl: " __VA_ARGS__) |
| 92 |
|
| 93 |
/* this is defined in DynaLoader.a */ |
| 94 |
void boot_DynaLoader(PerlInterpreter *, CV *); |
| 95 |
|
| 96 |
static XS(Collectd_plugin_register_read); |
| 97 |
static XS(Collectd_plugin_register_write); |
| 98 |
static XS(Collectd_plugin_register_log); |
| 99 |
static XS(Collectd_plugin_register_notification); |
| 100 |
static XS(Collectd_plugin_register_flush); |
| 101 |
static XS(Collectd_plugin_unregister_read); |
| 102 |
static XS(Collectd_plugin_unregister_write); |
| 103 |
static XS(Collectd_plugin_unregister_log); |
| 104 |
static XS(Collectd_plugin_unregister_notification); |
| 105 |
static XS(Collectd_plugin_unregister_flush); |
| 106 |
static XS(Collectd_plugin_register_ds); |
| 107 |
static XS(Collectd_plugin_unregister_ds); |
| 108 |
static XS(Collectd_plugin_dispatch_values); |
| 109 |
static XS(Collectd_plugin_get_interval); |
| 110 |
static XS(Collectd__plugin_write); |
| 111 |
static XS(Collectd__plugin_flush); |
| 112 |
static XS(Collectd_plugin_dispatch_notification); |
| 113 |
static XS(Collectd_plugin_log); |
| 114 |
static XS(Collectd__fc_register); |
| 115 |
static XS(Collectd_call_by_name); |
| 116 |
|
| 117 |
static int perl_read(user_data_t *ud); |
| 118 |
static int perl_write(const data_set_t *ds, const value_list_t *vl, |
| 119 |
user_data_t *user_data); |
| 120 |
static void perl_log(int level, const char *msg, user_data_t *user_data); |
| 121 |
static int perl_notify(const notification_t *notif, user_data_t *user_data); |
| 122 |
static int perl_flush(cdtime_t timeout, const char *identifier, |
| 123 |
user_data_t *user_data); |
| 124 |
|
| 125 |
/* |
| 126 |
* private data types |
| 127 |
*/ |
| 128 |
|
| 129 |
typedef struct c_ithread_s { |
| 130 |
/* the thread's Perl interpreter */ |
| 131 |
PerlInterpreter *interp; |
| 132 |
bool running; /* thread is inside Perl interpreter */ |
| 133 |
bool shutdown; |
| 134 |
pthread_t pthread; |
| 135 |
|
| 136 |
/* double linked list of threads */ |
| 137 |
struct c_ithread_s *prev; |
| 138 |
struct c_ithread_s *next; |
| 139 |
} c_ithread_t; |
| 140 |
|
| 141 |
typedef struct { |
| 142 |
c_ithread_t *head; |
| 143 |
c_ithread_t *tail; |
| 144 |
|
| 145 |
#if COLLECT_DEBUG |
| 146 |
/* some usage stats */ |
| 147 |
int number_of_threads; |
| 148 |
#endif /* COLLECT_DEBUG */ |
| 149 |
|
| 150 |
pthread_mutex_t mutex; |
| 151 |
pthread_mutexattr_t mutexattr; |
| 152 |
} c_ithread_list_t; |
| 153 |
|
| 154 |
/* name / user_data for Perl matches / targets */ |
| 155 |
typedef struct { |
| 156 |
char *name; |
| 157 |
SV *user_data; |
| 158 |
} pfc_user_data_t; |
| 159 |
|
| 160 |
#define PFC_USER_DATA_FREE(data) \ |
| 161 |
do { \ |
| 162 |
sfree((data)->name); \ |
| 163 |
if (NULL != (data)->user_data) \ |
| 164 |
sv_free((data)->user_data); \ |
| 165 |
sfree(data); \ |
| 166 |
} while (0) |
| 167 |
|
| 168 |
/* |
| 169 |
* Public variable |
| 170 |
*/ |
| 171 |
extern char **environ; |
| 172 |
|
| 173 |
/* |
| 174 |
* private variables |
| 175 |
*/ |
| 176 |
|
| 177 |
static bool register_legacy_flush = true; |
| 178 |
|
| 179 |
/* if perl_threads != NULL perl_threads->head must |
| 180 |
* point to the "base" thread */ |
| 181 |
static c_ithread_list_t *perl_threads; |
| 182 |
|
| 183 |
/* the key used to store each pthread's ithread */ |
| 184 |
static pthread_key_t perl_thr_key; |
| 185 |
|
| 186 |
static int perl_argc; |
| 187 |
static char **perl_argv; |
| 188 |
|
| 189 |
static char base_name[DATA_MAX_NAME_LEN] = ""; |
| 190 |
|
| 191 |
static struct { |
| 192 |
char name[64]; |
| 193 |
XS((*f)); |
| 194 |
} api[] = { |
| 195 |
{"Collectd::plugin_register_read", Collectd_plugin_register_read}, |
| 196 |
{"Collectd::plugin_register_write", Collectd_plugin_register_write}, |
| 197 |
{"Collectd::plugin_register_log", Collectd_plugin_register_log}, |
| 198 |
{"Collectd::plugin_register_notification", |
| 199 |
Collectd_plugin_register_notification}, |
| 200 |
{"Collectd::plugin_register_flush", Collectd_plugin_register_flush}, |
| 201 |
{"Collectd::plugin_unregister_read", Collectd_plugin_unregister_read}, |
| 202 |
{"Collectd::plugin_unregister_write", Collectd_plugin_unregister_write}, |
| 203 |
{"Collectd::plugin_unregister_log", Collectd_plugin_unregister_log}, |
| 204 |
{"Collectd::plugin_unregister_notification", |
| 205 |
Collectd_plugin_unregister_notification}, |
| 206 |
{"Collectd::plugin_unregister_flush", Collectd_plugin_unregister_flush}, |
| 207 |
{"Collectd::plugin_register_data_set", Collectd_plugin_register_ds}, |
| 208 |
{"Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds}, |
| 209 |
{"Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values}, |
| 210 |
{"Collectd::plugin_get_interval", Collectd_plugin_get_interval}, |
| 211 |
{"Collectd::_plugin_write", Collectd__plugin_write}, |
| 212 |
{"Collectd::_plugin_flush", Collectd__plugin_flush}, |
| 213 |
{"Collectd::plugin_dispatch_notification", |
| 214 |
Collectd_plugin_dispatch_notification}, |
| 215 |
{"Collectd::plugin_log", Collectd_plugin_log}, |
| 216 |
{"Collectd::_fc_register", Collectd__fc_register}, |
| 217 |
{"Collectd::call_by_name", Collectd_call_by_name}, |
| 218 |
{"", NULL}}; |
| 219 |
|
| 220 |
struct { |
| 221 |
char name[64]; |
| 222 |
int value; |
| 223 |
} constants[] = {{"Collectd::TYPE_INIT", PLUGIN_INIT}, |
| 224 |
{"Collectd::TYPE_READ", PLUGIN_READ}, |
| 225 |
{"Collectd::TYPE_WRITE", PLUGIN_WRITE}, |
| 226 |
{"Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN}, |
| 227 |
{"Collectd::TYPE_LOG", PLUGIN_LOG}, |
| 228 |
{"Collectd::TYPE_NOTIF", PLUGIN_NOTIF}, |
| 229 |
{"Collectd::TYPE_FLUSH", PLUGIN_FLUSH}, |
| 230 |
{"Collectd::TYPE_CONFIG", PLUGIN_CONFIG}, |
| 231 |
{"Collectd::TYPE_DATASET", PLUGIN_DATASET}, |
| 232 |
{"Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER}, |
| 233 |
{"Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE}, |
| 234 |
{"Collectd::DS_TYPE_DERIVE", DS_TYPE_DERIVE}, |
| 235 |
{"Collectd::DS_TYPE_ABSOLUTE", DS_TYPE_ABSOLUTE}, |
| 236 |
{"Collectd::LOG_ERR", LOG_ERR}, |
| 237 |
{"Collectd::LOG_WARNING", LOG_WARNING}, |
| 238 |
{"Collectd::LOG_NOTICE", LOG_NOTICE}, |
| 239 |
{"Collectd::LOG_INFO", LOG_INFO}, |
| 240 |
{"Collectd::LOG_DEBUG", LOG_DEBUG}, |
| 241 |
{"Collectd::FC_MATCH", FC_MATCH}, |
| 242 |
{"Collectd::FC_TARGET", FC_TARGET}, |
| 243 |
{"Collectd::FC_CB_CREATE", FC_CB_CREATE}, |
| 244 |
{"Collectd::FC_CB_DESTROY", FC_CB_DESTROY}, |
| 245 |
{"Collectd::FC_CB_EXEC", FC_CB_EXEC}, |
| 246 |
{"Collectd::FC_MATCH_NO_MATCH", FC_MATCH_NO_MATCH}, |
| 247 |
{"Collectd::FC_MATCH_MATCHES", FC_MATCH_MATCHES}, |
| 248 |
{"Collectd::FC_TARGET_CONTINUE", FC_TARGET_CONTINUE}, |
| 249 |
{"Collectd::FC_TARGET_STOP", FC_TARGET_STOP}, |
| 250 |
{"Collectd::FC_TARGET_RETURN", FC_TARGET_RETURN}, |
| 251 |
{"Collectd::NOTIF_FAILURE", NOTIF_FAILURE}, |
| 252 |
{"Collectd::NOTIF_WARNING", NOTIF_WARNING}, |
| 253 |
{"Collectd::NOTIF_OKAY", NOTIF_OKAY}, |
| 254 |
{"", 0}}; |
| 255 |
/* |
| 256 |
* Helper functions for data type conversion. |
| 257 |
*/ |
| 258 |
|
| 259 |
/* |
| 260 |
* data source: |
| 261 |
* [ |
| 262 |
* { |
| 263 |
* name => $ds_name, |
| 264 |
* type => $ds_type, |
| 265 |
* min => $ds_min, |
| 266 |
* max => $ds_max |
| 267 |
* }, |
| 268 |
* ... |
| 269 |
* ] |
| 270 |
*/ |
| 271 |
static int hv2data_source(pTHX_ HV *hash, data_source_t *ds) { |
| 272 |
SV **tmp = NULL; |
| 273 |
|
| 274 |
if ((NULL == hash) || (NULL == ds)) |
| 275 |
return -1; |
| 276 |
|
| 277 |
if (NULL != (tmp = hv_fetch(hash, "name", 4, 0))) { |
| 278 |
sstrncpy(ds->name, SvPV_nolen(*tmp), sizeof(ds->name)); |
| 279 |
} else { |
| 280 |
log_err("hv2data_source: No DS name given."); |
| 281 |
return -1; |
| 282 |
} |
| 283 |
|
| 284 |
if (NULL != (tmp = hv_fetch(hash, "type", 4, 0))) { |
| 285 |
ds->type = SvIV(*tmp); |
| 286 |
|
| 287 |
if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type) && |
| 288 |
(DS_TYPE_DERIVE != ds->type) && (DS_TYPE_ABSOLUTE != ds->type)) { |
| 289 |
log_err("hv2data_source: Invalid DS type."); |
| 290 |
return -1; |
| 291 |
} |
| 292 |
} else { |
| 293 |
ds->type = DS_TYPE_COUNTER; |
| 294 |
} |
| 295 |
|
| 296 |
if (NULL != (tmp = hv_fetch(hash, "min", 3, 0))) |
| 297 |
ds->min = SvNV(*tmp); |
| 298 |
else |
| 299 |
ds->min = NAN; |
| 300 |
|
| 301 |
if (NULL != (tmp = hv_fetch(hash, "max", 3, 0))) |
| 302 |
ds->max = SvNV(*tmp); |
| 303 |
else |
| 304 |
ds->max = NAN; |
| 305 |
return 0; |
| 306 |
} /* static int hv2data_source (HV *, data_source_t *) */ |
| 307 |
|
| 308 |
/* av2value converts at most "len" elements from "array" to "value". Returns the |
| 309 |
* number of elements converted or zero on error. */ |
| 310 |
static size_t av2value(pTHX_ char *name, AV *array, value_t *value, |
| 311 |
size_t array_len) { |
| 312 |
const data_set_t *ds; |
| 313 |
|
| 314 |
if ((NULL == name) || (NULL == array) || (NULL == value) || (array_len == 0)) |
| 315 |
return 0; |
| 316 |
|
| 317 |
ds = plugin_get_ds(name); |
| 318 |
if (NULL == ds) { |
| 319 |
log_err("av2value: Unknown dataset \"%s\"", name); |
| 320 |
return 0; |
| 321 |
} |
| 322 |
|
| 323 |
if (array_len < ds->ds_num) { |
| 324 |
log_warn("av2value: array does not contain enough elements for type " |
| 325 |
"\"%s\": got %" PRIsz ", want %" PRIsz, |
| 326 |
name, array_len, ds->ds_num); |
| 327 |
return 0; |
| 328 |
} else if (array_len > ds->ds_num) { |
| 329 |
log_warn("av2value: array contains excess elements for type \"%s\": got " |
| 330 |
"%" PRIsz ", want %" PRIsz, |
| 331 |
name, array_len, ds->ds_num); |
| 332 |
} |
| 333 |
|
| 334 |
for (size_t i = 0; i < ds->ds_num; ++i) { |
| 335 |
SV **tmp = av_fetch(array, i, 0); |
| 336 |
|
| 337 |
if (NULL != tmp) { |
| 338 |
if (DS_TYPE_COUNTER == ds->ds[i].type) |
| 339 |
value[i].counter = SvIV(*tmp); |
| 340 |
else if (DS_TYPE_GAUGE == ds->ds[i].type) |
| 341 |
value[i].gauge = SvNV(*tmp); |
| 342 |
else if (DS_TYPE_DERIVE == ds->ds[i].type) |
| 343 |
value[i].derive = SvIV(*tmp); |
| 344 |
else if (DS_TYPE_ABSOLUTE == ds->ds[i].type) |
| 345 |
value[i].absolute = SvIV(*tmp); |
| 346 |
} else { |
| 347 |
return 0; |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
return ds->ds_num; |
| 352 |
} /* static size_t av2value (char *, AV *, value_t *, size_t) */ |
| 353 |
|
| 354 |
/* |
| 355 |
* value list: |
| 356 |
* { |
| 357 |
* values => [ @values ], |
| 358 |
* time => $time, |
| 359 |
* host => $host, |
| 360 |
* plugin => $plugin, |
| 361 |
* plugin_instance => $pinstance, |
| 362 |
* type_instance => $tinstance, |
| 363 |
* } |
| 364 |
*/ |
| 365 |
static int hv2value_list(pTHX_ HV *hash, value_list_t *vl) { |
| 366 |
SV **tmp; |
| 367 |
|
| 368 |
if ((NULL == hash) || (NULL == vl)) |
| 369 |
return -1; |
| 370 |
|
| 371 |
if (NULL == (tmp = hv_fetch(hash, "type", 4, 0))) { |
| 372 |
log_err("hv2value_list: No type given."); |
| 373 |
return -1; |
| 374 |
} |
| 375 |
|
| 376 |
sstrncpy(vl->type, SvPV_nolen(*tmp), sizeof(vl->type)); |
| 377 |
|
| 378 |
if ((NULL == (tmp = hv_fetch(hash, "values", 6, 0))) || |
| 379 |
(!(SvROK(*tmp) && (SVt_PVAV == SvTYPE(SvRV(*tmp)))))) { |
| 380 |
log_err("hv2value_list: No valid values given."); |
| 381 |
return -1; |
| 382 |
} |
| 383 |
|
| 384 |
{ |
| 385 |
AV *array = (AV *)SvRV(*tmp); |
| 386 |
/* av_len returns the highest index, not the actual length. */ |
| 387 |
size_t array_len = (size_t)(av_len(array) + 1); |
| 388 |
if (array_len == 0) |
| 389 |
return -1; |
| 390 |
|
| 391 |
vl->values = calloc(array_len, sizeof(*vl->values)); |
| 392 |
vl->values_len = |
| 393 |
av2value(aTHX_ vl->type, (AV *)SvRV(*tmp), vl->values, array_len); |
| 394 |
if (vl->values_len == 0) { |
| 395 |
sfree(vl->values); |
| 396 |
return -1; |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
if (NULL != (tmp = hv_fetch(hash, "time", 4, 0))) { |
| 401 |
double t = SvNV(*tmp); |
| 402 |
vl->time = DOUBLE_TO_CDTIME_T(t); |
| 403 |
} |
| 404 |
|
| 405 |
if (NULL != (tmp = hv_fetch(hash, "interval", 8, 0))) { |
| 406 |
double t = SvNV(*tmp); |
| 407 |
vl->interval = DOUBLE_TO_CDTIME_T(t); |
| 408 |
} |
| 409 |
|
| 410 |
if (NULL != (tmp = hv_fetch(hash, "host", 4, 0))) |
| 411 |
sstrncpy(vl->host, SvPV_nolen(*tmp), sizeof(vl->host)); |
| 412 |
|
| 413 |
if (NULL != (tmp = hv_fetch(hash, "plugin", 6, 0))) |
| 414 |
sstrncpy(vl->plugin, SvPV_nolen(*tmp), sizeof(vl->plugin)); |
| 415 |
|
| 416 |
if (NULL != (tmp = hv_fetch(hash, "plugin_instance", 15, 0))) |
| 417 |
sstrncpy(vl->plugin_instance, SvPV_nolen(*tmp), |
| 418 |
sizeof(vl->plugin_instance)); |
| 419 |
|
| 420 |
if (NULL != (tmp = hv_fetch(hash, "type_instance", 13, 0))) |
| 421 |
sstrncpy(vl->type_instance, SvPV_nolen(*tmp), sizeof(vl->type_instance)); |
| 422 |
return 0; |
| 423 |
} /* static int hv2value_list (pTHX_ HV *, value_list_t *) */ |
| 424 |
|
| 425 |
static int av2data_set(pTHX_ AV *array, char *name, data_set_t *ds) { |
| 426 |
int len; |
| 427 |
|
| 428 |
if ((NULL == array) || (NULL == name) || (NULL == ds)) |
| 429 |
return -1; |
| 430 |
|
| 431 |
len = av_len(array); |
| 432 |
|
| 433 |
if (-1 == len) { |
| 434 |
log_err("av2data_set: Invalid data set."); |
| 435 |
return -1; |
| 436 |
} |
| 437 |
|
| 438 |
ds->ds = smalloc((len + 1) * sizeof(*ds->ds)); |
| 439 |
ds->ds_num = len + 1; |
| 440 |
|
| 441 |
for (int i = 0; i <= len; ++i) { |
| 442 |
SV **elem = av_fetch(array, i, 0); |
| 443 |
|
| 444 |
if (NULL == elem) { |
| 445 |
log_err("av2data_set: Failed to fetch data source %i.", i); |
| 446 |
return -1; |
| 447 |
} |
| 448 |
|
| 449 |
if (!(SvROK(*elem) && (SVt_PVHV == SvTYPE(SvRV(*elem))))) { |
| 450 |
log_err("av2data_set: Invalid data source."); |
| 451 |
return -1; |
| 452 |
} |
| 453 |
|
| 454 |
if (-1 == hv2data_source(aTHX_(HV *) SvRV(*elem), &ds->ds[i])) |
| 455 |
return -1; |
| 456 |
|
| 457 |
log_debug("av2data_set: " |
| 458 |
"DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f", |
| 459 |
ds->ds[i].name, ds->ds[i].type, ds->ds[i].min, ds->ds[i].max); |
| 460 |
} |
| 461 |
|
| 462 |
sstrncpy(ds->type, name, sizeof(ds->type)); |
| 463 |
return 0; |
| 464 |
} /* static int av2data_set (pTHX_ AV *, data_set_t *) */ |
| 465 |
|
| 466 |
/* |
| 467 |
* notification: |
| 468 |
* { |
| 469 |
* severity => $severity, |
| 470 |
* time => $time, |
| 471 |
* message => $msg, |
| 472 |
* host => $host, |
| 473 |
* plugin => $plugin, |
| 474 |
* type => $type, |
| 475 |
* plugin_instance => $instance, |
| 476 |
* type_instance => $type_instance, |
| 477 |
* meta => [ { name => <name>, value => <value> }, ... ] |
| 478 |
* } |
| 479 |
*/ |
| 480 |
static int av2notification_meta(pTHX_ AV *array, |
| 481 |
notification_meta_t **ret_meta) { |
| 482 |
notification_meta_t *tail = NULL; |
| 483 |
|
| 484 |
int len = av_len(array); |
| 485 |
|
| 486 |
for (int i = 0; i <= len; ++i) { |
| 487 |
SV **tmp = av_fetch(array, i, 0); |
| 488 |
|
| 489 |
if (tmp == NULL) |
| 490 |
return -1; |
| 491 |
|
| 492 |
if (!(SvROK(*tmp) && (SVt_PVHV == SvTYPE(SvRV(*tmp))))) { |
| 493 |
log_warn("av2notification_meta: Skipping invalid " |
| 494 |
"meta information."); |
| 495 |
continue; |
| 496 |
} |
| 497 |
|
| 498 |
HV *hash = (HV *)SvRV(*tmp); |
| 499 |
|
| 500 |
notification_meta_t *m = calloc(1, sizeof(*m)); |
| 501 |
if (m == NULL) |
| 502 |
return ENOMEM; |
| 503 |
|
| 504 |
SV **name = hv_fetch(hash, "name", strlen("name"), 0); |
| 505 |
if (name == NULL) { |
| 506 |
log_warn("av2notification_meta: Skipping invalid " |
| 507 |
"meta information."); |
| 508 |
sfree(m); |
| 509 |
continue; |
| 510 |
} |
| 511 |
sstrncpy(m->name, SvPV_nolen(*name), sizeof(m->name)); |
| 512 |
|
| 513 |
SV **value = hv_fetch(hash, "value", strlen("value"), 0); |
| 514 |
if (value == NULL) { |
| 515 |
log_warn("av2notification_meta: Skipping invalid " |
| 516 |
"meta information."); |
| 517 |
sfree(m); |
| 518 |
continue; |
| 519 |
} |
| 520 |
|
| 521 |
if (SvNOK(*value)) { |
| 522 |
m->nm_value.nm_double = SvNVX(*value); |
| 523 |
m->type = NM_TYPE_DOUBLE; |
| 524 |
} else if (SvUOK(*value)) { |
| 525 |
m->nm_value.nm_unsigned_int = SvUVX(*value); |
| 526 |
m->type = NM_TYPE_UNSIGNED_INT; |
| 527 |
} else if (SvIOK(*value)) { |
| 528 |
m->nm_value.nm_signed_int = SvIVX(*value); |
| 529 |
m->type = NM_TYPE_SIGNED_INT; |
| 530 |
} else { |
| 531 |
m->nm_value.nm_string = sstrdup(SvPV_nolen(*value)); |
| 532 |
m->type = NM_TYPE_STRING; |
| 533 |
} |
| 534 |
|
| 535 |
m->next = NULL; |
| 536 |
if (tail == NULL) |
| 537 |
*ret_meta = m; |
| 538 |
else |
| 539 |
tail->next = m; |
| 540 |
tail = m; |
| 541 |
} |
| 542 |
|
| 543 |
return 0; |
| 544 |
} /* static int av2notification_meta (AV *, notification_meta_t *) */ |
| 545 |
|
| 546 |
static int hv2notification(pTHX_ HV *hash, notification_t *n) { |
| 547 |
SV **tmp = NULL; |
| 548 |
|
| 549 |
if ((NULL == hash) || (NULL == n)) |
| 550 |
return -1; |
| 551 |
|
| 552 |
if (NULL != (tmp = hv_fetch(hash, "severity", 8, 0))) |
| 553 |
n->severity = SvIV(*tmp); |
| 554 |
else |
| 555 |
n->severity = NOTIF_FAILURE; |
| 556 |
|
| 557 |
if (NULL != (tmp = hv_fetch(hash, "time", 4, 0))) { |
| 558 |
double t = SvNV(*tmp); |
| 559 |
n->time = DOUBLE_TO_CDTIME_T(t); |
| 560 |
} else |
| 561 |
n->time = cdtime(); |
| 562 |
|
| 563 |
if (NULL != (tmp = hv_fetch(hash, "message", 7, 0))) |
| 564 |
sstrncpy(n->message, SvPV_nolen(*tmp), sizeof(n->message)); |
| 565 |
|
| 566 |
if (NULL != (tmp = hv_fetch(hash, "host", 4, 0))) |
| 567 |
sstrncpy(n->host, SvPV_nolen(*tmp), sizeof(n->host)); |
| 568 |
else |
| 569 |
sstrncpy(n->host, hostname_g, sizeof(n->host)); |
| 570 |
|
| 571 |
if (NULL != (tmp = hv_fetch(hash, "plugin", 6, 0))) |
| 572 |
sstrncpy(n->plugin, SvPV_nolen(*tmp), sizeof(n->plugin)); |
| 573 |
|
| 574 |
if (NULL != (tmp = hv_fetch(hash, "plugin_instance", 15, 0))) |
| 575 |
sstrncpy(n->plugin_instance, SvPV_nolen(*tmp), sizeof(n->plugin_instance)); |
| 576 |
|
| 577 |
if (NULL != (tmp = hv_fetch(hash, "type", 4, 0))) |
| 578 |
sstrncpy(n->type, SvPV_nolen(*tmp), sizeof(n->type)); |
| 579 |
|
| 580 |
if (NULL != (tmp = hv_fetch(hash, "type_instance", 13, 0))) |
| 581 |
sstrncpy(n->type_instance, SvPV_nolen(*tmp), sizeof(n->type_instance)); |
| 582 |
|
| 583 |
n->meta = NULL; |
| 584 |
while (NULL != (tmp = hv_fetch(hash, "meta", 4, 0))) { |
| 585 |
if (!(SvROK(*tmp) && (SVt_PVAV == SvTYPE(SvRV(*tmp))))) { |
| 586 |
log_warn("hv2notification: Ignoring invalid meta information."); |
| 587 |
break; |
| 588 |
} |
| 589 |
|
| 590 |
if (0 != av2notification_meta(aTHX_(AV *) SvRV(*tmp), &n->meta)) { |
| 591 |
plugin_notification_meta_free(n->meta); |
| 592 |
n->meta = NULL; |
| 593 |
return -1; |
| 594 |
} |
| 595 |
break; |
| 596 |
} |
| 597 |
return 0; |
| 598 |
} /* static int hv2notification (pTHX_ HV *, notification_t *) */ |
| 599 |
|
| 600 |
static int data_set2av(pTHX_ data_set_t *ds, AV *array) { |
| 601 |
if ((NULL == ds) || (NULL == array)) |
| 602 |
return -1; |
| 603 |
|
| 604 |
av_extend(array, ds->ds_num); |
| 605 |
|
| 606 |
for (size_t i = 0; i < ds->ds_num; ++i) { |
| 607 |
HV *source = newHV(); |
| 608 |
|
| 609 |
if (NULL == hv_store(source, "name", 4, newSVpv(ds->ds[i].name, 0), 0)) |
| 610 |
return -1; |
| 611 |
|
| 612 |
if (NULL == hv_store(source, "type", 4, newSViv(ds->ds[i].type), 0)) |
| 613 |
return -1; |
| 614 |
|
| 615 |
if (!isnan(ds->ds[i].min)) |
| 616 |
if (NULL == hv_store(source, "min", 3, newSVnv(ds->ds[i].min), 0)) |
| 617 |
return -1; |
| 618 |
|
| 619 |
if (!isnan(ds->ds[i].max)) |
| 620 |
if (NULL == hv_store(source, "max", 3, newSVnv(ds->ds[i].max), 0)) |
| 621 |
return -1; |
| 622 |
|
| 623 |
if (NULL == av_store(array, i, newRV_noinc((SV *)source))) |
| 624 |
return -1; |
| 625 |
} |
| 626 |
return 0; |
| 627 |
} /* static int data_set2av (data_set_t *, AV *) */ |
| 628 |
|
| 629 |
static int value_list2hv(pTHX_ value_list_t *vl, data_set_t *ds, HV *hash) { |
| 630 |
AV *values = NULL; |
| 631 |
size_t i; |
| 632 |
|
| 633 |
if ((NULL == vl) || (NULL == ds) || (NULL == hash)) |
| 634 |
return -1; |
| 635 |
|
| 636 |
values = newAV(); |
| 637 |
/* av_extend takes the last *index* to which the array should be extended. */ |
| 638 |
av_extend(values, vl->values_len - 1); |
| 639 |
|
| 640 |
assert(ds->ds_num == vl->values_len); |
| 641 |
for (i = 0; i < vl->values_len; ++i) { |
| 642 |
SV *val = NULL; |
| 643 |
|
| 644 |
if (DS_TYPE_COUNTER == ds->ds[i].type) |
| 645 |
val = newSViv(vl->values[i].counter); |
| 646 |
else if (DS_TYPE_GAUGE == ds->ds[i].type) |
| 647 |
val = newSVnv(vl->values[i].gauge); |
| 648 |
else if (DS_TYPE_DERIVE == ds->ds[i].type) |
| 649 |
val = newSViv(vl->values[i].derive); |
| 650 |
else if (DS_TYPE_ABSOLUTE == ds->ds[i].type) |
| 651 |
val = newSViv(vl->values[i].absolute); |
| 652 |
|
| 653 |
if (NULL == av_store(values, i, val)) { |
| 654 |
av_undef(values); |
| 655 |
return -1; |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
if (NULL == hv_store(hash, "values", 6, newRV_noinc((SV *)values), 0)) |
| 660 |
return -1; |
| 661 |
|
| 662 |
if (0 != vl->time) { |
| 663 |
double t = CDTIME_T_TO_DOUBLE(vl->time); |
| 664 |
if (NULL == hv_store(hash, "time", 4, newSVnv(t), 0)) |
| 665 |
return -1; |
| 666 |
} |
| 667 |
|
| 668 |
{ |
| 669 |
double t = CDTIME_T_TO_DOUBLE(vl->interval); |
| 670 |
if (NULL == hv_store(hash, "interval", 8, newSVnv(t), 0)) |
| 671 |
return -1; |
| 672 |
} |
| 673 |
|
| 674 |
if ('\0' != vl->host[0]) |
| 675 |
if (NULL == hv_store(hash, "host", 4, newSVpv(vl->host, 0), 0)) |
| 676 |
return -1; |
| 677 |
|
| 678 |
if ('\0' != vl->plugin[0]) |
| 679 |
if (NULL == hv_store(hash, "plugin", 6, newSVpv(vl->plugin, 0), 0)) |
| 680 |
return -1; |
| 681 |
|
| 682 |
if ('\0' != vl->plugin_instance[0]) |
| 683 |
if (NULL == hv_store(hash, "plugin_instance", 15, |
| 684 |
newSVpv(vl->plugin_instance, 0), 0)) |
| 685 |
return -1; |
| 686 |
|
| 687 |
if ('\0' != vl->type[0]) |
| 688 |
if (NULL == hv_store(hash, "type", 4, newSVpv(vl->type, 0), 0)) |
| 689 |
return -1; |
| 690 |
|
| 691 |
if ('\0' != vl->type_instance[0]) |
| 692 |
if (NULL == |
| 693 |
hv_store(hash, "type_instance", 13, newSVpv(vl->type_instance, 0), 0)) |
| 694 |
return -1; |
| 695 |
return 0; |
| 696 |
} /* static int value2av (value_list_t *, data_set_t *, HV *) */ |
| 697 |
|
| 698 |
static int notification_meta2av(pTHX_ notification_meta_t *meta, AV *array) { |
| 699 |
int meta_num = 0; |
| 700 |
for (notification_meta_t *m = meta; m != NULL; m = m->next) { |
| 701 |
++meta_num; |
| 702 |
} |
| 703 |
|
| 704 |
av_extend(array, meta_num); |
| 705 |
|
| 706 |
for (int i = 0; NULL != meta; meta = meta->next, ++i) { |
| 707 |
HV *m = newHV(); |
| 708 |
SV *value; |
| 709 |
|
| 710 |
if (NULL == hv_store(m, "name", 4, newSVpv(meta->name, 0), 0)) |
| 711 |
return -1; |
| 712 |
|
| 713 |
if (NM_TYPE_STRING == meta->type) |
| 714 |
value = newSVpv(meta->nm_value.nm_string, 0); |
| 715 |
else if (NM_TYPE_SIGNED_INT == meta->type) |
| 716 |
value = newSViv(meta->nm_value.nm_signed_int); |
| 717 |
else if (NM_TYPE_UNSIGNED_INT == meta->type) |
| 718 |
value = newSVuv(meta->nm_value.nm_unsigned_int); |
| 719 |
else if (NM_TYPE_DOUBLE == meta->type) |
| 720 |
value = newSVnv(meta->nm_value.nm_double); |
| 721 |
else if (NM_TYPE_BOOLEAN == meta->type) |
| 722 |
value = meta->nm_value.nm_boolean ? &PL_sv_yes : &PL_sv_no; |
| 723 |
else |
| 724 |
return -1; |
| 725 |
|
| 726 |
if (NULL == hv_store(m, "value", 5, value, 0)) { |
| 727 |
sv_free(value); |
| 728 |
return -1; |
| 729 |
} |
| 730 |
|
| 731 |
if (NULL == av_store(array, i, newRV_noinc((SV *)m))) { |
| 732 |
hv_clear(m); |
| 733 |
hv_undef(m); |
| 734 |
return -1; |
| 735 |
} |
| 736 |
} |
| 737 |
return 0; |
| 738 |
} /* static int notification_meta2av (notification_meta_t *, AV *) */ |
| 739 |
|
| 740 |
static int notification2hv(pTHX_ notification_t *n, HV *hash) { |
| 741 |
if (NULL == hv_store(hash, "severity", 8, newSViv(n->severity), 0)) |
| 742 |
return -1; |
| 743 |
|
| 744 |
if (0 != n->time) { |
| 745 |
double t = CDTIME_T_TO_DOUBLE(n->time); |
| 746 |
if (NULL == hv_store(hash, "time", 4, newSVnv(t), 0)) |
| 747 |
return -1; |
| 748 |
} |
| 749 |
|
| 750 |
if ('\0' != *n->message) |
| 751 |
if (NULL == hv_store(hash, "message", 7, newSVpv(n->message, 0), 0)) |
| 752 |
return -1; |
| 753 |
|
| 754 |
if ('\0' != *n->host) |
| 755 |
if (NULL == hv_store(hash, "host", 4, newSVpv(n->host, 0), 0)) |
| 756 |
return -1; |
| 757 |
|
| 758 |
if ('\0' != *n->plugin) |
| 759 |
if (NULL == hv_store(hash, "plugin", 6, newSVpv(n->plugin, 0), 0)) |
| 760 |
return -1; |
| 761 |
|
| 762 |
if ('\0' != *n->plugin_instance) |
| 763 |
if (NULL == hv_store(hash, "plugin_instance", 15, |
| 764 |
newSVpv(n->plugin_instance, 0), 0)) |
| 765 |
return -1; |
| 766 |
|
| 767 |
if ('\0' != *n->type) |
| 768 |
if (NULL == hv_store(hash, "type", 4, newSVpv(n->type, 0), 0)) |
| 769 |
return -1; |
| 770 |
|
| 771 |
if ('\0' != *n->type_instance) |
| 772 |
if (NULL == |
| 773 |
hv_store(hash, "type_instance", 13, newSVpv(n->type_instance, 0), 0)) |
| 774 |
return -1; |
| 775 |
|
| 776 |
if (NULL != n->meta) { |
| 777 |
AV *meta = newAV(); |
| 778 |
if ((0 != notification_meta2av(aTHX_ n->meta, meta)) || |
| 779 |
(NULL == hv_store(hash, "meta", 4, newRV_noinc((SV *)meta), 0))) { |
| 780 |
av_clear(meta); |
| 781 |
av_undef(meta); |
| 782 |
return -1; |
| 783 |
} |
| 784 |
} |
| 785 |
return 0; |
| 786 |
} /* static int notification2hv (notification_t *, HV *) */ |
| 787 |
|
| 788 |
static int oconfig_item2hv(pTHX_ oconfig_item_t *ci, HV *hash) { |
| 789 |
AV *values; |
| 790 |
AV *children; |
| 791 |
|
| 792 |
if (NULL == hv_store(hash, "key", 3, newSVpv(ci->key, 0), 0)) |
| 793 |
return -1; |
| 794 |
|
| 795 |
values = newAV(); |
| 796 |
if (0 < ci->values_num) |
| 797 |
av_extend(values, ci->values_num); |
| 798 |
|
| 799 |
if (NULL == hv_store(hash, "values", 6, newRV_noinc((SV *)values), 0)) { |
| 800 |
av_clear(values); |
| 801 |
av_undef(values); |
| 802 |
return -1; |
| 803 |
} |
| 804 |
|
| 805 |
for (int i = 0; i < ci->values_num; ++i) { |
| 806 |
SV *value; |
| 807 |
|
| 808 |
switch (ci->values[i].type) { |
| 809 |
case OCONFIG_TYPE_STRING: |
| 810 |
value = newSVpv(ci->values[i].value.string, 0); |
| 811 |
break; |
| 812 |
case OCONFIG_TYPE_NUMBER: |
| 813 |
value = newSVnv((NV)ci->values[i].value.number); |
| 814 |
break; |
| 815 |
case OCONFIG_TYPE_BOOLEAN: |
| 816 |
value = ci->values[i].value.boolean ? &PL_sv_yes : &PL_sv_no; |
| 817 |
break; |
| 818 |
default: |
| 819 |
log_err("oconfig_item2hv: Invalid value type %i.", ci->values[i].type); |
| 820 |
value = &PL_sv_undef; |
| 821 |
} |
| 822 |
|
| 823 |
if (NULL == av_store(values, i, value)) { |
| 824 |
sv_free(value); |
| 825 |
return -1; |
| 826 |
} |
| 827 |
} |
| 828 |
|
| 829 |
/* ignoring 'parent' member which is uninteresting in this case */ |
| 830 |
|
| 831 |
children = newAV(); |
| 832 |
if (0 < ci->children_num) |
| 833 |
av_extend(children, ci->children_num); |
| 834 |
|
| 835 |
if (NULL == hv_store(hash, "children", 8, newRV_noinc((SV *)children), 0)) { |
| 836 |
av_clear(children); |
| 837 |
av_undef(children); |
| 838 |
return -1; |
| 839 |
} |
| 840 |
|
| 841 |
for (int i = 0; i < ci->children_num; ++i) { |
| 842 |
HV *child = newHV(); |
| 843 |
|
| 844 |
if (0 != oconfig_item2hv(aTHX_ ci->children + i, child)) { |
| 845 |
hv_clear(child); |
| 846 |
hv_undef(child); |
| 847 |
return -1; |
| 848 |
} |
| 849 |
|
| 850 |
if (NULL == av_store(children, i, newRV_noinc((SV *)child))) { |
| 851 |
hv_clear(child); |
| 852 |
hv_undef(child); |
| 853 |
return -1; |
| 854 |
} |
| 855 |
} |
| 856 |
return 0; |
| 857 |
} /* static int oconfig_item2hv (pTHX_ oconfig_item_t *, HV *) */ |
| 858 |
|
| 859 |
/* |
| 860 |
* Internal functions. |
| 861 |
*/ |
| 862 |
|
| 863 |
static char *get_module_name(char *buf, size_t buf_len, const char *module) { |
| 864 |
int status = 0; |
| 865 |
if (base_name[0] == '\0') |
| 866 |
status = ssnprintf(buf, buf_len, "%s", module); |
| 867 |
else |
| 868 |
status = ssnprintf(buf, buf_len, "%s::%s", base_name, module); |
| 869 |
if ((status < 0) || ((unsigned int)status >= buf_len)) |
| 870 |
return NULL; |
| 871 |
return buf; |
| 872 |
} /* char *get_module_name */ |
| 873 |
|
| 874 |
/* |
| 875 |
* Add a plugin's data set definition. |
| 876 |
*/ |
| 877 |
static int pplugin_register_data_set(pTHX_ char *name, AV *dataset) { |
| 878 |
int ret = 0; |
| 879 |
|
| 880 |
data_set_t ds; |
| 881 |
|
| 882 |
if ((NULL == name) || (NULL == dataset)) |
| 883 |
return -1; |
| 884 |
|
| 885 |
if (0 != av2data_set(aTHX_ dataset, name, &ds)) |
| 886 |
return -1; |
| 887 |
|
| 888 |
ret = plugin_register_data_set(&ds); |
| 889 |
|
| 890 |
free(ds.ds); |
| 891 |
return ret; |
| 892 |
} /* static int pplugin_register_data_set (char *, SV *) */ |
| 893 |
|
| 894 |
/* |
| 895 |
* Remove a plugin's data set definition. |
| 896 |
*/ |
| 897 |
static int pplugin_unregister_data_set(char *name) { |
| 898 |
if (NULL == name) |
| 899 |
return 0; |
| 900 |
return plugin_unregister_data_set(name); |
| 901 |
} /* static int pplugin_unregister_data_set (char *) */ |
| 902 |
|
| 903 |
/* |
| 904 |
* Submit the values to the write functions. |
| 905 |
*/ |
| 906 |
static int pplugin_dispatch_values(pTHX_ HV *values) { |
| 907 |
value_list_t vl = VALUE_LIST_INIT; |
| 908 |
|
| 909 |
int ret = 0; |
| 910 |
|
| 911 |
if (NULL == values) |
| 912 |
return -1; |
| 913 |
|
| 914 |
if (0 != hv2value_list(aTHX_ values, &vl)) |
| 915 |
return -1; |
| 916 |
|
| 917 |
ret = plugin_dispatch_values(&vl); |
| 918 |
|
| 919 |
sfree(vl.values); |
| 920 |
return ret; |
| 921 |
} /* static int pplugin_dispatch_values (char *, HV *) */ |
| 922 |
|
| 923 |
/* |
| 924 |
* Submit the values to a single write function. |
| 925 |
*/ |
| 926 |
static int pplugin_write(pTHX_ const char *plugin, AV *data_set, HV *values) { |
| 927 |
data_set_t ds; |
| 928 |
value_list_t vl = VALUE_LIST_INIT; |
| 929 |
|
| 930 |
int ret; |
| 931 |
|
| 932 |
if (NULL == values) |
| 933 |
return -1; |
| 934 |
|
| 935 |
if (0 != hv2value_list(aTHX_ values, &vl)) |
| 936 |
return -1; |
| 937 |
|
| 938 |
if ((NULL != data_set) && (0 != av2data_set(aTHX_ data_set, vl.type, &ds))) |
| 939 |
return -1; |
| 940 |
|
| 941 |
ret = plugin_write(plugin, NULL == data_set ? NULL : &ds, &vl); |
| 942 |
if (0 != ret) |
| 943 |
log_warn("Dispatching value to plugin \"%s\" failed with status %i.", |
| 944 |
NULL == plugin ? "<any>" : plugin, ret); |
| 945 |
|
| 946 |
if (NULL != data_set) |
| 947 |
sfree(ds.ds); |
| 948 |
sfree(vl.values); |
| 949 |
return ret; |
| 950 |
} /* static int pplugin_write (const char *plugin, HV *, HV *) */ |
| 951 |
|
| 952 |
/* |
| 953 |
* Dispatch a notification. |
| 954 |
*/ |
| 955 |
static int pplugin_dispatch_notification(pTHX_ HV *notif) { |
| 956 |
notification_t n = {0}; |
| 957 |
|
| 958 |
int ret; |
| 959 |
|
| 960 |
if (NULL == notif) |
| 961 |
return -1; |
| 962 |
|
| 963 |
if (0 != hv2notification(aTHX_ notif, &n)) |
| 964 |
return -1; |
| 965 |
|
| 966 |
ret = plugin_dispatch_notification(&n); |
| 967 |
plugin_notification_meta_free(n.meta); |
| 968 |
return ret; |
| 969 |
} /* static int pplugin_dispatch_notification (HV *) */ |
| 970 |
|
| 971 |
/* |
| 972 |
* Call perl sub with thread locking flags handled. |
| 973 |
*/ |
| 974 |
static int call_pv_locked(pTHX_ const char *sub_name) { |
| 975 |
bool old_running; |
| 976 |
int ret; |
| 977 |
|
| 978 |
c_ithread_t *t = (c_ithread_t *)pthread_getspecific(perl_thr_key); |
| 979 |
if (t == NULL) /* thread destroyed */ |
| 980 |
return 0; |
| 981 |
|
| 982 |
old_running = t->running; |
| 983 |
t->running = true; |
| 984 |
|
| 985 |
if (t->shutdown) { |
| 986 |
t->running = old_running; |
| 987 |
return 0; |
| 988 |
} |
| 989 |
|
| 990 |
ret = call_pv(sub_name, G_SCALAR | G_EVAL); |
| 991 |
|
| 992 |
t->running = old_running; |
| 993 |
return ret; |
| 994 |
} /* static int call_pv_locked (pTHX, *sub_name) */ |
| 995 |
|
| 996 |
/* |
| 997 |
* Call all working functions of the given type. |
| 998 |
*/ |
| 999 |
static int pplugin_call(pTHX_ int type, ...) { |
| 1000 |
int retvals = 0; |
| 1001 |
|
| 1002 |
va_list ap; |
| 1003 |
int ret = 0; |
| 1004 |
char *subname; |
| 1005 |
|
| 1006 |
dSP; |
| 1007 |
|
| 1008 |
if ((type < 0) || (type >= PLUGIN_TYPES)) |
| 1009 |
return -1; |
| 1010 |
|
| 1011 |
va_start(ap, type); |
| 1012 |
|
| 1013 |
ENTER; |
| 1014 |
SAVETMPS; |
| 1015 |
|
| 1016 |
PUSHMARK(SP); |
| 1017 |
|
| 1018 |
if (PLUGIN_READ == type) { |
| 1019 |
subname = va_arg(ap, char *); |
| 1020 |
} else if (PLUGIN_WRITE == type) { |
| 1021 |
data_set_t *ds; |
| 1022 |
value_list_t *vl; |
| 1023 |
|
| 1024 |
AV *pds = newAV(); |
| 1025 |
HV *pvl = newHV(); |
| 1026 |
|
| 1027 |
subname = va_arg(ap, char *); |
| 1028 |
/* |
| 1029 |
* $_[0] = $plugin_type; |
| 1030 |
* |
| 1031 |
* $_[1] = |
| 1032 |
* [ |
| 1033 |
* { |
| 1034 |
* name => $ds_name, |
| 1035 |
* type => $ds_type, |
| 1036 |
* min => $ds_min, |
| 1037 |
* max => $ds_max |
| 1038 |
* }, |
| 1039 |
* ... |
| 1040 |
* ]; |
| 1041 |
* |
| 1042 |
* $_[2] = |
| 1043 |
* { |
| 1044 |
* values => [ $v1, ... ], |
| 1045 |
* time => $time, |
| 1046 |
* host => $hostname, |
| 1047 |
* plugin => $plugin, |
| 1048 |
* type => $type, |
| 1049 |
* plugin_instance => $instance, |
| 1050 |
* type_instance => $type_instance |
| 1051 |
* }; |
| 1052 |
*/ |
| 1053 |
ds = va_arg(ap, data_set_t *); |
| 1054 |
vl = va_arg(ap, value_list_t *); |
| 1055 |
|
| 1056 |
if (-1 == data_set2av(aTHX_ ds, pds)) { |
| 1057 |
av_clear(pds); |
| 1058 |
av_undef(pds); |
| 1059 |
pds = (AV *)&PL_sv_undef; |
| 1060 |
ret = -1; |
| 1061 |
} |
| 1062 |
|
| 1063 |
if (-1 == value_list2hv(aTHX_ vl, ds, pvl)) { |
| 1064 |
hv_clear(pvl); |
| 1065 |
hv_undef(pvl); |
| 1066 |
pvl = (HV *)&PL_sv_undef; |
| 1067 |
ret = -1; |
| 1068 |
} |
| 1069 |
|
| 1070 |
XPUSHs(sv_2mortal(newSVpv(ds->type, 0))); |
| 1071 |
XPUSHs(sv_2mortal(newRV_noinc((SV *)pds))); |
| 1072 |
XPUSHs(sv_2mortal(newRV_noinc((SV *)pvl))); |
| 1073 |
} else if (PLUGIN_LOG == type) { |
| 1074 |
subname = va_arg(ap, char *); |
| 1075 |
/* |
| 1076 |
* $_[0] = $level; |
| 1077 |
* |
| 1078 |
* $_[1] = $message; |
| 1079 |
*/ |
| 1080 |
XPUSHs(sv_2mortal(newSViv(va_arg(ap, int)))); |
| 1081 |
XPUSHs(sv_2mortal(newSVpv(va_arg(ap, char *), 0))); |
| 1082 |
} else if (PLUGIN_NOTIF == type) { |
| 1083 |
notification_t *n; |
| 1084 |
HV *notif = newHV(); |
| 1085 |
|
| 1086 |
subname = va_arg(ap, char *); |
| 1087 |
/* |
| 1088 |
* $_[0] = |
| 1089 |
* { |
| 1090 |
* severity => $severity, |
| 1091 |
* time => $time, |
| 1092 |
* message => $msg, |
| 1093 |
* host => $host, |
| 1094 |
* plugin => $plugin, |
| 1095 |
* type => $type, |
| 1096 |
* plugin_instance => $instance, |
| 1097 |
* type_instance => $type_instance |
| 1098 |
* }; |
| 1099 |
*/ |
| 1100 |
n = va_arg(ap, notification_t *); |
| 1101 |
|
| 1102 |
if (-1 == notification2hv(aTHX_ n, notif)) { |
| 1103 |
hv_clear(notif); |
| 1104 |
hv_undef(notif); |
| 1105 |
notif = (HV *)&PL_sv_undef; |
| 1106 |
ret = -1; |
| 1107 |
} |
| 1108 |
|
| 1109 |
XPUSHs(sv_2mortal(newRV_noinc((SV *)notif))); |
| 1110 |
} else if (PLUGIN_FLUSH == type) { |
| 1111 |
cdtime_t timeout; |
| 1112 |
subname = va_arg(ap, char *); |
| 1113 |
/* |
| 1114 |
* $_[0] = $timeout; |
| 1115 |
* $_[1] = $identifier; |
| 1116 |
*/ |
| 1117 |
timeout = va_arg(ap, cdtime_t); |
| 1118 |
|
| 1119 |
XPUSHs(sv_2mortal(newSVnv(CDTIME_T_TO_DOUBLE(timeout)))); |
| 1120 |
XPUSHs(sv_2mortal(newSVpv(va_arg(ap, char *), 0))); |
| 1121 |
} else if (PLUGIN_FLUSH_ALL == type) { |
| 1122 |
cdtime_t timeout; |
| 1123 |
subname = "Collectd::plugin_call_all"; |
| 1124 |
/* |
| 1125 |
* $_[0] = $timeout; |
| 1126 |
* $_[1] = $identifier; |
| 1127 |
*/ |
| 1128 |
timeout = va_arg(ap, cdtime_t); |
| 1129 |
|
| 1130 |
XPUSHs(sv_2mortal(newSViv((IV)PLUGIN_FLUSH))); |
| 1131 |
XPUSHs(sv_2mortal(newSVnv(CDTIME_T_TO_DOUBLE(timeout)))); |
| 1132 |
XPUSHs(sv_2mortal(newSVpv(va_arg(ap, char *), 0))); |
| 1133 |
} else if (PLUGIN_INIT == type) { |
| 1134 |
subname = "Collectd::plugin_call_all"; |
| 1135 |
XPUSHs(sv_2mortal(newSViv((IV)type))); |
| 1136 |
} else if (PLUGIN_SHUTDOWN == type) { |
| 1137 |
subname = "Collectd::plugin_call_all"; |
| 1138 |
XPUSHs(sv_2mortal(newSViv((IV)type))); |
| 1139 |
} else { /* Unknown type. Run 'plugin_call_all' and make compiler happy */ |
| 1140 |
subname = "Collectd::plugin_call_all"; |
| 1141 |
XPUSHs(sv_2mortal(newSViv((IV)type))); |
| 1142 |
} |
| 1143 |
|
| 1144 |
PUTBACK; |
| 1145 |
|
| 1146 |
retvals = call_pv_locked(aTHX_ subname); |
| 1147 |
|
| 1148 |
SPAGAIN; |
| 1149 |
if (SvTRUE(ERRSV)) { |
| 1150 |
if (PLUGIN_LOG != type) |
| 1151 |
ERROR("perl: %s error: %s", subname, SvPV_nolen(ERRSV)); |
| 1152 |
ret = -1; |
| 1153 |
} else if (0 < retvals) { |
| 1154 |
SV *tmp = POPs; |
| 1155 |
if (!SvTRUE(tmp)) |
| 1156 |
ret = -1; |
| 1157 |
} |
| 1158 |
|
| 1159 |
PUTBACK; |
| 1160 |
FREETMPS; |
| 1161 |
LEAVE; |
| 1162 |
|
| 1163 |
va_end(ap); |
| 1164 |
return ret; |
| 1165 |
} /* static int pplugin_call (int, ...) */ |
| 1166 |
|
| 1167 |
/* |
| 1168 |
* collectd's Perl interpreter based thread implementation. |
| 1169 |
* |
| 1170 |
* This has been inspired by Perl's ithreads introduced in version 5.6.0. |
| 1171 |
*/ |
| 1172 |
|
| 1173 |
/* must be called with perl_threads->mutex locked */ |
| 1174 |
static void c_ithread_destroy(c_ithread_t *ithread) { |
| 1175 |
dTHXa(ithread->interp); |
| 1176 |
|
| 1177 |
assert(NULL != perl_threads); |
| 1178 |
|
| 1179 |
PERL_SET_CONTEXT(aTHX); |
| 1180 |
/* Mark as running to avoid deadlock: |
| 1181 |
c_ithread_destroy -> log_debug -> perl_log() |
| 1182 |
*/ |
| 1183 |
ithread->running = true; |
| 1184 |
log_debug("Shutting down Perl interpreter %p...", aTHX); |
| 1185 |
|
| 1186 |
#if COLLECT_DEBUG |
| 1187 |
sv_report_used(); |
| 1188 |
|
| 1189 |
--perl_threads->number_of_threads; |
| 1190 |
#endif /* COLLECT_DEBUG */ |
| 1191 |
|
| 1192 |
perl_destruct(aTHX); |
| 1193 |
perl_free(aTHX); |
| 1194 |
|
| 1195 |
if (NULL == ithread->prev) |
| 1196 |
perl_threads->head = ithread->next; |
| 1197 |
else |
| 1198 |
ithread->prev->next = ithread->next; |
| 1199 |
|
| 1200 |
if (NULL == ithread->next) |
| 1201 |
perl_threads->tail = ithread->prev; |
| 1202 |
else |
| 1203 |
ithread->next->prev = ithread->prev; |
| 1204 |
|
| 1205 |
sfree(ithread); |
| 1206 |
return; |
| 1207 |
} /* static void c_ithread_destroy (c_ithread_t *) */ |
| 1208 |
|
| 1209 |
static void c_ithread_destructor(void *arg) { |
| 1210 |
c_ithread_t *ithread = (c_ithread_t *)arg; |
| 1211 |
c_ithread_t *t = NULL; |
| 1212 |
|
| 1213 |
if (NULL == perl_threads) |
| 1214 |
return; |
| 1215 |
|
| 1216 |
pthread_mutex_lock(&perl_threads->mutex); |
| 1217 |
|
| 1218 |
for (t = perl_threads->head; NULL != t; t = t->next) |
| 1219 |
if (t == ithread) |
| 1220 |
break; |
| 1221 |
|
| 1222 |
/* the ithread no longer exists */ |
| 1223 |
if (NULL == t) { |
| 1224 |
pthread_mutex_unlock(&perl_threads->mutex); |
| 1225 |
return; |
| 1226 |
} |
| 1227 |
|
| 1228 |
c_ithread_destroy(ithread); |
| 1229 |
|
| 1230 |
pthread_mutex_unlock(&perl_threads->mutex); |
| 1231 |
return; |
| 1232 |
} /* static void c_ithread_destructor (void *) */ |
| 1233 |
|
| 1234 |
/* must be called with perl_threads->mutex locked */ |
| 1235 |
static c_ithread_t *c_ithread_create(PerlInterpreter *base) { |
| 1236 |
c_ithread_t *t = NULL; |
| 1237 |
dTHXa(NULL); |
| 1238 |
|
| 1239 |
assert(NULL != perl_threads); |
| 1240 |
|
| 1241 |
t = smalloc(sizeof(*t)); |
| 1242 |
memset(t, 0, sizeof(c_ithread_t)); |
| 1243 |
|
| 1244 |
t->interp = (NULL == base) ? NULL : perl_clone(base, CLONEf_KEEP_PTR_TABLE); |
| 1245 |
|
| 1246 |
aTHX = t->interp; |
| 1247 |
|
| 1248 |
if ((NULL != base) && (NULL != PL_endav)) { |
| 1249 |
av_clear(PL_endav); |
| 1250 |
av_undef(PL_endav); |
| 1251 |
PL_endav = Nullav; |
| 1252 |
} |
| 1253 |
|
| 1254 |
#if COLLECT_DEBUG |
| 1255 |
++perl_threads->number_of_threads; |
| 1256 |
#endif /* COLLECT_DEBUG */ |
| 1257 |
|
| 1258 |
t->next = NULL; |
| 1259 |
|
| 1260 |
if (NULL == perl_threads->tail) { |
| 1261 |
perl_threads->head = t; |
| 1262 |
t->prev = NULL; |
| 1263 |
} else { |
| 1264 |
perl_threads->tail->next = t; |
| 1265 |
t->prev = perl_threads->tail; |
| 1266 |
} |
| 1267 |
|
| 1268 |
t->pthread = pthread_self(); |
| 1269 |
t->running = false; |
| 1270 |
t->shutdown = false; |
| 1271 |
perl_threads->tail = t; |
| 1272 |
|
| 1273 |
pthread_setspecific(perl_thr_key, (const void *)t); |
| 1274 |
return t; |
| 1275 |
} /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */ |
| 1276 |
|
| 1277 |
/* |
| 1278 |
* Filter chains implementation. |
| 1279 |
*/ |
| 1280 |
|
| 1281 |
static int fc_call(pTHX_ int type, int cb_type, pfc_user_data_t *data, ...) { |
| 1282 |
int retvals = 0; |
| 1283 |
|
| 1284 |
va_list ap; |
| 1285 |
int ret = 0; |
| 1286 |
|
| 1287 |
notification_meta_t **meta = NULL; |
| 1288 |
AV *pmeta = NULL; |
| 1289 |
|
| 1290 |
dSP; |
| 1291 |
|
| 1292 |
if ((type < 0) || (type >= FC_TYPES)) |
| 1293 |
return -1; |
| 1294 |
|
| 1295 |
if ((cb_type < 0) || (cb_type >= FC_CB_TYPES)) |
| 1296 |
return -1; |
| 1297 |
|
| 1298 |
va_start(ap, data); |
| 1299 |
|
| 1300 |
ENTER; |
| 1301 |
SAVETMPS; |
| 1302 |
|
| 1303 |
PUSHMARK(SP); |
| 1304 |
|
| 1305 |
XPUSHs(sv_2mortal(newSViv((IV)type))); |
| 1306 |
XPUSHs(sv_2mortal(newSVpv(data->name, 0))); |
| 1307 |
XPUSHs(sv_2mortal(newSViv((IV)cb_type))); |
| 1308 |
|
| 1309 |
if (FC_CB_CREATE == cb_type) { |
| 1310 |
/* |
| 1311 |
* $_[0] = $ci; |
| 1312 |
* $_[1] = $user_data; |
| 1313 |
*/ |
| 1314 |
oconfig_item_t *ci; |
| 1315 |
HV *config = newHV(); |
| 1316 |
|
| 1317 |
ci = va_arg(ap, oconfig_item_t *); |
| 1318 |
|
| 1319 |
if (0 != oconfig_item2hv(aTHX_ ci, config)) { |
| 1320 |
hv_clear(config); |
| 1321 |
hv_undef(config); |
| 1322 |
config = (HV *)&PL_sv_undef; |
| 1323 |
ret = -1; |
| 1324 |
} |
| 1325 |
|
| 1326 |
XPUSHs(sv_2mortal(newRV_noinc((SV *)config))); |
| 1327 |
} else if (FC_CB_DESTROY == cb_type) { |
| 1328 |
/* |
| 1329 |
* $_[1] = $user_data; |
| 1330 |
*/ |
| 1331 |
|
| 1332 |
/* nothing to be done - the user data pointer |
| 1333 |
* is pushed onto the stack later */ |
| 1334 |
} else if (FC_CB_EXEC == cb_type) { |
| 1335 |
/* |
| 1336 |
* $_[0] = $ds; |
| 1337 |
* $_[1] = $vl; |
| 1338 |
* $_[2] = $meta; |
| 1339 |
* $_[3] = $user_data; |
| 1340 |
*/ |
| 1341 |
data_set_t *ds; |
| 1342 |
value_list_t *vl; |
| 1343 |
|
| 1344 |
AV *pds = newAV(); |
| 1345 |
HV *pvl = newHV(); |
| 1346 |
|
| 1347 |
ds = va_arg(ap, data_set_t *); |
| 1348 |
vl = va_arg(ap, value_list_t *); |
| 1349 |
meta = va_arg(ap, notification_meta_t **); |
| 1350 |
|
| 1351 |
if (0 != data_set2av(aTHX_ ds, pds)) { |
| 1352 |
av_clear(pds); |
| 1353 |
av_undef(pds); |
| 1354 |
pds = (AV *)&PL_sv_undef; |
| 1355 |
ret = -1; |
| 1356 |
} |
| 1357 |
|
| 1358 |
if (0 != value_list2hv(aTHX_ vl, ds, pvl)) { |
| 1359 |
hv_clear(pvl); |
| 1360 |
hv_undef(pvl); |
| 1361 |
pvl = (HV *)&PL_sv_undef; |
| 1362 |
ret = -1; |
| 1363 |
} |
| 1364 |
|
| 1365 |
if (NULL != meta) { |
| 1366 |
pmeta = newAV(); |
| 1367 |
|
| 1368 |
if (0 != notification_meta2av(aTHX_ * meta, pmeta)) { |
| 1369 |
av_clear(pmeta); |
| 1370 |
av_undef(pmeta); |
| 1371 |
pmeta = (AV *)&PL_sv_undef; |
| 1372 |
ret = -1; |
| 1373 |
} |
| 1374 |
} else { |
| 1375 |
pmeta = (AV *)&PL_sv_undef; |
| 1376 |
} |
| 1377 |
|
| 1378 |
XPUSHs(sv_2mortal(newRV_noinc((SV *)pds))); |
| 1379 |
XPUSHs(sv_2mortal(newRV_noinc((SV *)pvl))); |
| 1380 |
XPUSHs(sv_2mortal(newRV_noinc((SV *)pmeta))); |
| 1381 |
} |
| 1382 |
|
| 1383 |
XPUSHs(sv_2mortal(newRV_inc(data->user_data))); |
| 1384 |
|
| 1385 |
PUTBACK; |
| 1386 |
|
| 1387 |
retvals = call_pv_locked(aTHX_ "Collectd::fc_call"); |
| 1388 |
|
| 1389 |
if ((FC_CB_EXEC == cb_type) && (meta != NULL)) { |
| 1390 |
assert(pmeta != NULL); |
| 1391 |
|
| 1392 |
plugin_notification_meta_free(*meta); |
| 1393 |
av2notification_meta(aTHX_ pmeta, meta); |
| 1394 |
} |
| 1395 |
|
| 1396 |
SPAGAIN; |
| 1397 |
if (SvTRUE(ERRSV)) { |
| 1398 |
ERROR("perl: Collectd::fc_call error: %s", SvPV_nolen(ERRSV)); |
| 1399 |
ret = -1; |
| 1400 |
} else if (0 < retvals) { |
| 1401 |
SV *tmp = POPs; |
| 1402 |
|
| 1403 |
/* the exec callbacks return a status, while |
| 1404 |
* the others return a boolean value */ |
| 1405 |
if (FC_CB_EXEC == cb_type) |
| 1406 |
ret = SvIV(tmp); |
| 1407 |
else if (!SvTRUE(tmp)) |
| 1408 |
ret = -1; |
| 1409 |
} |
| 1410 |
|
| 1411 |
PUTBACK; |
| 1412 |
FREETMPS; |
| 1413 |
LEAVE; |
| 1414 |
|
| 1415 |
va_end(ap); |
| 1416 |
return ret; |
| 1417 |
} /* static int fc_call (int, int, pfc_user_data_t *, ...) */ |
| 1418 |
|
| 1419 |
static int fc_create(int type, const oconfig_item_t *ci, void **user_data) { |
| 1420 |
pfc_user_data_t *data; |
| 1421 |
|
| 1422 |
int ret = 0; |
| 1423 |
|
| 1424 |
dTHX; |
| 1425 |
|
| 1426 |
if (NULL == perl_threads) |
| 1427 |
return 0; |
| 1428 |
|
| 1429 |
if (NULL == aTHX) { |
| 1430 |
c_ithread_t *t = NULL; |
| 1431 |
|
| 1432 |
pthread_mutex_lock(&perl_threads->mutex); |
| 1433 |
t = c_ithread_create(perl_threads->head->interp); |
| 1434 |
pthread_mutex_unlock(&perl_threads->mutex); |
| 1435 |
|
| 1436 |
aTHX = t->interp; |
| 1437 |
} |
| 1438 |
|
| 1439 |
log_debug("fc_create: c_ithread: interp = %p (active threads: %i)", aTHX, |
| 1440 |
perl_threads->number_of_threads); |
| 1441 |
|
| 1442 |
if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) { |
| 1443 |
log_warn("A \"%s\" block expects a single string argument.", |
| 1444 |
(FC_MATCH == type) ? "Match" : "Target"); |
| 1445 |
return -1; |
| 1446 |
} |
| 1447 |
|
| 1448 |
data = smalloc(sizeof(*data)); |
| 1449 |
data->name = sstrdup(ci->values[0].value.string); |
| 1450 |
data->user_data = newSV(0); |
| 1451 |
|
| 1452 |
ret = fc_call(aTHX_ type, FC_CB_CREATE, data, ci); |
| 1453 |
|
| 1454 |
if (0 != ret) |
| 1455 |
PFC_USER_DATA_FREE(data); |
| 1456 |
else |
| 1457 |
*user_data = data; |
| 1458 |
return ret; |
| 1459 |
} /* static int fc_create (int, const oconfig_item_t *, void **) */ |
| 1460 |
|
| 1461 |
static int fc_destroy(int type, void **user_data) { |
| 1462 |
pfc_user_data_t *data = *(pfc_user_data_t **)user_data; |
| 1463 |
|
| 1464 |
int ret = 0; |
| 1465 |
|
| 1466 |
dTHX; |
| 1467 |
|
| 1468 |
if ((NULL == perl_threads) || (NULL == data)) |
| 1469 |
return 0; |
| 1470 |
|
| 1471 |
if (NULL == aTHX) { |
| 1472 |
c_ithread_t *t = NULL; |
| 1473 |
|
| 1474 |
pthread_mutex_lock(&perl_threads->mutex); |
| 1475 |
t = c_ithread_create(perl_threads->head->interp); |
| 1476 |
pthread_mutex_unlock(&perl_threads->mutex); |
| 1477 |
|
| 1478 |
aTHX = t->interp; |
| 1479 |
} |
| 1480 |
|
| 1481 |
log_debug("fc_destroy: c_ithread: interp = %p (active threads: %i)", aTHX, |
| 1482 |
perl_threads->number_of_threads); |
| 1483 |
|
| 1484 |
ret = fc_call(aTHX_ type, FC_CB_DESTROY, data); |
| 1485 |
|
| 1486 |
PFC_USER_DATA_FREE(data); |
| 1487 |
*user_data = NULL; |
| 1488 |
return ret; |
| 1489 |
} /* static int fc_destroy (int, void **) */ |
| 1490 |
|
| 1491 |
static int fc_exec(int type, const data_set_t *ds, const value_list_t *vl, |
| 1492 |
notification_meta_t **meta, void **user_data) { |
| 1493 |
pfc_user_data_t *data = *(pfc_user_data_t **)user_data; |
| 1494 |
|
| 1495 |
dTHX; |
| 1496 |
|
| 1497 |
if (NULL == perl_threads) |
| 1498 |
return 0; |
| 1499 |
|
| 1500 |
assert(NULL != data); |
| 1501 |
|
| 1502 |
if (NULL == aTHX) { |
| 1503 |
c_ithread_t *t = NULL; |
| 1504 |
|
| 1505 |
pthread_mutex_lock(&perl_threads->mutex); |
| 1506 |
t = c_ithread_create(perl_threads->head->interp); |
| 1507 |
pthread_mutex_unlock(&perl_threads->mutex); |
| 1508 |
|
| 1509 |
aTHX = t->interp; |
| 1510 |
} |
| 1511 |
|
| 1512 |
log_debug("fc_exec: c_ithread: interp = %p (active threads: %i)", aTHX, |
| 1513 |
perl_threads->number_of_threads); |
| 1514 |
|
| 1515 |
return fc_call(aTHX_ type, FC_CB_EXEC, data, ds, vl, meta); |
| 1516 |
} /* static int fc_exec (int, const data_set_t *, const value_list_t *, |
| 1517 |
notification_meta_t **, void **) */ |
| 1518 |
|
| 1519 |
static int pmatch_create(const oconfig_item_t *ci, void **user_data) { |
| 1520 |
return fc_create(FC_MATCH, ci, user_data); |
| 1521 |
} /* static int pmatch_create (const oconfig_item_t *, void **) */ |
| 1522 |
|
| 1523 |
static int pmatch_destroy(void **user_data) { |
| 1524 |
return fc_destroy(FC_MATCH, user_data); |
| 1525 |
} /* static int pmatch_destroy (void **) */ |
| 1526 |
|
| 1527 |
static int pmatch_match(const data_set_t *ds, const value_list_t *vl, |
| 1528 |
notification_meta_t **meta, void **user_data) { |
| 1529 |
return fc_exec(FC_MATCH, ds, vl, meta, user_data); |
| 1530 |
} /* static int pmatch_match (const data_set_t *, const value_list_t *, |
| 1531 |
notification_meta_t **, void **) */ |
| 1532 |
|
| 1533 |
static match_proc_t pmatch = {pmatch_create, pmatch_destroy, pmatch_match}; |
| 1534 |
|
| 1535 |
static int ptarget_create(const oconfig_item_t *ci, void **user_data) { |
| 1536 |
return fc_create(FC_TARGET, ci, user_data); |
| 1537 |
} /* static int ptarget_create (const oconfig_item_t *, void **) */ |
| 1538 |
|
| 1539 |
static int ptarget_destroy(void **user_data) { |
| 1540 |
return fc_destroy(FC_TARGET, user_data); |
| 1541 |
} /* static int ptarget_destroy (void **) */ |
| 1542 |
|
| 1543 |
static int ptarget_invoke(const data_set_t *ds, value_list_t *vl, |
| 1544 |
notification_meta_t **meta, void **user_data) { |
| 1545 |
return fc_exec(FC_TARGET, ds, vl, meta, user_data); |
| 1546 |
} /* static int ptarget_invoke (const data_set_t *, value_list_t *, |
| 1547 |
notification_meta_t **, void **) */ |
| 1548 |
|
| 1549 |
static target_proc_t ptarget = {ptarget_create, ptarget_destroy, |
| 1550 |
ptarget_invoke}; |
| 1551 |
|
| 1552 |
/* |
| 1553 |
* Exported Perl API. |
| 1554 |
*/ |
| 1555 |
|
| 1556 |
static void _plugin_register_generic_userdata(pTHX, int type, |
| 1557 |
const char *desc) { |
| 1558 |
int ret = 0; |
| 1559 |
user_data_t userdata; |
| 1560 |
char *pluginname; |
| 1561 |
|
| 1562 |
dXSARGS; |
| 1563 |
|
| 1564 |
if (2 != items) { |
| 1565 |
log_err("Usage: Collectd::plugin_register_%s(pluginname, subname)", desc); |
| 1566 |
XSRETURN_EMPTY; |
| 1567 |
} |
| 1568 |
|
| 1569 |
if (!SvOK(ST(0))) { |
| 1570 |
log_err("Collectd::plugin_register_%s(pluginname, subname): " |
| 1571 |
"Invalid pluginname", |
| 1572 |
desc); |
| 1573 |
XSRETURN_EMPTY; |
| 1574 |
} |
| 1575 |
if (!SvOK(ST(1))) { |
| 1576 |
log_err("Collectd::plugin_register_%s(pluginname, subname): " |
| 1577 |
"Invalid subname", |
| 1578 |
desc); |
| 1579 |
XSRETURN_EMPTY; |
| 1580 |
} |
| 1581 |
|
| 1582 |
/* Use pluginname as-is to allow flush a single perl plugin */ |
| 1583 |
pluginname = SvPV_nolen(ST(0)); |
| 1584 |
|
| 1585 |
log_debug("Collectd::plugin_register_%s: " |
| 1586 |
"plugin = \"%s\", sub = \"%s\"", |
| 1587 |
desc, pluginname, SvPV_nolen(ST(1))); |
| 1588 |
|
| 1589 |
memset(&userdata, 0, sizeof(userdata)); |
| 1590 |
userdata.data = strdup(SvPV_nolen(ST(1))); |
| 1591 |
userdata.free_func = free; |
| 1592 |
|
| 1593 |
if (PLUGIN_READ == type) { |
| 1594 |
ret = plugin_register_complex_read( |
| 1595 |
"perl", /* group */ |
| 1596 |
pluginname, perl_read, plugin_get_interval(), /* Default interval */ |
| 1597 |
&userdata); |
| 1598 |
} else if (PLUGIN_WRITE == type) { |
| 1599 |
ret = plugin_register_write(pluginname, perl_write, &userdata); |
| 1600 |
} else if (PLUGIN_LOG == type) { |
| 1601 |
ret = plugin_register_log(pluginname, perl_log, &userdata); |
| 1602 |
} else if (PLUGIN_NOTIF == type) { |
| 1603 |
ret = plugin_register_notification(pluginname, perl_notify, &userdata); |
| 1604 |
} else if (PLUGIN_FLUSH == type) { |
| 1605 |
if (1 == register_legacy_flush) { /* For collectd-5.7 only, #1731 */ |
| 1606 |
register_legacy_flush = 0; |
| 1607 |
ret = plugin_register_flush("perl", perl_flush, /* user_data = */ NULL); |
| 1608 |
} |
| 1609 |
|
| 1610 |
if (0 == ret) { |
| 1611 |
ret = plugin_register_flush(pluginname, perl_flush, &userdata); |
| 1612 |
} else { |
| 1613 |
free(userdata.data); |
| 1614 |
} |
| 1615 |
} else { |
| 1616 |
ret = -1; |
| 1617 |
} |
| 1618 |
|
| 1619 |
if (0 == ret) |
| 1620 |
XSRETURN_YES; |
| 1621 |
else |
| 1622 |
XSRETURN_EMPTY; |
| 1623 |
} /* static void _plugin_register_generic_userdata ( ... ) */ |
| 1624 |
|
| 1625 |
/* |
| 1626 |
* Collectd::plugin_register_TYPE (pluginname, subname). |
| 1627 |
* |
| 1628 |
* pluginname: |
| 1629 |
* name of the perl plugin |
| 1630 |
* |
| 1631 |
* subname: |
| 1632 |
* name of the plugin's subroutine that does the work |
| 1633 |
*/ |
| 1634 |
|
| 1635 |
static XS(Collectd_plugin_register_read) { |
| 1636 |
_plugin_register_generic_userdata(aTHX, PLUGIN_READ, "read"); |
| 1637 |
} |
| 1638 |
|
| 1639 |
static XS(Collectd_plugin_register_write) { |
| 1640 |
_plugin_register_generic_userdata(aTHX, PLUGIN_WRITE, "write"); |
| 1641 |
} |
| 1642 |
|
| 1643 |
static XS(Collectd_plugin_register_log) { |
| 1644 |
_plugin_register_generic_userdata(aTHX, PLUGIN_LOG, "log"); |
| 1645 |
} |
| 1646 |
|
| 1647 |
static XS(Collectd_plugin_register_notification) { |
| 1648 |
_plugin_register_generic_userdata(aTHX, PLUGIN_NOTIF, "notification"); |
| 1649 |
} |
| 1650 |
|
| 1651 |
static XS(Collectd_plugin_register_flush) { |
| 1652 |
_plugin_register_generic_userdata(aTHX, PLUGIN_FLUSH, "flush"); |
| 1653 |
} |
| 1654 |
|
| 1655 |
typedef int perl_unregister_function_t(const char *name); |
| 1656 |
|
| 1657 |
static void _plugin_unregister_generic(pTHX, perl_unregister_function_t *unreg, |
| 1658 |
const char *desc) { |
| 1659 |
dXSARGS; |
| 1660 |
|
| 1661 |
if (1 != items) { |
| 1662 |
log_err("Usage: Collectd::plugin_unregister_%s(pluginname)", desc); |
| 1663 |
XSRETURN_EMPTY; |
| 1664 |
} |
| 1665 |
|
| 1666 |
if (!SvOK(ST(0))) { |
| 1667 |
log_err("Collectd::plugin_unregister_%s(pluginname): " |
| 1668 |
"Invalid pluginname", |
| 1669 |
desc); |
| 1670 |
XSRETURN_EMPTY; |
| 1671 |
} |
| 1672 |
|
| 1673 |
log_debug("Collectd::plugin_unregister_%s: plugin = \"%s\"", desc, |
| 1674 |
SvPV_nolen(ST(0))); |
| 1675 |
|
| 1676 |
unreg(SvPV_nolen(ST(0))); |
| 1677 |
|
| 1678 |
XSRETURN_EMPTY; |
| 1679 |
} /* static void _plugin_unregister_generic ( ... ) */ |
| 1680 |
|
| 1681 |
/* |
| 1682 |
* Collectd::plugin_unregister_TYPE (pluginname). |
| 1683 |
* |
| 1684 |
* TYPE: |
| 1685 |
* type of callback to be unregistered: read, write, log, notification, flush |
| 1686 |
* |
| 1687 |
* pluginname: |
| 1688 |
* name of the perl plugin |
| 1689 |
*/ |
| 1690 |
|
| 1691 |
static XS(Collectd_plugin_unregister_read) { |
| 1692 |
_plugin_unregister_generic(aTHX, plugin_unregister_read, "read"); |
| 1693 |
} |
| 1694 |
|
| 1695 |
static XS(Collectd_plugin_unregister_write) { |
| 1696 |
_plugin_unregister_generic(aTHX, plugin_unregister_write, "write"); |
| 1697 |
} |
| 1698 |
|
| 1699 |
static XS(Collectd_plugin_unregister_log) { |
| 1700 |
_plugin_unregister_generic(aTHX, plugin_unregister_log, "log"); |
| 1701 |
} |
| 1702 |
|
| 1703 |
static XS(Collectd_plugin_unregister_notification) { |
| 1704 |
_plugin_unregister_generic(aTHX, plugin_unregister_notification, |
| 1705 |
"notification"); |
| 1706 |
} |
| 1707 |
|
| 1708 |
static XS(Collectd_plugin_unregister_flush) { |
| 1709 |
_plugin_unregister_generic(aTHX, plugin_unregister_flush, "flush"); |
| 1710 |
} |
| 1711 |
|
| 1712 |
/* |
| 1713 |
* Collectd::plugin_register_data_set (type, dataset). |
| 1714 |
* |
| 1715 |
* type: |
| 1716 |
* type of the dataset |
| 1717 |
* |
| 1718 |
* dataset: |
| 1719 |
* dataset to be registered |
| 1720 |
*/ |
| 1721 |
static XS(Collectd_plugin_register_ds) { |
| 1722 |
SV *data = NULL; |
| 1723 |
int ret = 0; |
| 1724 |
|
| 1725 |
dXSARGS; |
| 1726 |
|
| 1727 |
log_warn("Using plugin_register() to register new data-sets is " |
| 1728 |
"deprecated - add new entries to a custom types.db instead."); |
| 1729 |
|
| 1730 |
if (2 != items) { |
| 1731 |
log_err("Usage: Collectd::plugin_register_data_set(type, dataset)"); |
| 1732 |
XSRETURN_EMPTY; |
| 1733 |
} |
| 1734 |
|
| 1735 |
log_debug("Collectd::plugin_register_data_set: " |
| 1736 |
"type = \"%s\", dataset = \"%s\"", |
| 1737 |
SvPV_nolen(ST(0)), SvPV_nolen(ST(1))); |
| 1738 |
|
| 1739 |
data = ST(1); |
| 1740 |
|
| 1741 |
if (SvROK(data) && (SVt_PVAV == SvTYPE(SvRV(data)))) { |
| 1742 |
ret = pplugin_register_data_set(aTHX_ SvPV_nolen(ST(0)), (AV *)SvRV(data)); |
| 1743 |
} else { |
| 1744 |
log_err("Collectd::plugin_register_data_set: Invalid data."); |
| 1745 |
XSRETURN_EMPTY; |
| 1746 |
} |
| 1747 |
|
| 1748 |
if (0 == ret) |
| 1749 |
XSRETURN_YES; |
| 1750 |
else |
| 1751 |
XSRETURN_EMPTY; |
| 1752 |
} /* static XS (Collectd_plugin_register_ds) */ |
| 1753 |
|
| 1754 |
/* |
| 1755 |
* Collectd::plugin_unregister_data_set (type). |
| 1756 |
* |
| 1757 |
* type: |
| 1758 |
* type of the dataset |
| 1759 |
*/ |
| 1760 |
static XS(Collectd_plugin_unregister_ds) { |
| 1761 |
dXSARGS; |
| 1762 |
|
| 1763 |
if (1 != items) { |
| 1764 |
log_err("Usage: Collectd::plugin_unregister_data_set(type)"); |
| 1765 |
XSRETURN_EMPTY; |
| 1766 |
} |
| 1767 |
|
| 1768 |
log_debug("Collectd::plugin_unregister_data_set: type = \"%s\"", |
| 1769 |
SvPV_nolen(ST(0))); |
| 1770 |
|
| 1771 |
if (0 == pplugin_unregister_data_set(SvPV_nolen(ST(0)))) |
| 1772 |
XSRETURN_YES; |
| 1773 |
else |
| 1774 |
XSRETURN_EMPTY; |
| 1775 |
} /* static XS (Collectd_plugin_register_ds) */ |
| 1776 |
|
| 1777 |
/* |
| 1778 |
* Collectd::plugin_dispatch_values (name, values). |
| 1779 |
* |
| 1780 |
* name: |
| 1781 |
* name of the plugin |
| 1782 |
* |
| 1783 |
* values: |
| 1784 |
* value list to submit |
| 1785 |
*/ |
| 1786 |
static XS(Collectd_plugin_dispatch_values) { |
| 1787 |
SV *values = NULL; |
| 1788 |
|
| 1789 |
int ret = 0; |
| 1790 |
|
| 1791 |
dXSARGS; |
| 1792 |
|
| 1793 |
if (1 != items) { |
| 1794 |
log_err("Usage: Collectd::plugin_dispatch_values(values)"); |
| 1795 |
XSRETURN_EMPTY; |
| 1796 |
} |
| 1797 |
|
| 1798 |
log_debug("Collectd::plugin_dispatch_values: values=\"%s\"", |
| 1799 |
SvPV_nolen(ST(/* stack index = */ 0))); |
| 1800 |
|
| 1801 |
values = ST(/* stack index = */ 0); |
| 1802 |
|
| 1803 |
if (NULL == values) |
| 1804 |
XSRETURN_EMPTY; |
| 1805 |
|
| 1806 |
/* Make sure the argument is a hash reference. */ |
| 1807 |
if (!(SvROK(values) && (SVt_PVHV == SvTYPE(SvRV(values))))) { |
| 1808 |
log_err("Collectd::plugin_dispatch_values: Invalid values."); |
| 1809 |
XSRETURN_EMPTY; |
| 1810 |
} |
| 1811 |
|
| 1812 |
ret = pplugin_dispatch_values(aTHX_(HV *) SvRV(values)); |
| 1813 |
|
| 1814 |
if (0 == ret) |
| 1815 |
XSRETURN_YES; |
| 1816 |
else |
| 1817 |
XSRETURN_EMPTY; |
| 1818 |
} /* static XS (Collectd_plugin_dispatch_values) */ |
| 1819 |
|
| 1820 |
/* |
| 1821 |
* Collectd::plugin_get_interval (). |
| 1822 |
*/ |
| 1823 |
static XS(Collectd_plugin_get_interval) { |
| 1824 |
dXSARGS; |
| 1825 |
|
| 1826 |
/* make sure we don't get any unused variable warnings for 'items'; |
| 1827 |
* don't abort, though */ |
| 1828 |
if (items) |
| 1829 |
log_err("Usage: Collectd::plugin_get_interval()"); |
| 1830 |
|
| 1831 |
XSRETURN_NV((NV)CDTIME_T_TO_DOUBLE(plugin_get_interval())); |
| 1832 |
} /* static XS (Collectd_plugin_get_interval) */ |
| 1833 |
|
| 1834 |
/* Collectd::plugin_write (plugin, ds, vl). |
| 1835 |
* |
| 1836 |
* plugin: |
| 1837 |
* name of the plugin to call, may be 'undef' |
| 1838 |
* |
| 1839 |
* ds: |
| 1840 |
* data-set that describes the submitted values, may be 'undef' |
| 1841 |
* |
| 1842 |
* vl: |
| 1843 |
* value-list to be written |
| 1844 |
*/ |
| 1845 |
static XS(Collectd__plugin_write) { |
| 1846 |
char *plugin; |
| 1847 |
SV *ds, *vl; |
| 1848 |
AV *ds_array; |
| 1849 |
|
| 1850 |
int ret; |
| 1851 |
|
| 1852 |
dXSARGS; |
| 1853 |
|
| 1854 |
if (3 != items) { |
| 1855 |
log_err("Usage: Collectd::plugin_write(plugin, ds, vl)"); |
| 1856 |
XSRETURN_EMPTY; |
| 1857 |
} |
| 1858 |
|
| 1859 |
log_debug("Collectd::plugin_write: plugin=\"%s\", ds=\"%s\", vl=\"%s\"", |
| 1860 |
SvPV_nolen(ST(0)), SvOK(ST(1)) ? SvPV_nolen(ST(1)) : "", |
| 1861 |
SvPV_nolen(ST(2))); |
| 1862 |
|
| 1863 |
if (!SvOK(ST(0))) |
| 1864 |
plugin = NULL; |
| 1865 |
else |
| 1866 |
plugin = SvPV_nolen(ST(0)); |
| 1867 |
|
| 1868 |
ds = ST(1); |
| 1869 |
if (SvROK(ds) && (SVt_PVAV == SvTYPE(SvRV(ds)))) |
| 1870 |
ds_array = (AV *)SvRV(ds); |
| 1871 |
else if (!SvOK(ds)) |
| 1872 |
ds_array = NULL; |
| 1873 |
else { |
| 1874 |
log_err("Collectd::plugin_write: Invalid data-set."); |
| 1875 |
XSRETURN_EMPTY; |
| 1876 |
} |
| 1877 |
|
| 1878 |
vl = ST(2); |
| 1879 |
if (!(SvROK(vl) && (SVt_PVHV == SvTYPE(SvRV(vl))))) { |
| 1880 |
log_err("Collectd::plugin_write: Invalid value-list."); |
| 1881 |
XSRETURN_EMPTY; |
| 1882 |
} |
| 1883 |
|
| 1884 |
ret = pplugin_write(aTHX_ plugin, ds_array, (HV *)SvRV(vl)); |
| 1885 |
|
| 1886 |
if (0 == ret) |
| 1887 |
XSRETURN_YES; |
| 1888 |
else |
| 1889 |
XSRETURN_EMPTY; |
| 1890 |
} /* static XS (Collectd__plugin_write) */ |
| 1891 |
|
| 1892 |
/* |
| 1893 |
* Collectd::_plugin_flush (plugin, timeout, identifier). |
| 1894 |
* |
| 1895 |
* plugin: |
| 1896 |
* name of the plugin to flush |
| 1897 |
* |
| 1898 |
* timeout: |
| 1899 |
* timeout to use when flushing the data |
| 1900 |
* |
| 1901 |
* identifier: |
| 1902 |
* data-set identifier to flush |
| 1903 |
*/ |
| 1904 |
static XS(Collectd__plugin_flush) { |
| 1905 |
char *plugin = NULL; |
| 1906 |
int timeout = -1; |
| 1907 |
char *id = NULL; |
| 1908 |
|
| 1909 |
dXSARGS; |
| 1910 |
|
| 1911 |
if (3 != items) { |
| 1912 |
log_err("Usage: Collectd::_plugin_flush(plugin, timeout, id)"); |
| 1913 |
XSRETURN_EMPTY; |
| 1914 |
} |
| 1915 |
|
| 1916 |
if (SvOK(ST(0))) |
| 1917 |
plugin = SvPV_nolen(ST(0)); |
| 1918 |
|
| 1919 |
if (SvOK(ST(1))) |
| 1920 |
timeout = (int)SvIV(ST(1)); |
| 1921 |
|
| 1922 |
if (SvOK(ST(2))) |
| 1923 |
id = SvPV_nolen(ST(2)); |
| 1924 |
|
| 1925 |
log_debug("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, " |
| 1926 |
"id = \"%s\"", |
| 1927 |
plugin, timeout, id); |
| 1928 |
|
| 1929 |
if (0 == plugin_flush(plugin, timeout, id)) |
| 1930 |
XSRETURN_YES; |
| 1931 |
else |
| 1932 |
XSRETURN_EMPTY; |
| 1933 |
} /* static XS (Collectd__plugin_flush) */ |
| 1934 |
|
| 1935 |
/* |
| 1936 |
* Collectd::plugin_dispatch_notification (notif). |
| 1937 |
* |
| 1938 |
* notif: |
| 1939 |
* notification to dispatch |
| 1940 |
*/ |
| 1941 |
static XS(Collectd_plugin_dispatch_notification) { |
| 1942 |
SV *notif = NULL; |
| 1943 |
|
| 1944 |
int ret = 0; |
| 1945 |
|
| 1946 |
dXSARGS; |
| 1947 |
|
| 1948 |
if (1 != items) { |
| 1949 |
log_err("Usage: Collectd::plugin_dispatch_notification(notif)"); |
| 1950 |
XSRETURN_EMPTY; |
| 1951 |
} |
| 1952 |
|
| 1953 |
log_debug("Collectd::plugin_dispatch_notification: notif = \"%s\"", |
| 1954 |
SvPV_nolen(ST(0))); |
| 1955 |
|
| 1956 |
notif = ST(0); |
| 1957 |
|
| 1958 |
if (!(SvROK(notif) && (SVt_PVHV == SvTYPE(SvRV(notif))))) { |
| 1959 |
log_err("Collectd::plugin_dispatch_notification: Invalid notif."); |
| 1960 |
XSRETURN_EMPTY; |
| 1961 |
} |
| 1962 |
|
| 1963 |
ret = pplugin_dispatch_notification(aTHX_(HV *) SvRV(notif)); |
| 1964 |
|
| 1965 |
if (0 == ret) |
| 1966 |
XSRETURN_YES; |
| 1967 |
else |
| 1968 |
XSRETURN_EMPTY; |
| 1969 |
} /* static XS (Collectd_plugin_dispatch_notification) */ |
| 1970 |
|
| 1971 |
/* |
| 1972 |
* Collectd::plugin_log (level, message). |
| 1973 |
* |
| 1974 |
* level: |
| 1975 |
* log level (LOG_DEBUG, ... LOG_ERR) |
| 1976 |
* |
| 1977 |
* message: |
| 1978 |
* log message |
| 1979 |
*/ |
| 1980 |
static XS(Collectd_plugin_log) { |
| 1981 |
dXSARGS; |
| 1982 |
|
| 1983 |
if (2 != items) { |
| 1984 |
log_err("Usage: Collectd::plugin_log(level, message)"); |
| 1985 |
XSRETURN_EMPTY; |
| 1986 |
} |
| 1987 |
|
| 1988 |
plugin_log(SvIV(ST(0)), "%s", SvPV_nolen(ST(1))); |
| 1989 |
XSRETURN_YES; |
| 1990 |
} /* static XS (Collectd_plugin_log) */ |
| 1991 |
|
| 1992 |
/* |
| 1993 |
* Collectd::_fc_register (type, name) |
| 1994 |
* |
| 1995 |
* type: |
| 1996 |
* match | target |
| 1997 |
* |
| 1998 |
* name: |
| 1999 |
* name of the match |
| 2000 |
*/ |
| 2001 |
static XS(Collectd__fc_register) { |
| 2002 |
int type; |
| 2003 |
char *name; |
| 2004 |
|
| 2005 |
int ret = 0; |
| 2006 |
|
| 2007 |
dXSARGS; |
| 2008 |
|
| 2009 |
if (2 != items) { |
| 2010 |
log_err("Usage: Collectd::_fc_register(type, name)"); |
| 2011 |
XSRETURN_EMPTY; |
| 2012 |
} |
| 2013 |
|
| 2014 |
type = SvIV(ST(0)); |
| 2015 |
name = SvPV_nolen(ST(1)); |
| 2016 |
|
| 2017 |
if (FC_MATCH == type) |
| 2018 |
ret = fc_register_match(name, pmatch); |
| 2019 |
else if (FC_TARGET == type) |
| 2020 |
ret = fc_register_target(name, ptarget); |
| 2021 |
|
| 2022 |
if (0 == ret) |
| 2023 |
XSRETURN_YES; |
| 2024 |
else |
| 2025 |
XSRETURN_EMPTY; |
| 2026 |
} /* static XS (Collectd_fc_register) */ |
| 2027 |
|
| 2028 |
/* |
| 2029 |
* Collectd::call_by_name (...). |
| 2030 |
* |
| 2031 |
* Call a Perl sub identified by its name passed through $Collectd::cb_name. |
| 2032 |
*/ |
| 2033 |
static XS(Collectd_call_by_name) { |
| 2034 |
SV *tmp = NULL; |
| 2035 |
char *name = NULL; |
| 2036 |
|
| 2037 |
if (NULL == (tmp = get_sv("Collectd::cb_name", 0))) { |
| 2038 |
sv_setpv(get_sv("@", 1), "cb_name has not been set"); |
| 2039 |
CLEAR_STACK_FRAME; |
| 2040 |
return; |
| 2041 |
} |
| 2042 |
|
| 2043 |
name = SvPV_nolen(tmp); |
| 2044 |
|
| 2045 |
if (NULL == get_cv(name, 0)) { |
| 2046 |
sv_setpvf(get_sv("@", 1), "unknown callback \"%s\"", name); |
| 2047 |
CLEAR_STACK_FRAME; |
| 2048 |
return; |
| 2049 |
} |
| 2050 |
|
| 2051 |
/* simply pass on the subroutine call without touching the stack, |
| 2052 |
* thus leaving any arguments and return values in place */ |
| 2053 |
call_pv(name, 0); |
| 2054 |
} /* static XS (Collectd_call_by_name) */ |
| 2055 |
|
| 2056 |
/* |
| 2057 |
* Interface to collectd. |
| 2058 |
*/ |
| 2059 |
|
| 2060 |
static int perl_init(void) { |
| 2061 |
int status; |
| 2062 |
dTHX; |
| 2063 |
|
| 2064 |
if (NULL == perl_threads) |
| 2065 |
return 0; |
| 2066 |
|
| 2067 |
if (NULL == aTHX) { |
| 2068 |
c_ithread_t *t = NULL; |
| 2069 |
|
| 2070 |
pthread_mutex_lock(&perl_threads->mutex); |
| 2071 |
t = c_ithread_create(perl_threads->head->interp); |
| 2072 |
pthread_mutex_unlock(&perl_threads->mutex); |
| 2073 |
|
| 2074 |
aTHX = t->interp; |
| 2075 |
} |
| 2076 |
|
| 2077 |
log_debug("perl_init: c_ithread: interp = %p (active threads: %i)", aTHX, |
| 2078 |
perl_threads->number_of_threads); |
| 2079 |
|
| 2080 |
/* Lock the base thread to avoid race conditions with c_ithread_create(). |
| 2081 |
* See https://github.com/collectd/collectd/issues/9 and |
| 2082 |
* https://github.com/collectd/collectd/issues/1706 for details. |
| 2083 |
*/ |
| 2084 |
assert(aTHX == perl_threads->head->interp); |
| 2085 |
pthread_mutex_lock(&perl_threads->mutex); |
| 2086 |
|
| 2087 |
status = pplugin_call(aTHX_ PLUGIN_INIT); |
| 2088 |
|
| 2089 |
pthread_mutex_unlock(&perl_threads->mutex); |
| 2090 |
|
| 2091 |
return status; |
| 2092 |
} /* static int perl_init (void) */ |
| 2093 |
|
| 2094 |
static int perl_read(user_data_t *user_data) { |
| 2095 |
dTHX; |
| 2096 |
|
| 2097 |
if (NULL == perl_threads) |
| 2098 |
return 0; |
| 2099 |
|
| 2100 |
if (NULL == aTHX) { |
| 2101 |
c_ithread_t *t = NULL; |
| 2102 |
|
| 2103 |
pthread_mutex_lock(&perl_threads->mutex); |
| 2104 |
t = c_ithread_create(perl_threads->head->interp); |
| 2105 |
pthread_mutex_unlock(&perl_threads->mutex); |
| 2106 |
|
| 2107 |
aTHX = t->interp; |
| 2108 |
} |
| 2109 |
|
| 2110 |
/* Assert that we're not running as the base thread. Otherwise, we might |
| 2111 |
* run into concurrency issues with c_ithread_create(). See |
| 2112 |
* https://github.com/collectd/collectd/issues/9 for details. */ |
| 2113 |
assert(aTHX != perl_threads->head->interp); |
| 2114 |
|
| 2115 |
log_debug("perl_read: c_ithread: interp = %p (active threads: %i)", aTHX, |
| 2116 |
perl_threads->number_of_threads); |
| 2117 |
|
| 2118 |
return pplugin_call(aTHX_ PLUGIN_READ, user_data->data); |
| 2119 |
} /* static int perl_read (user_data_t *user_data) */ |
| 2120 |
|
| 2121 |
static int perl_write(const data_set_t *ds, const value_list_t *vl, |
| 2122 |
user_data_t *user_data) { |
| 2123 |
int status; |
| 2124 |
dTHX; |
| 2125 |
|
| 2126 |
if (NULL == perl_threads) |
| 2127 |
return 0; |
| 2128 |
|
| 2129 |
if (NULL == aTHX) { |
| 2130 |
c_ithread_t *t = NULL; |
| 2131 |
|
| 2132 |
pthread_mutex_lock(&perl_threads->mutex); |
| 2133 |
t = c_ithread_create(perl_threads->head->interp); |
| 2134 |
pthread_mutex_unlock(&perl_threads->mutex); |
| 2135 |
|
| 2136 |
aTHX = t->interp; |
| 2137 |
} |
| 2138 |
|
| 2139 |
/* Lock the base thread if this is not called from one of the read threads |
| 2140 |
* to avoid race conditions with c_ithread_create(). See |
| 2141 |
* https://github.com/collectd/collectd/issues/9 for details. */ |
| 2142 |
if (aTHX == perl_threads->head->interp) |
| 2143 |
pthread_mutex_lock(&perl_threads->mutex); |
| 2144 |
|
| 2145 |
log_debug("perl_write: c_ithread: interp = %p (active threads: %i)", aTHX, |
| 2146 |
perl_threads->number_of_threads); |
| 2147 |
status = pplugin_call(aTHX_ PLUGIN_WRITE, user_data->data, ds, vl); |
| 2148 |
|
| 2149 |
if (aTHX == perl_threads->head->interp) |
| 2150 |
pthread_mutex_unlock(&perl_threads->mutex); |
| 2151 |
|
| 2152 |
return status; |
| 2153 |
} /* static int perl_write (const data_set_t *, const value_list_t *) */ |
| 2154 |
|
| 2155 |
static void perl_log(int level, const char *msg, user_data_t *user_data) { |
| 2156 |
dTHX; |
| 2157 |
|
| 2158 |
if (NULL == perl_threads) |
| 2159 |
return; |
| 2160 |
|
| 2161 |
if (NULL == aTHX) { |
| 2162 |
c_ithread_t *t = NULL; |
| 2163 |
|
| 2164 |
pthread_mutex_lock(&perl_threads->mutex); |
| 2165 |
t = c_ithread_create(perl_threads->head->interp); |
| 2166 |
pthread_mutex_unlock(&perl_threads->mutex); |
| 2167 |
|
| 2168 |
aTHX = t->interp; |
| 2169 |
} |
| 2170 |
|
| 2171 |
/* Lock the base thread if this is not called from one of the read threads |
| 2172 |
* to avoid race conditions with c_ithread_create(). See |
| 2173 |
* https://github.com/collectd/collectd/issues/9 for details. |
| 2174 |
*/ |
| 2175 |
|
| 2176 |
if (aTHX == perl_threads->head->interp) |
| 2177 |
pthread_mutex_lock(&perl_threads->mutex); |
| 2178 |
|
| 2179 |
pplugin_call(aTHX_ PLUGIN_LOG, user_data->data, level, msg); |
| 2180 |
|
| 2181 |
if (aTHX == perl_threads->head->interp) |
| 2182 |
pthread_mutex_unlock(&perl_threads->mutex); |
| 2183 |
|
| 2184 |
return; |
| 2185 |
} /* static void perl_log (int, const char *) */ |
| 2186 |
|
| 2187 |
static int perl_notify(const notification_t *notif, user_data_t *user_data) { |
| 2188 |
dTHX; |
| 2189 |
|
| 2190 |
if (NULL == perl_threads) |
| 2191 |
return 0; |
| 2192 |
|
| 2193 |
if (NULL == aTHX) { |
| 2194 |
c_ithread_t *t = NULL; |
| 2195 |
|
| 2196 |
pthread_mutex_lock(&perl_threads->mutex); |
| 2197 |
t = c_ithread_create(perl_threads->head->interp); |
| 2198 |
pthread_mutex_unlock(&perl_threads->mutex); |
| 2199 |
|
| 2200 |
aTHX = t->interp; |
| 2201 |
} |
| 2202 |
return pplugin_call(aTHX_ PLUGIN_NOTIF, user_data->data, notif); |
| 2203 |
} /* static int perl_notify (const notification_t *) */ |
| 2204 |
|
| 2205 |
static int perl_flush(cdtime_t timeout, const char *identifier, |
| 2206 |
user_data_t *user_data) { |
| 2207 |
dTHX; |
| 2208 |
|
| 2209 |
if (NULL == perl_threads) |
| 2210 |
return 0; |
| 2211 |
|
| 2212 |
if (NULL == aTHX) { |
| 2213 |
c_ithread_t *t = NULL; |
| 2214 |
|
| 2215 |
pthread_mutex_lock(&perl_threads->mutex); |
| 2216 |
t = c_ithread_create(perl_threads->head->interp); |
| 2217 |
pthread_mutex_unlock(&perl_threads->mutex); |
| 2218 |
|
| 2219 |
aTHX = t->interp; |
| 2220 |
} |
| 2221 |
|
| 2222 |
/* For collectd-5.6 only, #1731 */ |
| 2223 |
if (user_data == NULL || user_data->data == NULL) |
| 2224 |
return pplugin_call(aTHX_ PLUGIN_FLUSH_ALL, timeout, identifier); |
| 2225 |
|
| 2226 |
return pplugin_call(aTHX_ PLUGIN_FLUSH, user_data->data, timeout, identifier); |
| 2227 |
} /* static int perl_flush (const int) */ |
| 2228 |
|
| 2229 |
static int perl_shutdown(void) { |
| 2230 |
c_ithread_t *t; |
| 2231 |
int ret; |
| 2232 |
|
| 2233 |
dTHX; |
| 2234 |
|
| 2235 |
plugin_unregister_complex_config("perl"); |
| 2236 |
plugin_unregister_read_group("perl"); |
| 2237 |
|
| 2238 |
if (NULL == perl_threads) |
| 2239 |
return 0; |
| 2240 |
|
| 2241 |
if (NULL == aTHX) { |
| 2242 |
pthread_mutex_lock(&perl_threads->mutex); |
| 2243 |
t = c_ithread_create(perl_threads->head->interp); |
| 2244 |
pthread_mutex_unlock(&perl_threads->mutex); |
| 2245 |
|
| 2246 |
aTHX = t->interp; |
| 2247 |
} |
| 2248 |
|
| 2249 |
log_debug("perl_shutdown: c_ithread: interp = %p (active threads: %i)", aTHX, |
| 2250 |
perl_threads->number_of_threads); |
| 2251 |
|
| 2252 |
plugin_unregister_init("perl"); |
| 2253 |
plugin_unregister_flush("perl"); /* For collectd-5.6 only, #1731 */ |
| 2254 |
|
| 2255 |
ret = pplugin_call(aTHX_ PLUGIN_SHUTDOWN); |
| 2256 |
|
| 2257 |
pthread_mutex_lock(&perl_threads->mutex); |
| 2258 |
t = perl_threads->tail; |
| 2259 |
|
| 2260 |
while (NULL != t) { |
| 2261 |
struct timespec ts_wait; |
| 2262 |
c_ithread_t *thr = t; |
| 2263 |
|
| 2264 |
/* the pointer has to be advanced before destroying |
| 2265 |
* the thread as this will free the memory */ |
| 2266 |
t = t->prev; |
| 2267 |
|
| 2268 |
thr->shutdown = true; |
| 2269 |
if (thr->running) { |
| 2270 |
/* Give some time to thread to exit from Perl interpreter */ |
| 2271 |
WARNING("perl shutdown: Thread is running inside Perl. Waiting."); |
| 2272 |
ts_wait.tv_sec = 0; |
| 2273 |
ts_wait.tv_nsec = 500000; |
| 2274 |
nanosleep(&ts_wait, NULL); |
| 2275 |
} |
| 2276 |
if (thr->running) { |
| 2277 |
pthread_kill(thr->pthread, SIGTERM); |
| 2278 |
ERROR("perl shutdown: Thread hangs inside Perl. Thread killed."); |
| 2279 |
} |
| 2280 |
c_ithread_destroy(thr); |
| 2281 |
} |
| 2282 |
|
| 2283 |
pthread_mutex_unlock(&perl_threads->mutex); |
| 2284 |
pthread_mutex_destroy(&perl_threads->mutex); |
| 2285 |
pthread_mutexattr_destroy(&perl_threads->mutexattr); |
| 2286 |
|
| 2287 |
sfree(perl_threads); |
| 2288 |
|
| 2289 |
pthread_key_delete(perl_thr_key); |
| 2290 |
|
| 2291 |
PERL_SYS_TERM(); |
| 2292 |
|
| 2293 |
plugin_unregister_shutdown("perl"); |
| 2294 |
return ret; |
| 2295 |
} /* static void perl_shutdown (void) */ |
| 2296 |
|
| 2297 |
/* |
| 2298 |
* Access functions for global variables. |
| 2299 |
* |
| 2300 |
* These functions implement the "magic" used to access |
| 2301 |
* the global variables from Perl. |
| 2302 |
*/ |
| 2303 |
|
| 2304 |
static int g_pv_get(pTHX_ SV *var, MAGIC *mg) { |
| 2305 |
char *pv = mg->mg_ptr; |
| 2306 |
sv_setpv(var, pv); |
| 2307 |
return 0; |
| 2308 |
} /* static int g_pv_get (pTHX_ SV *, MAGIC *) */ |
| 2309 |
|
| 2310 |
static int g_pv_set(pTHX_ SV *var, MAGIC *mg) { |
| 2311 |
char *pv = mg->mg_ptr; |
| 2312 |
sstrncpy(pv, SvPV_nolen(var), DATA_MAX_NAME_LEN); |
| 2313 |
return 0; |
| 2314 |
} /* static int g_pv_set (pTHX_ SV *, MAGIC *) */ |
| 2315 |
|
| 2316 |
static int g_interval_get(pTHX_ SV *var, MAGIC *mg) { |
| 2317 |
log_warn("Accessing $interval_g is deprecated (and might not " |
| 2318 |
"give the desired results) - plugin_get_interval() should " |
| 2319 |
"be used instead."); |
| 2320 |
sv_setnv(var, CDTIME_T_TO_DOUBLE(interval_g)); |
| 2321 |
return 0; |
| 2322 |
} /* static int g_interval_get (pTHX_ SV *, MAGIC *) */ |
| 2323 |
|
| 2324 |
static int g_interval_set(pTHX_ SV *var, MAGIC *mg) { |
| 2325 |
double nv = (double)SvNV(var); |
| 2326 |
log_warn("Accessing $interval_g is deprecated (and might not " |
| 2327 |
"give the desired results) - plugin_get_interval() should " |
| 2328 |
"be used instead."); |
| 2329 |
interval_g = DOUBLE_TO_CDTIME_T(nv); |
| 2330 |
return 0; |
| 2331 |
} /* static int g_interval_set (pTHX_ SV *, MAGIC *) */ |
| 2332 |
|
| 2333 |
static MGVTBL g_pv_vtbl = {g_pv_get, |
| 2334 |
g_pv_set, |
| 2335 |
NULL, |
| 2336 |
NULL, |
| 2337 |
NULL, |
| 2338 |
NULL, |
| 2339 |
NULL |
| 2340 |
#if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL |
| 2341 |
, |
| 2342 |
NULL |
| 2343 |
#endif |
| 2344 |
}; |
| 2345 |
static MGVTBL g_interval_vtbl = {g_interval_get, |
| 2346 |
g_interval_set, |
| 2347 |
NULL, |
| 2348 |
NULL, |
| 2349 |
NULL, |
| 2350 |
NULL, |
| 2351 |
NULL |
| 2352 |
#if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL |
| 2353 |
, |
| 2354 |
NULL |
| 2355 |
#endif |
| 2356 |
}; |
| 2357 |
|
| 2358 |
/* bootstrap the Collectd module */ |
| 2359 |
static void xs_init(pTHX) { |
| 2360 |
HV *stash = NULL; |
| 2361 |
SV *tmp = NULL; |
| 2362 |
char *file = __FILE__; |
| 2363 |
|
| 2364 |
dXSUB_SYS; |
| 2365 |
|
| 2366 |
/* enable usage of Perl modules using shared libraries */ |
| 2367 |
newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file); |
| 2368 |
|
| 2369 |
/* register API */ |
| 2370 |
for (int i = 0; NULL != api[i].f; ++i) |
| 2371 |
newXS(api[i].name, api[i].f, file); |
| 2372 |
|
| 2373 |
stash = gv_stashpv("Collectd", 1); |
| 2374 |
|
| 2375 |
/* export "constants" */ |
| 2376 |
for (int i = 0; '\0' != constants[i].name[0]; ++i) |
| 2377 |
newCONSTSUB(stash, constants[i].name, newSViv(constants[i].value)); |
| 2378 |
|
| 2379 |
/* export global variables |
| 2380 |
* by adding "magic" to the SV's representing the globale variables |
| 2381 |
* perl is able to automagically call the get/set function when |
| 2382 |
* accessing any such variable (this is basically the same as using |
| 2383 |
* tie() in Perl) */ |
| 2384 |
/* global strings */ |
| 2385 |
struct { |
| 2386 |
char name[64]; |
| 2387 |
char *var; |
| 2388 |
} g_strings[] = {{"Collectd::hostname_g", hostname_g}, {"", NULL}}; |
| 2389 |
|
| 2390 |
for (int i = 0; '\0' != g_strings[i].name[0]; ++i) { |
| 2391 |
tmp = get_sv(g_strings[i].name, 1); |
| 2392 |
sv_magicext(tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl, g_strings[i].var, 0); |
| 2393 |
} |
| 2394 |
|
| 2395 |
tmp = get_sv("Collectd::interval_g", /* create = */ 1); |
| 2396 |
sv_magicext(tmp, NULL, /* how = */ PERL_MAGIC_ext, |
| 2397 |
/* vtbl = */ &g_interval_vtbl, |
| 2398 |
/* name = */ NULL, /* namelen = */ 0); |
| 2399 |
|
| 2400 |
return; |
| 2401 |
} /* static void xs_init (pTHX) */ |
| 2402 |
|
| 2403 |
/* Initialize the global Perl interpreter. */ |
| 2404 |
static int init_pi(int argc, char **argv) { |
| 2405 |
dTHXa(NULL); |
| 2406 |
|
| 2407 |
if (NULL != perl_threads) |
| 2408 |
return 0; |
| 2409 |
|
| 2410 |
log_info("Initializing Perl interpreter..."); |
| 2411 |
#if COLLECT_DEBUG |
| 2412 |
{ |
| 2413 |
for (int i = 0; i < argc; ++i) |
| 2414 |
log_debug("argv[%i] = \"%s\"", i, argv[i]); |
| 2415 |
} |
| 2416 |
#endif /* COLLECT_DEBUG */ |
| 2417 |
|
| 2418 |
if (0 != pthread_key_create(&perl_thr_key, c_ithread_destructor)) { |
| 2419 |
log_err("init_pi: pthread_key_create failed"); |
| 2420 |
|
| 2421 |
/* this must not happen - cowardly giving up if it does */ |
| 2422 |
return -1; |
| 2423 |
} |
| 2424 |
|
| 2425 |
#ifdef __FreeBSD__ |
| 2426 |
/* On FreeBSD, PERL_SYS_INIT3 expands to some expression which |
| 2427 |
* triggers a "value computed is not used" warning by gcc. */ |
| 2428 |
(void) |
| 2429 |
#endif |
| 2430 |
PERL_SYS_INIT3(&argc, &argv, &environ); |
| 2431 |
|
| 2432 |
perl_threads = smalloc(sizeof(*perl_threads)); |
| 2433 |
memset(perl_threads, 0, sizeof(c_ithread_list_t)); |
| 2434 |
|
| 2435 |
pthread_mutexattr_init(&perl_threads->mutexattr); |
| 2436 |
pthread_mutexattr_settype(&perl_threads->mutexattr, PTHREAD_MUTEX_RECURSIVE); |
| 2437 |
pthread_mutex_init(&perl_threads->mutex, &perl_threads->mutexattr); |
| 2438 |
/* locking the mutex should not be necessary at this point |
| 2439 |
* but let's just do it for the sake of completeness */ |
| 2440 |
pthread_mutex_lock(&perl_threads->mutex); |
| 2441 |
|
| 2442 |
perl_threads->head = c_ithread_create(NULL); |
| 2443 |
perl_threads->tail = perl_threads->head; |
| 2444 |
|
| 2445 |
if (NULL == (perl_threads->head->interp = perl_alloc())) { |
| 2446 |
log_err("init_pi: Not enough memory."); |
| 2447 |
exit(3); |
| 2448 |
} |
| 2449 |
|
| 2450 |
aTHX = perl_threads->head->interp; |
| 2451 |
pthread_mutex_unlock(&perl_threads->mutex); |
| 2452 |
|
| 2453 |
perl_construct(aTHX); |
| 2454 |
|
| 2455 |
PL_exit_flags |= PERL_EXIT_DESTRUCT_END; |
| 2456 |
|
| 2457 |
if (0 != perl_parse(aTHX_ xs_init, argc, argv, NULL)) { |
| 2458 |
SV *err = get_sv("@", 1); |
| 2459 |
log_err("init_pi: Unable to bootstrap Collectd: %s", SvPV_nolen(err)); |
| 2460 |
|
| 2461 |
perl_destruct(perl_threads->head->interp); |
| 2462 |
perl_free(perl_threads->head->interp); |
| 2463 |
sfree(perl_threads); |
| 2464 |
|
| 2465 |
pthread_key_delete(perl_thr_key); |
| 2466 |
return -1; |
| 2467 |
} |
| 2468 |
|
| 2469 |
/* Set $0 to "collectd" because perl_parse() has to set it to "-e". */ |
| 2470 |
sv_setpv(get_sv("0", 0), "collectd"); |
| 2471 |
|
| 2472 |
perl_run(aTHX); |
| 2473 |
|
| 2474 |
plugin_register_init("perl", perl_init); |
| 2475 |
plugin_register_shutdown("perl", perl_shutdown); |
| 2476 |
return 0; |
| 2477 |
} /* static int init_pi (const char **, const int) */ |
| 2478 |
|
| 2479 |
/* |
| 2480 |
* LoadPlugin "<Plugin>" |
| 2481 |
*/ |
| 2482 |
static int perl_config_loadplugin(pTHX_ oconfig_item_t *ci) { |
| 2483 |
char module_name[DATA_MAX_NAME_LEN]; |
| 2484 |
|
| 2485 |
char *value = NULL; |
| 2486 |
|
| 2487 |
if ((0 != ci->children_num) || (1 != ci->values_num) || |
| 2488 |
(OCONFIG_TYPE_STRING != ci->values[0].type)) { |
| 2489 |
log_err("LoadPlugin expects a single string argument."); |
| 2490 |
return 1; |
| 2491 |
} |
| 2492 |
|
| 2493 |
value = ci->values[0].value.string; |
| 2494 |
|
| 2495 |
if (NULL == get_module_name(module_name, sizeof(module_name), value)) { |
| 2496 |
log_err("Invalid module name %s", value); |
| 2497 |
return 1; |
| 2498 |
} |
| 2499 |
|
| 2500 |
if (0 != init_pi(perl_argc, perl_argv)) |
| 2501 |
return -1; |
| 2502 |
|
| 2503 |
assert(NULL != perl_threads); |
| 2504 |
assert(NULL != perl_threads->head); |
| 2505 |
|
| 2506 |
aTHX = perl_threads->head->interp; |
| 2507 |
|
| 2508 |
log_debug("perl_config: Loading Perl plugin \"%s\"", value); |
| 2509 |
load_module(PERL_LOADMOD_NOIMPORT, newSVpv(module_name, strlen(module_name)), |
| 2510 |
Nullsv); |
| 2511 |
return 0; |
| 2512 |
} /* static int perl_config_loadplugin (oconfig_item_it *) */ |
| 2513 |
|
| 2514 |
/* |
| 2515 |
* BaseName "<Name>" |
| 2516 |
*/ |
| 2517 |
static int perl_config_basename(pTHX_ oconfig_item_t *ci) { |
| 2518 |
char *value = NULL; |
| 2519 |
|
| 2520 |
if ((0 != ci->children_num) || (1 != ci->values_num) || |
| 2521 |
(OCONFIG_TYPE_STRING != ci->values[0].type)) { |
| 2522 |
log_err("BaseName expects a single string argument."); |
| 2523 |
return 1; |
| 2524 |
} |
| 2525 |
|
| 2526 |
value = ci->values[0].value.string; |
| 2527 |
|
| 2528 |
log_debug("perl_config: Setting plugin basename to \"%s\"", value); |
| 2529 |
sstrncpy(base_name, value, sizeof(base_name)); |
| 2530 |
return 0; |
| 2531 |
} /* static int perl_config_basename (oconfig_item_it *) */ |
| 2532 |
|
| 2533 |
/* |
| 2534 |
* EnableDebugger "<Package>"|"" |
| 2535 |
*/ |
| 2536 |
static int perl_config_enabledebugger(pTHX_ oconfig_item_t *ci) { |
| 2537 |
char *value = NULL; |
| 2538 |
|
| 2539 |
if ((0 != ci->children_num) || (1 != ci->values_num) || |
| 2540 |
(OCONFIG_TYPE_STRING != ci->values[0].type)) { |
| 2541 |
log_err("EnableDebugger expects a single string argument."); |
| 2542 |
return 1; |
| 2543 |
} |
| 2544 |
|
| 2545 |
if (NULL != perl_threads) { |
| 2546 |
log_warn("EnableDebugger has no effects if used after LoadPlugin."); |
| 2547 |
return 1; |
| 2548 |
} |
| 2549 |
|
| 2550 |
value = ci->values[0].value.string; |
| 2551 |
|
| 2552 |
perl_argv = realloc(perl_argv, (++perl_argc + 1) * sizeof(char *)); |
| 2553 |
|
| 2554 |
if (NULL == perl_argv) { |
| 2555 |
log_err("perl_config: Not enough memory."); |
| 2556 |
exit(3); |
| 2557 |
} |
| 2558 |
|
| 2559 |
if ('\0' == value[0]) { |
| 2560 |
perl_argv[perl_argc - 1] = "-d"; |
| 2561 |
} else { |
| 2562 |
perl_argv[perl_argc - 1] = smalloc(strlen(value) + 4); |
| 2563 |
sstrncpy(perl_argv[perl_argc - 1], "-d:", 4); |
| 2564 |
sstrncpy(perl_argv[perl_argc - 1] + 3, value, strlen(value) + 1); |
| 2565 |
} |
| 2566 |
|
| 2567 |
perl_argv[perl_argc] = NULL; |
| 2568 |
return 0; |
| 2569 |
} /* static int perl_config_enabledebugger (oconfig_item_it *) */ |
| 2570 |
|
| 2571 |
/* |
| 2572 |
* IncludeDir "<Dir>" |
| 2573 |
*/ |
| 2574 |
static int perl_config_includedir(pTHX_ oconfig_item_t *ci) { |
| 2575 |
char *value = NULL; |
| 2576 |
|
| 2577 |
if ((0 != ci->children_num) || (1 != ci->values_num) || |
| 2578 |
(OCONFIG_TYPE_STRING != ci->values[0].type)) { |
| 2579 |
log_err("IncludeDir expects a single string argument."); |
| 2580 |
return 1; |
| 2581 |
} |
| 2582 |
|
| 2583 |
value = ci->values[0].value.string; |
| 2584 |
|
| 2585 |
if (NULL == aTHX) { |
| 2586 |
perl_argv = realloc(perl_argv, (++perl_argc + 1) * sizeof(char *)); |
| 2587 |
|
| 2588 |
if (NULL == perl_argv) { |
| 2589 |
log_err("perl_config: Not enough memory."); |
| 2590 |
exit(3); |
| 2591 |
} |
| 2592 |
|
| 2593 |
perl_argv[perl_argc - 1] = smalloc(strlen(value) + 3); |
| 2594 |
sstrncpy(perl_argv[perl_argc - 1], "-I", 3); |
| 2595 |
sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen(value) + 1); |
| 2596 |
|
| 2597 |
perl_argv[perl_argc] = NULL; |
| 2598 |
} else { |
| 2599 |
/* prepend the directory to @INC */ |
| 2600 |
av_unshift(GvAVn(PL_incgv), 1); |
| 2601 |
av_store(GvAVn(PL_incgv), 0, newSVpv(value, strlen(value))); |
| 2602 |
} |
| 2603 |
return 0; |
| 2604 |
} /* static int perl_config_includedir (oconfig_item_it *) */ |
| 2605 |
|
| 2606 |
/* |
| 2607 |
* <Plugin> block |
| 2608 |
*/ |
| 2609 |
static int perl_config_plugin(pTHX_ oconfig_item_t *ci) { |
| 2610 |
int retvals = 0; |
| 2611 |
int ret = 0; |
| 2612 |
|
| 2613 |
char *plugin; |
| 2614 |
HV *config; |
| 2615 |
|
| 2616 |
if (NULL == perl_threads) { |
| 2617 |
log_err("A `Plugin' block was encountered but no plugin was loaded yet. " |
| 2618 |
"Put the appropriate `LoadPlugin' option in front of it."); |
| 2619 |
return -1; |
| 2620 |
} |
| 2621 |
|
| 2622 |
dSP; |
| 2623 |
|
| 2624 |
if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) { |
| 2625 |
log_err("LoadPlugin expects a single string argument."); |
| 2626 |
return 1; |
| 2627 |
} |
| 2628 |
|
| 2629 |
plugin = ci->values[0].value.string; |
| 2630 |
config = newHV(); |
| 2631 |
|
| 2632 |
if (0 != oconfig_item2hv(aTHX_ ci, config)) { |
| 2633 |
hv_clear(config); |
| 2634 |
hv_undef(config); |
| 2635 |
|
| 2636 |
log_err("Unable to convert configuration to a Perl hash value."); |
| 2637 |
config = (HV *)&PL_sv_undef; |
| 2638 |
} |
| 2639 |
|
| 2640 |
ENTER; |
| 2641 |
SAVETMPS; |
| 2642 |
|
| 2643 |
PUSHMARK(SP); |
| 2644 |
|
| 2645 |
XPUSHs(sv_2mortal(newSVpv(plugin, 0))); |
| 2646 |
XPUSHs(sv_2mortal(newRV_noinc((SV *)config))); |
| 2647 |
|
| 2648 |
PUTBACK; |
| 2649 |
|
| 2650 |
retvals = call_pv("Collectd::_plugin_dispatch_config", G_SCALAR); |
| 2651 |
|
| 2652 |
SPAGAIN; |
| 2653 |
if (0 < retvals) { |
| 2654 |
SV *tmp = POPs; |
| 2655 |
if (!SvTRUE(tmp)) |
| 2656 |
ret = 1; |
| 2657 |
} else |
| 2658 |
ret = 1; |
| 2659 |
|
| 2660 |
PUTBACK; |
| 2661 |
FREETMPS; |
| 2662 |
LEAVE; |
| 2663 |
return ret; |
| 2664 |
} /* static int perl_config_plugin (oconfig_item_it *) */ |
| 2665 |
|
| 2666 |
static int perl_config(oconfig_item_t *ci) { |
| 2667 |
int status = 0; |
| 2668 |
|
| 2669 |
dTHXa(NULL); |
| 2670 |
|
| 2671 |
for (int i = 0; i < ci->children_num; ++i) { |
| 2672 |
oconfig_item_t *c = ci->children + i; |
| 2673 |
int current_status = 0; |
| 2674 |
|
| 2675 |
if (NULL != perl_threads) { |
| 2676 |
if ((aTHX = PERL_GET_CONTEXT) == NULL) |
| 2677 |
return -1; |
| 2678 |
} |
| 2679 |
|
| 2680 |
if (0 == strcasecmp(c->key, "LoadPlugin")) |
| 2681 |
current_status = perl_config_loadplugin(aTHX_ c); |
| 2682 |
else if (0 == strcasecmp(c->key, "BaseName")) |
| 2683 |
current_status = perl_config_basename(aTHX_ c); |
| 2684 |
else if (0 == strcasecmp(c->key, "EnableDebugger")) |
| 2685 |
current_status = perl_config_enabledebugger(aTHX_ c); |
| 2686 |
else if (0 == strcasecmp(c->key, "IncludeDir")) |
| 2687 |
current_status = perl_config_includedir(aTHX_ c); |
| 2688 |
else if (0 == strcasecmp(c->key, "Plugin")) |
| 2689 |
current_status = perl_config_plugin(aTHX_ c); |
| 2690 |
else if (0 == strcasecmp(c->key, "RegisterLegacyFlush")) |
| 2691 |
cf_util_get_boolean(c, ®ister_legacy_flush); |
| 2692 |
else { |
| 2693 |
log_warn("Ignoring unknown config key \"%s\".", c->key); |
| 2694 |
current_status = 0; |
| 2695 |
} |
| 2696 |
|
| 2697 |
/* fatal error - it's up to perl_config_* to clean up */ |
| 2698 |
if (0 > current_status) { |
| 2699 |
log_err("Configuration failed with a fatal error - " |
| 2700 |
"plugin disabled!"); |
| 2701 |
return current_status; |
| 2702 |
} |
| 2703 |
|
| 2704 |
status += current_status; |
| 2705 |
} |
| 2706 |
return status; |
| 2707 |
} /* static int perl_config (oconfig_item_t *) */ |
| 2708 |
|
| 2709 |
void module_register(void) { |
| 2710 |
perl_argc = 4; |
| 2711 |
perl_argv = smalloc((perl_argc + 1) * sizeof(*perl_argv)); |
| 2712 |
|
| 2713 |
/* default options for the Perl interpreter */ |
| 2714 |
perl_argv[0] = ""; |
| 2715 |
perl_argv[1] = "-MCollectd"; |
| 2716 |
perl_argv[2] = "-e"; |
| 2717 |
perl_argv[3] = "1"; |
| 2718 |
perl_argv[4] = NULL; |
| 2719 |
|
| 2720 |
plugin_register_complex_config("perl", perl_config); |
| 2721 |
return; |
| 2722 |
} /* void module_register (void) */ |