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