Skip to content

Commit b5b87ae

Browse files
committed
update collectd contrib
1 parent 8176e66 commit b5b87ae

File tree

5 files changed

+171
-54
lines changed

5 files changed

+171
-54
lines changed

contrib/collectd/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
CC=gcc
2-
CFLAGS+= -I/usr/include -Wall -DHAVE_CONFIG_H
2+
CFLAGS+= -I/usr/include -Wall -DHAVE_CONFIG_H -I. -I/usr/include/collectd -I/usr/include/collectd/core
33
LDFLAGS+= -L/usr/lib -lstatgrab -shared -fPIC
44
PLUGIN_INSTALL_DIR = /usr/lib/collectd
55

66
build: istatd ipcu
77

88
istatd:
9+
./statgrab_detect.sh
910
$(CC) plugins/istatd.c $(CFLAGS) $(LDFLAGS) -o plugins/istatd.so
1011

1112
ipcu:

contrib/collectd/plugins/icpu.c

+9-35
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,10 @@
1-
/**
2-
* Copyright (C) 2011-2014 IMVU, Inc
3-
* Copyright (C) 2005-2010 Florian octo Forster
4-
* Copyright (C) 2008 Oleg King
5-
* Copyright (C) 2009 Simon Kuhnle
6-
* Copyright (C) 2009 Manuel Sanmartin
7-
*
8-
* This program is free software; you can redistribute it and/or modify it
9-
* under the terms of the GNU General Public License as published by the
10-
* Free Software Foundation; only version 2 of the License is applicable.
11-
*
12-
* This program is distributed in the hope that it will be useful, but
13-
* WITHOUT ANY WARRANTY; without even the implied warranty of
14-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15-
* General Public License for more details.
16-
*
17-
* You should have received a copy of the GNU General Public License along
18-
* with this program; if not, write to the Free Software Foundation, Inc.,
19-
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20-
*
21-
* ICPU Plugin Authors:
22-
* IMVU, Inc
23-
* Original CPU Plugin Authors:
24-
* Florian octo Forster <octo at verplant.org>
25-
* Oleg King <king2 at kaluga.ru>
26-
* Simon Kuhnle <simon at blarzwurst.de>
27-
* Manuel Sanmartin
28-
**/
29-
30-
/* count cpu stats in percentages. mostly lifted from collectd's src/cpu.c plugin */
31-
32-
#include <collectd/collectd.h>
33-
#include <collectd/common.h>
34-
#include <collectd/plugin.h>
1+
/* mostly lifted from collectd's cpu.c */
2+
#include <collectd.h>
3+
#include <common.h>
4+
#include <plugin.h>
355

366
#include <statgrab.h>
7+
#include "config.h"
378

389
#define ICPU_PLUGIN_NAME "icpu"
3910

