X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=contrib%2Fexamples%2Fmyplugin.c;h=9539062c9383b88ace7e06954ec60e6bb9fa6684;hb=03223b683c5c7340550028bf445e144004d1f24d;hp=fc7856b969e77517dd0949a01d97532f4519991d;hpb=58ecdb7ff9a4d90e87daecca05b9cb4a43070fd7;p=collectd.git diff --git a/contrib/examples/myplugin.c b/contrib/examples/myplugin.c index fc7856b9..9539062c 100644 --- a/contrib/examples/myplugin.c +++ b/contrib/examples/myplugin.c @@ -18,7 +18,8 @@ * is optional */ -#include +#if ! HAVE_CONFIG_H + #include #include @@ -33,6 +34,10 @@ # undef __USE_ISOC99 #endif /* DISABLE_ISOC99 */ +#include + +#endif /* ! HAVE_CONFIG */ + #include #include #include @@ -54,6 +59,12 @@ static data_source_t dsrc[1] = * - name of the data set * - number of data sources * - list of data sources + * + * NOTE: If you're defining a custom data-set, you have to make that known to + * any servers as well. Else, the server is not able to store values using the + * type defined by that data-set. + * It is strongly recommended to use one of the types and data-sets + * pre-defined in the types.db file. */ static data_set_t ds = { @@ -87,15 +98,18 @@ static int my_read (void) vl.values = values; vl.values_len = 1; vl.time = time (NULL); - strcpy (vl.host, hostname_g); - strcpy (vl.plugin, "myplugin"); + 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.plugin)); /* 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 */ - plugin_dispatch_values ("myplugin", &vl); + * write functions */ + plugin_dispatch_values (&vl); /* A return value != 0 indicates an error and the plugin will be skipped * for an increasing amount of time. */ @@ -105,7 +119,8 @@ 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; @@ -139,13 +154,51 @@ 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; } /* static void my_log (int, const char *) */ /* + * This function is called when plugin_dispatch_notification () has been used. + */ +static int my_notify (const notification_t *notif, user_data_t *ud) +{ + char time_str[32] = ""; + struct tm *tm = NULL; + + int n = 0; + + if (NULL == (tm = localtime (¬if->time))) + time_str[0] = '\0'; + + n = strftime (time_str, 32, "%F %T", tm); + if (n >= 32) n = 31; + time_str[n] = '\0'; + + printf ("NOTIF (%s): %i - ", time_str, notif->severity); + + if ('\0' != *notif->host) + printf ("%s: ", notif->host); + + if ('\0' != *notif->plugin) + printf ("%s: ", notif->plugin); + + if ('\0' != *notif->plugin_instance) + printf ("%s: ", notif->plugin_instance); + + if ('\0' != *notif->type) + printf ("%s: ", notif->type); + + if ('\0' != *notif->type_instance) + printf ("%s: ", notif->type_instance); + + printf ("%s\n", notif->message); + return 0; +} /* static int my_notify (notification_t *) */ + +/* * This function is called before shutting down collectd. */ static int my_shutdown (void) @@ -160,11 +213,13 @@ static int my_shutdown (void) */ void module_register (void) { - plugin_register_log ("myplugin", my_log); + 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) */