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#pragma GCC diagnostic push /* save the actual diag context */
54#pragma GCC diagnostic ignored "-Wdate-time" /* locally disable warnings because of non reproductible build triggered by pbuild */
55 printf("Compiled %s at %s\n",__DATE__, __TIME__);
56#pragma GCC diagnostic pop /* restore previous diag context */
57 printf("Copyright 2024 François Cerbelle\n");
58 printf("Report bugs to %s\n\n", BYEL PACKAGE_BUGREPORT RESET);
59 /* AutoGen option parsing and consumming */
60 {
61 int arg_ct = optionProcess( &rsstatsOptions, argc, argv );
62 argc -= arg_ct;
63 argv += arg_ct;
64 }
65
66 printf("==> Read input file (cluster definitions): %s\n",OPT_ARG(INPUT));
67 /* Open the cluster definitions files */
68 configfile = fopen(OPT_ARG(INPUT), "r");
69 if (!configfile) {
70 perror("main open(configfile)");
71 exit(EXIT_FAILURE);
72 }
73
74 unsigned int lineno = 0;
75 while (fgets(configline, sizeof(configline), configfile) != NULL) {
76 lineno++;
77
78 /* Allocate a cluster record */
79 if(NULL==(cluster=malloc(sizeof(struct cluster_s)))) {
80 perror("main malloc(cluster)");
81 exit(EXIT_FAILURE);
82 }
83
84 /* Extract cluster information from the configline */
85 if (NULL==(cluster->host = csvtok(configline))) {
86 fprintf(stderr,"Bad configline (%s:%u)\n", OPT_ARG(INPUT), lineno);
87 free(cluster);
88 continue;
89 }
90 if (NULL==(cluster->user = csvtok(NULL))) {
91 fprintf(stderr,"Bad configline (%s:%u)\n", OPT_ARG(INPUT), lineno);
92 free(cluster->host);
93 free(cluster);
94 continue;
95 }
96 if (NULL==(cluster->pass = csvtok(NULL))) {
97 fprintf(stderr,"Bad configline (%s:%u)\n", OPT_ARG(INPUT), lineno);
98 free(cluster->host);
99 free(cluster->user);
100 free(cluster);
101 continue;
102 }
103 if (NULL==(cluster->insecure = csvtok(NULL))) {
104 fprintf(stderr,"Bad configline (%s:%u)\n", OPT_ARG(INPUT), lineno);
105 free(cluster->host);
106 free(cluster->user);
107 free(cluster->pass);
108 free(cluster);
109 continue;
110 }
111 if (NULL==(cluster->cacert = csvtok(NULL))) {
112 fprintf(stderr,"Bad configline (%s:%u)\n", OPT_ARG(INPUT), lineno);
113 free(cluster->host);
114 free(cluster->user);
115 free(cluster->pass);
116 free(cluster->insecure);
117 free(cluster);
118 continue;
119 }
120
121 /* Check if the configline should be processed */
122 if ((NULL!=strstr(OPT_ARG(CLUSTERS),"all"))
123 ||(NULL!=strstr(OPT_ARG(CLUSTERS),cluster->host)))
124 cluster->enabled=1;
125 else
126 cluster->enabled=0;
127 if (clusterlist_find(cluster->host))
128 fprintf(stderr,"Double cluster definition (%s @ %s:%u)\n",
129 cluster->host, OPT_ARG(INPUT), lineno);
130 clusterlist_add(cluster);
131 }
132 fclose(configfile);
133
134 printf("==> Clusters to query : ");
135 cluster=clusterlist_first();
136 while(cluster) {
137 if (cluster->enabled==1)
138 printf("%s ",cluster->host);
139 cluster = clusterlist_next();
140 }
141 printf("\n");
142
143 printf("==> Open output file (report): %s\n",OPT_ARG(OUTPUT));
144
145 /* Open the output file */
146 reportfile = fopen(OPT_ARG(OUTPUT),"w");
147 if (!reportfile) {
148 perror("Opening output file");
149 exit(EXIT_FAILURE);
150 }
151
152 /* Execute sample report ? all or sample specified */
154 printf("==> Running reports (sample)\n");
155 fprintf(reportfile,"\nsample:\n");
156 report_sample_header(reportfile);
157 /* Execute report against enabled clusters */
158 cluster=clusterlist_first();
159 while(cluster) {
160 /* Check if the configline should be processed */
161 if (cluster->enabled==1) {
162 printf("==> Running report (sample) on cluster %s\n",cluster->host);
163 report_sample(reportfile, cluster);
164 }
165 cluster = clusterlist_next();
166 }
167 }
168
169 /* Execute cluster report ? all or cluster specified */
171 printf("==> Running reports (cluster)\n");
172 fprintf(reportfile,"\nclusters:\n");
173 report_cluster_header(reportfile);
174 /* Execute report against enabled clusters */
175 cluster=clusterlist_first();
176 while(cluster) {
177 /* Check if the configline should be processed */
178 if (cluster->enabled==1) {
179 printf("==> Running report (clusters) on cluster %s\n",cluster->host);
180 report_cluster(reportfile, cluster);
181 }
182 cluster = clusterlist_next();
183 }
184 }
185
186 /* Execute bdbs report ? all or bdbs specified */
188 printf("==> Running reports (bdbs)\n");
189 fprintf(reportfile,"\nbdbs:\n");
190 report_bdbs_header(reportfile);
191 /* Execute report against enabled clusters */
192 cluster=clusterlist_first();
193 while(cluster) {
194 /* Check if the configline should be processed */
195 if (cluster->enabled==1) {
196 printf("==> Running report (bdbs) on cluster %s\n",cluster->host);
197 report_bdbs(reportfile, cluster);
198 }
199 cluster = clusterlist_next();
200 }
201 }
202
203 fclose(reportfile);
204
205#ifdef _WIN32
206 system("PAUSE"); /* For windows console window to wait. */
207#endif
208
209 return EXIT_SUCCESS;
210}
211/* 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