LCOV - code coverage report
Current view: top level - progs - mstat.cxx (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 357 0
Test Date: 2025-11-11 10:26:08 Functions: 0.0 % 3 0

            Line data    Source code
       1              : /********************************************************************\
       2              :   
       3              :   Name:         mstat.c
       4              :   Created by:   Pierre-Andre Amaudruz
       5              :   
       6              :   Contents:     Display/log some pertinent information of the ODB
       7              :   
       8              :   $Id$
       9              : 
      10              : \********************************************************************/
      11              : 
      12              : #undef NDEBUG // midas required assert() to be always enabled
      13              : 
      14              : #include <assert.h> // assert()
      15              : #include <fcntl.h>
      16              : #include "midas.h"
      17              : #include "msystem.h"
      18              : #include "mstrlcpy.h"
      19              : #include <sstream>
      20              : 
      21              : #define MAX_LINE        80
      22              : #define LINE_LENGTH     80
      23              : 
      24              : #if defined( OS_WINNT )
      25              : #define ESC_FLAG 0
      26              : #else
      27              : #define ESC_FLAG 1
      28              : #endif
      29              : 
      30              : INT rn;
      31              : 
      32              : /* Global variables 
      33              :  *  (required since no way to pass these inside callbacks) */
      34              : INT numprocs;
      35              : DWORD checktime, lastchecktime, delta_time;
      36              : INT nlocal;
      37              : HNDLE hKeynlocal, hKeychktime;
      38              : BOOL active_flag, esc_flag;
      39              : INT loop, cur_max_line;
      40              : std::string xststr[MAX_LINE];
      41              : 
      42              : /*------------------------------------------------------------------*/
      43            0 : INT open_log_midstat(INT file_mode, INT runn, char *svpath)
      44              : {
      45              :    char srun[32];
      46              :    INT fHandle;
      47              : 
      48            0 :    if (file_mode == 1) {        /* append run number */
      49            0 :       strcat(svpath, ".");
      50            0 :       sprintf(srun, "Run%4.4i", runn);
      51            0 :       strncat(svpath, srun, 256);
      52            0 :       printf("output with run file:%s-\n", svpath);
      53              :    }
      54              :    /* open device */
      55              : #ifdef OS_UNIX
      56            0 :    if ((fHandle = open(svpath, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1)
      57              : #endif
      58              : #ifdef OS_OSF1
      59              :       if ((fHandle = open(svpath, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1)
      60              : #endif
      61              : #ifdef OS_VXWORKS
      62              :          if ((fHandle = open(svpath, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1)
      63              : #endif
      64              : #ifdef OS_WINNT
      65              :             if ((fHandle =
      66              :                  _open(svpath, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IWRITE)) == -1)
      67              : #endif
      68              :             {
      69            0 :                printf("File %s cannot be created\n", svpath);
      70              :             }
      71            0 :    return (fHandle);
      72              : }
      73              : 
      74              : /*------------------------------------------------------------------*/
      75            0 : void compose_status(HNDLE hDB, HNDLE hKey)
      76              : {
      77              :    BOOL atleastone_active;
      78              :    INT i, size;
      79              :    char str[80], path[256];
      80              :    KEY key;
      81              :    HNDLE hSubkey;
      82              :    char strtmp[256];
      83              : 
      84              :    /* Clear string page */
      85            0 :    for (int jj = 0; jj < MAX_LINE; jj++) {
      86            0 :       xststr[jj].clear();
      87              :    }
      88              : 
      89            0 :    int j = 0;
      90              : 
      91              : /* --------------------- Run info -------------------------------- */
      92              :    {
      93              : 
      94              :       INT rs, rt;
      95              :       char cs[80], stt[80], spt[80], ex[80], rev[256], rev1[256];
      96              :       DWORD tb, tsb;
      97              : 
      98            0 :       size = sizeof(rs);
      99            0 :       db_get_value(hDB, 0, "/Runinfo/State", &rs, &size, TID_INT, TRUE);
     100            0 :       if (rs == STATE_RUNNING)
     101            0 :          strcpy(cs, "Running");
     102            0 :       if (rs == STATE_PAUSED)
     103            0 :          strcpy(cs, "Paused ");
     104            0 :       if (rs == STATE_STOPPED)
     105            0 :          strcpy(cs, "Stopped");
     106            0 :       size = sizeof(rn);
     107            0 :       db_get_value(hDB, 0, "/Runinfo/run number", &rn, &size, TID_INT, TRUE);
     108            0 :       size = sizeof(stt);
     109            0 :       db_get_value(hDB, 0, "/Runinfo/start time", stt, &size, TID_STRING, TRUE);
     110            0 :       size = sizeof(spt);
     111            0 :       db_get_value(hDB, 0, "/Runinfo/stop time", spt, &size, TID_STRING, TRUE);
     112            0 :       size = sizeof(tb);
     113            0 :       db_get_value(hDB, 0, "/runinfo/Start Time binary", &tb, &size, TID_DWORD, TRUE);
     114            0 :       size = sizeof(tsb);
     115            0 :       db_get_value(hDB, 0, "/runinfo/Stop Time binary", &tsb, &size, TID_DWORD, TRUE);
     116            0 :       size = sizeof(rt);
     117            0 :       db_get_value(hDB, 0, "/Runinfo/Requested transition", &rt, &size, TID_INT, TRUE);
     118              : 
     119              :       /* Experiment info */
     120            0 :       size = sizeof(ex);
     121            0 :       db_get_value(hDB, 0, "/experiment/name", ex, &size, TID_STRING, TRUE);
     122              : 
     123              :       {
     124              :          time_t full_time;
     125            0 :          time(&full_time);
     126              :          assert(sizeof(str) >= 32);
     127            0 :          ctime_r(&full_time, str);
     128            0 :          str[24] = 0;
     129              :       }
     130              : //      if (active_flag) {
     131              : //         sprintf(&(ststr[j++][0]), "*- MIDAS revision: %s - MIDAS status -- Alarm Checker active-------*", cm_get_revision());
     132              : //         j++; sprintf(&(ststr[j++][0]), "-%s-*", str);
     133              : //      } else 
     134              :       {
     135            0 :          sprintf(rev1, "%s", cm_get_revision());
     136            0 :          strcpy(rev, strstr(rev1, "midas"));
     137            0 :          xststr[j++] = msprintf("*- MIDAS Status Page  -%s ----------------------------*", str);
     138            0 :          xststr[j++] = msprintf("*- Revision  -%s---------*", rev);
     139              :       }  
     140              : 
     141            0 :       xststr[j] = msprintf("Experiment: %s, ", ex);
     142            0 :       xststr[j] += msprintf("Run: %d, ", rn);
     143              : 
     144              :       /* PAA revisit the /Runinfo for run time display */
     145              :       /* state */
     146            0 :       if (rs == STATE_RUNNING) {
     147            0 :          if (rt == TR_STOP)
     148            0 :             xststr[j] += msprintf(" Deferred_Stop");
     149              :          else {
     150            0 :             if (esc_flag)
     151            0 :                xststr[j] += msprintf("State: \033[1m%s\033[m", cs);
     152              :             else
     153            0 :                xststr[j] += msprintf("State: %s", cs);
     154              :          }
     155              :       } else {
     156            0 :          if (rt == TR_START)
     157            0 :             xststr[j] += msprintf("Deferred_Start");
     158              :          else
     159            0 :             xststr[j] += msprintf("State: %s", cs);
     160              :       }
     161              : 
     162            0 :       j++;
     163              : 
     164              :       /* time */
     165            0 :       if (rs != STATE_STOPPED) {
     166              :          DWORD full_time;
     167            0 :          cm_time(&full_time);
     168            0 :          DWORD difftime = (DWORD) full_time - tb;
     169            0 :          if (esc_flag)
     170            0 :             xststr[j++] = msprintf("Run time: %02d:%02d:%02d", difftime / 3600, difftime % 3600 / 60, difftime % 60);
     171              :          else
     172            0 :             xststr[j++] = msprintf("Run time: %02d:%02d:%02d", difftime / 3600, difftime % 3600 / 60, difftime % 60);
     173              : 
     174            0 :          xststr[j++] = msprintf("Start time: %s", stt);
     175            0 :       } else if (rs == STATE_STOPPED) {
     176              :          DWORD difftime;
     177            0 :          if (tsb < tb)
     178            0 :             difftime = 0;
     179              :          else
     180            0 :             difftime = tsb - tb;
     181            0 :          if (esc_flag)
     182            0 :             xststr[j++] = msprintf("Full Run time: %02d:%02d:%02d", difftime / 3600, difftime % 3600 / 60, difftime % 60);
     183              :          else
     184            0 :             xststr[j++] = msprintf("Full Run time: %02d:%02d:%02d", difftime / 3600, difftime % 3600 / 60, difftime % 60);
     185              : 
     186            0 :          xststr[j++] = msprintf("Start time: %s", stt);
     187            0 :          xststr[j++] += msprintf("Stop time:  %s", spt);
     188              :       }
     189            0 :       xststr[j++] = "";
     190              :    }  /* --- run info --- */
     191              : 
     192              : /* --------------------- Equipment tree -------------------------- */
     193              :    {
     194              :       double equevtsend;
     195              :       double equevtpsec;
     196              :       char equclient[256];
     197              :       char equnode[256];
     198              :       double equkbpsec;
     199              :       BOOL equenabled;
     200              : 
     201            0 :       size = sizeof(str);
     202            0 :       atleastone_active = FALSE;
     203              :       /* check if dir exists */
     204            0 :       if (db_find_key(hDB, 0, "/equipment", &hKey) == DB_SUCCESS) {
     205            0 :          xststr[j++] = "FE Equip.   Node              Evts Taken     Evt Rate[/s]   Data Rate[Kb/s]";
     206            0 :          for (i = 0;; i++) {
     207            0 :             db_enum_key(hDB, hKey, i, &hSubkey);
     208            0 :             if (!hSubkey)
     209            0 :                break;
     210            0 :             db_get_key(hDB, hSubkey, &key);
     211            0 :             if ((key.type == TID_KEY) &&
     212            0 :                 ((strstr(key.name, "ODB")) == NULL) &&
     213            0 :                 ((strstr(key.name, "BOR")) == NULL) &&
     214            0 :                 ((strstr(key.name, "EOR")) == NULL)) {
     215              :                /* check if client running this equipment is present */
     216              :                /* extract client name from equipment */
     217            0 :                size = sizeof(equclient);
     218            0 :                sprintf(strtmp, "/equipment/%s/common/Frontend name", key.name);
     219            0 :                db_get_value(hDB, 0, strtmp, equclient, &size, TID_STRING, TRUE);
     220              :                /* search client name under /system/clients/xxx/name */
     221            0 :                if (cm_exist(equclient, TRUE) == CM_SUCCESS) {
     222            0 :                   atleastone_active = TRUE;
     223            0 :                   size = sizeof(equenabled);
     224            0 :                   sprintf(strtmp, "/equipment/%s/common/enabled", key.name);
     225            0 :                   db_get_value(hDB, 0, strtmp, &equenabled, &size, TID_BOOL, TRUE);
     226              : 
     227            0 :                   size = sizeof(equevtsend);
     228            0 :                   sprintf(strtmp, "/equipment/%s/statistics/events sent", key.name);
     229            0 :                   db_get_value(hDB, 0, strtmp, &equevtsend, &size, TID_DOUBLE, TRUE);
     230              : 
     231            0 :                   size = sizeof(equevtpsec);
     232            0 :                   sprintf(strtmp, "/equipment/%s/statistics/events per sec.", key.name);
     233            0 :                   db_get_value(hDB, 0, strtmp, &equevtpsec, &size, TID_DOUBLE, TRUE);
     234              : 
     235            0 :                   size = sizeof(equkbpsec);
     236            0 :                   sprintf(strtmp, "/equipment/%s/statistics/kBytes per sec.", key.name);
     237            0 :                   db_get_value(hDB, 0, strtmp, &equkbpsec, &size, TID_DOUBLE, TRUE);
     238              : 
     239            0 :                   size = sizeof(equnode);
     240            0 :                   sprintf(strtmp, "/equipment/%s/common/Frontend host", key.name);
     241            0 :                   db_get_value(hDB, 0, strtmp, equnode, &size, TID_STRING, TRUE);
     242              :                   {
     243              :                      char *pp, sdummy[257];
     244            0 :                      memset(sdummy, 0, 64);
     245            0 :                      xststr[j] = msprintf("%-11s ", key.name);
     246            0 :                      pp = strchr(equnode, '.');
     247            0 :                      if (pp != NULL)
     248            0 :                         xststr[j] += msprintf("%-18s", strncpy(sdummy, equnode, pp - equnode));
     249              :                      else
     250            0 :                         xststr[j] += msprintf("%-18s", strncpy(sdummy, equnode, sizeof(sdummy)-1));
     251              : 
     252            0 :                      if (equevtsend > 1E9)
     253            0 :                         xststr[j] += msprintf("%10.3lfG", equevtsend / 1E9);
     254            0 :                      else if (equevtsend > 1E6)
     255            0 :                         xststr[j] += msprintf("%10.3lfM", equevtsend / 1E6);
     256              :                      else
     257            0 :                         xststr[j] += msprintf("%10.0lf", equevtsend);
     258              : 
     259            0 :                      xststr[j] += "     ";
     260              :                      
     261            0 :                      if (equenabled) {
     262            0 :                         if (esc_flag) {
     263            0 :                            xststr[j] += msprintf("\033[7m%12.1lf\033[m", equevtpsec);
     264            0 :                            xststr[j] += "      ";
     265            0 :                            xststr[j] += msprintf("%12.1lf", equkbpsec);
     266              :                         } else {
     267            0 :                            xststr[j] += msprintf("%12.1lf", equevtpsec);
     268            0 :                            xststr[j] += "      ";
     269            0 :                            xststr[j] += msprintf("%12.1lf", equkbpsec);
     270              :                         }
     271              :                      } else {
     272            0 :                         xststr[j] += msprintf("%12.1lf", equevtpsec);
     273            0 :                         xststr[j] += "      ";
     274            0 :                         xststr[j] += msprintf("%12.1lf", equkbpsec);
     275              :                      }
     276            0 :                      j++;
     277              :                   }             /* get value */
     278              :                }                /* active */
     279              :             }                   /* eor==NULL */
     280            0 :          }                      /* for equipment */
     281              :       }
     282              :       /* Front-End message */
     283            0 :       if (!atleastone_active) {
     284            0 :          xststr[j++] = "... No Front-End currently running...";
     285              :       }
     286              :    }                            /* --- Equipment tree --- */
     287              : 
     288              : /* --------------------- Logger tree ----------------------------- */
     289              : /* search client name "Logger" under /system/clients/xxx/name */
     290            0 :    if ((cm_exist("logger", FALSE) == CM_SUCCESS)
     291            0 :        || (cm_exist("fal", FALSE) == CM_SUCCESS)) {
     292              :       char datadir[256];
     293              :       BOOL wd, lactive;
     294              :       char lpath[256];
     295              :       char ltype[64];
     296              :       char lstate[64];
     297              :       double levt;
     298              :       double lbyt;
     299              : 
     300              :       /* logger */
     301            0 :       xststr[j++] = "";
     302            0 :       size = sizeof(datadir);
     303            0 :       db_get_value(hDB, 0, "/logger/data dir", datadir, &size, TID_STRING, TRUE);
     304            0 :       std::string mesfile;
     305            0 :       cm_msg_get_logfile(NULL, 0, &mesfile, NULL, NULL);
     306            0 :       size = sizeof(wd);
     307            0 :       db_get_value(hDB, 0, "/logger/write data", &wd, &size, TID_BOOL, TRUE);
     308            0 :       xststr[j] = msprintf("Logger Data dir: %s", datadir);
     309            0 :       j++;
     310            0 :       xststr[j] = msprintf("Msg File: %s", mesfile.c_str());
     311            0 :       j++;
     312              : 
     313              :       /* check if dir exists */
     314            0 :       if (db_find_key(hDB, 0, "/logger/channels", &hKey) == DB_SUCCESS) {
     315            0 :          xststr[j++] = "Chan.   Active Type    Filename            Events Taken     KBytes Taken";
     316            0 :          for (i = 0;; i++) {
     317            0 :             db_enum_key(hDB, hKey, i, &hSubkey);
     318            0 :             if (!hSubkey)
     319            0 :                break;
     320            0 :             db_get_key(hDB, hSubkey, &key);
     321            0 :             if (key.type == TID_KEY) {
     322            0 :                size = sizeof(lactive);
     323            0 :                sprintf(strtmp, "/logger/channels/%s/settings/active", key.name);
     324            0 :                db_get_value(hDB, 0, strtmp, &lactive, &size, TID_BOOL, TRUE);
     325            0 :                sprintf(lstate, "No");
     326            0 :                if (lactive)
     327            0 :                   sprintf(lstate, "Yes");
     328            0 :                size = sizeof(lpath);
     329            0 :                sprintf(strtmp, "/logger/channels/%s/settings/Filename", key.name);
     330            0 :                db_get_value(hDB, 0, strtmp, lpath, &size, TID_STRING, TRUE);
     331              : 
     332              :                /* substitue "%d" by current run number */
     333            0 :                str[0] = 0;
     334            0 :                strcat(str, lpath);
     335            0 :                if (strchr(str, '%'))
     336            0 :                   sprintf(path, str, rn);
     337              :                else
     338            0 :                   strcpy(path, str);
     339            0 :                strcpy(lpath, path);
     340              : 
     341            0 :                size = sizeof(ltype);
     342            0 :                sprintf(strtmp, "/logger/channels/%s/settings/type", key.name);
     343            0 :                db_get_value(hDB, 0, strtmp, ltype, &size, TID_STRING, TRUE);
     344              : 
     345            0 :                size = sizeof(levt);
     346            0 :                sprintf(strtmp, "/logger/channels/%s/statistics/Events written", key.name);
     347            0 :                db_get_value(hDB, 0, strtmp, &levt, &size, TID_DOUBLE, TRUE);
     348              : 
     349            0 :                size = sizeof(lbyt);
     350            0 :                sprintf(strtmp, "/logger/channels/%s/statistics/Bytes written", key.name);
     351            0 :                db_get_value(hDB, 0, strtmp, &lbyt, &size, TID_DOUBLE, TRUE);
     352            0 :                lbyt /= 1024;
     353            0 :                if (lactive) {
     354            0 :                   if (esc_flag) {
     355            0 :                      xststr[j] = msprintf("  \033[7m%-3s\033[m", key.name);
     356              :                   } else {      /* no esc */
     357            0 :                      xststr[j] = msprintf("  %-3s", key.name);
     358              :                   }
     359            0 :                   xststr[j] += "   ";
     360            0 :                   if (wd == 1)
     361            0 :                      xststr[j] += msprintf("%-6s", lstate);
     362              :                   else
     363            0 :                      xststr[j] += msprintf("(%-4s)", lstate);
     364            0 :                   xststr[j] += " ";
     365            0 :                   xststr[j] += msprintf("%-7s", ltype);
     366            0 :                   xststr[j] += " ";
     367            0 :                   xststr[j] += msprintf("%-15s", lpath);
     368            0 :                   xststr[j] += "     ";
     369            0 :                   xststr[j] += msprintf("%12.0f", levt);
     370            0 :                   xststr[j] += "     ";
     371            0 :                   xststr[j] += msprintf("%12.2e", lbyt);
     372              :                } else {         /* not active */
     373            0 :                   xststr[j] = msprintf("  %-3s", key.name);
     374            0 :                   xststr[j] += "   ";
     375            0 :                   if (wd == 1)
     376            0 :                      xststr[j] += msprintf("%-6s", lstate);
     377              :                   else
     378            0 :                      xststr[j] += msprintf("(%-4s)", lstate);
     379            0 :                   xststr[j] += " ";
     380            0 :                   xststr[j] += msprintf("%-7s", ltype);
     381            0 :                   xststr[j] += " ";
     382            0 :                   xststr[j] += msprintf("%-15s", lpath);
     383            0 :                   xststr[j] += "     ";
     384            0 :                   xststr[j] += msprintf("%12.0f", levt);
     385            0 :                   xststr[j] += "     ";
     386            0 :                   xststr[j] += msprintf("%12.2e", lbyt);
     387              :                }
     388            0 :                j++;
     389              :             }                   /* key */
     390              :          }                      /* for */
     391              :       }                         /* exists */
     392            0 :    } else {
     393            0 :       xststr[j++] = msprintf("... Logger currently not running...");
     394              :    }
     395              : 
     396              : /* --------------------- Lazy logger tree ------------------------ */
     397              : /* include lazy if running */
     398              : /* search client name under /system/clients/xxx/name */
     399              :    {
     400              :       float cr, bs;
     401              :       INT status, nf, size, i, k;
     402              :       char bn[128], tl[128];
     403              :       HNDLE hlKey, hKey, hSubkey;
     404              :       char client_name[NAME_LENGTH];
     405              : 
     406            0 :       status = db_find_key(hDB, 0, "System/Clients", &hKey);
     407            0 :       if (status != DB_SUCCESS)
     408            0 :          return;
     409              : 
     410            0 :       k = 0;
     411              :       /* loop over all clients */
     412            0 :       for (i = 0;; i++) {
     413            0 :          status = db_enum_key(hDB, hKey, i, &hSubkey);
     414            0 :          if (status == DB_NO_MORE_SUBKEYS)
     415            0 :             break;
     416              : 
     417            0 :          if (status == DB_SUCCESS) {
     418              :             /* get client name */
     419            0 :             size = sizeof(client_name);
     420            0 :             db_get_value(hDB, hSubkey, "Name", client_name, &size, TID_STRING, TRUE);
     421            0 :             client_name[4] = 0; /* search only for the 4 first char */
     422            0 :             if (equal_ustring(client_name, "Lazy")) {
     423            0 :                sprintf(str, "/Lazy/%s", &client_name[5]);
     424            0 :                status = db_find_key(hDB, 0, str, &hlKey);
     425            0 :                if (status == DB_SUCCESS) {
     426            0 :                   size = sizeof(tl);
     427            0 :                   db_get_value(hDB, hlKey, "/Settings/List label", tl, &size, TID_STRING, TRUE);
     428            0 :                   if (*tl == '\0')
     429            0 :                      sprintf(tl, "<empty>");
     430            0 :                   size = sizeof(cr);
     431            0 :                   db_get_value(hDB, hlKey, "statistics/Copy progress (%)", &cr, &size, TID_DOUBLE, TRUE);
     432            0 :                   size = sizeof(nf);
     433            0 :                   db_get_value(hDB, hlKey, "statistics/Number of Files", &nf, &size, TID_INT, TRUE);
     434            0 :                   size = sizeof(bs);
     435            0 :                   db_get_value(hDB, hlKey, "statistics/Backup status (%)", &bs, &size, TID_DOUBLE, TRUE);
     436            0 :                   size = sizeof(bn);
     437            0 :                   db_get_value(hDB, hlKey, "statistics/Backup file", bn, &size, TID_STRING, TRUE);
     438              : 
     439            0 :                   if (k == 0) {
     440            0 :                      xststr[j++] = "";
     441              :                      //sprintf(ststr[j++],"");
     442            0 :                      xststr[j] = msprintf("%s %15s %25s %45s %60s", "Lazy Label", "Progress", "File name", "#files", "Total"); // FIXME
     443              :                   }
     444            0 :                   xststr[j] = msprintf("%15s %.0f[%%] %s %i %.1f[%%]", tl, cr, bn, nf, bs); // FIXME
     445            0 :                   k++;
     446              :                }
     447              :             }
     448              :          }
     449              :       }
     450              :    }
     451              : 
     452            0 :    xststr[j++] = "";
     453              : 
     454              : /* --------------------- System client list ---------------------- */
     455              : /* Get current Client listing */
     456            0 :    if (db_find_key(hDB, 0, "/system/clients", &hKey) == DB_SUCCESS) {
     457              :       char clientn[256], clienth[256];
     458              :       char *pp, sdummy[64];
     459              : 
     460            0 :       xststr[j] = "Clients:";
     461            0 :       for (int i = 0;; i++) {
     462            0 :          db_enum_key(hDB, hKey, i, &hSubkey);
     463            0 :          if (!hSubkey)
     464            0 :             break;
     465            0 :          db_get_key(hDB, hSubkey, &key);
     466              : 
     467            0 :          memset(strtmp, 0, sizeof(strtmp));
     468            0 :          size = sizeof(clientn);
     469            0 :          sprintf(strtmp, "name");
     470            0 :          db_get_value(hDB, hSubkey, strtmp, clientn, &size, TID_STRING, TRUE);
     471            0 :          memset(strtmp, 0, sizeof(strtmp));
     472            0 :          size = sizeof(clienth);
     473            0 :          sprintf(strtmp, "host");
     474            0 :          db_get_value(hDB, hSubkey, strtmp, clienth, &size, TID_STRING, TRUE);
     475            0 :          memset(sdummy, 0, 64);
     476            0 :          pp = strchr(clienth, '.');
     477            0 :          if (pp != NULL)
     478            0 :             xststr[j] += msprintf(" %s/%s", clientn, strncpy(sdummy, clienth, pp - clienth));
     479              :          else
     480            0 :             xststr[j] += msprintf(" %s/%s", clientn, clienth);
     481              :       }
     482            0 :       j++;
     483              :    }
     484              : 
     485            0 :    if (loop == 1) {
     486            0 :       xststr[j++] = msprintf("*- [!] to Exit ------- [R] to Refresh ---------------------- Delay:%2.i [sec]-*", delta_time / 1000);
     487              :    } else {
     488            0 :       xststr[j++] = "*---------------------------------------------------------------------------*";
     489              :    }
     490              : 
     491            0 :    cur_max_line = j;
     492              : 
     493              :    ///* remove '/0' */
     494              :    //for (int j = 0; j < MAX_LINE; j++)
     495              :    //   while (strlen(ststr[j]) < (LINE_LENGTH - 1))
     496              :    //      ststr[j][strlen(ststr[j])] = ' ';
     497            0 :    return;
     498              : }
     499              : 
     500              : /*------------------------------------------------------------------*/
     501            0 : int main(int argc, char **argv)
     502              : {
     503            0 :    INT status, last_time = 0, file_mode;
     504              :    HNDLE hDB, hKey;
     505              :    char host_name[HOST_NAME_LENGTH], expt_name[NAME_LENGTH], str[32];
     506              :    char svpath[256], strdis[80];
     507              :    signed char ch;
     508            0 :    INT fHandle, i, j = 0, last_max_line = 0;
     509              :    INT msg;
     510              :    BOOL debug;
     511              : 
     512            0 :    esc_flag = ESC_FLAG;
     513              : 
     514              :    /* set default */
     515            0 :    cm_get_environment(host_name, sizeof(host_name), expt_name, sizeof(expt_name));
     516            0 :    svpath[0] = 0;
     517            0 :    file_mode = 1;
     518            0 :    loop = 0;
     519            0 :    delta_time = 5000;
     520              : 
     521              :    /* get parameters */
     522              :    /* parse command line parameters */
     523            0 :    for (i = 1; i < argc; i++) {
     524            0 :       if (argv[i][0] == '-' && argv[i][1] == 'd')
     525            0 :          debug = TRUE;
     526            0 :       else if (strncmp(argv[i], "-l", 2) == 0)
     527            0 :          loop = 1;
     528            0 :       else if (argv[i][0] == '-') {
     529            0 :          if (i + 1 >= argc || argv[i + 1][0] == '-')
     530            0 :             goto usage;
     531            0 :          if (strncmp(argv[i], "-w", 2) == 0)
     532            0 :             delta_time = 1000 * (atoi(argv[++i]));
     533            0 :          else if (strncmp(argv[i], "-f", 2) == 0)
     534            0 :             strcpy(svpath, argv[++i]);
     535            0 :          else if (strncmp(argv[i], "-e", 2) == 0)
     536            0 :             strcpy(expt_name, argv[++i]);
     537            0 :          else if (strncmp(argv[i], "-h", 2) == 0)
     538            0 :             strcpy(host_name, argv[++i]);
     539            0 :          else if (strncmp(argv[i], "-c", 2) == 0) {
     540            0 :             strcpy(str, argv[++i]);
     541            0 :             if (strncmp(str, "n", 1) == 0 || strncmp(str, "N", 1) == 0)
     542            0 :                file_mode = 0;
     543              :          } else {
     544            0 :           usage:
     545            0 :             printf("usage: mstat  -l (loop) -w delay (5sec) -f filename (null)\n");
     546            0 :             printf("              -c compose (Addrun#/norun#)\n");
     547            0 :             printf("             [-h Hostname] [-e Experiment]\n\n");
     548            0 :             return 0;
     549              :          }
     550              :       }
     551              :    }
     552              : 
     553            0 :    if (debug) // avoid complaint about unused "debug"
     554            0 :       status = SUCCESS;
     555              : 
     556              :    /* connect to experiment */
     557            0 :    status = cm_connect_experiment(host_name, expt_name, "MStatus", 0);
     558            0 :    if (status != CM_SUCCESS)
     559            0 :       return 1;
     560              : 
     561              : #ifdef _DEBUG
     562              :    cm_set_watchdog_params(TRUE, 0);
     563              : #endif
     564              : 
     565              :    /* turn off message display, turn on message logging */
     566            0 :    cm_set_msg_print(MT_ALL, 0, NULL);
     567              : 
     568              :    /* connect to the database */
     569            0 :    cm_get_experiment_database(&hDB, &hKey);
     570              : 
     571              :    /* generate status page */
     572            0 :    if (loop == 0) {
     573            0 :       j = 0;
     574            0 :       if (svpath[0] != 0) {
     575            0 :          compose_status(hDB, hKey);
     576            0 :          fHandle = open_log_midstat(file_mode, rn, svpath);
     577            0 :          esc_flag = 0;
     578            0 :          compose_status(hDB, hKey);
     579            0 :          while ((j < cur_max_line) && (xststr[j][0] != '\0')) {
     580              :             int wr;
     581            0 :             strncpy(svpath, xststr[j].c_str(), 80);
     582            0 :             svpath[80] = '\0';
     583            0 :             printf("%s\n", svpath);
     584            0 :             wr = write(fHandle, "\n", 1);
     585            0 :             assert(wr == 1);
     586            0 :             wr = write(fHandle, xststr[j].c_str(), xststr[j].length());
     587            0 :             assert(wr == (int)xststr[j].length());
     588            0 :             j++;
     589              :          }
     590            0 :          close(fHandle);
     591              :       } else {
     592            0 :          esc_flag = 0;
     593            0 :          compose_status(hDB, hKey);
     594            0 :          for (int k=0; k<cur_max_line; k++) {
     595            0 :             printf("%s\n", xststr[k].c_str());
     596              :          }
     597              :       }
     598              :    } else {
     599              : 
     600              :       /* initialize ss_getchar() */
     601            0 :       ss_getchar(0);
     602              : 
     603            0 :       ss_clear_screen();
     604              : 
     605              :       do {
     606            0 :          if ((ss_millitime() - last_time) > delta_time) {
     607            0 :             last_time = ss_millitime();
     608            0 :             compose_status(hDB, hKey);
     609            0 :             if (cur_max_line < last_max_line)
     610            0 :                ss_clear_screen();
     611            0 :             last_max_line = cur_max_line;
     612              : 
     613            0 :             for (int j=0; j<cur_max_line; j++) {
     614            0 :                mstrlcpy(strdis, xststr[j].c_str(), sizeof(strdis));
     615            0 :                ss_printf(0, j, "%s", xststr[j].c_str());
     616              :             }
     617              :          }
     618            0 :          ch = 0;
     619            0 :          while (ss_kbhit()) {
     620            0 :             ch = ss_getchar(0);
     621            0 :             if (ch == -1)
     622            0 :                ch = getchar();
     623            0 :             if (ch == 'R')
     624            0 :                ss_clear_screen();
     625            0 :             if ((char) ch == '!')
     626            0 :                break;
     627              :          }
     628            0 :          msg = cm_yield(200);
     629            0 :       } while (msg != RPC_SHUTDOWN && msg != SS_ABORT && ch != '!');
     630              :    }
     631            0 :    printf("\n");
     632              : 
     633              :    /* reset terminal */
     634            0 :    ss_getchar(TRUE);
     635              : 
     636            0 :    cm_disconnect_experiment();
     637            0 :    return 1;
     638              : }
        

Generated by: LCOV version 2.0-1