X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=contrib%2Fexamples%2Fmyplugin.c;h=9539062c9383b88ace7e06954ec60e6bb9fa6684;hb=51bc7c4a5523f2b03c24e32064b28c7c8426534d;hp=f68cc1ac85c090792efa1bfe9433272327b378fa;hpb=66b7c4bce4e628d8c11cc6762621f155bd68536d;p=collectd.git diff --git a/contrib/examples/myplugin.c b/contrib/examples/myplugin.c index f68cc1ac..71fb5f8b 100644 --- a/contrib/examples/myplugin.c +++ b/contrib/examples/myplugin.c @@ -39,6 +39,7 @@ #endif /* ! HAVE_CONFIG */ #include + #include #include @@ -84,40 +85,57 @@ static int my_init (void) } /* static int my_init (void) */ /* - * This function is called in regular intervalls to collect the data. + * This is a utility function used by the read callback to populate a + * value_list_t and pass it to plugin_dispatch_values. */ -static int my_read (void) +static int my_submit (gauge_t value) { - value_t values[1]; /* the size of this list should equal the number of - data sources */ value_list_t vl = VALUE_LIST_INIT; - /* do the magic to read the data */ - values[0].gauge = random (); - - vl.values = values; + /* Convert the gauge_t to a value_t and add it to the value_list_t. */ + vl.values = &(value_t) { .gauge = value }; vl.values_len = 1; - vl.time = time (NULL); + + /* Only set vl.time yourself if you update multiple metrics (i.e. you + * have multiple calls to plugin_dispatch_values()) and they need to all + * have the same timestamp. */ + /* vl.time = cdtime(); */ + sstrncpy (vl.host, hostname_g, sizeof (vl.host)); sstrncpy (vl.plugin, "myplugin", sizeof (vl.plugin)); + + /* it is strongly recommended to use a type defined in the types.db file + * instead of a custom type */ + sstrncpy (vl.type, "myplugin", sizeof (vl.type)); /* optionally set vl.plugin_instance and vl.type_instance to reasonable * values (default: "") */ /* dispatch the values to collectd which passes them on to all registered - * write functions - the first argument is used to lookup the data set - * definition (it is strongly recommended to use a type defined in the - * types.db file) */ - plugin_dispatch_values ("myplugin", &vl); + * write functions */ + return plugin_dispatch_values (&vl); +} + +/* + * This function is called in regular intervalls to collect the data. + */ +static int my_read (void) +{ + /* do the magic to read the data */ + gauge_t value = random (); + + if (my_submit (value) != 0) + WARNING ("myplugin plugin: Dispatching a random value failed."); /* A return value != 0 indicates an error and the plugin will be skipped * for an increasing amount of time. */ - return 0; + return 0; } /* static int my_read (void) */ /* * This function is called after values have been dispatched to collectd. */ -static int my_write (const data_set_t *ds, const value_list_t *vl) +static int my_write (const data_set_t *ds, const value_list_t *vl, + user_data_t *ud) { char name[1024] = ""; int i = 0; @@ -151,7 +169,7 @@ static int my_write (const data_set_t *ds, const value_list_t *vl) /* * This function is called when plugin_log () has been used. */ -static void my_log (int severity, const char *msg) +static void my_log (int severity, const char *msg, user_data_t *ud) { printf ("LOG: %i - %s\n", severity, msg); return; @@ -160,7 +178,7 @@ static void my_log (int severity, const char *msg) /* * This function is called when plugin_dispatch_notification () has been used. */ -static int my_notify (const notification_t *notif) +static int my_notify (const notification_t *notif, user_data_t *ud) { char time_str[32] = ""; struct tm *tm = NULL; @@ -210,12 +228,13 @@ static int my_shutdown (void) */ void module_register (void) { - plugin_register_log ("myplugin", my_log); - plugin_register_notification ("myplugin", my_notify); + plugin_register_log ("myplugin", my_log, /* user data */ NULL); + plugin_register_notification ("myplugin", my_notify, + /* user data */ NULL); plugin_register_data_set (&ds); plugin_register_read ("myplugin", my_read); plugin_register_init ("myplugin", my_init); - plugin_register_write ("myplugin", my_write); + plugin_register_write ("myplugin", my_write, /* user data */ NULL); plugin_register_shutdown ("myplugin", my_shutdown); return; } /* void module_register (void) */