rsstats 0.0.1
Redis Enterprise Statistic collector
rptbdbs.c
Go to the documentation of this file.
1
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 */
32static void csv_addjsonfield(FILE* reportfile, const cJSON* json, char* fieldname) {
33 char* text = json2text(cJSON_GetObjectItemCaseSensitive(json, fieldname));
34 csv_addfield(reportfile,text);
35 free(text);
36}
37
38void report_bdbs_header(FILE* reportfile) {
39 fprintf(reportfile,
40 "cluster_host,uid,name,shards_count,replication,data_persistence,memory_size,used_memory,module_list\r\n"
41 );
42}
43
44static cJSON* report_querygetjson(const cluster_t* cluster, const char* endpoint) {
45 rsclustercon_t* rsclustercon;
46
47 if (NULL==(rsclustercon = cluster_new(cluster))) {
48 fprintf(stderr,"report_querygetjson cluster_new failed\n");
49 return NULL;
50 }
51 if (0!=cluster_open(rsclustercon)) {
52 fprintf(stderr,"report_querygetjson cluster_open failed\n");
53 cluster_del(rsclustercon);
54 return NULL;
55 }
56
57 cJSON* retval;
58 if (NULL==(retval = cluster_queryget(rsclustercon, endpoint))) {
59 const char *error_ptr = cJSON_GetErrorPtr();
60 if (error_ptr != NULL)
61 fprintf(stderr, "Error before: %s\n", error_ptr);
62 retval = cJSON_CreateObject();
63 }
64
65 cluster_close(rsclustercon);
66 cluster_del(rsclustercon);
67
68 return retval;
69}
70
71void report_bdbs(FILE* reportfile, const cluster_t* cluster) {
72 cJSON* bdbs_json;
73 cJSON* bdbsstats_json;
74
75 bdbs_json = report_querygetjson(cluster, "/v1/bdbs");
76 bdbsstats_json = report_querygetjson(cluster, "/v1/bdbs/stats/last");
77
78 const cJSON* bdb_json;
79 cJSON_ArrayForEach(bdb_json, bdbs_json) {
80 char* uid=json2text(cJSON_GetObjectItemCaseSensitive(bdb_json, "uid"));
81 cJSON* stats_json = cJSON_GetObjectItemCaseSensitive(bdbsstats_json, uid);
82 free(uid);
83 csv_addfield(reportfile,cluster->host);
84 csv_addjsonfield(reportfile, bdb_json, "uid");
85 csv_addjsonfield(reportfile, bdb_json, "name");
86 csv_addjsonfield(reportfile, bdb_json, "shards_count");
87 csv_addjsonfield(reportfile, bdb_json, "replication");
88 csv_addjsonfield(reportfile, bdb_json, "data_persistence");
89 csv_addjsonfield(reportfile, bdb_json, "memory_size");
90 csv_addjsonfield(reportfile, stats_json, "used_memory");
91
92 /* Iterate the module list and build the text list */
93 char* modlst;
94 if (NULL==(modlst=strdup(""))) {
95 perror("rptbdbs repport_bdbs module_list");
96 exit(EXIT_FAILURE);
97 }
98
99 const cJSON* module_json;
100 cJSON_ArrayForEach(module_json, cJSON_GetObjectItemCaseSensitive(bdb_json, "module_list")) {
101 char* modulename = cJSON_GetObjectItemCaseSensitive(module_json, "module_name")->valuestring;
102 char* newmodlst;
103 if (NULL==(newmodlst = (char*)realloc(modlst,strlen(modlst)+strlen(modulename?modulename:"")+2+1))) {
104 perror("rptbdbs repport_bdbs module_list");
105 free(modlst);
106 return;
107 } else
108 modlst = newmodlst;
109 strcat(modlst,(modulename?modulename:""));
110 strcat(modlst,", ");
111 }
112
113 /* Remove last comma if applicable */
114 if (strlen(modlst)>2)
115 modlst[strlen(modlst)-2]=0;
116 csv_addfield(reportfile,modlst);
117 free(modlst);
118
119 csv_addline(reportfile);
120 }
121 cJSON_Delete(bdbs_json);
122 cJSON_Delete(bdbsstats_json);
123}
124/* vim: set tw=80: */
#define cJSON_ArrayForEach(element, array)
Definition: cJSON.h:294
void cluster_close(rsclustercon_t *rsclustercon)
Definition: clustercon.c:206
void cluster_del(rsclustercon_t *rsclustercon)
Definition: clustercon.c:214
rsclustercon_t * cluster_new(const cluster_t *cluster)
Definition: clustercon.c:38
int cluster_open(rsclustercon_t *rsclustercon)
Definition: clustercon.c:75
cJSON * cluster_queryget(const rsclustercon_t *rsclustercon, const char *endpoint)
Definition: clustercon.c:139
<+DETAILED+>
void csv_addfield(FILE *reportfile, const char *value)
Definition: csv.c:37
void csv_addline(FILE *reportfile)
Definition: csv.c:32
<+DETAILED+>
char * json2text(cJSON *value_json)
Convert a cJSON object in a C String.
Definition: json.c:31
Wrapper around cJSON library with helpers.
void report_bdbs_header(FILE *reportfile)
Definition: rptbdbs.c:38
void report_bdbs(FILE *reportfile, const cluster_t *cluster)
Definition: rptbdbs.c:71
<+DETAILED+>
#define NULL
Definition: rsstats-opts.c:64
Definition: cJSON.h:103
char * host
Definition: cluster.h:26