rsstats 0.0.1
Redis Enterprise Statistic collector
main.c
Go to the documentation of this file.
1
19#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23#include "ansi-color-codes.h"
24#include "revision.h"
25#include "csv.h" /* CSV manipulation functions */
26#include "clusterlst.h" /* Cluster list structure */
27#ifndef NOCPPCHECK
28#include "rsstats-opts.h" /* Libopts generated options */
29#endif
30#include "rptsample.h"
31#include "rptbdbs.h"
32#include "rptcluster.h"
33
34#include <stdlib.h>
35#include <string.h>
36
37int main (int argc, char** argv, char** env) {
38 FILE *reportfile;
39 FILE *configfile;
40 char configline[1024];
41 cluster_t* cluster;
42
43 (void)env; /* Avoid unused warning/error */
44 printf(BCYN PACKAGE_NAME " " PACKAGE_VERSION RESET "\n");
45#ifdef REVISION
46 printf("Revision " BBLU REVISION RESET);
47#endif
48#ifdef BBID
49 printf(" Build #" BBID);
50#endif
51 printf("\n");
52
53#ifndef NDEBUG
54 printf("Debug build\n");
55#else
56 printf("Release build\n");
57#endif
58
59#pragma GCC diagnostic push /* save the actual diag context */
60#pragma GCC diagnostic ignored "-Wdate-time" /* locally disable warnings because of non reproductible build triggered by pbuild */
61 printf("Compiled %s at %s\n",__DATE__, __TIME__);
62#pragma GCC diagnostic pop /* restore previous diag context */
63 printf("Copyright 2024 François Cerbelle\n");
64 printf("Report bugs to %s\n\n", BYEL PACKAGE_BUGREPORT RESET);
65 /* AutoGen option parsing and consumming */
66 {
67 int arg_ct = optionProcess( &rsstatsOptions, argc, argv );
68 argc -= arg_ct;
69 argv += arg_ct;
70 }
71
72 printf("==> Read input file (cluster definitions): %s\n",OPT_ARG(INPUT));
73 /* Open the cluster definitions files */
74 configfile = fopen(OPT_ARG(INPUT), "r");
75 if (!configfile) {
76 perror("main open(configfile)");
77 exit(EXIT_FAILURE);
78 }
79
80 unsigned int lineno = 0;
81 while (fgets(configline, sizeof(configline), configfile) != NULL) {
82 lineno++;
83
84 /* Allocate a cluster record */
85 if(NULL==(cluster=malloc(sizeof(struct cluster_s)))) {
86 perror("main malloc(cluster)");
87 exit(EXIT_FAILURE);
88 }
89
90 /* Extract cluster information from the configline */
91 if (NULL==(cluster->host = csvtok(configline))) {
92 fprintf(stderr,"Bad configline (%s:%u)\n", OPT_ARG(INPUT), lineno);
93 free(cluster);
94 continue;
95 }
96 if (NULL==(cluster->user = csvtok(NULL))) {
97 fprintf(stderr,"Bad configline (%s:%u)\n", OPT_ARG(INPUT), lineno);
98 free(cluster->host);
99 free(cluster);
100 continue;
101 }
102 if (NULL==(cluster->pass = csvtok(NULL))) {
103 fprintf(stderr,"Bad configline (%s:%u)\n", OPT_ARG(INPUT), lineno);
104 free(cluster->host);
105 free(cluster->user);
106 free(cluster);
107 continue;
108 }
109 if (NULL==(cluster->insecure = csvtok(NULL))) {
110 fprintf(stderr,"Bad configline (%s:%u)\n", OPT_ARG(INPUT), lineno);
111 free(cluster->host);
112 free(cluster->user);
113 free(cluster->pass);
114 free(cluster);
115 continue;
116 }
117 if (NULL==(cluster->cacert = csvtok(NULL))) {
118 fprintf(stderr,"Bad configline (%s:%u)\n", OPT_ARG(INPUT), lineno);
119 free(cluster->host);
120 free(cluster->user);
121 free(cluster->pass);
122 free(cluster->insecure);
123 free(cluster);
124 continue;
125 }
126
127 /* Check if the configline should be processed */
128 if ((NULL!=strstr(OPT_ARG(CLUSTERS),"all"))
129 ||(NULL!=strstr(OPT_ARG(CLUSTERS),cluster->host)))
130 cluster->enabled=1;
131 else
132 cluster->enabled=0;
133 if (clusterlist_find(cluster->host))
134 fprintf(stderr,"Double cluster definition (%s @ %s:%u)\n",
135 cluster->host, OPT_ARG(INPUT), lineno);
136 clusterlist_add(cluster);
137 }
138 fclose(configfile);
139
140 printf("==> Clusters to query : ");
141 cluster=clusterlist_first();
142 while(cluster) {
143 if (cluster->enabled==1)
144 printf("%s ",cluster->host);
145 cluster = clusterlist_next();
146 }
147 printf("\n");
148
149 printf("==> Open output file (report): %s\n",OPT_ARG(OUTPUT));
150
151 /* Open the output file */
152 reportfile = fopen(OPT_ARG(OUTPUT),"w");
153 if (!reportfile) {
154 perror("Opening output file");
155 exit(EXIT_FAILURE);
156 }
157
158 /* Execute sample report ? all or sample specified */
160 printf("==> Running reports (sample)\n");
161 fprintf(reportfile,"\nsample:\n");
162 report_sample_header(reportfile);
163 /* Execute report against enabled clusters */
164 cluster=clusterlist_first();
165 while(cluster) {
166 /* Check if the configline should be processed */
167 if (cluster->enabled==1) {
168 printf("==> Running report (sample) on cluster %s\n",cluster->host);
169 report_sample(reportfile, cluster);
170 }
171 cluster = clusterlist_next();
172 }
173 }
174
175 /* Execute cluster report ? all or cluster specified */
177 printf("==> Running reports (cluster)\n");
178 fprintf(reportfile,"\nclusters:\n");
179 report_cluster_header(reportfile);
180 /* Execute report against enabled clusters */
181 cluster=clusterlist_first();
182 while(cluster) {
183 /* Check if the configline should be processed */
184 if (cluster->enabled==1) {
185 printf("==> Running report (clusters) on cluster %s\n",cluster->host);
186 report_cluster(reportfile, cluster);
187 }
188 cluster = clusterlist_next();
189 }
190 }
191
192 /* Execute bdbs report ? all or bdbs specified */
194 printf("==> Running reports (bdbs)\n");
195 fprintf(reportfile,"\nbdbs:\n");
196 report_bdbs_header(reportfile);
197 /* Execute report against enabled clusters */
198 cluster=clusterlist_first();
199 while(cluster) {
200 /* Check if the configline should be processed */
201 if (cluster->enabled==1) {
202 printf("==> Running report (bdbs) on cluster %s\n",cluster->host);
203 report_bdbs(reportfile, cluster);
204 }
205 cluster = clusterlist_next();
206 }
207 }
208
209 fclose(reportfile);
210
211#ifdef _WIN32
212 system("PAUSE"); /* For windows console window to wait. */
213#endif
214
215 return EXIT_SUCCESS;
216}
217/* vim: set tw=80: */
#define BYEL
#define BBLU
#define RESET
#define BCYN
cluster_t *(* clusterlist_first)()
Definition: clusterlst.c:91
void(* clusterlist_add)(cluster_t *cluster)
Definition: clusterlst.c:60
cluster_t *(* clusterlist_next)()
Definition: clusterlst.c:102
cluster_t *(* clusterlist_find)(const char *host)
Definition: clusterlst.c:80
Self initialized cluster records list (non thread-safe)
char * csvtok(char *source)
Definition: csv.c:50
<+DETAILED+>
int main(int argc, char **argv, char **env)
Definition: main.c:37
#define REVISION
Definition: revision.h:3
void report_bdbs_header(FILE *reportfile)
Definition: rptbdbs.c:38
void report_bdbs(FILE *reportfile, const cluster_t *cluster)
Definition: rptbdbs.c:71
<+DETAILED+>
void report_cluster(FILE *reportfile, const cluster_t *cluster)
Definition: rptcluster.c:79
void report_cluster_header(FILE *reportfile)
Definition: rptcluster.c:38
<+DETAILED+>
void report_sample(FILE *reportfile, const cluster_t *cluster)
Definition: rptsample.c:42
void report_sample_header(FILE *reportfile)
Definition: rptsample.c:36
Basic report without connection to test output format.
#define NULL
Definition: rsstats-opts.c:64
tOptions rsstatsOptions
The option definitions for rsstats.
Definition: rsstats-opts.c:533
#define REPORTS_BDBS
Definition: rsstats-opts.h:150
#define REPORTS_CLUSTER
Definition: rsstats-opts.h:151
#define OPT_ARG(n)
The string argument to an option.
Definition: rsstats-opts.h:103
#define REPORTS_SAMPLE
Definition: rsstats-opts.h:149
#define OPT_VALUE_REPORTS
Definition: rsstats-opts.h:153
char * host
Definition: cluster.h:26
char * insecure
Definition: cluster.h:29
unsigned short int enabled
Definition: cluster.h:25
char * pass
Definition: cluster.h:28
char * user
Definition: cluster.h:27
char * cacert
Definition: cluster.h:30