X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=contrib%2Fexamples%2Fmyplugin.c;h=d95f10b55c539ea82cf549b1bef0c86c47f91ac1;hb=dd73647f366f003b30792821bfd93164af259c8e;hp=aa8c49c67862eef61be4fc28bdaf6449cbf8f967;hpb=79963d13c1884d1d92667cc502ad20758b084a12;p=collectd.git diff --git a/contrib/examples/myplugin.c b/contrib/examples/myplugin.c index aa8c49c6..d95f10b5 100644 --- a/contrib/examples/myplugin.c +++ b/contrib/examples/myplugin.c @@ -78,20 +78,21 @@ 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) { - value_t values[1]; /* the size of this list should equal the number of - data sources */ +static int my_submit(gauge_t value) { 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); - sstrncpy(vl.host, hostname_g, sizeof(vl.host)); + + /* 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.plugin, "myplugin", sizeof(vl.plugin)); /* it is strongly recommended to use a type defined in the types.db file @@ -102,7 +103,18 @@ static int my_read(void) { /* dispatch the values to collectd which passes them on to all registered * write functions */ - plugin_dispatch_values(&vl); + 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. */