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
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #include <SUM.h>
00056 #include <sys/errno.h>
00057 #include <sys/time.h>
00058 #include <signal.h>
00059 #include <sum_rpc.h>
00060 #include <soi_error.h>
00061 #include <printk.h>
00062 #include <sys/statvfs.h>
00063 #include "serverdefs.h"
00064
00065 #define CFG_FILE "/home/production/cvs/JSOC/base/sums/apps/data/sum_rm.cfg"
00066 #define NEWLOG_FILE "/home/production/cvs/JSOC/base/sums/apps/data/sum_rm_0.newlog"
00067 #define RM_ASSIGN "/usr/local/logs/SUM/sum_rm_assign.txt"
00068
00069 int SUMN = 0;
00070 int stat_storage();
00071 void get_cfg();
00072 static char *datestring(void);
00073 static int ponoff[MAX_PART];
00074
00075 char mod_name[] = "sum_rm";
00076
00077 extern PART ptab[];
00078
00079 FILE *logfp;
00080 char thishost[MAX_STR];
00081 char logname[MAX_STR];
00082 char *dbname;
00083 char *timetag;
00084 char *username;
00085 int active;
00086 int datehr;
00087 int num_sets_in_cfg;
00088 int debugflg, sim;
00089 struct timeval tvalr;
00090 struct tm *t_ptr;
00091 char datestr[32];
00092 char *dptr;
00093 int soi_errno = NO_ERROR;
00094 int usr1_sig_flag = 0;
00095 int logvers = 0;
00096
00097
00098
00099 int ixptab[MAX_PART];
00100
00101
00102 int noopflg;
00103 int sleep_sec;
00104 int norun_start;
00105 int norun_stop;
00106 char xlogfile[256];
00107 char mailto[256];
00108 char userrun[256];
00109 double max_free_set_need[MAX_PART];
00110 double max_free_set_current[MAX_PART];
00111 double max_free_set_percent[MAXSUMSETS];
00112
00113
00114
00115 void open_log(char *filename)
00116 {
00117 if((logfp=fopen(filename, "w")) == NULL) {
00118 fprintf(stderr, "Can't open the log file %s for %s on %s\n",
00119 filename, mod_name, thishost);
00120 }
00121 }
00122
00123
00124
00125
00126 int write_log(const char *fmt, ...)
00127 {
00128 va_list args;
00129 char string[32768];
00130
00131 va_start(args, fmt);
00132 vsprintf(string, fmt, args);
00133 if(logfp) {
00134 fprintf(logfp, string);
00135 fflush(logfp);
00136 }
00137 else
00138 fprintf(stderr, string);
00139 va_end(args);
00140 return(0);
00141 }
00142
00143 void sighandler(sig)
00144 int sig;
00145 {
00146 if(sig == SIGTERM) {
00147 write_log("*** %s sum_rm got SIGTERM. Exiting.\n", datestring());
00148 DS_DisConnectDB();
00149 exit(1);
00150 }
00151 if(sig == SIGINT) {
00152 write_log("*** %s sum_rm got SIGINT. Exiting.\n", datestring());
00153 DS_DisConnectDB();
00154 exit(1);
00155 }
00156 write_log("*** %s sum_rm got an illegal signal %d, ignoring...\n",
00157 datestring(), sig);
00158 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
00159 signal(SIGINT, sighandler);
00160 if (signal(SIGALRM, SIG_IGN) != SIG_IGN)
00161 signal(SIGALRM, sighandler);
00162 }
00163
00164
00165
00166 void usr1_sig(int sig)
00167 {
00168 write_log("%s usr1_sig received by sum_rm_0\n", datestring());
00169 write_log("sum_rm_0 will exit at its next opportunity\n");
00170 usr1_sig_flag = 1;
00171 if(!active) {
00172 write_log("%s sum_rm_0 exit from usr1_sig\n", datestring());
00173 exit(0);
00174 }
00175 }
00176
00177
00178
00179 void alrm_sig(int sig)
00180 {
00181 FILE *tstfp;
00182 char cmd[MAX_STR];
00183 int norunflg, update;
00184 double bytesdeleted, availstore;
00185
00186 if(usr1_sig_flag) {
00187 write_log("%s sum_rm_0 exit\n", datestring());
00188 exit(0);
00189 }
00190 if((tstfp=fopen(NEWLOG_FILE, "r"))) {
00191 write_log("sum_rm_0: Close this log %d\n", logvers);
00192 fclose(tstfp);
00193 fclose(logfp);
00194 logvers++;
00195 sprintf(logname, "%s_0.%s.%d", xlogfile, timetag, logvers);
00196 open_log(logname);
00197 write_log("sum_rm_0: Start new log file %d\n", logvers);
00198 sprintf(cmd, "/bin/rm %s", NEWLOG_FILE);
00199 if(system(cmd)) {
00200 write_log("sum_rm_0: Can't execute %s.\n", cmd);
00201 }
00202 }
00203 write_log("Called alrm_sig()\n");
00204 signal(SIGALRM, &alrm_sig);
00205 if(!active) {
00206 active = 1;
00207 update = 0;
00208 get_cfg();
00209 write_log("%s\n", dptr);
00210 if(noopflg) {
00211 write_log("NOOP\n");
00212 } else {
00213 norunflg = 0;
00214 if(norun_start <= datehr) {
00215 norunflg = 1;
00216 }
00217 if(norun_stop <= datehr) {
00218 norunflg = 0;
00219 }
00220 if(norunflg) {
00221 write_log("NORUN interval active\n");
00222 } else {
00223 if(strcmp(username, userrun)) {
00224 write_log("Current user %s is not the configured user %s\n",
00225 username, userrun);
00226 } else {
00227 update = stat_storage();
00228 }
00229 }
00230 }
00231 active = 0;
00232 }
00233 DS_Rm_Commit();
00234 alarm(sleep_sec);
00235 }
00236
00237
00238
00239
00240
00241
00242
00243
00244 int stat_storage()
00245 {
00246 PART *pptr;
00247 int i, j, status;
00248 int updated = 0;
00249 int partnchange = 0;
00250 double df_avail, df_total, df_del, total, upercent;
00251 struct statvfs vfs;
00252
00253
00254
00255
00256 for(j=0; j<MAX_PART-1; j++) {
00257 if(usr1_sig_flag) {
00258 write_log("%s sum_rm_0 exit from usr1_sig\n", datestring());
00259 exit(0);
00260 }
00261 i = ixptab[j];
00262 if(i == -1) break;
00263 pptr=(PART *)&ptab[i];
00264 if(pptr->name == NULL) break;
00265
00266 if(!strcmp(pptr->name, "/SUM100")) continue;
00267 if(!strcmp(pptr->name, "/SUM101")) continue;
00268 if(!strcmp(pptr->name, "/SUM102")) continue;
00269 if(!strcmp(pptr->name, "/SUM103")) continue;
00270
00272
00273
00274
00275
00276 if(status = statvfs(pptr->name, &vfs)) {
00277 printk("Error %d on statvfs() for %s\n",status,pptr->name);
00278 }
00279 else {
00280 df_total = (double)vfs.f_blocks;
00281 df_total = df_total * (double)vfs.f_frsize;
00282 df_avail = (double)vfs.f_bavail;
00283 df_avail = df_avail * (double)vfs.f_bsize;
00284
00285
00286
00287
00288 if(pptr->pds_set_num != -2) {
00289 upercent = df_avail/df_total;
00290 if(upercent < 0.02) {
00291 if(ponoff[i] >= 0) {
00292 printk("Turning off full partition %s\n", pptr->name);
00293 SUMLIB_PavailOff(pptr->name);
00294 ponoff[i] = -1;
00295 partnchange = 1;
00296 }
00297 }
00298 else if(upercent >= 0.05) {
00299 if(ponoff[i] == -1) {
00300 printk("Turning on former full partition %s\n", pptr->name);
00301 SUMLIB_PavailOn(pptr->name, ptab[i].pds_set_prime);
00302 ponoff[i] = 0;
00303 partnchange = 1;
00304 }
00305 }
00306 }
00307 if(SUMLIB_PavailUpdate(pptr->name, df_avail))
00308 printk("Err: SUMLIB_PavailUpdate(%s, %e, ...)\n",
00309 pptr->name, df_avail);
00310 else
00311
00312
00313 max_free_set_need[i] = df_total * max_free_set_percent[0];
00314 max_free_set_current[i] = df_avail;
00315 if(max_free_set_current[i] < max_free_set_need[i]) {
00316 updated = 1;
00317 df_del = max_free_set_need[i] - max_free_set_current[i];
00318
00319 if (df_del < 0.01 * df_total) df_del = 0.01 * df_total;
00320 printk("%s Attempt to del %e bytes\n", pptr->name, df_del);
00321 DS_RmDoX(pptr->name, df_del);
00322 }
00323 }
00324 }
00325 if(partnchange) {
00326 if(system("sumrepartn -y")) {
00327 printk("Error on system() call to resync mem w/db\n");
00328 }
00329 }
00330 return(updated);
00331 }
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357 void get_cfg()
00358 {
00359 FILE *cfgfp;
00360 char *token;
00361 char line[256], cfgfile[256], datestrhr[16];
00362 int i, num;
00363
00364 gettimeofday(&tvalr, NULL);
00365 t_ptr = localtime((const time_t *)&tvalr);
00366 sprintf(datestr, "%s", asctime(t_ptr));
00367 dptr = &datestr[4];
00368 datestr[19] = (char)NULL;
00369 strncpy(datestrhr, &datestr[11], 2);
00370 datestrhr[2] = (char)NULL;
00371 datehr = atoi(datestrhr);
00372 noopflg = 0;
00373 norun_start = norun_stop = 0;
00374 sleep_sec = 300;
00375 for(i=0; i < MAXSUMSETS; i++) {
00376
00377 max_free_set_percent[i] = 3.0/100.0;
00378 }
00379 num_sets_in_cfg = MAXSUMSETS;
00380 strcpy(xlogfile, "/tmp/sum_rm_0.log");
00381 strcpy(mailto, "sys2@solar2");
00382 strcpy(userrun, "production");
00383 #ifdef __LOCALIZED_DEFS__
00384 sprintf (cfgfile, "%s/sum_rm.cfg", SUMLOG_BASEDIR);
00385 #else
00386 sprintf(cfgfile, "%s.%s", CFG_FILE, dbname);
00387 #endif
00388 if(!(cfgfp=fopen(cfgfile, "r"))) {
00389 write_log("Can't open config file %s. Using defaults...\n", cfgfile);
00390 return;
00391 }
00392 num_sets_in_cfg = 0;
00393 while(fgets(line, 256, cfgfp)) {
00394 if(line[0] == '#' || line[0] == '\n') continue;
00395 if(strstr(line, "SLEEP=")) {
00396 token=(char *)strtok(line, "=\n");
00397 if(token=(char *)strtok(NULL, "\n")) {
00398 sleep_sec=atoi(token);
00399 }
00400 }
00401 else if(strstr(line, "PART_PERCENT_FREE")) {
00402 token=(char *)strtok(line, "=\n");
00403
00404 if(token=(char *)strtok(NULL, "\n")) {
00405 num=atoi(token);
00406
00407
00408 max_free_set_percent[0] = (double)num/100.0;
00409 }
00410 }
00411 else if(strstr(line, "LOG=")) {
00412 token=(char *)strtok(line, "=\n");
00413 if(token=(char *)strtok(NULL, "\n")) {
00414 strcpy(xlogfile, token);
00415 }
00416 }
00417 else if(strstr(line, "MAIL=")) {
00418 token=(char *)strtok(line, "=\n");
00419 if(token=(char *)strtok(NULL, "\n")) {
00420 strcpy(mailto, token);
00421 }
00422 }
00423 else if(strstr(line, "NOOP=")) {
00424 token=(char *)strtok(line, "=\n");
00425 if(token=(char *)strtok(NULL, "\n")) {
00426 noopflg=atoi(token);
00427 }
00428 }
00429 else if(strstr(line, "USER=")) {
00430 token=(char *)strtok(line, "=\n");
00431 if(token=(char *)strtok(NULL, "\n")) {
00432 strcpy(userrun, token);
00433 }
00434 }
00435 else if(strstr(line, "NORUN_START=")) {
00436 token=(char *)strtok(line, "=\n");
00437 if(token=(char *)strtok(NULL, "\n")) {
00438 norun_start=atoi(token);
00439 }
00440 }
00441 else if(strstr(line, "NORUN_STOP=")) {
00442 token=(char *)strtok(line, "=\n");
00443 if(token=(char *)strtok(NULL, "\n")) {
00444 norun_stop=atoi(token);
00445 }
00446 }
00447 }
00448 if(norun_stop < norun_start) {
00449 write_log("Error in config file %s\nNORUN_STOP < NORUN_START\n", cfgfile);
00450 write_log("Proceeding without NORUN interval...\n");
00451 norun_start = norun_stop = 0;
00452 }
00453 fclose(cfgfp);
00454 if(num_sets_in_cfg > MAXSUMSETS) {
00455 write_log("***Error in config file %s. Too many SUM sets\n", cfgfile);
00456 exit(1);
00457 }
00458 }
00459
00460 void get_cmd(int argc, char *argv[])
00461 {
00462 int c;
00463
00464 while(--argc > 0 && (*++argv)[0] == '-') {
00465 while((c = *++argv[0]))
00466 switch(c) {
00467 case 'd':
00468 debugflg=1;
00469 break;
00470 case 's':
00471 sim=1;
00472 break;
00473 default:
00474 break;
00475 }
00476 }
00477 if(argc != 2) {
00478 printf("\nERROR: sum_rm must be call with dbname and timestamp\n");
00479 exit(1);
00480 }
00481 else {
00482 dbname = argv[0];
00483 timetag = argv[1];
00484 }
00485 }
00486
00487
00488
00489 void setup()
00490 {
00491 FILE *fplog, *asfp;
00492 char cmd[MAX_STR], lfile[MAX_STR], line[MAX_STR];
00493 char target[80], gfile[MAX_STR], assignfile[80], spartname[80];
00494 int i, j, pid, sumn, no_rm;
00495 char *cptr;
00496
00497 gethostname(thishost,MAX_STR);
00498 cptr = index(thishost, '.');
00499 if(cptr) *cptr = (char)NULL;
00500 if(!(username = (char *)getenv("USER"))) username = "nouser";
00501 get_cfg();
00502 pid = getppid();
00503 sprintf(logname, "%s_0.%s.%d", xlogfile, timetag, logvers);
00504 open_log(logname);
00505 printk_set(write_log, write_log);
00506 write_log("\n\n## %s for %s pid = %d ##\n",
00507 mod_name, username, pid);
00508 if(!strcmp(username, userrun))
00509 write_log("You are configured as the active user\n");
00510 else
00511 write_log("You are NOT configured as the active user = %s\n", userrun);
00512 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
00513 signal(SIGINT, sighandler);
00514 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
00515 signal(SIGTERM, sighandler);
00516 signal(SIGUSR1, &usr1_sig);
00517
00518
00519
00520
00521
00522
00523
00524
00525
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547 #ifdef NETDRMS_BUILD
00548
00549
00550
00551 pid_t parent = getppid();
00552 pid_t child = getpid();
00553 char pidSupp[PATH_MAX];
00554 FILE *fptr = NULL;
00555
00556 snprintf(pidSupp, sizeof(pidSupp), "/tmp/pidSupp.%d.log", parent);
00557 if ((fptr = fopen(pidSupp, "w")) == NULL)
00558 {
00559 write_log("sum_rm: Unable to open supplementary pid file %s.\n", pidSupp);
00560 exit(1);
00561 }
00562
00563 fprintf(fptr, "%d", child);
00564 fclose(fptr);
00565 fptr = NULL;
00566 #endif
00567
00568 active = 0;
00569 DS_ConnectDB(dbname);
00570 if(DS_PavailRequestEx())
00571 exit(1);
00572 for(i=0; i < MAX_PART-1; i++) {
00573
00574 ponoff[i] = ptab[i].pds_set_num;
00575 }
00576 sprintf(assignfile, "%s", RM_ASSIGN);
00577 if((asfp=fopen(assignfile, "r")) == NULL) {
00578 fprintf(stderr, "Can't open the assignment file %s\n", assignfile);
00579 exit(1);
00580 }
00581 else {
00582 i = 0;
00583
00584
00586 while(fgets(line, 128, asfp)) {
00587 write_log("%s", line);
00588 if(!strncmp(line, "#", 1)) {
00589 continue;
00590 }
00591 sscanf(line, "%s %d %d", spartname, &sumn, &no_rm);
00592 if(sumn != SUMN) continue;
00593
00594 for(j=0; j < MAX_PART-1; j++) {
00595 if(ptab[j].name == NULL) break;
00596 if(!strcmp(spartname, ptab[j].name)) ixptab[i++] = j;
00597 }
00598 ixptab[i] = -1;
00599 }
00600 fclose(asfp);
00601 }
00602 signal(SIGALRM, &alrm_sig);
00603 alarm(2);
00604 }
00605
00606
00607
00608
00609
00610 int main(int argc, char *argv[])
00611 {
00612 get_cmd(argc, argv);
00613 setup();
00614 while(1) {
00615 sleep(60);
00616 }
00617 }
00618
00619
00620 static char *datestring(void)
00621 {
00622 time_t t;
00623 char *str;
00624
00625 t = time(NULL);
00626 str = ctime(&t);
00627 str[19] = 0;
00628 return str+4;
00629 }
00630