00001 #include "drms.h"
00002 #include <printk.h>
00003 #include <stdio.h>
00004 #include <time.h>
00005 #include <strings.h>
00006
00007 #ifdef __linux__
00008 #include <sched.h>
00009 #endif
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 ModuleArgs_t module_args[] =
00033 {
00034 {ARG_INT, "sunum", NULL, "SU number", ""},
00035 {ARG_INT, "size", NULL, "Size in bytes of the SU", ""},
00036 {ARG_STRING, "seriesname", NULL, "SU series name", ""},
00037 {ARG_INT, "retention", "-3", "retention time (days)", ""},
00038 {ARG_STRING, "scpcommand", NULL, "Command to scp with, like /usr/bin/scp", ""},
00039 {ARG_STRING, "scphost", NULL, "Host to copy from", ""},
00040 {ARG_STRING, "scpuser", NULL, "User to copy as", ""},
00041 {ARG_INT, "scpport", "55000", "Port to scp over, like 55000", ""},
00042 {ARG_STRING, "scpremotepath", NULL, "Remote path to copy from", ""},
00043 {ARG_INT, "debug", "0", "How noisy to be, default silent", ""},
00044 {ARG_STRING, "logfile", "NONE", "Log file for ERRORS ONLY. NONE means do not log.", ""},
00045 {ARG_END}
00046 };
00047
00048 ModuleArgs_t *gModArgs = module_args;
00049 CmdParams_t cmdparams;
00050
00051
00052 void error_message_to_logfile(char *logfile, char *message, long sunum, long size, int status){
00053
00054 FILE *fp;
00055 time_t now;
00056 struct tm *runTime;
00057 char runTimeStr[64];
00058
00059
00060 if (strcmp(logfile, "NONE") == 0){
00061 return;
00062 }
00063
00064
00065
00066
00067 fp = fopen(logfile, "a");
00068 if (fp == NULL) return;
00069
00070 now = time(NULL);
00071 runTime = localtime( &now );
00072 sprintf(runTimeStr, "%d/%02d/%02d %02d:%02d:%02d",
00073 runTime->tm_year + 1900, runTime->tm_mon + 1, runTime->tm_mday,
00074 runTime->tm_hour, runTime->tm_min, runTime->tm_sec);
00075
00076
00077 fprintf(fp, "%s : %s : sunum=%ld, size=%ld status=%d\n", runTimeStr, message, sunum, size, status);
00078
00079 fclose(fp);
00080
00081 return;
00082
00083 }
00084
00085
00086 double timespecToDouble(struct timespec t){
00087 return (double)t.tv_sec + (double)t.tv_nsec / 1000000000.0;
00088 }
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 void printFinal(double elapsedMillisec, int returnVal, char *localDir){
00110 if (localDir == NULL){
00111 fprintf(stdout, "elapsedMillisec:%d;exitStatus:%d;localpath:NONE\n", (int)elapsedMillisec, returnVal);
00112 } else {
00113 fprintf(stdout, "elapsedMillisec:%d;exitStatus:%d;localpath:%s\n", (int)elapsedMillisec, returnVal, localDir);
00114 }
00115 fflush(stdout);
00116 return;
00117 }
00118
00119
00120
00121 int main(int argc, char *argv[]) {
00122
00123
00124 long long sunum = 0;
00125 uint64_t size = 0;
00126 SUM_t *sum = (SUM_t *) NULL;
00127
00128
00129 int retention = 60;
00130 char *seriesname = (char *) NULL;
00131
00132
00133 char *scpcommand;
00134 char *scphost;
00135 char *scpuser;
00136 int scpport;
00137 char *scpremotepath;
00138 char *scplocalpath;
00139 char *logfile;
00140
00141 struct timespec t1,t2;
00142 double elapsedTimeMilli;
00143
00144 char command[1024];
00145 int status = 0;
00146
00147 int debug;
00148 int retVal;
00149
00150 double bytesPerSec;
00151 int numSpeedFactors=4;
00152 int speedFactorIndex;
00153 double speedScaleFactor[] = { 1.0, 1024.0, 1048576.0, 1073741824.0 };
00154 char *speedDesc[] = { "bytes/sec", "kilobytes/sec", "megabytes/sec", "gigabytes/sec" };
00155 int i;
00156
00157 FILE *goFile;
00158 FILE *debugFile;
00159 char goFileName[512];
00160 char debugFileName[512];
00161 time_t now;
00162 struct tm *dbgTime;
00163 char dbgTimeStr[64];
00164
00165
00166 if ((status = cmdparams_parse(&cmdparams, argc, argv)) < CMDPARAMS_SUCCESS){
00167 fprintf(stderr,"Error: Command line parsing failed. Aborting. [%d]\n\n", status);
00168 fprintf(stderr, "Usage : %s sunum=X size=X seriesname=X scpcommand=X scphost=X \\\n", argv[0]);
00169 fprintf(stderr, " scpuser=X scpremotepath=X [scpport=X retention=X debug=X logfile=X]\n\n");
00170 fprintf(stderr, "Items in square brackets are optional, by default scpport=55000 retention=3 debug=0\n\n");
00171
00172 fprintf(stderr, "EXAMPLE : vso_sum_alloc_put sunum=665937791 size=565107497 seriesname=hmi.s_720s \\\n");
00173 fprintf(stderr, " scpcommand=/opt/bin/scp scphost=jsocport.stanford.edu scpuser=jsocexp \\\n");
00174 fprintf(stderr, " scpremotepath=/SUM60/D665937791 debug=5 logfile=/home/production/vso_sum_alloc_put.log\n\n");
00175
00176 fprintf(stderr, "NOTE : No logging is done if logfile=NONE, which is the default. Also, only errors\n");
00177 fprintf(stderr, " are appended to this file - in normal operation, this log file will not even\n");
00178 fprintf(stderr, " be created.\n\n");
00179
00180 fprintf(stderr, "FEATURE : If the file $HOME/vso_sum_alloc_put.debug exists (in the home directory\n");
00181 fprintf(stderr, " of the user running the JMD, which in turn runs this) then a brief message\n");
00182 fprintf(stderr, " will be written for successfully downloaded sunums to the file\n");
00183 fprintf(stderr, " $HOME/vso_sum_alloc_put.debugLog. Use this sparingly.\n\n");
00184
00185 fprintf(stderr, "Niles Oien oien@nso.edu April 2015\n\n");
00186 printFinal(-1.0, -1, NULL);
00187 return -1;
00188 }
00189
00190 sunum = cmdparams_get_int64(&cmdparams, "sunum", NULL);
00191 size = cmdparams_get_int64(&cmdparams, "size", NULL);
00192 seriesname = strdup(cmdparams_get_str(&cmdparams, "seriesname", NULL));
00193 retention = cmdparams_get_int(&cmdparams, "retention", NULL);
00194
00195 scpcommand = strdup(cmdparams_get_str(&cmdparams, "scpcommand", NULL));
00196 scphost = strdup(cmdparams_get_str(&cmdparams, "scphost", NULL));
00197 scpuser = strdup(cmdparams_get_str(&cmdparams, "scpuser", NULL));
00198 scpport = cmdparams_get_int64(&cmdparams, "scpport", NULL);
00199 scpremotepath = strdup(cmdparams_get_str(&cmdparams, "scpremotepath", NULL));
00200 debug = cmdparams_get_int64(&cmdparams, "debug", NULL);
00201 logfile = strdup(cmdparams_get_str(&cmdparams, "logfile", NULL));
00202
00203
00204 if ((sum = SUM_open(NULL, NULL, printkerr)) == NULL){
00205 fprintf(stderr,"ERROR: drms_open: Failed to connect to SUMS.\n");
00206 printFinal(-2.0, -2, NULL);
00207 error_message_to_logfile(logfile, "SUM_open() returned NULL", (long)sunum, (long)size, 0);
00208 return -2;
00209 }
00210
00211 if (sum->status){
00212 fprintf(stderr,"ERROR: drms_open: Returned status %d\n", sum->status);
00213 error_message_to_logfile(logfile, "SUM_open() returned bad sum->status", (long)sunum, (long)size, sum->status);
00214 printFinal(-2.0, -2, NULL);
00215 return -2;
00216 }
00217
00218 if (debug > 1){
00219 fprintf(stdout, "SUM_open() successful\n");
00220 }
00221
00222
00223 sum->reqcnt = 1;
00224 sum->bytes = size;
00225
00226
00227 if ((status = SUM_alloc2(sum, sunum, printf))){
00228 fprintf(stderr,"ERROR: SUM_alloc2 RPC call failed with error code %d\n", status);
00229 #if defined(SUMS_USEMTSUMS_CONNECTION) && SUMS_USEMTSUMS_CONNECTION
00230 SUM_rollback(sum, printf);
00231 #else
00232 SUM_close(sum, printf);
00233 #endif
00234 error_message_to_logfile(logfile, "SUM_alloc2() returned bad status", (long)sunum, (long)size, status);
00235 printFinal(-3.0, -3, NULL);
00236 return -3;
00237 }
00238
00239 if (sum->status){
00240 fprintf(stderr,"ERROR: SUM_alloc2 RPC call returned status %d\n", sum->status);
00241 #if defined(SUMS_USEMTSUMS_CONNECTION) && SUMS_USEMTSUMS_CONNECTION
00242 SUM_rollback(sum, printf);
00243 #else
00244 SUM_close(sum,printf);
00245 #endif
00246 error_message_to_logfile(logfile, "SUM_alloc2() returned bad sum->status", (long)sunum, (long)size, sum->status);
00247 printFinal(-3.0, -3, NULL);
00248 return -3;
00249 }
00250
00251 scplocalpath = strdup(sum->wd[0]);
00252
00253 if (debug > 1){
00254 fprintf(stdout, "SUM_alloc2() successful for %d bytes, have local directory %s\n",
00255 size, scplocalpath);
00256 }
00257
00258
00259
00260 if (debug){
00261 sprintf(command,"%s -pr -P%d %s@%s:%s/* %s",
00262 scpcommand, scpport, scpuser, scphost, scpremotepath, scplocalpath);
00263 } else {
00264 sprintf(command,"%s -qpr -P%d %s@%s:%s/* %s",
00265 scpcommand, scpport, scpuser, scphost, scpremotepath, scplocalpath);
00266 }
00267
00268 if (debug > 0){
00269 fprintf(stdout, "Preparing to execute command : %s\n", command);
00270 }
00271
00272 if (clock_gettime(CLOCK_REALTIME, &t1)){
00273 fprintf(stderr, "Failed to get start time\n");
00274 #if defined(SUMS_USEMTSUMS_CONNECTION) && SUMS_USEMTSUMS_CONNECTION
00275 SUM_rollback(sum, printf);
00276 #else
00277 SUM_close(sum,printf);
00278 #endif
00279 error_message_to_logfile(logfile, "clock_gettime() failed", (long)sunum, (long)size, 0);
00280 printFinal(-4.0, -4, scplocalpath);
00281 return -4;
00282 }
00283
00284 retVal=system(command);
00285
00286 if (clock_gettime(CLOCK_REALTIME, &t2)){
00287 fprintf(stderr, "Failed to get end time\n");
00288 #if defined(SUMS_USEMTSUMS_CONNECTION) && SUMS_USEMTSUMS_CONNECTION
00289 SUM_rollback(sum, printf);
00290 #else
00291 SUM_close(sum,printf);
00292 #endif
00293 error_message_to_logfile(logfile, "clock_gettime() Failed", (long)sunum, (long)size, 0);
00294 printFinal(-4.0, -4, scplocalpath);
00295 return -4;
00296 }
00297
00298 elapsedTimeMilli=1000.0*(timespecToDouble(t2)-timespecToDouble(t1));
00299
00300 if (debug > 0){
00301 bytesPerSec=(double)size/(elapsedTimeMilli/1000.0);
00302 speedFactorIndex = 0;
00303 for (i=0; i < numSpeedFactors-1; i++){
00304 if (bytesPerSec >= speedScaleFactor[i+1]){
00305 speedFactorIndex++;
00306 }
00307 }
00308 fprintf(stdout, "Command took %lf ms (%lf min) [%lf %s] and returned %d\n",
00309 elapsedTimeMilli, elapsedTimeMilli/60000.0, bytesPerSec/speedScaleFactor[speedFactorIndex],
00310 speedDesc[speedFactorIndex], retVal);
00311 }
00312
00313
00314 if (retVal){
00315 fprintf(stderr, "scp command failed with return %d : %s\n", retVal, command);
00316 #if defined(SUMS_USEMTSUMS_CONNECTION) && SUMS_USEMTSUMS_CONNECTION
00317 SUM_rollback(sum, printf);
00318 #else
00319 SUM_close(sum,printf);
00320 #endif
00321 error_message_to_logfile(logfile, "scp returned non-zero", (long)sunum, (long)size, retVal);
00322 printFinal(-5.0, -5, scplocalpath);
00323 return -5;
00324 }
00325
00326 sum->dsname = seriesname;
00327 sum->group = 0;
00328 sum->mode = TEMP + TOUCH;
00329 sum->tdays = retention;
00330 sum->reqcnt = 1;
00331 sum->history_comment = "";
00332
00333 sum->dsix_ptr[0] = sunum;
00334 sum->wd[0] = scplocalpath;
00335
00336
00337 if ((status = SUM_put(sum, printf))) {
00338 fprintf(stderr,"ERROR: SUM_put RPC call failed with error code %d\n", status);
00339 #if defined(SUMS_USEMTSUMS_CONNECTION) && SUMS_USEMTSUMS_CONNECTION
00340 SUM_rollback(sum, printf);
00341 #else
00342 SUM_close(sum,printf);
00343 #endif
00344 error_message_to_logfile(logfile, "SUM_put() returned non-zero status", (long)sunum, (long)size, status);
00345 printFinal(-6.0, -6, scplocalpath);
00346 return -6;
00347 }
00348
00349 if (sum->status){
00350 fprintf(stderr,"ERROR: SUM_put RPC returned %d\n", sum->status);
00351 #if defined(SUMS_USEMTSUMS_CONNECTION) && SUMS_USEMTSUMS_CONNECTION
00352
00353 #else
00354 SUM_close(sum,printf);
00355 #endif
00356 error_message_to_logfile(logfile, "SUM_put() returned non-zero sum->status", (long)sunum, (long)size, sum->status);
00357 printFinal(-6.0, -6, scplocalpath);
00358 return -6;
00359 }
00360
00361
00362 if (debug > 1){
00363 fprintf(stdout, "SUM_put() succeeded\n");
00364 }
00365
00366 status=SUM_close(sum,printf);
00367 if (status){
00368 fprintf(stderr,"ERROR: SUM_close RPC call failed with error code %d\n", status);
00369 error_message_to_logfile(logfile, "SUM_close() returned non-zero status", (long)sunum, (long)size, status);
00370 printFinal(-7.0, -7, scplocalpath);
00371 return -7;
00372 }
00373
00374 if (sum->status){
00375 fprintf(stderr,"ERROR: SUM_close RPC call failed with sum->status %d\n", sum->status);
00376 error_message_to_logfile(logfile, "SUM_close() created non-zero sum->status", (long)sunum, (long)size, sum->status);
00377 printFinal(-7.0, -7, scplocalpath);
00378 return -7;
00379 }
00380
00381 if (debug > 1){
00382 fprintf(stdout, "SUM_close() complete\n");
00383 }
00384
00385
00386 printFinal(elapsedTimeMilli, 0, scplocalpath);
00387
00388
00389
00390
00391
00392 sprintf(goFileName, "%s/vso_sum_alloc_put.debug", getenv("HOME"));
00393 goFile=fopen(goFileName, "r");
00394 if (goFile != NULL){
00395 fclose(goFile);
00396 sprintf(debugFileName, "%s/vso_sum_alloc_put.debugLog", getenv("HOME"));
00397 debugFile=fopen(debugFileName, "a");
00398 if (debugFile != NULL){
00399
00400 now = time(NULL);
00401 dbgTime = localtime( &now );
00402 sprintf(dbgTimeStr, "%d/%02d/%02d %02d:%02d:%02d",
00403 dbgTime->tm_year + 1900, dbgTime->tm_mon + 1, dbgTime->tm_mday,
00404 dbgTime->tm_hour, dbgTime->tm_min, dbgTime->tm_sec);
00405
00406 fprintf(debugFile, "%s : completed sunum=%ld size=%ld elapsedMilliSec=%d path=%s\n", dbgTimeStr, (long)sunum, (long)size,
00407 (int)elapsedTimeMilli, scplocalpath);
00408 fclose(debugFile);
00409 }
00410 }
00411
00412
00413
00414 if (seriesname != NULL) {
00415 free(seriesname); seriesname=NULL;
00416 }
00417
00418 if (scpcommand != NULL) {
00419 free(scpcommand); scpcommand=NULL;
00420 }
00421
00422 if (scphost != NULL) {
00423 free(scphost); scphost=NULL;
00424 }
00425
00426 if (scpuser != NULL) {
00427 free(scpuser); scpuser=NULL;
00428 }
00429
00430 if (scpremotepath != NULL) {
00431 free(scpremotepath); scpremotepath=NULL;
00432 }
00433
00434 if (scplocalpath != NULL) {
00435 free(scplocalpath); scplocalpath=NULL;
00436 }
00437
00438 if (logfile != NULL) {
00439 free(logfile); logfile=NULL;
00440 }
00441
00442 return 0;
00443
00444 }
00445