respondd.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. Copyright (c) 2016, Matthias Schiffer <mschiffer@universe-factory.net>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  12. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  13. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  14. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  15. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  16. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  18. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  19. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  20. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. #include <respondd.h>
  23. #include <json-c/json.h>
  24. #include <libgluonutil.h>
  25. #include <libplatforminfo.h>
  26. #include <uci.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <time.h>
  31. #include <sys/vfs.h>
  32. static struct json_object * gluon_version(void) {
  33. char *version = gluonutil_read_line("/lib/gluon/gluon-version");
  34. if (!version)
  35. return NULL;
  36. char full_version[6 + strlen(version) + 1];
  37. snprintf(full_version, sizeof(full_version), "gluon-%s", version);
  38. free(version);
  39. return json_object_new_string(full_version);
  40. }
  41. static struct json_object * get_site_code(void) {
  42. struct json_object *site = gluonutil_load_site_config();
  43. if (!site)
  44. return NULL;
  45. struct json_object *ret = NULL;
  46. json_object_object_get_ex(site, "site_code", &ret);
  47. if (ret)
  48. json_object_get(ret);
  49. json_object_put(site);
  50. return ret;
  51. }
  52. static struct json_object * get_domain_code(void) {
  53. return gluonutil_wrap_and_free_string(gluonutil_get_domain());
  54. }
  55. static struct json_object * get_hostname(void) {
  56. struct json_object *ret = NULL;
  57. struct uci_context *ctx = uci_alloc_context();
  58. if (!ctx)
  59. return NULL;
  60. ctx->flags &= ~UCI_FLAG_STRICT;
  61. char section[] = "system.@system[0]";
  62. struct uci_ptr ptr;
  63. if (uci_lookup_ptr(ctx, &ptr, section, true))
  64. goto error;
  65. struct uci_section *s = ptr.s;
  66. const char *hostname = uci_lookup_option_string(ctx, s, "pretty_hostname");
  67. if (!hostname)
  68. hostname = uci_lookup_option_string(ctx, s, "hostname");
  69. ret = gluonutil_wrap_string(hostname);
  70. error:
  71. uci_free_context(ctx);
  72. return ret;
  73. }
  74. static struct json_object * respondd_provider_nodeinfo(void) {
  75. struct json_object *ret = json_object_new_object();
  76. json_object_object_add(ret, "node_id", gluonutil_wrap_and_free_string(gluonutil_get_node_id()));
  77. json_object_object_add(ret, "hostname", get_hostname());
  78. struct json_object *hardware = json_object_new_object();
  79. const char *model = platforminfo_get_model();
  80. if (model)
  81. json_object_object_add(hardware, "model", json_object_new_string(model));
  82. json_object_object_add(hardware, "nproc", json_object_new_int(sysconf(_SC_NPROCESSORS_ONLN)));
  83. json_object_object_add(ret, "hardware", hardware);
  84. struct json_object *network = json_object_new_object();
  85. json_object_object_add(network, "mac", gluonutil_wrap_and_free_string(gluonutil_get_sysconfig("primary_mac")));
  86. json_object_object_add(ret, "network", network);
  87. struct json_object *software = json_object_new_object();
  88. struct json_object *software_firmware = json_object_new_object();
  89. json_object_object_add(software_firmware, "base", gluon_version());
  90. json_object_object_add(software_firmware, "release", gluonutil_wrap_and_free_string(gluonutil_read_line("/lib/gluon/release")));
  91. json_object_object_add(software, "firmware", software_firmware);
  92. json_object_object_add(ret, "software", software);
  93. struct json_object *system = json_object_new_object();
  94. json_object_object_add(system, "site_code", get_site_code());
  95. if (gluonutil_has_domains())
  96. json_object_object_add(system, "domain_code", get_domain_code());
  97. json_object_object_add(ret, "system", system);
  98. return ret;
  99. }
  100. static void add_uptime(struct json_object *obj) {
  101. FILE *f = fopen("/proc/uptime", "r");
  102. struct json_object* jso;
  103. if (!f)
  104. return;
  105. double uptime, idletime;
  106. if (fscanf(f, "%lf %lf", &uptime, &idletime) == 2) {
  107. jso = json_object_new_double(uptime);
  108. json_object_set_serializer(jso, json_object_double_to_json_string, "%.2f", NULL);
  109. json_object_object_add(obj, "uptime", jso);
  110. jso = json_object_new_double(idletime);
  111. json_object_set_serializer(jso, json_object_double_to_json_string, "%.2f", NULL);
  112. json_object_object_add(obj, "idletime", jso);
  113. }
  114. fclose(f);
  115. }
  116. static void add_loadavg(struct json_object *obj) {
  117. FILE *f = fopen("/proc/loadavg", "r");
  118. if (!f)
  119. return;
  120. double loadavg;
  121. unsigned proc_running, proc_total;
  122. if (fscanf(f, "%lf %*f %*f %u/%u", &loadavg, &proc_running, &proc_total) == 3) {
  123. struct json_object *jso = json_object_new_double(loadavg);
  124. json_object_set_serializer(jso, json_object_double_to_json_string, "%.2f", NULL);
  125. json_object_object_add(obj, "loadavg", jso);
  126. struct json_object *processes = json_object_new_object();
  127. json_object_object_add(processes, "running", json_object_new_int(proc_running));
  128. json_object_object_add(processes, "total", json_object_new_int(proc_total));
  129. json_object_object_add(obj, "processes", processes);
  130. }
  131. fclose(f);
  132. }
  133. static struct json_object * get_memory(void) {
  134. FILE *f = fopen("/proc/meminfo", "r");
  135. if (!f)
  136. return NULL;
  137. struct json_object *ret = json_object_new_object();
  138. char *line = NULL;
  139. size_t len = 0;
  140. while (getline(&line, &len, f) >= 0) {
  141. char label[32];
  142. unsigned value;
  143. if (sscanf(line, "%31[^:]: %u", label, &value) != 2)
  144. continue;
  145. if (!strcmp(label, "MemTotal"))
  146. json_object_object_add(ret, "total", json_object_new_int(value));
  147. else if (!strcmp(label, "MemFree"))
  148. json_object_object_add(ret, "free", json_object_new_int(value));
  149. else if (!strcmp(label, "Buffers"))
  150. json_object_object_add(ret, "buffers", json_object_new_int(value));
  151. else if (!strcmp(label, "Cached"))
  152. json_object_object_add(ret, "cached", json_object_new_int(value));
  153. }
  154. free(line);
  155. fclose(f);
  156. return ret;
  157. }
  158. static struct json_object * get_rootfs_usage(void) {
  159. struct statfs s;
  160. if (statfs("/", &s))
  161. return NULL;
  162. struct json_object *jso = json_object_new_double(1 - (double)s.f_bfree / s.f_blocks);
  163. json_object_set_serializer(jso, json_object_double_to_json_string, "%.4f", NULL);
  164. return jso;
  165. }
  166. static struct json_object * get_time(void) {
  167. struct timespec now;
  168. if (clock_gettime(CLOCK_REALTIME, &now) != 0)
  169. return NULL;
  170. return json_object_new_int64(now.tv_sec);
  171. }
  172. static struct json_object * respondd_provider_statistics(void) {
  173. struct json_object *ret = json_object_new_object();
  174. json_object_object_add(ret, "node_id", gluonutil_wrap_and_free_string(gluonutil_get_node_id()));
  175. json_object *time = get_time();
  176. if (time != NULL)
  177. json_object_object_add(ret, "time", time);
  178. json_object_object_add(ret, "rootfs_usage", get_rootfs_usage());
  179. json_object_object_add(ret, "memory", get_memory());
  180. add_uptime(ret);
  181. add_loadavg(ret);
  182. return ret;
  183. }
  184. static struct json_object * respondd_provider_neighbours(void) {
  185. struct json_object *ret = json_object_new_object();
  186. json_object_object_add(ret, "node_id", gluonutil_wrap_and_free_string(gluonutil_get_node_id()));
  187. return ret;
  188. }
  189. const struct respondd_provider_info respondd_providers[] = {
  190. {"nodeinfo", respondd_provider_nodeinfo},
  191. {"statistics", respondd_provider_statistics},
  192. {"neighbours", respondd_provider_neighbours},
  193. {}
  194. };