Line data Source code
1 : /**
2 : * @file rptcluster.c
3 : * @brief
4 : *
5 : * <+DETAILED+>
6 : *
7 : * @author François Cerbelle (Fanfan), francois@cerbelle.net
8 : *
9 : * @internal
10 : * Created: 29/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 "rptcluster.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_cluster_header(FILE* reportfile) {
39 4 : fprintf(reportfile,
40 : "cluster_host,name,rack_aware,created_time,free_memory,"
41 : "available_memory,available_memory_no_overbooking,available_flash,"
42 : "available_flash_no_overbooking,ephemeral_storage_avail,"
43 : "ephemeral_storage_free,persistent_storage_avail,"
44 : "persistent_storage_free,provisional_flash,"
45 : "provisional_flash_no_overbooking,provisional_memory,"
46 : "provisional_memory_no_overbooking,activation_date,expiration_date,"
47 : "cluster_name,owner,features,shards_limit,ram_shards_in_use,"
48 : "ram_shards_limit,flash_shards_in_use,flash_shards_limit,expired\r\n"
49 : );
50 4 : }
51 :
52 0 : static cJSON* report_querygetjson(const cluster_t* cluster, const char* endpoint) {
53 : rsclustercon_t* rsclustercon;
54 :
55 0 : if (NULL==(rsclustercon = cluster_new(cluster))) {
56 0 : fprintf(stderr,"report_querygetjson cluster_new failed\n");
57 0 : return NULL;
58 : }
59 0 : if (0!=cluster_open(rsclustercon)) {
60 0 : fprintf(stderr,"report_querygetjson cluster_open failed\n");
61 0 : cluster_del(rsclustercon);
62 0 : return NULL;
63 : }
64 :
65 : cJSON* retval;
66 0 : if (NULL==(retval = cluster_queryget(rsclustercon, endpoint))) {
67 0 : const char *error_ptr = cJSON_GetErrorPtr();
68 0 : if (error_ptr != NULL)
69 0 : fprintf(stderr, "Error before: %s\n", error_ptr);
70 0 : retval = cJSON_CreateObject();
71 : }
72 :
73 0 : cluster_close(rsclustercon);
74 0 : cluster_del(rsclustercon);
75 :
76 0 : return retval;
77 : }
78 :
79 0 : void report_cluster(FILE* reportfile, const cluster_t* cluster) {
80 : cJSON* cluster_json;
81 : cJSON* clusterstats_json;
82 : cJSON* license_json;
83 :
84 0 : cluster_json = report_querygetjson(cluster, "/v1/cluster");
85 0 : clusterstats_json = report_querygetjson(cluster, "/v1/cluster/stats/last");
86 0 : license_json = report_querygetjson(cluster, "/v1/license");
87 :
88 0 : csv_addfield(reportfile,cluster->host);
89 0 : csv_addjsonfield(reportfile, cluster_json, "name");
90 0 : csv_addjsonfield(reportfile, cluster_json, "rack_aware");
91 0 : csv_addjsonfield(reportfile, cluster_json, "created_time");
92 0 : csv_addjsonfield(reportfile, clusterstats_json, "free_memory");
93 0 : csv_addjsonfield(reportfile, clusterstats_json, "available_memory");
94 0 : csv_addjsonfield(reportfile, clusterstats_json, "available_memory_no_overbooking");
95 0 : csv_addjsonfield(reportfile, clusterstats_json, "available_flash");
96 0 : csv_addjsonfield(reportfile, clusterstats_json, "available_flash_no_overbooking");
97 0 : csv_addjsonfield(reportfile, clusterstats_json, "ephemeral_storage_avail");
98 0 : csv_addjsonfield(reportfile, clusterstats_json, "ephemeral_storage_free");
99 0 : csv_addjsonfield(reportfile, clusterstats_json, "persistent_storage_avail");
100 0 : csv_addjsonfield(reportfile, clusterstats_json, "persistent_storage_free");
101 0 : csv_addjsonfield(reportfile, clusterstats_json, "provisional_flash");
102 0 : csv_addjsonfield(reportfile, clusterstats_json, "provisional_flash_no_overbooking");
103 0 : csv_addjsonfield(reportfile, clusterstats_json, "provisional_memory");
104 0 : csv_addjsonfield(reportfile, clusterstats_json, "provisional_memory_no_overbooking");
105 0 : csv_addjsonfield(reportfile, license_json, "activation_date");
106 0 : csv_addjsonfield(reportfile, license_json, "expiration_date");
107 0 : csv_addjsonfield(reportfile, license_json, "cluster_name");
108 0 : csv_addjsonfield(reportfile, license_json, "owner");
109 0 : csv_addjsonfield(reportfile, license_json, "features");
110 0 : csv_addjsonfield(reportfile, license_json, "shards_limit");
111 0 : csv_addjsonfield(reportfile, license_json, "ram_shards_in_use");
112 0 : csv_addjsonfield(reportfile, license_json, "ram_shards_limit");
113 0 : csv_addjsonfield(reportfile, license_json, "flash_shards_in_use");
114 0 : csv_addjsonfield(reportfile, license_json, "flash_shards_limit");
115 0 : csv_addjsonfield(reportfile, license_json, "expired");
116 0 : csv_addline(reportfile);
117 :
118 0 : cJSON_Delete(cluster_json);
119 0 : cJSON_Delete(clusterstats_json);
120 0 : cJSON_Delete(license_json);
121 0 : }
122 :
123 :
124 : /*
125 :
126 :
127 :
128 : clusterdef.host
129 : nodes.uid
130 : nodes.addr
131 : nodes.external_addr
132 : nodes.cores
133 : nodes.total_memory
134 : nodes.ephemeral_storage_size
135 : nodes.persistent_storage_size
136 : nodes.os_version
137 : nodes.rack_id
138 : nodes.max_listeners
139 : nodes.max_redis_servers
140 : nodes.ram_shard_count
141 : nodes.flash_shard_count
142 : nodes.shard_count
143 : nodes.software_version
144 : nodes.uptime
145 : nodes.accept_servers
146 : nodes.status
147 : nodesstatus.cores
148 : nodesstatus.free_provisional_ram
149 : nodesstatus.free_ram
150 : nodesstatus.hostname
151 : nodesstatus.node_overbooking_depth
152 : nodesstatus.node_status
153 : nodesstatus.role
154 : nodesstatus.software_version
155 : nodesstatus.total_memory
156 : nodesstatus.total_provisional_ram
157 : nodesstatslast.available_memory
158 : nodesstatslast.available_memory_no_overbooking
159 : nodesstatslast.ephemeral_storage_avail
160 : nodesstatslast.ephemeral_storage_free
161 : nodesstatslast.free_memory
162 : nodesstatslast.persistent_storage_avail
163 : nodesstatslast.persistent_storage_free
164 : nodesstatslast.provisional_memory
165 : nodesstatslast.provisional_memory_no_overbooking
166 : */
167 : /* vim: set tw=80: */
|