1 /*
2 * SysDB - t/integration/mock_plugin.c
3 * Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #if HAVE_CONFIG_H
29 # include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "sysdb.h"
33 #include "core/plugin.h"
34 #include "core/store.h"
35 #include "utils/error.h"
37 #include "liboconfig/utils.h"
39 #include <stdlib.h>
40 #include <string.h>
42 #define MAGIC_DATA (void *)0x4711
44 SDB_PLUGIN_MAGIC;
46 static const char *hostnames[] = {
47 "some.host.name",
48 "other.host.name",
49 "host1.example.com",
50 "host2.example.com",
51 "localhost",
52 };
54 static struct {
55 const char *hostname;
56 const char *metric;
57 } metrics[] = {
58 { "some.host.name", "foo/bar/qux" },
59 { "some.host.name", "foo/bar/baz" },
60 { "some.host.name", "foo2/bar/qux" },
61 { "some.host.name", "foo2/bar/baz" },
62 { "other.host.name", "foo/bar/qux" },
63 { "other.host.name", "foo/bar/baz" },
64 { "other.host.name", "foo2/bar/qux" },
65 { "other.host.name", "foo2/bar/baz" },
66 };
68 static struct {
69 const char *hostname;
70 const char *service;
71 } services[] = {
72 { "some.host.name", "mock service" },
73 { "some.host.name", "other service" },
74 { "some.host.name", "database" },
75 { "host1.example.com", "mock service" },
76 { "host1.example.com", "example service one" },
77 { "host1.example.com", "example service two" },
78 { "host1.example.com", "example service three" },
79 { "host2.example.com", "mock service" },
80 { "host2.example.com", "example service one" },
81 { "host2.example.com", "example service two" },
82 { "host2.example.com", "example service three" },
83 { "localhost", "sysdbd" },
84 };
86 static struct {
87 const char *hostname;
88 const char *name;
89 const char *value;
90 } attributes[] = {
91 { "other.host.name", "attribute", "value" },
92 { "other.host.name", "architecture", "varch" },
93 { "other.host.name", "processor0", "Vendor TYPE4711 CPU MAGIC" },
94 { "other.host.name", "processor1", "Vendor TYPE4711 CPU MAGIC" },
95 { "other.host.name", "processor2", "Vendor TYPE4711 CPU MAGIC" },
96 { "other.host.name", "processor3", "Vendor TYPE4711 CPU MAGIC" },
97 { "host1.example.com", "other attribute", "special value" },
98 { "host1.example.com", "architecture", "x42" },
99 { "host1.example.com", "timezone", "UTC" },
100 { "host2.example.com", "other attribute", "special value" },
101 { "host2.example.com", "architecture", "x42" },
102 { "host2.example.com", "timezone", "UTC" },
103 { "localhost", "attr1", "value1" },
104 { "localhost", "attr2", "value2" },
105 { "localhost", "attr3", "value3" },
106 };
108 /*
109 * plugin API
110 */
112 static int
113 mock_init(sdb_object_t *user_data)
114 {
115 if (SDB_OBJ_WRAPPER(user_data)->data != MAGIC_DATA) {
116 sdb_log(SDB_LOG_ERR, "mock::plugin: Invalid user data %p "
117 "passed to init", SDB_OBJ_WRAPPER(user_data)->data);
118 exit(1);
119 }
120 return 0;
121 } /* mock_init */
123 static int
124 mock_shutdown(sdb_object_t *user_data)
125 {
126 if (SDB_OBJ_WRAPPER(user_data)->data != MAGIC_DATA) {
127 sdb_log(SDB_LOG_ERR, "mock::plugin: Invalid user data %p "
128 "passed to shutdown", SDB_OBJ_WRAPPER(user_data)->data);
129 exit(1);
130 }
131 return 0;
132 } /* mock_shutdown */
134 static int
135 mock_collect(sdb_object_t *user_data)
136 {
137 size_t i;
138 int check;
140 if (SDB_OBJ_WRAPPER(user_data)->data != MAGIC_DATA) {
141 sdb_log(SDB_LOG_ERR, "mock::plugin: Invalid user data %p "
142 "passed to collect", SDB_OBJ_WRAPPER(user_data)->data);
143 exit(1);
144 }
146 for (i = 0; i < SDB_STATIC_ARRAY_LEN(hostnames); ++i) {
147 if ((check = sdb_store_host(hostnames[i], sdb_gettime()))) {
148 sdb_log(SDB_LOG_ERR, "mock::plugin: Failed to store host: "
149 "status %d", check);
150 exit(1);
151 }
152 }
153 for (i = 0; i < SDB_STATIC_ARRAY_LEN(metrics); ++i) {
154 if ((check = sdb_store_metric(metrics[i].hostname,
155 metrics[i].metric, sdb_gettime()))) {
156 sdb_log(SDB_LOG_ERR, "mock::plugin: Failed to store metric: "
157 "status %d", check);
158 exit(1);
159 }
160 }
161 for (i = 0; i < SDB_STATIC_ARRAY_LEN(services); ++i) {
162 if ((check = sdb_store_service(services[i].hostname,
163 services[i].service, sdb_gettime()))) {
164 sdb_log(SDB_LOG_ERR, "mock::plugin: Failed to store service: "
165 "status %d", check);
166 exit(1);
167 }
168 }
169 for (i = 0; i < SDB_STATIC_ARRAY_LEN(attributes); ++i) {
170 sdb_data_t datum = { SDB_TYPE_STRING, { .string = NULL } };
171 datum.data.string = strdup(attributes[i].value);
173 if ((check = sdb_store_attribute(attributes[i].hostname,
174 attributes[i].name, &datum, sdb_gettime()))) {
175 sdb_log(SDB_LOG_ERR, "mock::plugin: Failed to store attribute: "
176 "status %d", check);
177 exit(1);
178 }
180 free(datum.data.string);
181 }
182 return 0;
183 } /* mock_collect */
185 static int
186 mock_config(oconfig_item_t *ci)
187 {
188 sdb_object_t *user_data;
190 int i;
192 if (! ci) /* deconfiguration */
193 return 0;
195 for (i = 0; i < ci->children_num; ++i) {
196 oconfig_item_t *child = ci->children + i;
198 sdb_log(SDB_LOG_WARNING, "mock::plugin: Ignoring unknown config "
199 "option '%s'", child->key);
200 }
202 user_data = sdb_object_create_wrapper("mock_data", MAGIC_DATA, NULL);
203 if (! user_data) {
204 sdb_log(SDB_LOG_ERR, "mock::plugin: Failed to allocate user data");
205 exit(1);
206 }
208 sdb_plugin_register_init("main", mock_init, user_data);
209 sdb_plugin_register_shutdown("main", mock_shutdown, user_data);
210 sdb_plugin_register_collector("main", mock_collect,
211 /* interval = */ NULL, user_data);
213 sdb_object_deref(user_data);
214 return 0;
215 } /* mock_config */
217 int
218 sdb_module_init(sdb_plugin_info_t *info)
219 {
220 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC, "a mock plugin");
221 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
222 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
223 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
224 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
225 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
227 sdb_plugin_register_config(mock_config);
228 return 0;
229 } /* sdb_module_init */
231 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */