Midas History Functions (hs_xxx)


Functions

INT hs_set_path (char *path)
INT hs_dump (DWORD event_id, DWORD start_time, DWORD end_time, DWORD interval, BOOL binary_time)


Function Documentation

INT hs_dump ( DWORD  event_id,
DWORD  start_time,
DWORD  end_time,
DWORD  interval,
BOOL  binary_time 
)

Display history for a given event at stdout. The output can be redirected to be read by Excel for example.

Parameters:
event_id Event ID
start_time Starting Date/Time
end_time End Date/Time
interval Minimum time in seconds between reported events. Can be used to skip events
binary_time Display DWORD time stamp
Returns:
HS_SUCCESS, HS_FILE_ERROR

Definition at line 1747 of file history.c.

01748 {
01749    DWORD prev_time, last_irec_time;
01750    time_t ltime;
01751    int fh, fhd, fhi;
01752    INT i, j, delta, status, n_tag = 0, old_n_tag = 0;
01753    INDEX_RECORD irec;
01754    HIST_RECORD rec, drec;
01755    INT old_def_offset, offset;
01756    TAG *tag = NULL, *old_tag = NULL;
01757    char str[NAME_LENGTH], data_buffer[10000];
01758    struct tm *tms;
01759 
01760    /* if not time given, use present to one hour in past */
01761    if (start_time == 0)
01762       start_time = (DWORD) time(NULL) - 3600;
01763    if (end_time == 0)
01764       end_time = (DWORD) time(NULL);
01765 
01766    /* search history file for start_time */
01767    status = hs_search_file(&start_time, 1);
01768    if (status != HS_SUCCESS) {
01769       cm_msg(MERROR, "hs_dump", "cannot find recent history file");
01770       return HS_FILE_ERROR;
01771    }
01772 
01773    /* open history and definition files */
01774    hs_open_file(start_time, "hst", O_RDONLY, &fh);
01775    hs_open_file(start_time, "idf", O_RDONLY, &fhd);
01776    hs_open_file(start_time, "idx", O_RDONLY, &fhi);
01777    if (fh < 0 || fhd < 0 || fhi < 0) {
01778       cm_msg(MERROR, "hs_dump", "cannot open index files");
01779       return HS_FILE_ERROR;
01780    }
01781 
01782    /* search record closest to start time */
01783    lseek(fhi, 0, SEEK_END);
01784    delta = (TELL(fhi) / sizeof(irec)) / 2;
01785    lseek(fhi, delta * sizeof(irec), SEEK_SET);
01786    do {
01787       delta = (int) (abs(delta) / 2.0 + 0.5);
01788       read(fhi, (char *) &irec, sizeof(irec));
01789       if (irec.time > start_time)
01790          delta = -delta;
01791 
01792       i = lseek(fhi, (delta - 1) * sizeof(irec), SEEK_CUR);
01793    } while (abs(delta) > 1 && irec.time != start_time);
01794    read(fhi, (char *) &irec, sizeof(irec));
01795    if (irec.time > start_time)
01796       delta = -abs(delta);
01797 
01798    i = TELL(fhi) + (delta - 1) * sizeof(irec);
01799    if (i <= 0)
01800       lseek(fhi, 0, SEEK_SET);
01801    else
01802       lseek(fhi, (delta - 1) * sizeof(irec), SEEK_CUR);
01803    read(fhi, (char *) &irec, sizeof(irec));
01804 
01805    /* read records, skip wrong IDs */
01806    old_def_offset = -1;
01807    prev_time = 0;
01808    last_irec_time = 0;
01809    do {
01810       if (irec.time < last_irec_time) {
01811          cm_msg(MERROR, "hs_dump", "corrupted history data: time does not increase: %d -> %d", last_irec_time, irec.time);
01812          hs_gen_index(last_irec_time);
01813          return HS_FILE_ERROR;
01814       }
01815       last_irec_time = irec.time;
01816       if (irec.event_id == event_id && irec.time <= end_time && irec.time >= start_time) {
01817          if (irec.time >= prev_time + interval) {
01818             prev_time = irec.time;
01819             lseek(fh, irec.offset, SEEK_SET);
01820             read(fh, (char *) &rec, sizeof(rec));
01821 
01822             /* if definition changed, read new definition */
01823             if ((INT) rec.def_offset != old_def_offset) {
01824                lseek(fh, rec.def_offset, SEEK_SET);
01825                read(fh, (char *) &drec, sizeof(drec));
01826                read(fh, str, NAME_LENGTH);
01827 
01828                if (tag == NULL)
01829                   tag = (TAG *) M_MALLOC(drec.data_size);
01830                else
01831                   tag = (TAG *) realloc(tag, drec.data_size);
01832                if (tag == NULL)
01833                   return HS_NO_MEMORY;
01834                read(fh, (char *) tag, drec.data_size);
01835                n_tag = drec.data_size / sizeof(TAG);
01836 
01837                /* print tag names if definition has changed */
01838                if (old_tag == NULL || old_n_tag != n_tag || memcmp(old_tag, tag, drec.data_size) != 0) {
01839                   printf("Date\t");
01840                   for (i = 0; i < n_tag; i++) {
01841                      if (tag[i].n_data == 1 || tag[i].type == TID_STRING)
01842                         printf("%s\t", tag[i].name);
01843                      else
01844                         for (j = 0; j < (INT) tag[i].n_data; j++)
01845                            printf("%s%d\t", tag[i].name, j);
01846                   }
01847                   printf("\n");
01848 
01849                   if (old_tag == NULL)
01850                      old_tag = (TAG *) M_MALLOC(drec.data_size);
01851                   else
01852                      old_tag = (TAG *) realloc(old_tag, drec.data_size);
01853                   memcpy(old_tag, tag, drec.data_size);
01854                   old_n_tag = n_tag;
01855                }
01856 
01857                old_def_offset = rec.def_offset;
01858                lseek(fh, irec.offset + sizeof(rec), SEEK_SET);
01859             }
01860 
01861             /* print time from header */
01862             if (binary_time)
01863                printf("%d ", irec.time);
01864             else {
01865                ltime = (time_t) irec.time;
01866                sprintf(str, "%s", ctime(&ltime) + 4);
01867                str[20] = '\t';
01868                printf(str);
01869             }
01870 
01871             /* read data */
01872             read(fh, data_buffer, rec.data_size);
01873 
01874             /* interprete data from tag definition */
01875             offset = 0;
01876             for (i = 0; i < n_tag; i++) {
01877                /* strings have a length of n_data */
01878                if (tag[i].type == TID_STRING) {
01879                   printf("%s\t", data_buffer + offset);
01880                   offset += tag[i].n_data;
01881                } else if (tag[i].n_data == 1) {
01882                   /* non-array data */
01883                   db_sprintf(str, data_buffer + offset, rpc_tid_size(tag[i].type), 0, tag[i].type);
01884                   printf("%s\t", str);
01885                   offset += rpc_tid_size(tag[i].type);
01886                } else
01887                   /* loop over array data */
01888                   for (j = 0; j < (INT) tag[i].n_data; j++) {
01889                      db_sprintf(str, data_buffer + offset, rpc_tid_size(tag[i].type), 0, tag[i].type);
01890                      printf("%s\t", str);
01891                      offset += rpc_tid_size(tag[i].type);
01892                   }
01893             }
01894             printf("\n");
01895          }
01896       }
01897 
01898       /* read next index record */
01899       i = read(fhi, (char *) &irec, sizeof(irec));
01900 
01901       /* end of file: search next history file */
01902       if (i <= 0) {
01903          close(fh);
01904          close(fhd);
01905          close(fhi);
01906 
01907          /* advance one day */
01908          ltime = (time_t) last_irec_time;
01909          tms = localtime(&ltime);
01910          tms->tm_hour = tms->tm_min = tms->tm_sec = 0;
01911          last_irec_time = (DWORD) mktime(tms);
01912 
01913          last_irec_time += 3600 * 24;
01914          if (last_irec_time > end_time)
01915             break;
01916 
01917          /* search next file */
01918          status = hs_search_file((DWORD *) & last_irec_time, 1);
01919          if (status != HS_SUCCESS)
01920             break;
01921 
01922          /* open history and definition files */
01923          hs_open_file(last_irec_time, "hst", O_RDONLY, &fh);
01924          hs_open_file(last_irec_time, "idf", O_RDONLY, &fhd);
01925          hs_open_file(last_irec_time, "idx", O_RDONLY, &fhi);
01926          if (fh < 0 || fhd < 0 || fhi < 0) {
01927             cm_msg(MERROR, "hs_dump", "cannot open index files");
01928             break;
01929          }
01930 
01931          /* read first record */
01932          i = read(fhi, (char *) &irec, sizeof(irec));
01933          if (i <= 0)
01934             break;
01935 
01936          /* old definition becomes invalid */
01937          old_def_offset = -1;
01938       }
01939    } while (irec.time < end_time);
01940 
01941    M_FREE(tag);
01942    M_FREE(old_tag);
01943    close(fh);
01944    close(fhd);
01945    close(fhi);
01946 
01947    return HS_SUCCESS;
01948 }