@@ -63,8 +34,11 @@ static int icpu_init() {
6334

6435
static int icpu_read() {
6536
sg_cpu_percents *cp;
66-
37+
#if defined(LIBSTATGRAB_API_0_90)
38+
cp = sg_get_cpu_percents(NULL);
39+
#else
6740
cp = sg_get_cpu_percents();
41+
#endif
6842
if (cp == NULL) {
6943
ERROR("icpu plugin: sg_get_cpu_percents failed.");
7044
return -1;

contrib/collectd/plugins/istatd.c

+46-10
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,19 @@
2727
* This plugin writes collectd stats to istatd. It started life as the collectd
2828
* CSV plugin.
2929
*/
30-
#include <collectd/collectd.h>
31-
#include <collectd/plugin.h>
32-
#include <collectd/common.h>
33-
#include <collectd/utils_cache.h>
34-
#include <collectd/utils_parse_option.h>
30+
#include <collectd.h>
31+
#include <plugin.h>
32+
#include <common.h>
33+
#include <utils_cache.h>
34+
#include <utils_parse_option.h>
3535

3636
#include <stdio.h>
3737
#include <sys/types.h>
3838
#include <sys/socket.h>
3939
#include <unistd.h>
4040
#include <netinet/in.h>
4141
#include <arpa/inet.h>
42+
#include <stdbool.h>
4243

4344
#define ISTATD_PLUGIN_DEBUG 0
4445

@@ -48,10 +49,12 @@
4849
static const char *config_keys[] =
4950
{
5051
"Port",
52+
"Suffix",
5153
};
5254
static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
5355

5456
static int istatd_agent_port = 0;
57+
static bool add_suffixes = true;
5558
static char *counter_suffix = "";
5659
static char *istatd_categories = "/etc/istatd.categories";
5760

@@ -281,12 +284,16 @@ static char *get_my_hostname(char *buffer, int buffer_len) {
281284
return buffer;
282285
}
283286

284-
static char *get_counter_suffix(char *buffer, int buffer_len, const char* fname, const char* hostname) {
287+
static char *get_counter_suffix(char *buffer, int buffer_len, const char* fname, const char* hostname, const bool should_add_suffixes) {
285288
const char *validchars = "ABCDEFGHIJKLMNOPQRSTUVWXZY"
286289
"abcdefghijklmnopqrstuvwxzy"
287290
"0123456789"
288291
"-_.";
289292
memset(buffer,'\0',buffer_len);
293+
if (!should_add_suffixes)
294+
{
295+
return buffer;
296+
}
290297

291298
FILE *f = fopen(fname, "rt");
292299
if (f != NULL) {
@@ -342,15 +349,43 @@ static int istatd_config (const char *key, const char *value)
342349
{
343350
INFO("processing port value %s", value);
344351
istatd_agent_port = atoi(value);
352+
return (0);
353+
}
354+
else if (strcasecmp ("suffix", key) == 0)
355+
{
356+
INFO("processing suffix value %s", value);
357+
if ( IS_TRUE ( value ) )
358+
{
359+
add_suffixes = true;
360+
}
361+
else
362+
{
363+
add_suffixes = false;
364+
}
365+
return (0);
366+
}
367+
return (-1);
368+
} /* int istatd_config */
369+
370+
static int should_skip_recording(const value_list_t *vl)
371+
{
372+
if ((strcmp(vl->plugin, "disk") == 0) && strncmp("md", vl->plugin_instance, 2) != 0 && isdigit(vl->plugin_instance[strlen(vl->plugin_instance)-1])) {
373+
// we don't needs disk.sda1 or disk.sdb2, etc.
374+
return (1);
345375
}
346376
else
347377
{
348-
return (-1);
378+
return (0);
349379
}
380+
}
381+
382+
383+
static int istatd_init (void)
384+
{
350385
char *hostname = get_my_hostname((char *)malloc(128), 128);
351-
counter_suffix = get_counter_suffix((char*)malloc(2048), 2048, istatd_categories, hostname);
386+
counter_suffix = get_counter_suffix((char*)malloc(2048), 2048, istatd_categories, hostname, add_suffixes);
352387
return (0);
353-
} /* int istatd_config */
388+
} /* int istatd_init */
354389

355390
#if ISTATD_PLUGIN_DEBUG
356391
static void log_incoming_data(const data_set_t *ds, const value_list_t *vl)
@@ -380,7 +415,7 @@ static int istatd_write (const data_set_t *ds, const value_list_t *vl,
380415
log_incoming_data(ds, vl);
381416
#endif
382417

383-
if ((strcmp(vl->plugin, "disk") == 0) && isdigit(vl->plugin_instance[strlen(vl->plugin_instance)-1])) {
418+
if (should_skip_recording(vl)) {
384419
// we don't needs disk.sda1 or disk.sdb2, etc.
385420
return (0);
386421
}
@@ -396,5 +431,6 @@ void module_register (void)
396431
INFO ("istatd plugin: register");
397432
plugin_register_config ("istatd", istatd_config,
398433
config_keys, config_keys_num);
434+
plugin_register_init ("istatd", istatd_init);
399435
plugin_register_write ("istatd", istatd_write, /* user_data = */ NULL);
400436
} /* void module_register */

contrib/collectd/plugins/test_istatd.c

+93-8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ gauge_t *uc_get_rate(const data_set_t *dset, const value_list_t *vl) {
3030
if (dset->ds[i].type != DS_TYPE_GAUGE) {
3131
INFO ("uc_get_rate: %d, calc fake rate", i);
3232
rates[i] = (gauge_t)(val/10.0);
33+
INFO ("uc_get_rate: %f, calc fake rate was ", rates[i]);
3334
}
3435
}
3536

@@ -51,6 +52,9 @@ int plugin_register_config (const char *name,
5152
int (*callback) (const char *key, const char *val),
5253
const char **keys, int keys_num) { return 0;}
5354

55+
int plugin_register_init (const char *name,
56+
int (*callback) (void)) { return 0;}
57+
5458
int plugin_register_write (const char *name,
5559
plugin_write_cb callback, user_data_t *user_data) { return 0;}
5660

@@ -63,7 +67,19 @@ static void c_assert_str_equal(const char *fname, int line, const char *s1, cons
6367
}
6468
}
6569

70+
static void c_assert_equal(const char *fname, int line, const int s1, const int s2) {
71+
if (s1 != s2) {
72+
fprintf(stderr, "ERROR %s: %d - \"%d\" does not equal \"%d\"\n", fname, line, s1, s2);
73+
error_count++;
74+
}
75+
}
76+
77+
6678
#define assert_str_equal(s1,s2) c_assert_str_equal(__FILE__, __LINE__, (s1), (s2))
79+
#define assert_equal(v1,v2) c_assert_equal(__FILE__, __LINE__, (v1), (v2))
80+
#define assert_true(b1) c_assert_equal(__FILE__, __LINE__, (b1), (1))
81+
#define assert_false(b1) c_assert_equal(__FILE__, __LINE__, (b1), (0))
82+
6783

6884
void test_make_istatd_metric_name() {
6985
char buffer[1024];
@@ -151,8 +167,8 @@ void make_fake_data(data_set_t *dset, value_list_t *vl,
151167

152168
vl->values = values;
153169
vl->values_len = num_values;
154-
vl->time = (cdtime_t)0;
155-
vl->interval = (cdtime_t)10;
170+
vl->time = TIME_T_TO_CDTIME_T(0);
171+
vl->interval = TIME_T_TO_CDTIME_T(10);
156172
strcpy(vl->host, "localhost");
157173
strcpy(vl->plugin, plugin);
158174
strcpy(vl->plugin_instance, plugin_instance);
@@ -177,7 +193,7 @@ void test_map_to_istatd() {
177193
make_fake_data(&ds, &vl, DS_TYPE_COUNTER, "cpu", "", "idle", "", values, 1);
178194

179195
map_to_istatd(buffer, sizeof(buffer), &ds, &vl);
180-
assert_str_equal("cpu.idle^buildbot.linu-15-14^host.linu-15-14 0.900000\n", buffer);
196+
assert_str_equal("*cpu.idle^buildbot.linu-15-14^host.linu-15-14 9\n", buffer);
181197
destroy_fake_data(&ds, &vl);
182198

183199
// test memcache_ps_count
@@ -216,8 +232,8 @@ void test_map_to_istatd() {
216232
values[1].derive = 5.0;
217233
make_fake_data(&ds, &vl, DS_TYPE_DERIVE, "interface", "eth0", "if_packets", "", values, 2);
218234
map_to_istatd(buffer, sizeof(buffer), &ds, &vl);
219-
assert_str_equal("interface.eth0.if_packets.in^buildbot.linu-15-14^host.linu-15-14 0.100000\n"
220-
"interface.eth0.if_packets.out^buildbot.linu-15-14^host.linu-15-14 0.500000\n"
235+
assert_str_equal("*interface.eth0.if_packets.fake_data_source^buildbot.linu-15-14^host.linu-15-14 1\n"
236+
"*interface.eth0.if_packets.fake_data_source^buildbot.linu-15-14^host.linu-15-14 5\n"
221237
, buffer);
222238
destroy_fake_data(&ds, &vl);
223239

@@ -256,21 +272,90 @@ void test_get_counter_suffix() {
256272
char *suffix;
257273

258274
// test non-existant file
259-
suffix = get_counter_suffix(buffer, sizeof(buffer), "test_data/does_not_exist", "fakehost");
275+
suffix = get_counter_suffix(buffer, sizeof(buffer), "test_data/does_not_exist", "fakehost", true);
260276
assert_str_equal("^host.fakehost", suffix);
261277

262278
// test empty file
263-
suffix = get_counter_suffix(buffer, sizeof(buffer), "test_data/empty_istatd.categories", "fakehost");
279+
suffix = get_counter_suffix(buffer, sizeof(buffer), "test_data/empty_istatd.categories", "fakehost", true);
264280
assert_str_equal("^host.fakehost", suffix);
265281

266282
// test sample file
267-
suffix = get_counter_suffix(buffer, sizeof(buffer), "test_data/istatd.categories", "fakehost");
283+
suffix = get_counter_suffix(buffer, sizeof(buffer), "test_data/istatd.categories", "fakehost", true);
268284
assert_str_equal("^class^trailing_ws^leading_ws^surrounded_ws^clean_me_up__por_-favor5^host.fakehost", suffix);
285+
286+
//Test no suffix addition
287+
288+
// test non-existant file
289+
suffix = get_counter_suffix(buffer, sizeof(buffer), "test_data/does_not_exist", "fakehost", false);
290+
assert_str_equal("", suffix);
291+
292+
// test empty file
293+
suffix = get_counter_suffix(buffer, sizeof(buffer), "test_data/empty_istatd.categories", "fakehost", false);
294+
assert_str_equal("", suffix);
295+
296+
// test sample file
297+
suffix = get_counter_suffix(buffer, sizeof(buffer), "test_data/istatd.categories", "fakehost", false);
298+
assert_str_equal("", suffix);
299+
}
300+
301+
void test_should_skip_recording() {
302+
value_list_t vl;
303+
int res = 0;
304+
305+
memset(&vl, 0, sizeof(vl));
306+
strcpy(vl.type, "type");
307+
strcpy(vl.plugin, "disk");
308+
strcpy(vl.plugin_instance, "sda1");
309+
strcpy(vl.type_instance, "more");
310+
res = should_skip_recording(&vl);
311+
assert_true(res);
312+
313+
memset(&vl, 0, sizeof(vl));
314+
strcpy(vl.type, "type");
315+
strcpy(vl.plugin, "disk");
316+
strcpy(vl.plugin_instance, "sdb");
317+
strcpy(vl.type_instance, "more");
318+
res = should_skip_recording(&vl);
319+
assert_false(res);
320+
321+
memset(&vl, 0, sizeof(vl));
322+
strcpy(vl.type, "type");
323+
strcpy(vl.plugin, "disk");
324+
strcpy(vl.plugin_instance, "md0");
325+
strcpy(vl.type_instance, "more");
326+
res = should_skip_recording(&vl);
327+
assert_false(res);
328+
329+
memset(&vl, 0, sizeof(vl));
330+
strcpy(vl.type, "type");
331+
strcpy(vl.plugin, "disk");
332+
strcpy(vl.plugin_instance, "sdmd0");
333+
strcpy(vl.type_instance, "more");
334+
res = should_skip_recording(&vl);
335+
assert_true(res);
336+
337+
memset(&vl, 0, sizeof(vl));
338+
strcpy(vl.type, "type");
339+
strcpy(vl.plugin, "cpu");
340+
strcpy(vl.plugin_instance, "idle");
341+
strcpy(vl.type_instance, "more");
342+
res = should_skip_recording(&vl);
343+
assert_false(res);
344+
345+
memset(&vl, 0, sizeof(vl));
346+
strcpy(vl.type, "type");
347+
strcpy(vl.plugin, "others");
348+
strcpy(vl.plugin_instance, "idle");
349+
strcpy(vl.type_instance, "more");
350+
res = should_skip_recording(&vl);
351+
assert_false(res);
269352
}
270353

354+
271355
int main(int argc, char** argv) {
272356
test_make_istatd_metric_name();
273357
test_map_to_istatd();
274358
test_get_counter_suffix();
359+
test_should_skip_recording();
275360
return(0);
276361
}

contrib/collectd/statgrab_detect.sh

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
set -e
3+
set -u
4+
5+
echo "checking for libstatgrab interface version"
6+
echo "/* config.h generated by configure script */" >> config.h
7+
echo "" >> config.h
8+
9+
mkdir -p tmp
10+
fn=tmp/sg.c
11+
echo "#include <statgrab.h>" > "$fn"
12+
echo "int main() { sg_get_cpu_percents(); }" >> "$fn"
13+
if ! gcc -o tmp/sg.o -c "$fn" > /dev/null 2>&1; then
14+
echo "libstatgrab 0.17 interface doesn't work, assuming 0.90"
15+
echo "#define LIBSTATGRAB_API_0_90" >> config.h
16+
else
17+
echo "found libstatgrab 0.17 interface"
18+
echo "#define LIBSTATGRAB_API_0_17" >> config.h
19+
fi
20+
rm -rf tmp
21+

0 commit comments

Comments
 (0)