00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #include "jsoc_main.h"
00045 #include "drms_types.h"
00046 #include <time.h>
00047 #include <math.h>
00048 #include <timeio.h>
00049 #include <sys/types.h>
00050 #include <sys/stat.h>
00051 #include <unistd.h>
00052
00053
00054
00055
00056
00057 #define DIE(msg) do { \
00058 fflush(stdout); \
00059 fprintf(stderr, "%s: FATAL: %s. (status=%d)\n", module_name, msg, status); \
00060 return (status ? status : 1); \
00061 } while (0)
00062
00063
00064 #define WARN(msg) do { \
00065 fflush(stdout); \
00066 fprintf(stderr, "%s: WARNING: %s. Continuing.\n", module_name, msg); \
00067 fflush(stderr); \
00068 } while (0)
00069
00070
00071
00072
00073
00074
00075
00076 void
00077 V_printf(int flag, char *first, char *format, ...) {
00078 va_list args;
00079 extern char *module_name;
00080 FILE *fp = (flag > 0) ? stdout : stderr;
00081
00082 va_start(args, format);
00083 if (flag != 0) {
00084
00085
00086 if (first)
00087 fprintf(fp, "%s: %s", module_name, first);
00088 vfprintf(fp, format, args);
00089 fflush(fp);
00090 }
00091 va_end(args);
00092 }
00093
00094 #define kRootDir "root"
00095 #define kRecSetOut "out"
00096 #define kTRec "trec"
00097 #define kLogFile "log"
00098 #define kErrFile "err"
00099 #define kCkpFile "checkpoint"
00100 #define kVerbosity "verb"
00101 #define kNOT_SPEC "Not Specified"
00102
00103 #define STR_MAX 512
00104
00105 #define FN_TRACK_PARAM "track-param.txt"
00106
00107
00108
00109
00110
00111 typedef struct {
00112 int n_par;
00113 char **par_names;
00114 char **par_vals;
00115 } run_info_t;
00116
00117
00118 char *module_name = "ingest_mharp_log";
00119 static int verbflag;
00120
00121
00122 static const char *tracker_run_info_keys[] = {
00123
00124 "TKP_RUNN", "run_name",
00125 "TKP_RUNC", "run_number",
00126 "TKP_RUNT", "run_time",
00127 "T_FRST", "first_frame_trec",
00128 "T_LAST", "last_frame_trec",
00129 "T_HORIZN", "horizon_trec",
00130
00131 NULL, NULL,
00132 };
00133
00134
00135
00136
00137
00138 int
00139 load_tracker_params(const char *rootDir, run_info_t *ri)
00140 {
00141 FILE *fp;
00142 int p, np, llen;
00143 char fn[STR_MAX];
00144 char line[STR_MAX];
00145 char *val, *name_end;
00146
00147 snprintf(fn, sizeof(fn), "%s/%s", rootDir, FN_TRACK_PARAM);
00148 if ((fp = fopen(fn, "r")) == NULL) {
00149 V_printf(-1, "", "Failed to open `%s'.\n", fn);
00150 return 1;
00151 }
00152
00153 np = 0;
00154 while (fgets(line, sizeof(line), fp) != NULL) {
00155 llen = strlen(line);
00156
00157 if (line[llen-1] != '\n') {
00158 V_printf(-1, "", "Line: `%.20s ...' in param file `%s' is too long.\n", line, fn);
00159 return 1;
00160 }
00161 if (line[0] == '#' || line[0] == '\n') continue;
00162 np++;
00163 }
00164 ri->n_par = np;
00165 if (np == 0) {
00166 fclose(fp);
00167 return 0;
00168 }
00169
00170 ri->par_names = calloc(np, sizeof(*(ri->par_names)));
00171 ri->par_vals = calloc(np, sizeof(*(ri->par_vals )));
00172 if (ri->par_names == NULL || ri->par_vals == NULL) {
00173 V_printf(-1, "", "Calloc fail in load_tracker_params for np = %d\n", np);
00174 fclose(fp);
00175 return 1;
00176 }
00177
00178 rewind(fp);
00179 p = 0;
00180 while (fgets(line, sizeof(line), fp) != NULL) {
00181 llen = strlen(line);
00182 if (line[0] == '#' || line[0] == '\n') continue;
00183
00184 line[--llen] = '\0';
00185
00186 while (line[llen-1] == ' ' || line[llen-1] == '\t')
00187 line[--llen] = '\0';
00188
00189 val = strchr(line, (int) ':');
00190 if (val == NULL) {
00191 V_printf(-1, "", "load_tracker_params: no colon in line `%s' in file `%s'\n",
00192 line, fn);
00193 fclose(fp);
00194 return 1;
00195 }
00196
00197 for (name_end = val-1; *name_end == ' ' || *name_end == '\t'; name_end--)
00198 *name_end = '\0';
00199 *val++ = '\0';
00200
00201 while (*val == ' ' || *val == '\t')
00202 *val++ == '\0';
00203
00204 ri->par_names[p] = strdup(line);
00205 ri->par_vals [p] = strdup( val);
00206 p += 1;
00207 }
00208 fclose(fp);
00209 V_printf(verbflag > 0, "", "Got %d tracker params from `%s'\n", np, fn);
00210 return 0;
00211 }
00212
00213
00214
00215
00216
00217 static
00218 int
00219 set_keys_runinfo(DRMS_Record_t *rec,
00220 run_info_t *runInfo)
00221 {
00222 const char **keys = tracker_run_info_keys;
00223 const char *drms_key;
00224 const char *tracker_key;
00225 int not_ok, iKey;
00226 int p, found, ok;
00227
00228 for (not_ok = iKey = 0; keys[iKey] != NULL; iKey += 2) {
00229
00230 drms_key = keys[iKey];
00231 tracker_key = keys[iKey+1];
00232
00233 for (found = p = 0; p < runInfo->n_par; p++) {
00234 if (strcasecmp(tracker_key, runInfo->par_names[p]) == 0) {
00235 found = 1;
00236 ok = drms_setkey_string(rec, drms_key, runInfo->par_vals[p]);
00237 if (ok != DRMS_SUCCESS) not_ok++;
00238 }
00239 }
00240 if (!found) not_ok++;
00241 }
00242 return not_ok;
00243 }
00244
00245
00246
00247
00248
00249
00250
00251
00252 static
00253 int
00254 count_errlog_times(char *fn)
00255 {
00256 FILE *fp;
00257 char line[STR_MAX];
00258 int count;
00259 const char *lookfor = "T_REC = ";
00260
00261 if ((fp = fopen(fn, "r")) == NULL)
00262 return -1;
00263 count = 0;
00264 while (!feof(fp)) {
00265 fgets(line, sizeof(line), fp);
00266 if (strncmp(line, lookfor, sizeof(lookfor)) == 0)
00267 count++;
00268 }
00269 fclose(fp);
00270 return count;
00271 }
00272
00273
00274
00275
00276
00277
00278
00279 ModuleArgs_t module_args[] =
00280 {
00281 {ARG_STRING, kRootDir, kNOT_SPEC, "Input root directory name (typ. ends in Tracks/jsoc)"},
00282 {ARG_STRING, kRecSetOut, kNOT_SPEC, "Output data series"},
00283 {ARG_STRING, kTRec, kNOT_SPEC, "T_REC as primary key"},
00284 {ARG_STRING, kLogFile, "track-latest.log", "Log file name (typ. .log extension)"},
00285 {ARG_STRING, kErrFile, "track-latest.err", "Error file name (typ. .err extension)"},
00286 {ARG_STRING, kCkpFile, "track-post.mat", "Checkpoint file name (.mat extension)"},
00287 {ARG_INT, kVerbosity, "1", "Verbosity: 0=errs only; 1, 2 = more"},
00288 {ARG_END}
00289 };
00290
00291 int DoIt(void)
00292
00293 {
00294 int status = DRMS_SUCCESS;
00295 const char *outQuery, *trec;
00296 const char *rootDir;
00297 const char *log;
00298 const char *errlog;
00299 const char *ckp;
00300 char logFile[STR_MAX];
00301 char errlogFile[STR_MAX];
00302 char ckpFile[STR_MAX];
00303 int errlog_present;
00304 int errlog_count;
00305 DRMS_Record_t *outRec;
00306 DRMS_Segment_t *outSegLog, *outSegErr, *outSegChkpt;
00307 TIME trec_num;
00308 char trec_buf[32];
00309 run_info_t runInfo;
00310 struct stat buf;
00311
00312 printf("%s: Beginning.\n", module_name);
00313
00314 rootDir = cmdparams_get_str(&cmdparams, kRootDir, NULL);
00315 if (strcmp(rootDir, kNOT_SPEC) == 0) DIE("Root directory must be specified");
00316 outQuery = cmdparams_get_str(&cmdparams, kRecSetOut, NULL);
00317 if (strcmp(outQuery, kNOT_SPEC) == 0) DIE("Output series must be specified");
00318 trec = cmdparams_get_str(&cmdparams, kTRec, NULL);
00319 if (strcmp(trec, kNOT_SPEC) == 0) DIE("T_REC must be specified");
00320
00321 log = cmdparams_get_str(&cmdparams, kLogFile, NULL);
00322 errlog = cmdparams_get_str(&cmdparams, kErrFile, NULL);
00323 ckp = cmdparams_get_str(&cmdparams, kCkpFile, NULL);
00324 verbflag = cmdparams_get_int(&cmdparams, kVerbosity, NULL);
00325
00326
00327 if (snprintf(logFile, sizeof(logFile), "%s/%s", rootDir, log) >= sizeof(logFile))
00328 DIE("Log filename was too long.");
00329 if (snprintf(errlogFile, sizeof(errlogFile), "%s/%s", rootDir, errlog) >= sizeof(errlogFile))
00330 DIE("Error log filename was too long.");
00331 if (snprintf(ckpFile, sizeof(ckpFile), "%s/%s", rootDir, ckp) >= sizeof(ckpFile))
00332 DIE("Checkpoint filename was too long.");
00333 printf("%s: Log filename: `%s'\n", module_name, logFile);
00334 printf("%s: Error log filename: `%s'\n", module_name, errlogFile);
00335 printf("%s: Checkpoint filename: `%s'\n", module_name, ckpFile);
00336 if (stat(logFile, &buf) < 0 || !S_ISREG(buf.st_mode))
00337 DIE("Log file does not exist or is not a regular file.");
00338 if (stat(ckpFile, &buf) < 0 || !S_ISREG(buf.st_mode))
00339 DIE("Checkpoint file does not exist or is not a regular file.");
00340 printf("%s: Found log and checkpoint files.\n", module_name);
00341
00342 if (stat(errlogFile, &buf) < 0 || !S_ISREG(buf.st_mode)) {
00343 errlog_present = 0;
00344 printf("%s: Error log file does not exist: this is OK.\n", module_name);
00345 } else {
00346 errlog_present = 1;
00347 printf("%s: Found an error log file `%s', some T_RECs probably were skipped.\n",
00348 module_name, errlog);
00349 }
00350
00351 if (load_tracker_params(rootDir, &runInfo))
00352 WARN("Failed to load tracker parameter file. Some keys will not be set");
00353
00354
00355 trec_num = sscan_time((char *) trec);
00356 if (time_is_invalid(trec_num))
00357 DIE("Could not understand T_REC input time");
00358 sprint_at(trec_buf, trec_num);
00359 printf("%s: T_REC = %s\n", module_name, trec_buf);
00360
00361
00362 outRec = drms_create_record(drms_env, (char *) outQuery, DRMS_PERMANENT, &status);
00363 if (status) {
00364 drms_close_record(outRec, DRMS_FREE_RECORD);
00365 DIE("Output record not found");
00366 }
00367
00368
00369
00370 outSegLog = drms_segment_lookup(outRec, "Log");
00371 if (!outSegLog)
00372 DIE("Log segment lookup failed.");
00373 if (drms_segment_write_from_file(outSegLog, logFile))
00374 DIE("Log file ingestion failed.");
00375 printf("%s: ingested `%s'\n", module_name, logFile);
00376
00377
00378 outSegLog = drms_segment_lookup(outRec, "ErrLog");
00379 if (!outSegLog)
00380 DIE("Error log segment lookup failed.");
00381 if (errlog_present) {
00382 if (drms_segment_write_from_file(outSegLog, errlogFile))
00383 DIE("Error log file ingestion failed (even though error log was present)");
00384 printf("%s: ingested `%s'\n", module_name, errlogFile);
00385 errlog_count = count_errlog_times(errlogFile);
00386 } else {
00387 if (drms_segment_write_from_file(outSegLog, "/dev/null"))
00388 DIE("Error log file ingestion failed (no error log present -> used /dev/null)");
00389 printf("%s: No error log, set error log file to `/dev/null'.\n", module_name);
00390 errlog_count = 0;
00391 }
00392
00393 outSegChkpt = drms_segment_lookup(outRec, "Checkpoint");
00394 if (!outSegChkpt)
00395 DIE("Checkpoint segment lookup failed");
00396 if (drms_segment_write_from_file(outSegChkpt, ckpFile))
00397 DIE("Checkpoint file ingestion failed");
00398 printf("%s: ingested `%s'\n", module_name, ckpFile);
00399
00400
00401
00402 int errs = 0;
00403 errs += (drms_setkey_time (outRec, "T_REC", trec_num) != DRMS_SUCCESS);
00404 errs += (drms_setkey_string(outRec, "BLD_VERS", jsoc_version) != DRMS_SUCCESS);
00405 errs += (drms_setkey_time (outRec, "DATE", CURRENT_SYSTEM_TIME)!= DRMS_SUCCESS);
00406 errs += (drms_setkey_int (outRec, "NFAILED", errlog_count) != DRMS_SUCCESS);
00407
00408 errs += set_keys_runinfo(outRec, &runInfo);
00409 printf("%s: finished keywords, %d keyword errors.\n", module_name, errs);
00410
00411
00412 drms_close_record(outRec, DRMS_INSERT_RECORD);
00413 printf("%s: done.\n", module_name);
00414 return DRMS_SUCCESS;
00415 }
00416