Code

Let the JSON formatter include a metric's data_names.
[sysdb.git] / src / plugins / backend / facter.cc
1 /*
2  * SysDB - src/plugins/backend/facter.cc
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
32 #include "sysdb.h"
33 #include "core/plugin.h"
34 #include "core/store.h"
35 #include "utils/error.h"
37 #include <facter/facts/collection.hpp>
38 #include <iostream>
39 #include <sstream>
41 static const char *hostname;
42 static sdb_time_t now;
44 static bool
45 fact_iter(std::string const &k, facter::facts::value const *v)
46 {
47         /* Don't ignore hidden values for now; they also provide the old,
48          * non-structured facts. */
50         std::stringstream ss;
51         v->write(ss, false);
52         std::string s = ss.str();
53         char *str = const_cast<char *>(s.c_str());
55         /* Ignore non-structured facts for now. */
56         if (str[0] == '{')
57                 return true;
59         sdb_data_t value = { SDB_TYPE_STRING, { .string = str } };
60         sdb_plugin_store_attribute(hostname, k.c_str(), &value, now);
61         return true;
62 } /* fact_iter */
64 /* SysDB interface */
65 extern "C" {
67         SDB_PLUGIN_MAGIC;
69         static int
70         facter_collect(sdb_object_t __attribute__((unused)) *user_data)
71         {
72                 facter::facts::collection facts;
74                 /* XXX: this may execute other programs; can we be sure that works
75                  * reasonably well in a multi-threaded program? */
76                 facts.add_default_facts();
77                 facts.add_external_facts();
79                 now = sdb_gettime();
80                 facter::facts::value const *v = facts["fqdn"];
81                 std::stringstream ss;
82                 v->write(ss, false);
83                 std::string s = ss.str();
84                 hostname = s.c_str();
86                 sdb_plugin_store_host(hostname, now);
87                 facts.each(fact_iter);
88                 sdb_log(SDB_LOG_DEBUG, "facter backend: Processed %zu facts "
89                                 "for host '%s'", facts.size(), hostname);
90                 return 0;
91         } /* main */
93         int
94         sdb_module_init(sdb_plugin_info_t *info)
95         {
96                 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
97                                 "backend retrieving local facter facts");
98                 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
99                                 "Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>");
100                 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
101                 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
102                 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
104                 sdb_plugin_register_collector("main", facter_collect,
105                                 /* interval */ NULL, /* user_data */ NULL);
106                 return 0;
107         } /* sdb_module_init */
109 } /* extern C */
111 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */