Line data Source code
1 : /**
2 : * @file rptbdbs.c
3 : * @brief
4 : *
5 : * <+DETAILED+>
6 : *
7 : * @author François Cerbelle (Fanfan), francois@cerbelle.net
8 : *
9 : * @internal
10 : * Created: 28/10/2024
11 : * Revision: none
12 : * Last modified: 2024-11-06 19:21
13 : * Compiler: gcc
14 : * Organization: Cerbelle.net
15 : * Copyright: Copyright (c) 2024, François Cerbelle
16 : *
17 : * This source code is released for free distribution under the terms of the
18 : * GNU General Public License as published by the Free Software Foundation.
19 : */
20 :
21 : #ifdef HAVE_CONFIG_H
22 : #include "config.h"
23 : #endif
24 :
25 : #include "rptbdbs.h"
26 : #include "clustercon.h"
27 : #include "json.h"
28 : #include "csv.h"
29 : #include <string.h>
30 :
31 : /* To refactor in a report common file */
32 0 : static void csv_addjsonfield(FILE* reportfile, const cJSON* json, char* fieldname) {
33 0 : char* text = json2text(cJSON_GetObjectItemCaseSensitive(json, fieldname));
34 0 : csv_addfield(reportfile,text);
35 0 : free(text);
36 0 : }
37 :
38 4 : void report_bdbs_header(FILE* reportfile) {
39 4 : fprintf(reportfile,
40 : "cluster_host,uid,name,shards_count,replication,data_persistence,memory_size,used_memory,module_list\r\n"
41 : );
42 4 : }
43 :
44 0 : static cJSON* report_querygetjson(const cluster_t* cluster, const char* endpoint) {
45 : rsclustercon_t* rsclustercon;
46 :
47 0 : if (NULL==(rsclustercon = cluster_new(cluster))) {
48 0 : fprintf(stderr,"report_querygetjson cluster_new failed\n");
49 0 : return NULL;
50 : }
51 0 : if (0!=cluster_open(rsclustercon)) {
52 0 : fprintf(stderr,"report_querygetjson cluster_open failed\n");
53 0 : cluster_del(rsclustercon);
54 0 : return NULL;
55 : }
56 :
57 : cJSON* retval;
58 0 : if (NULL==(retval = cluster_queryget(rsclustercon, endpoint))) {
59 0 : const char *error_ptr = cJSON_GetErrorPtr();
60 0 : if (error_ptr != NULL)
61 0 : fprintf(stderr, "Error before: %s\n", error_ptr);
62 0 : retval = cJSON_CreateObject();
63 : }
64 :
65 0 : cluster_close(rsclustercon);
66 0 : cluster_del(rsclustercon);
67 :
68 0 : return retval;
69 : }
70 :
71 0 : void report_bdbs(FILE* reportfile, const cluster_t* cluster) {
72 : cJSON* bdbs_json;
73 : cJSON* bdbsstats_json;
74 :
75 0 : bdbs_json = report_querygetjson(cluster, "/v1/bdbs");
76 0 : bdbsstats_json = report_querygetjson(cluster, "/v1/bdbs/stats/last");
77 :
78 : const cJSON* bdb_json;
79 0 : cJSON_ArrayForEach(bdb_json, bdbs_json) {
80 0 : char* uid=json2text(cJSON_GetObjectItemCaseSensitive(bdb_json, "uid"));
81 0 : cJSON* stats_json = cJSON_GetObjectItemCaseSensitive(bdbsstats_json, uid);
82 0 : free(uid);
83 0 : csv_addfield(reportfile,cluster->host);
84 0 : csv_addjsonfield(reportfile, bdb_json, "uid");
85 0 : csv_addjsonfield(reportfile, bdb_json, "name");
86 0 : csv_addjsonfield(reportfile, bdb_json, "shards_count");
87 0 : csv_addjsonfield(reportfile, bdb_json, "replication");
88 0 : csv_addjsonfield(reportfile, bdb_json, "data_persistence");
89 0 : csv_addjsonfield(reportfile, bdb_json, "memory_size");
90 0 : csv_addjsonfield(reportfile, stats_json, "used_memory");
91 :
92 : /* Iterate the module list and build the text list */
93 : char* modlst;
94 0 : if (NULL==(modlst=strdup(""))) {
95 0 : perror("rptbdbs repport_bdbs module_list");
96 0 : exit(EXIT_FAILURE);
97 : }
98 :
99 : const cJSON* module_json;
100 0 : cJSON_ArrayForEach(module_json, cJSON_GetObjectItemCaseSensitive(bdb_json, "module_list")) {
101 0 : char* modulename = cJSON_GetObjectItemCaseSensitive(module_json, "module_name")->valuestring;
102 : char* newmodlst;
103 0 : if (NULL==(newmodlst = (char*)realloc(modlst,strlen(modlst)+strlen(modulename?modulename:"")+2+1))) {
104 0 : perror("rptbdbs repport_bdbs module_list");
105 0 : free(modlst);
106 0 : return;
107 : } else
108 0 : modlst = newmodlst;
109 0 : strcat(modlst,(modulename?modulename:""));
110 0 : strcat(modlst,", ");
111 : }
112 :
113 : /* Remove last comma if applicable */
114 0 : if (strlen(modlst)>2)
115 0 : modlst[strlen(modlst)-2]=0;
116 0 : csv_addfield(reportfile,modlst);
117 0 : free(modlst);
118 :
119 0 : csv_addline(reportfile);
120 : }
121 0 : cJSON_Delete(bdbs_json);
122 0 : cJSON_Delete(bdbsstats_json);
123 : }
124 : /* vim: set tw=80: */
|