LCOV - code coverage report
Current view: top level - src - main.c (source / functions) Hit Total Coverage
Test: mkernel.info Lines: 74 105 70.5 %
Date: 2024-11-23 15:59:17 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /**
       2             :  *    @file  main.c
       3             :  *   @brief
       4             :  *
       5             :  *  @author  François Cerbelle (Fanfan), francois@cerbelle.net
       6             :  *
       7             :  *  @internal
       8             :  *       Created:  25/10/2024
       9             :  *      Revision:  none
      10             :  * Last modified:  2024-11-14 18:12
      11             :  *      Compiler:  gcc
      12             :  *  Organization:  Cerbelle.net
      13             :  *     Copyright:  Copyright (c) 2024, François Cerbelle
      14             :  *
      15             :  *  This source code is released for free distribution under the terms of the
      16             :  *  GNU General Public License as published by the Free Software Foundation.
      17             :  */
      18             : 
      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             : 
      37          26 : int 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          26 :     printf(BCYN PACKAGE_NAME " " PACKAGE_VERSION RESET "\n");
      45             : #ifdef REVISION
      46          26 :     printf("Revision " BBLU REVISION RESET);
      47             : #endif
      48             : #ifdef BBID
      49          26 :     printf(" Build #" BBID);
      50             : #endif
      51          26 :     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          26 :     printf("Compiled %s at %s\n",__DATE__, __TIME__);
      56             : #pragma GCC diagnostic pop                              /* restore previous diag context */
      57          26 :     printf("Copyright 2024 François Cerbelle\n");
      58          26 :     printf("Report bugs to %s\n\n", BYEL PACKAGE_BUGREPORT RESET);
      59             :     /* AutoGen option parsing and consumming */
      60             :     {
      61          26 :         int arg_ct = optionProcess( &rsstatsOptions, argc, argv );
      62          20 :         argc -= arg_ct;
      63          20 :         argv += arg_ct;
      64             :     }
      65             : 
      66          20 :     printf("==> Read input file (cluster definitions): %s\n",OPT_ARG(INPUT));
      67             :     /* Open the cluster definitions files */
      68          20 :     configfile = fopen(OPT_ARG(INPUT), "r");
      69          20 :     if (!configfile) {
      70           2 :         perror("main open(configfile)");
      71           2 :         exit(EXIT_FAILURE);
      72             :     }
      73             : 
      74          18 :     unsigned int lineno = 0;
      75          54 :     while (fgets(configline, sizeof(configline), configfile) != NULL) {
      76          36 :         lineno++;
      77             : 
      78             :         /* Allocate a cluster record */
      79          36 :         if(NULL==(cluster=malloc(sizeof(struct cluster_s)))) {
      80           0 :             perror("main malloc(cluster)");
      81           0 :             exit(EXIT_FAILURE);
      82             :         }
      83             : 
      84             :         /* Extract cluster information from the configline */
      85          36 :         if (NULL==(cluster->host = csvtok(configline))) {
      86           0 :             fprintf(stderr,"Bad configline (%s:%u)\n", OPT_ARG(INPUT), lineno);
      87           0 :             free(cluster);
      88           0 :             continue;
      89             :         }
      90          36 :         if (NULL==(cluster->user = csvtok(NULL))) {
      91           0 :             fprintf(stderr,"Bad configline (%s:%u)\n", OPT_ARG(INPUT), lineno);
      92           0 :             free(cluster->host);
      93           0 :             free(cluster);
      94           0 :             continue;
      95             :         }
      96          36 :         if (NULL==(cluster->pass = csvtok(NULL))) {
      97           0 :             fprintf(stderr,"Bad configline (%s:%u)\n", OPT_ARG(INPUT), lineno);
      98           0 :             free(cluster->host);
      99           0 :             free(cluster->user);
     100           0 :             free(cluster);
     101           0 :             continue;
     102             :         }
     103          36 :         if (NULL==(cluster->insecure = csvtok(NULL))) {
     104           0 :             fprintf(stderr,"Bad configline (%s:%u)\n", OPT_ARG(INPUT), lineno);
     105           0 :             free(cluster->host);
     106           0 :             free(cluster->user);
     107           0 :             free(cluster->pass);
     108           0 :             free(cluster);
     109           0 :             continue;
     110             :         }
     111          36 :         if (NULL==(cluster->cacert = csvtok(NULL))) {
     112           0 :             fprintf(stderr,"Bad configline (%s:%u)\n", OPT_ARG(INPUT), lineno);
     113           0 :             free(cluster->host);
     114           0 :             free(cluster->user);
     115           0 :             free(cluster->pass);
     116           0 :             free(cluster->insecure);
     117           0 :             free(cluster);
     118           0 :             continue;
     119             :         }
     120             : 
     121             :         /* Check if the configline should be processed */
     122          36 :         if ((NULL!=strstr(OPT_ARG(CLUSTERS),"all"))
     123          16 :                 ||(NULL!=strstr(OPT_ARG(CLUSTERS),cluster->host)))
     124          26 :             cluster->enabled=1;
     125             :         else
     126          10 :             cluster->enabled=0;
     127          36 :         if (clusterlist_find(cluster->host))
     128          10 :             fprintf(stderr,"Double cluster definition (%s @ %s:%u)\n",
     129          10 :                     cluster->host, OPT_ARG(INPUT), lineno);
     130          36 :         clusterlist_add(cluster);
     131             :     }
     132          18 :     fclose(configfile);
     133             : 
     134          18 :     printf("==> Clusters to query : ");
     135          18 :     cluster=clusterlist_first();
     136          54 :     while(cluster) {
     137          36 :         if (cluster->enabled==1)
     138          26 :             printf("%s ",cluster->host);
     139          36 :         cluster = clusterlist_next();
     140             :     }
     141          18 :     printf("\n");
     142             : 
     143          18 :     printf("==> Open output file (report): %s\n",OPT_ARG(OUTPUT));
     144             : 
     145             :     /* Open the output file */
     146          18 :     reportfile = fopen(OPT_ARG(OUTPUT),"w");
     147          18 :     if (!reportfile) {
     148           2 :         perror("Opening output file");
     149           2 :         exit(EXIT_FAILURE);
     150             :     }
     151             : 
     152             :     /* Execute sample report ? all or sample specified */
     153          16 :     if (OPT_VALUE_REPORTS & REPORTS_SAMPLE) {
     154          12 :         printf("==> Running reports (sample)\n");
     155          12 :         fprintf(reportfile,"\nsample:\n");
     156          12 :         report_sample_header(reportfile);
     157             :         /* Execute report against enabled clusters */
     158          12 :         cluster=clusterlist_first();
     159          36 :         while(cluster) {
     160             :             /* Check if the configline should be processed */
     161          24 :             if (cluster->enabled==1) {
     162          22 :                 printf("==> Running report (sample) on cluster %s\n",cluster->host);
     163          22 :                 report_sample(reportfile, cluster);
     164             :             }
     165          24 :             cluster = clusterlist_next();
     166             :         }
     167             :     }
     168             : 
     169             :     /* Execute cluster report ? all or cluster specified */
     170          16 :     if (OPT_VALUE_REPORTS & REPORTS_CLUSTER) {
     171           4 :         printf("==> Running reports (cluster)\n");
     172           4 :         fprintf(reportfile,"\nclusters:\n");
     173           4 :         report_cluster_header(reportfile);
     174             :         /* Execute report against enabled clusters */
     175           4 :         cluster=clusterlist_first();
     176          12 :         while(cluster) {
     177             :             /* Check if the configline should be processed */
     178           8 :             if (cluster->enabled==1) {
     179           0 :                 printf("==> Running report (clusters) on cluster %s\n",cluster->host);
     180           0 :                 report_cluster(reportfile, cluster);
     181             :             }
     182           8 :             cluster = clusterlist_next();
     183             :         }
     184             :     }
     185             : 
     186             :     /* Execute bdbs report ? all or bdbs specified */
     187          16 :     if (OPT_VALUE_REPORTS & REPORTS_BDBS) {
     188           4 :         printf("==> Running reports (bdbs)\n");
     189           4 :         fprintf(reportfile,"\nbdbs:\n");
     190           4 :         report_bdbs_header(reportfile);
     191             :         /* Execute report against enabled clusters */
     192           4 :         cluster=clusterlist_first();
     193          12 :         while(cluster) {
     194             :             /* Check if the configline should be processed */
     195           8 :             if (cluster->enabled==1) {
     196           0 :                 printf("==> Running report (bdbs) on cluster %s\n",cluster->host);
     197           0 :                 report_bdbs(reportfile, cluster);
     198             :             }
     199           8 :             cluster = clusterlist_next();
     200             :         }
     201             :     }
     202             : 
     203          16 :     fclose(reportfile);
     204             : 
     205             : #ifdef _WIN32
     206             :     system("PAUSE");  /* For windows console window to wait. */
     207             : #endif
     208             : 
     209          16 :     return EXIT_SUCCESS;
     210             : }
     211             : /* vim: set tw=80: */

Generated by: LCOV version 1.16