Work with unix socket more defensively
[collectd.git] / debian / examples / myplugin.c
1 /*
2  * /usr/share/doc/collectd/examples/sample_plugin.c
3  *
4  * A sample plugin for collectd.
5  *
6  * Written by Sebastian Harl <sh@tokkee.org>
7  *
8  * This is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License as published by the Free 
10  * Software Foundation; either version 2 of the License, or (at your 
11  * option) any later version.
12  */
13
14 #include <collectd/common.h>    /* rrd_update_file */
15 #include <collectd/plugin.h>    /* plugin_* */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19
20 /* Optional config file support */
21 /* #include <collectd/configfile.h> */
22
23 /* Optional debugging support 
24  * (only available if collectd was compiled with debugging support) */
25 /* #include <collectd/utils_debug.h> */
26
27 #define MODULE_NAME "myplugin"
28
29 /* Name of the rrd file under DataDir (/var/lib/collectd by default)
30  *
31  * The name may contain slashes to create subdirectories. */
32 static char *my_rrd = "myplugin.rrd";
33
34 /* DS definitions for the rrd file
35  *
36  * See the rrdcreate(1) manpage for details. The heartbeat is configurable in
37  * collectd. It defaults to 25. */
38 static char *ds_def[] =
39 {
40     "DS:my_ds:GAUGE:25:0:U",
41     NULL
42 };
43
44 /* DS count */
45 static int ds_num = 1;
46
47 /* Time at which the read function is called */
48 extern time_t curtime;
49
50 /* Initialize the plugin
51  *
52  * This function is called to set up a plugin before using it. */
53 static void my_init(void)
54 {
55     /* we have nothing to do here :-) */
56     return;
57 }
58
59 /* Get the data
60  *
61  * This function implements the magic used to get the desired values that
62  * should be stored in the rrd file. It uses plugin_submit to transfer the
63  * data to whatever place is configured in the config file. If there are more
64  * than one instances you should pass a uniq identifier as seconds argument to
65  * the plugin_submit function. */
66 #define BUFSIZE 256
67 static void my_read(void)
68 {
69     long int data = 0;
70     char buf[BUFSIZE] = "";
71
72     /* magic ;-) */
73     data = random();
74
75     if (snprintf(buf, BUFSIZE, "%u:%li", 
76                 (unsigned int)curtime, data) >= BUFSIZE)
77         return;
78
79     plugin_submit(MODULE_NAME, NULL, buf);
80     return;
81 }
82 #undef BUFSIZE
83
84 /* Save the data
85  *
86  * This function saves the data to the appropriate location by calling
87  * rrd_update_file. It is used to "calculate" the filename and DS definition
88  * appropriate for the given instance. */
89 static void my_write(host, inst, val)
90     char *host;
91     char *inst;
92     char *val;
93 {
94     rrd_update_file(host, my_rrd, val, ds_def, ds_num);
95     return;
96 }
97
98 /* Register the plugin
99  *
100  * This function registers the plugin with collectd. It has to be named
101  * "module_register". */
102 void module_register(void)
103 {
104     plugin_register(MODULE_NAME, my_init, my_read, my_write);
105     return;
106 }
107