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
00067 int stat_storage();
00068 void get_cfg();
00069 static char *datestring(void);
00070
00071 char mod_name[] = "sum_rm";
00072
00073 extern PART ptab[];
00074
00075 FILE *logfp;
00076 char thishost[MAX_STR];
00077 char *dbname;
00078 char *timetag;
00079 char *username;
00080 int active;
00081 int datehr;
00082 int num_sets_in_cfg;
00083 int debugflg, sim;
00084 struct timeval tvalr;
00085 struct tm *t_ptr;
00086 char datestr[32];
00087 char *dptr;
00088 int soi_errno = NO_ERROR;
00089
00090
00091
00092 int noopflg;
00093 int sleep_sec;
00094 int norun_start;
00095 int norun_stop;
00096 char xlogfile[256];
00097 char mailto[256];
00098 char userrun[256];
00099 double max_free_set_need[MAX_PART];
00100 double max_free_set_current[MAX_PART];
00101 double max_free_set_percent[MAXSUMSETS];
00102
00103
00104
00105 void open_log(char *filename)
00106 {
00107 if((logfp=fopen(filename, "w")) == NULL) {
00108 fprintf(stderr, "Can't open the log file %s for %s on %s\n",
00109 filename, mod_name, thishost);
00110 }
00111 }
00112
00113
00114
00115
00116 int write_log(const char *fmt, ...)
00117 {
00118 va_list args;
00119 char string[32768];
00120
00121 va_start(args, fmt);
00122 vsprintf(string, fmt, args);
00123 if(logfp) {
00124 fprintf(logfp, string);
00125 fflush(logfp);
00126 }
00127 else
00128 fprintf(stderr, string);
00129 va_end(args);
00130 return(0);
00131 }
00132
00133 void sighandler(sig)
00134 int sig;
00135 {
00136 if(sig == SIGTERM) {
00137 write_log("*** %s sum_rm got SIGTERM. Exiting.\n", datestring());
00138 DS_DisConnectDB();
00139 exit(1);
00140 }
00141 if(sig == SIGINT) {
00142 write_log("*** %s sum_rm got SIGINT. Exiting.\n", datestring());
00143 DS_DisConnectDB();
00144 exit(1);
00145 }
00146 write_log("*** %s sum_rm got an illegal signal %d, ignoring...\n",
00147 datestring(), sig);
00148 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
00149 signal(SIGINT, sighandler);
00150 if (signal(SIGALRM, SIG_IGN) != SIG_IGN)
00151 signal(SIGALRM, sighandler);
00152 }
00153
00154
00155
00156 void alrm_sig(int sig)
00157 {
00158 int norunflg, update;
00159 double bytesdeleted, availstore;
00160
00161 write_log("Called alrm_sig()\n");
00162 signal(SIGALRM, &alrm_sig);
00163 if(!active) {
00164 active = 1;
00165 update = 0;
00166 get_cfg();
00167 write_log("%s\n", dptr);
00168 if(noopflg) {
00169 write_log("NOOP\n");
00170 } else {
00171 norunflg = 0;
00172 if(norun_start <= datehr) {
00173 norunflg = 1;
00174 }
00175 if(norun_stop <= datehr) {
00176 norunflg = 0;
00177 }
00178 if(norunflg) {
00179 write_log("NORUN interval active\n");
00180 } else {
00181 if(strcmp(username, userrun)) {
00182 write_log("Current user %s is not the configured user %s\n",
00183 username, userrun);
00184 } else {
00185 update = stat_storage();
00186 }
00187 }
00188 }
00189 active = 0;
00190 }
00191 alarm(sleep_sec);
00192 }
00193
00194
00195
00196
00197
00198
00199
00200
00201 int stat_storage()
00202 {
00203 PART *pptr;
00204 int i, status;
00205 int updated = 0;
00206 double df_avail, df_total, df_del, total, upercent;
00207 struct statvfs vfs;
00208
00209 for(i=0; i<MAX_PART-1; i++) {
00210 pptr=(PART *)&ptab[i];
00211 if(pptr->name == NULL) break;
00212 if(status = statvfs(pptr->name, &vfs)) {
00213 printk("Error %d on statvfs() for %s\n",status,pptr->name);
00214 }
00215 else {
00216 df_total = (double)vfs.f_blocks;
00217 df_total = df_total * (double)vfs.f_frsize;
00218 df_avail = (double)vfs.f_bavail;
00219 df_avail = df_avail * (double)vfs.f_bsize;
00220
00221
00222
00223
00224 upercent = df_avail/df_total;
00225 if(upercent < 0.01) {
00226 printk("Turning off full partition %s\n", pptr->name);
00227 SUMLIB_PavailOff(pptr->name);
00228 }
00229 if(SUMLIB_PavailUpdate(pptr->name, df_avail))
00230 printk("Err: SUMLIB_PavailUpdate(%s, %e, ...)\n",
00231 pptr->name, df_avail);
00232 else
00233
00234
00235 max_free_set_need[i] = df_total * max_free_set_percent[0];
00236 max_free_set_current[i] = df_avail;
00237 if(max_free_set_current[i] < max_free_set_need[i]) {
00238 updated = 1;
00239 df_del = max_free_set_need[i] - max_free_set_current[i];
00240 printk("%s Attempt to del %e bytes\n", pptr->name, df_del);
00241 DS_RmDoX(pptr->name, df_del);
00242 }
00243 }
00244 }
00245 return(updated);
00246 }
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272 void get_cfg()
00273 {
00274 FILE *cfgfp;
00275 char *token;
00276 char line[256], cfgfile[256], datestrhr[16];
00277 int i, num;
00278
00279 gettimeofday(&tvalr, NULL);
00280 t_ptr = localtime((const time_t *)&tvalr);
00281 sprintf(datestr, "%s", asctime(t_ptr));
00282 dptr = &datestr[4];
00283 datestr[19] = (char)NULL;
00284 strncpy(datestrhr, &datestr[11], 2);
00285 datestrhr[2] = (char)NULL;
00286 datehr = atoi(datestrhr);
00287 noopflg = 0;
00288 norun_start = norun_stop = 0;
00289 sleep_sec = 300;
00290 for(i=0; i < MAXSUMSETS; i++) {
00291
00292 max_free_set_percent[i] = 3.0/100.0;
00293 }
00294 num_sets_in_cfg = MAXSUMSETS;
00295 strcpy(xlogfile, "/tmp/sum_rm.log");
00296 strcpy(mailto, "sys2@solar2");
00297 strcpy(userrun, "production");
00298 #ifdef __LOCALIZED_DEFS__
00299 sprintf (cfgfile, "%s/sum_rm.cfg", SUMLOG_BASEDIR);
00300 #else
00301 sprintf(cfgfile, "%s.%s", CFG_FILE, dbname);
00302 #endif
00303 if(!(cfgfp=fopen(cfgfile, "r"))) {
00304 write_log("Can't open config file %s. Using defaults...\n", cfgfile);
00305 return;
00306 }
00307 num_sets_in_cfg = 0;
00308 while(fgets(line, 256, cfgfp)) {
00309 if(line[0] == '#' || line[0] == '\n') continue;
00310 if(strstr(line, "SLEEP=")) {
00311 token=(char *)strtok(line, "=\n");
00312 if(token=(char *)strtok(NULL, "\n")) {
00313 sleep_sec=atoi(token);
00314 }
00315 }
00316 else if(strstr(line, "PART_PERCENT_FREE")) {
00317 token=(char *)strtok(line, "=\n");
00318
00319 if(token=(char *)strtok(NULL, "\n")) {
00320 num=atoi(token);
00321
00322
00323 max_free_set_percent[0] = (double)num/100.0;
00324 }
00325 }
00326 else if(strstr(line, "LOG=")) {
00327 token=(char *)strtok(line, "=\n");
00328 if(token=(char *)strtok(NULL, "\n")) {
00329 strcpy(xlogfile, token);
00330 }
00331 }
00332 else if(strstr(line, "MAIL=")) {
00333 token=(char *)strtok(line, "=\n");
00334 if(token=(char *)strtok(NULL, "\n")) {
00335 strcpy(mailto, token);
00336 }
00337 }
00338 else if(strstr(line, "NOOP=")) {
00339 token=(char *)strtok(line, "=\n");
00340 if(token=(char *)strtok(NULL, "\n")) {
00341 noopflg=atoi(token);
00342 }
00343 }
00344 else if(strstr(line, "USER=")) {
00345 token=(char *)strtok(line, "=\n");
00346 if(token=(char *)strtok(NULL, "\n")) {
00347 strcpy(userrun, token);
00348 }
00349 }
00350 else if(strstr(line, "NORUN_START=")) {
00351 token=(char *)strtok(line, "=\n");
00352 if(token=(char *)strtok(NULL, "\n")) {
00353 norun_start=atoi(token);
00354 }
00355 }
00356 else if(strstr(line, "NORUN_STOP=")) {
00357 token=(char *)strtok(line, "=\n");
00358 if(token=(char *)strtok(NULL, "\n")) {
00359 norun_stop=atoi(token);
00360 }
00361 }
00362 }
00363 if(norun_stop < norun_start) {
00364 write_log("Error in config file %s\nNORUN_STOP < NORUN_START\n", cfgfile);
00365 write_log("Proceeding without NORUN interval...\n");
00366 norun_start = norun_stop = 0;
00367 }
00368 fclose(cfgfp);
00369 if(num_sets_in_cfg > MAXSUMSETS) {
00370 write_log("***Error in config file %s. Too many SUM sets\n", cfgfile);
00371 exit(1);
00372 }
00373 }
00374
00375 void get_cmd(int argc, char *argv[])
00376 {
00377 int c;
00378
00379 while(--argc > 0 && (*++argv)[0] == '-') {
00380 while((c = *++argv[0]))
00381 switch(c) {
00382 case 'd':
00383 debugflg=1;
00384 break;
00385 case 's':
00386 sim=1;
00387 break;
00388 default:
00389 break;
00390 }
00391 }
00392 if(argc != 2) {
00393 printf("\nERROR: sum_rm must be call with dbname and timestamp\n");
00394 exit(1);
00395 }
00396 else {
00397 dbname = argv[0];
00398 timetag = argv[1];
00399 }
00400 }
00401
00402
00403
00404 void setup()
00405 {
00406 FILE *fplog;
00407 char logname[MAX_STR], cmd[MAX_STR], lfile[MAX_STR], line[MAX_STR];
00408 char target[80], gfile[MAX_STR];
00409 int i, pid;
00410 char *cptr;
00411
00412 gethostname(thishost,MAX_STR);
00413 cptr = index(thishost, '.');
00414 if(cptr) *cptr = (char)NULL;
00415 if(!(username = (char *)getenv("USER"))) username = "nouser";
00416 get_cfg();
00417 pid = getppid();
00418 sprintf(logname, "%s.%s", xlogfile, timetag);
00419 open_log(logname);
00420 printk_set(write_log, write_log);
00421 write_log("\n\n## %s for %s pid = %d ##\n",
00422 mod_name, username, pid);
00423 if(!strcmp(username, userrun))
00424 write_log("You are configured as the active user\n");
00425 else
00426 write_log("You are NOT configured as the active user = %s\n", userrun);
00427 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
00428 signal(SIGINT, sighandler);
00429 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
00430 signal(SIGTERM, sighandler);
00431
00432
00433 sprintf(lfile, "/tmp/sum_rm.%d.log", pid);
00434 sprintf(target, " sum_rm %s", dbname);
00435
00436 sprintf(gfile, "/tmp/cmdgrep.%d.log", pid);
00437 sprintf(cmd, "ps -ef | grep \"%s\" 1> %s", target, gfile);
00438 system(cmd);
00439
00440 sprintf(cmd, "cat %s | wc -l 1> %s", gfile, lfile);
00441 write_log("cmd: %s\n", cmd);
00442 if(system(cmd)) {
00443 write_log("sum_rm: Can't execute %s.\n", cmd);
00444 exit(1);
00445 }
00446 if((fplog=fopen(lfile, "r")) == NULL) {
00447 write_log("sum_rm: Can't open cmd log file %s\n", lfile);
00448 exit(1);
00449 }
00450 while(fgets(line, 128, fplog)) {
00451 i = atoi(line);
00452 if(i > 3) {
00453 write_log("Can't run more than 1 sum_rm for db=%s\n", dbname);
00454 exit(1);
00455 }
00456 }
00457 fclose(fplog);
00458
00459 #ifdef NETDRMS_BUILD
00460
00461
00462
00463 pid_t parent = getppid();
00464 pid_t child = getpid();
00465 char pidSupp[PATH_MAX];
00466 FILE *fptr = NULL;
00467
00468 snprintf(pidSupp, sizeof(pidSupp), "/tmp/pidSupp.%d.log", parent);
00469 if ((fptr = fopen(pidSupp, "w")) == NULL)
00470 {
00471 write_log("sum_rm: Unable to open supplementary pid file %s.\n", pidSupp);
00472 exit(1);
00473 }
00474
00475 fprintf(fptr, "%d", child);
00476 fclose(fptr);
00477 fptr = NULL;
00478 #endif
00479 active = 0;
00480 DS_ConnectDB(dbname);
00481 if(DS_PavailRequest())
00482 exit(1);
00483 signal(SIGALRM, &alrm_sig);
00484 alarm(2);
00485 }
00486
00487
00488
00489
00490
00491 int main(int argc, char *argv[])
00492 {
00493 get_cmd(argc, argv);
00494 setup();
00495 while(1) {
00496 sleep(60);
00497 }
00498 }
00499
00500
00501 static char *datestring(void)
00502 {
00503 time_t t;
00504 char *str;
00505
00506 t = time(NULL);
00507 str = ctime(&t);
00508 str[19] = 0;
00509 return str+4;
00510 }
00511