INT hs_set_path ( char *  path  ) 

Sets the path for future history file accesses. Should be called before any other history function is called.

Parameters:
path Directory where history files reside
Returns:
HS_SUCCESS

Definition at line 49 of file history.c.

Referenced by MidasHistory::hs_connect(), open_history(), and rpc_server_dispatch().

00050 {
00051    /* set path locally and remotely */
00052    if (rpc_is_remote())
00053       rpc_call(RPC_HS_SET_PATH, path);
00054 
00055    strcpy(_hs_path_name, path);
00056 
00057    /* check for trailing directory seperator */
00058    if (strlen(_hs_path_name) > 0 && _hs_path_name[strlen(_hs_path_name) - 1] != DIR_SEPARATOR)
00059       strcat(_hs_path_name, DIR_SEPARATOR_STR);
00060 
00061    return HS_SUCCESS;
00062 }


Midas DOC Version 3.0.0 ---- PSI Stefan Ritt ----
Contributions: Pierre-Andre Amaudruz - Sergio Ballestrero - Suzannah Daviel - Doxygen - Peter Green - Qing Gu - Greg Hackman - Gertjan Hofman - Paul Knowles - Exaos Lee - Rudi Meier - Glenn Moloney - Dave Morris - John M O'Donnell - Konstantin Olchanski - Renee Poutissou - Tamsen Schurman - Andreas Suter - Jan M.Wouters - Piotr Adam Zolnierczuk