Merge branch 'pr/1918'
[collectd.git] / src / target_v5upgrade.c
1 /**
2  * collectd - src/target_v5upgrade.c
3  * Copyright (C) 2008-2010  Florian Forster
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  *   Florian Forster <octo at collectd.org>
25  **/
26
27 #include "collectd.h"
28
29 #include "plugin.h"
30 #include "common.h"
31 #include "filter_chain.h"
32
33 static void v5_swap_instances (value_list_t *vl) /* {{{ */
34 {
35   char tmp[DATA_MAX_NAME_LEN];
36
37   assert (sizeof (tmp) == sizeof (vl->plugin_instance));
38   assert (sizeof (tmp) == sizeof (vl->type_instance));
39
40   memcpy (tmp, vl->plugin_instance, sizeof (tmp));
41   memcpy (vl->plugin_instance, vl->type_instance, sizeof (tmp));
42   memcpy (vl->type_instance, tmp, sizeof (tmp));
43 } /* }}} void v5_swap_instances */
44
45 /*
46  * Df type
47  *
48  * By default, the "df" plugin of version 4.* uses the "df" type and puts the
49  * mount point in the type instance. Detect this behavior and convert the type
50  * to "df_complex". This can be selected in versions 4.9 and 4.10 by setting
51  * the "ReportReserved" option of the "df" plugin.
52  */
53 static int v5_df (const data_set_t *ds, value_list_t *vl) /* {{{ */
54 {
55   value_list_t new_vl;
56
57   /* Can't upgrade if both instances have been set. */
58   if ((vl->plugin_instance[0] != 0)
59       && (vl->type_instance[0] != 0))
60     return (FC_TARGET_CONTINUE);
61
62   /* Copy everything: Time, interval, host, ... */
63   memcpy (&new_vl, vl, sizeof (new_vl));
64
65   /* Reset data we can't simply copy */
66   new_vl.values = &(value_t) { .gauge = NAN };
67   new_vl.values_len = 1;
68   new_vl.meta = NULL;
69
70   /* Move the mount point name to the plugin instance */
71   if (new_vl.plugin_instance[0] == 0)
72     v5_swap_instances (&new_vl);
73
74   /* Change the type to "df_complex" */
75   sstrncpy (new_vl.type, "df_complex", sizeof (new_vl.type));
76
77   /* Dispatch two new value lists instead of this one */
78   new_vl.values[0].gauge = vl->values[0].gauge;
79   sstrncpy (new_vl.type_instance, "used", sizeof (new_vl.type_instance));
80   plugin_dispatch_values (&new_vl);
81
82   new_vl.values[0].gauge = vl->values[1].gauge;
83   sstrncpy (new_vl.type_instance, "free", sizeof (new_vl.type_instance));
84   plugin_dispatch_values (&new_vl);
85
86   /* Abort processing */
87   return (FC_TARGET_STOP);
88 } /* }}} int v5_df */
89
90 /*
91  * Interface plugin
92  *
93  * 4.* stores the interface in the type instance and leaves the plugin
94  * instance empty. If this is the case, put the interface name into the plugin
95  * instance and clear the type instance.
96  */
97 static int v5_interface (const data_set_t *ds, value_list_t *vl) /* {{{ */
98 {
99   if ((vl->plugin_instance[0] != 0) || (vl->type_instance[0] == 0))
100     return (FC_TARGET_CONTINUE);
101
102   v5_swap_instances (vl);
103   return (FC_TARGET_CONTINUE);
104 } /* }}} int v5_interface */
105
106 /*
107  * MySQL query cache
108  *
109  * 4.* uses the "mysql_qcache" type which mixes different types of
110  * information. In 5.* this has been broken up.
111  */
112 static int v5_mysql_qcache (const data_set_t *ds, value_list_t *vl) /* {{{ */
113 {
114   value_list_t new_vl;
115
116   if (vl->values_len != 5)
117     return (FC_TARGET_STOP);
118
119   /* Copy everything: Time, interval, host, ... */
120   memcpy (&new_vl, vl, sizeof (new_vl));
121
122   /* Reset data we can't simply copy */
123   new_vl.values = &(value_t) { .gauge = NAN };
124   new_vl.values_len = 1;
125   new_vl.meta = NULL;
126
127   /* Change the type to "cache_result" */
128   sstrncpy (new_vl.type, "cache_result", sizeof (new_vl.type));
129
130   /* Dispatch new value lists instead of this one */
131   new_vl.values[0].derive = (derive_t) vl->values[0].counter;
132   sstrncpy (new_vl.type_instance, "qcache-hits",
133       sizeof (new_vl.type_instance));
134   plugin_dispatch_values (&new_vl);
135
136   new_vl.values[0].derive = (derive_t) vl->values[1].counter;
137   sstrncpy (new_vl.type_instance, "qcache-inserts",
138       sizeof (new_vl.type_instance));
139   plugin_dispatch_values (&new_vl);
140
141   new_vl.values[0].derive = (derive_t) vl->values[2].counter;
142   sstrncpy (new_vl.type_instance, "qcache-not_cached",
143       sizeof (new_vl.type_instance));
144   plugin_dispatch_values (&new_vl);
145
146   new_vl.values[0].derive = (derive_t) vl->values[3].counter;
147   sstrncpy (new_vl.type_instance, "qcache-prunes",
148       sizeof (new_vl.type_instance));
149   plugin_dispatch_values (&new_vl);
150
151   /* The last data source is a gauge value, so we have to use a different type
152    * here. */
153   new_vl.values[0].gauge = vl->values[4].gauge;
154   sstrncpy (new_vl.type, "cache_size", sizeof (new_vl.type));
155   sstrncpy (new_vl.type_instance, "qcache",
156       sizeof (new_vl.type_instance));
157   plugin_dispatch_values (&new_vl);
158
159   /* Abort processing */
160   return (FC_TARGET_STOP);
161 } /* }}} int v5_mysql_qcache */
162
163 /*
164  * MySQL thread count
165  *
166  * 4.* uses the "mysql_threads" type which mixes different types of
167  * information. In 5.* this has been broken up.
168  */
169 static int v5_mysql_threads (const data_set_t *ds, value_list_t *vl) /* {{{ */
170 {
171   value_list_t new_vl;
172
173   if (vl->values_len != 4)
174     return (FC_TARGET_STOP);
175
176   /* Copy everything: Time, interval, host, ... */
177   memcpy (&new_vl, vl, sizeof (new_vl));
178
179   /* Reset data we can't simply copy */
180   new_vl.values = &(value_t) { .gauge = NAN };
181   new_vl.values_len = 1;
182   new_vl.meta = NULL;
183
184   /* Change the type to "threads" */
185   sstrncpy (new_vl.type, "threads", sizeof (new_vl.type));
186
187   /* Dispatch new value lists instead of this one */
188   new_vl.values[0].gauge = vl->values[0].gauge;
189   sstrncpy (new_vl.type_instance, "running",
190       sizeof (new_vl.type_instance));
191   plugin_dispatch_values (&new_vl);
192
193   new_vl.values[0].gauge = vl->values[1].gauge;
194   sstrncpy (new_vl.type_instance, "connected",
195       sizeof (new_vl.type_instance));
196   plugin_dispatch_values (&new_vl);
197
198   new_vl.values[0].gauge = vl->values[2].gauge;
199   sstrncpy (new_vl.type_instance, "cached",
200       sizeof (new_vl.type_instance));
201   plugin_dispatch_values (&new_vl);
202
203   /* The last data source is a counter value, so we have to use a different
204    * type here. */
205   new_vl.values[0].derive = (derive_t) vl->values[3].counter;
206   sstrncpy (new_vl.type, "total_threads", sizeof (new_vl.type));
207   sstrncpy (new_vl.type_instance, "created",
208       sizeof (new_vl.type_instance));
209   plugin_dispatch_values (&new_vl);
210
211   /* Abort processing */
212   return (FC_TARGET_STOP);
213 } /* }}} int v5_mysql_threads */
214
215 /*
216  * ZFS ARC hit and miss counters
217  *
218  * 4.* uses the flawed "arc_counts" type. In 5.* this has been replaced by the
219  * more generic "cache_result" type.
220  */
221 static int v5_zfs_arc_counts (const data_set_t *ds, value_list_t *vl) /* {{{ */
222 {
223   value_list_t new_vl;
224   _Bool is_hits;
225
226   if (vl->values_len != 4)
227     return (FC_TARGET_STOP);
228
229   if (strcmp ("hits", vl->type_instance) == 0)
230     is_hits = 1;
231   else if (strcmp ("misses", vl->type_instance) == 0)
232     is_hits = 0;
233   else
234     return (FC_TARGET_STOP);
235
236   /* Copy everything: Time, interval, host, ... */
237   memcpy (&new_vl, vl, sizeof (new_vl));
238
239   /* Reset data we can't simply copy */
240   new_vl.values = &(value_t) { .gauge = NAN };
241   new_vl.values_len = 1;
242   new_vl.meta = NULL;
243
244   /* Change the type to "cache_result" */
245   sstrncpy (new_vl.type, "cache_result", sizeof (new_vl.type));
246
247   /* Dispatch new value lists instead of this one */
248   new_vl.values[0].derive = (derive_t) vl->values[0].counter;
249   ssnprintf (new_vl.type_instance, sizeof (new_vl.type_instance),
250       "demand_data-%s",
251       is_hits ? "hit" : "miss");
252   plugin_dispatch_values (&new_vl);
253
254   new_vl.values[0].derive = (derive_t) vl->values[1].counter;
255   ssnprintf (new_vl.type_instance, sizeof (new_vl.type_instance),
256       "demand_metadata-%s",
257       is_hits ? "hit" : "miss");
258   plugin_dispatch_values (&new_vl);
259
260   new_vl.values[0].derive = (derive_t) vl->values[2].counter;
261   ssnprintf (new_vl.type_instance, sizeof (new_vl.type_instance),
262       "prefetch_data-%s",
263       is_hits ? "hit" : "miss");
264   plugin_dispatch_values (&new_vl);
265
266   new_vl.values[0].derive = (derive_t) vl->values[3].counter;
267   ssnprintf (new_vl.type_instance, sizeof (new_vl.type_instance),
268       "prefetch_metadata-%s",
269       is_hits ? "hit" : "miss");
270   plugin_dispatch_values (&new_vl);
271
272   /* Abort processing */
273   return (FC_TARGET_STOP);
274 } /* }}} int v5_zfs_arc_counts */
275
276 /*
277  * ZFS ARC L2 bytes
278  *
279  * "arc_l2_bytes" -> "io_octets-L2".
280  */
281 static int v5_zfs_arc_l2_bytes (const data_set_t *ds, value_list_t *vl) /* {{{ */
282 {
283   value_list_t new_vl;
284
285   if (vl->values_len != 2)
286     return (FC_TARGET_STOP);
287
288   /* Copy everything: Time, interval, host, ... */
289   memcpy (&new_vl, vl, sizeof (new_vl));
290
291   /* Reset data we can't simply copy */
292   new_vl.meta = NULL;
293
294   /* Change the type/-instance to "io_octets-L2" */
295   sstrncpy (new_vl.type, "io_octets", sizeof (new_vl.type));
296   sstrncpy (new_vl.type_instance, "L2", sizeof (new_vl.type_instance));
297
298   /* Copy the actual values. */
299   value_t values[] = {
300     { .derive = (derive_t) vl->values[0].counter },
301     { .derive = (derive_t) vl->values[1].counter },
302   };
303   new_vl.values = values;
304   new_vl.values_len = STATIC_ARRAY_SIZE (values);
305
306   /* Dispatch new value lists instead of this one */
307   plugin_dispatch_values (&new_vl);
308
309   /* Abort processing */
310   return (FC_TARGET_STOP);
311 } /* }}} int v5_zfs_arc_l2_bytes */
312
313 /*
314  * ZFS ARC L2 cache size
315  *
316  * 4.* uses a separate type for this. 5.* uses the generic "cache_size" type
317  * instead.
318  */
319 static int v5_zfs_arc_l2_size (const data_set_t *ds, value_list_t *vl) /* {{{ */
320 {
321   value_list_t new_vl;
322
323   if (vl->values_len != 1)
324     return (FC_TARGET_STOP);
325
326   /* Copy everything: Time, interval, host, ... */
327   memcpy (&new_vl, vl, sizeof (new_vl));
328
329   /* Reset data we can't simply copy */
330   new_vl.values = &(value_t) { .gauge = NAN };
331   new_vl.values_len = 1;
332   new_vl.meta = NULL;
333
334   new_vl.values[0].gauge = (gauge_t) vl->values[0].gauge;
335
336   /* Change the type to "cache_size" */
337   sstrncpy (new_vl.type, "cache_size", sizeof (new_vl.type));
338
339   /* Adapt the type instance */
340   sstrncpy (new_vl.type_instance, "L2", sizeof (new_vl.type_instance));
341
342   /* Dispatch new value lists instead of this one */
343   plugin_dispatch_values (&new_vl);
344
345   /* Abort processing */
346   return (FC_TARGET_STOP);
347 } /* }}} int v5_zfs_arc_l2_size */
348
349 /*
350  * ZFS ARC ratio
351  *
352  * "arc_ratio-L1" -> "cache_ratio-arc"
353  * "arc_ratio-L2" -> "cache_ratio-L2"
354  */
355 static int v5_zfs_arc_ratio (const data_set_t *ds, value_list_t *vl) /* {{{ */
356 {
357   value_list_t new_vl;
358
359   if (vl->values_len != 1)
360     return (FC_TARGET_STOP);
361
362   /* Copy everything: Time, interval, host, ... */
363   memcpy (&new_vl, vl, sizeof (new_vl));
364
365   /* Reset data we can't simply copy */
366   new_vl.values = &(value_t) { .gauge = NAN };
367   new_vl.values_len = 1;
368   new_vl.meta = NULL;
369
370   new_vl.values[0].gauge = (gauge_t) vl->values[0].gauge;
371
372   /* Change the type to "cache_ratio" */
373   sstrncpy (new_vl.type, "cache_ratio", sizeof (new_vl.type));
374
375   /* Adapt the type instance */
376   if (strcmp ("L1", vl->type_instance) == 0)
377     sstrncpy (new_vl.type_instance, "arc", sizeof (new_vl.type_instance));
378
379   /* Dispatch new value lists instead of this one */
380   plugin_dispatch_values (&new_vl);
381
382   /* Abort processing */
383   return (FC_TARGET_STOP);
384 } /* }}} int v5_zfs_arc_ratio */
385
386 /*
387  * ZFS ARC size
388  *
389  * 4.* uses the "arc_size" type with four data sources. In 5.* this has been
390  * replaces with the "cache_size" type and static data has been removed.
391  */
392 static int v5_zfs_arc_size (const data_set_t *ds, value_list_t *vl) /* {{{ */
393 {
394   value_list_t new_vl;
395
396   if (vl->values_len != 4)
397     return (FC_TARGET_STOP);
398
399   /* Copy everything: Time, interval, host, ... */
400   memcpy (&new_vl, vl, sizeof (new_vl));
401
402   /* Reset data we can't simply copy */
403   new_vl.values = &(value_t) { .gauge = NAN };
404   new_vl.values_len = 1;
405   new_vl.meta = NULL;
406
407   /* Change the type to "cache_size" */
408   sstrncpy (new_vl.type, "cache_size", sizeof (new_vl.type));
409
410   /* Dispatch new value lists instead of this one */
411   new_vl.values[0].derive = (derive_t) vl->values[0].counter;
412   sstrncpy (new_vl.type_instance, "arc", sizeof (new_vl.type_instance));
413   plugin_dispatch_values (&new_vl);
414
415   /* Abort processing */
416   return (FC_TARGET_STOP);
417 } /* }}} int v5_zfs_arc_size */
418
419 static int v5_destroy (void **user_data) /* {{{ */
420 {
421   return (0);
422 } /* }}} int v5_destroy */
423
424 static int v5_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
425 {
426   *user_data = NULL;
427   return (0);
428 } /* }}} int v5_create */
429
430 static int v5_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
431     notification_meta_t __attribute__((unused)) **meta,
432     void __attribute__((unused)) **user_data)
433 {
434   if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
435     return (-EINVAL);
436
437   if (strcmp ("df", vl->type) == 0)
438     return (v5_df (ds, vl));
439   else if (strcmp ("interface", vl->plugin) == 0)
440     return (v5_interface (ds, vl));
441   else if (strcmp ("mysql_qcache", vl->type) == 0)
442     return (v5_mysql_qcache (ds, vl));
443   else if (strcmp ("mysql_threads", vl->type) == 0)
444     return (v5_mysql_threads (ds, vl));
445   else if (strcmp ("arc_counts", vl->type) == 0)
446     return (v5_zfs_arc_counts (ds, vl));
447   else if (strcmp ("arc_l2_bytes", vl->type) == 0)
448     return (v5_zfs_arc_l2_bytes (ds, vl));
449   else if (strcmp ("arc_l2_size", vl->type) == 0)
450     return (v5_zfs_arc_l2_size (ds, vl));
451   else if (strcmp ("arc_ratio", vl->type) == 0)
452     return (v5_zfs_arc_ratio (ds, vl));
453   else if (strcmp ("arc_size", vl->type) == 0)
454     return (v5_zfs_arc_size (ds, vl));
455
456   return (FC_TARGET_CONTINUE);
457 } /* }}} int v5_invoke */
458
459 void module_register (void)
460 {
461         target_proc_t tproc = { 0 };
462
463         tproc.create  = v5_create;
464         tproc.destroy = v5_destroy;
465         tproc.invoke  = v5_invoke;
466         fc_register_target ("v5upgrade", tproc);
467 } /* module_register */
468
469 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */
470