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

Generated by: LCOV version 1.16