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_1.newlog"
00067 #define RM_ASSIGN "/usr/local/logs/SUM/sum_rm_assign.txt"
00068
00069 int SUMN = 1;
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_1\n", datestring());
00169 write_log("sum_rm_1 will exit at its next opportunity\n");
00170 usr1_sig_flag = 1;
00171 if(!active) {
00172 write_log("%s sum_rm_1 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_1 exit\n", datestring());
00188 exit(0);
00189 }
00190 if((tstfp=fopen(NEWLOG_FILE, "r"))) {
00191 write_log("sum_rm_1: Close this log %d\n", logvers);
00192 fclose(tstfp);
00193 fclose(logfp);
00194 logvers++;
00195 sprintf(logname, "%s_1.%s.%d", xlogfile, timetag, logvers);
00196 open_log(logname);
00197 write_log("sum_rm_1: Start new log file %d\n", logvers);
00198 sprintf(cmd, "/bin/rm %s", NEWLOG_FILE);
00199 if(system(cmd)) {
00200 write_log("sum_rm_1: 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 for(j=0; j<MAX_PART-1; j++) {
00254 if(usr1_sig_flag) {
00255 write_log("%s sum_rm_1 exit from usr1_sig\n", datestring());
00256 exit(0);
00257 }
00258 i = ixptab[j];
00259 if(i == -1) break;
00260 pptr=(PART *)&ptab[i];
00261 if(pptr->name == NULL) break;
00262
00264
00265
00266
00267
00268 if(status = statvfs(pptr->name, &vfs)) {
00269 printk("Error %d on statvfs() for %s\n",status,pptr->name);
00270 }
00271 else {
00272 df_total = (double)vfs.f_blocks;
00273 df_total = df_total * (double)vfs.f_frsize;
00274 df_avail = (double)vfs.f_bavail;
00275 df_avail = df_avail * (double)vfs.f_bsize;
00276
00277
00278
00279
00280 if(pptr->pds_set_num != -2) {
00281 upercent = df_avail/df_total;
00282 if(upercent < 0.02) {
00283 if(ponoff[i] >= 0) {
00284 printk("Turning off full partition %s\n", pptr->name);
00285 SUMLIB_PavailOff(pptr->name);
00286 ponoff[i] = -1;
00287 partnchange = 1;
00288 }
00289 }
00290 else if(upercent >= 0.05) {
00291 if(ponoff[i] == -1) {
00292 printk("Turning on former full partition %s\n", pptr->name);
00293 SUMLIB_PavailOn(pptr->name, ptab[i].pds_set_prime);
00294 ponoff[i] = 0;
00295 partnchange = 1;
00296 }
00297 }
00298 }
00299 if(SUMLIB_PavailUpdate(pptr->name, df_avail))
00300 printk("Err: SUMLIB_PavailUpdate(%s, %e, ...)\n",
00301 pptr->name, df_avail);
00302 else
00303
00304
00305 max_free_set_need[i] = df_total * max_free_set_percent[0];
00306 max_free_set_current[i] = df_avail;
00307 if(max_free_set_current[i] < max_free_set_need[i]) {
00308 updated = 1;
00309 df_del = max_free_set_need[i] - max_free_set_current[i];
00310
00311 if (df_del < 0.01 * df_total) df_del = 0.01 * df_total;
00312 printk("%s Attempt to del %e bytes\n", pptr->name, df_del);
00313 DS_RmDoX(pptr->name, df_del);
00314 }
00315 }
00316 }
00317 if(partnchange) {
00318 if(system("sumrepartn -y")) {
00319 printk("Error on system() call to resync mem w/db\n");
00320 }
00321 }
00322 return(updated);
00323 }
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349 void get_cfg()
00350 {
00351 FILE *cfgfp;
00352 char *token;
00353 char line[256], cfgfile[256], datestrhr[16];
00354 int i, num;
00355
00356 gettimeofday(&tvalr, NULL);
00357 t_ptr = localtime((const time_t *)&tvalr);
00358 sprintf(datestr, "%s", asctime(t_ptr));
00359 dptr = &datestr[4];
00360 datestr[19] = (char)NULL;
00361 strncpy(datestrhr, &datestr[11], 2);
00362 datestrhr[2] = (char)NULL;
00363 datehr = atoi(datestrhr);
00364 noopflg = 0;
00365 norun_start = norun_stop = 0;
00366 sleep_sec = 300;
00367 for(i=0; i < MAXSUMSETS; i++) {
00368
00369 max_free_set_percent[i] = 3.0/100.0;
00370 }
00371 num_sets_in_cfg = MAXSUMSETS;
00372 strcpy(xlogfile, "/tmp/sum_rm_1.log");
00373 strcpy(mailto, "sys2@solar2");
00374 strcpy(userrun, "production");
00375 #ifdef __LOCALIZED_DEFS__
00376 sprintf (cfgfile, "%s/sum_rm.cfg", SUMLOG_BASEDIR);
00377 #else
00378 sprintf(cfgfile, "%s.%s", CFG_FILE, dbname);
00379 #endif
00380 if(!(cfgfp=fopen(cfgfile, "r"))) {
00381 write_log("Can't open config file %s. Using defaults...\n", cfgfile);
00382 return;
00383 }
00384 num_sets_in_cfg = 0;
00385 while(fgets(line, 256, cfgfp)) {
00386 if(line[0] == '#' || line[0] == '\n') continue;
00387 if(strstr(line, "SLEEP=")) {
00388 token=(char *)strtok(line, "=\n");
00389 if(token=(char *)strtok(NULL, "\n")) {
00390 sleep_sec=atoi(token);
00391 }
00392 }
00393 else if(strstr(line, "PART_PERCENT_FREE")) {
00394 token=(char *)strtok(line, "=\n");
00395
00396 if(token=(char *)strtok(NULL, "\n")) {
00397 num=atoi(token);
00398
00399
00400 max_free_set_percent[0] = (double)num/100.0;
00401 }
00402 }
00403 else if(strstr(line, "LOG=")) {
00404 token=(char *)strtok(line, "=\n");
00405 if(token=(char *)strtok(NULL, "\n")) {
00406 strcpy(xlogfile, token);
00407 }
00408 }
00409 else if(strstr(line, "MAIL=")) {
00410 token=(char *)strtok(line, "=\n");
00411 if(token=(char *)strtok(NULL, "\n")) {
00412 strcpy(mailto, token);
00413 }
00414 }
00415 else if(strstr(line, "NOOP=")) {
00416 token=(char *)strtok(line, "=\n");
00417 if(token=(char *)strtok(NULL, "\n")) {
00418 noopflg=atoi(token);
00419 }
00420 }
00421 else if(strstr(line, "USER=")) {
00422 token=(char *)strtok(line, "=\n");
00423 if(token=(char *)strtok(NULL, "\n")) {
00424 strcpy(userrun, token);
00425 }
00426 }
00427 else if(strstr(line, "NORUN_START=")) {
00428 token=(char *)strtok(line, "=\n");
00429 if(token=(char *)strtok(NULL, "\n")) {
00430 norun_start=atoi(token);
00431 }
00432 }
00433 else if(strstr(line, "NORUN_STOP=")) {
00434 token=(char *)strtok(line, "=\n");
00435 if(token=(char *)strtok(NULL, "\n")) {
00436 norun_stop=atoi(token);
00437 }
00438 }
00439 }
00440 if(norun_stop < norun_start) {
00441 write_log("Error in config file %s\nNORUN_STOP < NORUN_START\n", cfgfile);
00442 write_log("Proceeding without NORUN interval...\n");
00443 norun_start = norun_stop = 0;
00444 }
00445 fclose(cfgfp);
00446 if(num_sets_in_cfg > MAXSUMSETS) {
00447 write_log("***Error in config file %s. Too many SUM sets\n", cfgfile);
00448 exit(1);
00449 }
00450 }
00451
00452 void get_cmd(int argc, char *argv[])
00453 {
00454 int c;
00455
00456 while(--argc > 0 && (*++argv)[0] == '-') {
00457 while((c = *++argv[0]))
00458 switch(c) {
00459 case 'd':
00460 debugflg=1;
00461 break;
00462 case 's':
00463 sim=1;
00464 break;
00465 default:
00466 break;
00467 }
00468 }
00469 if(argc != 2) {
00470 printf("\nERROR: sum_rm must be call with dbname and timestamp\n");
00471 exit(1);
00472 }
00473 else {
00474 dbname = argv[0];
00475 timetag = argv[1];
00476 }
00477 }
00478
00479
00480
00481 void setup()
00482 {
00483 FILE *fplog, *asfp;
00484 char cmd[MAX_STR], lfile[MAX_STR], line[MAX_STR];
00485 char target[80], gfile[MAX_STR], assignfile[80], spartname[80];
00486 int i, j, pid, sumn, no_rm;
00487 char *cptr;
00488
00489 gethostname(thishost,MAX_STR);
00490 cptr = index(thishost, '.');
00491 if(cptr) *cptr = (char)NULL;
00492 if(!(username = (char *)getenv("USER"))) username = "nouser";
00493 get_cfg();
00494 pid = getppid();
00495 sprintf(logname, "%s_1.%s.%d", xlogfile, timetag, logvers);
00496 open_log(logname);
00497 printk_set(write_log, write_log);
00498 write_log("\n\n## %s for %s pid = %d ##\n",
00499 mod_name, username, pid);
00500 if(!strcmp(username, userrun))
00501 write_log("You are configured as the active user\n");
00502 else
00503 write_log("You are NOT configured as the active user = %s\n", userrun);
00504 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
00505 signal(SIGINT, sighandler);
00506 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
00507 signal(SIGTERM, sighandler);
00508 signal(SIGUSR1, &usr1_sig);
00509
00510
00511
00512
00513
00514
00515
00516
00517
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539 #ifdef NETDRMS_BUILD
00540
00541
00542
00543 pid_t parent = getppid();
00544 pid_t child = getpid();
00545 char pidSupp[PATH_MAX];
00546 FILE *fptr = NULL;
00547
00548 snprintf(pidSupp, sizeof(pidSupp), "/tmp/pidSupp.%d.log", parent);
00549 if ((fptr = fopen(pidSupp, "w")) == NULL)
00550 {
00551 write_log("sum_rm: Unable to open supplementary pid file %s.\n", pidSupp);
00552 exit(1);
00553 }
00554
00555 fprintf(fptr, "%d", child);
00556 fclose(fptr);
00557 fptr = NULL;
00558 #endif
00559
00560 active = 0;
00561 DS_ConnectDB(dbname);
00562 if(DS_PavailRequestEx())
00563 exit(1);
00564 for(i=0; i < MAX_PART-1; i++) {
00565 ponoff[i] = ptab[i].pds_set_num;
00566 }
00567 sprintf(assignfile, "%s", RM_ASSIGN);
00568 if((asfp=fopen(assignfile, "r")) == NULL) {
00569 fprintf(stderr, "Can't open the assignment file %s\n", assignfile);
00570 exit(1);
00571 }
00572 else {
00573 i = 0;
00574
00575
00577 while(fgets(line, 128, asfp)) {
00578 write_log("%s", line);
00579 if(!strncmp(line, "#", 1)) {
00580 continue;
00581 }
00582 sscanf(line, "%s %d %d", spartname, &sumn, &no_rm);
00583 if(sumn != SUMN) continue;
00584
00585 for(j=0; j < MAX_PART-1; j++) {
00586 if(ptab[j].name == NULL) break;
00587 if(!strcmp(spartname, ptab[j].name)) ixptab[i++] = j;
00588 }
00589 ixptab[i] = -1;
00590 }
00591 fclose(asfp);
00592 }
00593 signal(SIGALRM, &alrm_sig);
00594 alarm(2);
00595 }
00596
00597
00598
00599
00600
00601 int main(int argc, char *argv[])
00602 {
00603 get_cmd(argc, argv);
00604 setup();
00605 while(1) {
00606 sleep(60);
00607 }
00608 }
00609
00610
00611 static char *datestring(void)
00612 {
00613 time_t t;
00614 char *str;
00615
00616 t = time(NULL);
00617 str = ctime(&t);
00618 str[19] = 0;
00619 return str+4;
00620 }
00621