int checkcapacity() { char statDevicePath[50]; strcpy(statDevicePath,DISK_PATH); if(trim_last_slash(statDevicePath)==STAT_NOK) { statinfo("statDevicePath pattern error:%s",statDevicePath); return STAT_NOK; } int pcent = statDevice(statDevicePath); if(pcent < 0) {//statDevice error, ignore checking capacity process. retry next run return STAT_OK; } if(pcent >= SPACE_THRESHOLD) { stevedebug("pcent:%d exceed threashold %d delete a file every cam",pcent,SPACE_THRESHOLD); return STAT_NOK;//exceed threashold } stevedebug("pcent:%d capacity is ok",pcent); return STAT_OK; }
int readconfig() { spacechecker._time_duration=TIME_DURATION; spacechecker._space_threashold=SPACE_THRESHOLD; strcpy(spacechecker._file_expired_checked_path, DISK_PATH); if(trim_last_slash(spacechecker._file_expired_checked_path)==STAT_NOK) { statinfo("_file_expired_checked_path pattern error:%s",spacechecker._file_expired_checked_path); return STAT_NOK; } stevedebug("_file_expired_checked_path:%s",spacechecker._file_expired_checked_path); return STAT_OK; }
int main(int argc, char **argv) { int c; char *nodename = NULL; /* GTM Proxy nodename */ progname = "gtm_ctl"; /* * save argv[0] so do_start() can look for the gtm if necessary. we * don't look for gtm here because in many cases we won't need it. */ argv0 = argv[0]; umask(077); /* support --help and --version even if invoked as root */ if (argc > 1) { if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0) { do_help(); exit(0); } if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0) { puts("gtm_ctl (Postgres-XC) " PGXC_VERSION); exit(0); } } /* * Disallow running as root, to forestall any possible security holes. */ if (geteuid() == 0) { write_stderr(_("%s: cannot be run as root\n" "Please log in (using, e.g., \"su\") as the " "(unprivileged) user that will\n" "own the server process.\n"), progname); exit(1); } /* * 'Action' can be before or after args so loop over both. Some * getopt_long() implementations will reorder argv[] to place all flags * first (GNU?), but we don't rely on it. Our /port version doesn't do * that. */ optind = 1; /* process command-line options */ while (optind < argc) { while ((c = getopt(argc, argv, "D:i:l:m:o:p:t:wWZ:C:")) != -1) { switch (c) { case 'D': { char *env_var = pg_malloc(strlen(optarg) + 9); gtmdata_D = xstrdup(optarg); canonicalize_path(gtmdata_D); snprintf(env_var, strlen(optarg) + 9, "GTMDATA=%s", gtmdata_D); putenv(env_var); /* * We could pass GTMDATA just in an environment * variable but we do -D too for clearer gtm * 'ps' display */ gtmdata_opt = (char *) pg_malloc(strlen(gtmdata_D) + 8); snprintf(gtmdata_opt, strlen(gtmdata_D) + 8, "-D \"%s\" ", gtmdata_D); break; } case 'i': nodename = strdup(optarg); break; case 'l': log_file = xstrdup(optarg); break; case 'm': set_mode(optarg); break; case 'o': gtm_opts = xstrdup(optarg); break; case 'p': gtm_path = xstrdup(optarg); canonicalize_path(gtm_path); break; case 't': wait_seconds = atoi(optarg); break; case 'C': control_file = xstrdup(optarg); break; case 'w': do_wait = true; wait_set = true; break; case 'W': do_wait = false; wait_set = true; break; case 'Z': gtm_app = xstrdup(optarg); if (strcmp(gtm_app,"gtm_proxy") != 0 && strcmp(gtm_app,"gtm_standby") != 0 && strcmp(gtm_app,"gtm") != 0) { write_stderr(_("%s: %s launch name set not correct\n"), progname, gtm_app); do_advice(); exit(1); } break; default: /* getopt_long already issued a suitable error message */ do_advice(); exit(1); } } /* Process an action */ if (optind < argc) { if (ctl_command != NO_COMMAND) { write_stderr(_("%s: too many command-line arguments (first is \"%s\")\n"), progname, argv[optind]); do_advice(); exit(1); } if (strcmp(argv[optind], "start") == 0) ctl_command = START_COMMAND; else if (strcmp(argv[optind], "stop") == 0) ctl_command = STOP_COMMAND; else if (strcmp(argv[optind], "promote") == 0) ctl_command = PROMOTE_COMMAND; else if (strcmp(argv[optind], "restart") == 0) ctl_command = RESTART_COMMAND; else if (strcmp(argv[optind], "status") == 0) ctl_command = STATUS_COMMAND; else if (strcmp(argv[optind], "reconnect") == 0) ctl_command = RECONNECT_COMMAND; else { write_stderr(_("%s: unrecognized operation mode \"%s\"\n"), progname, argv[optind]); do_advice(); exit(1); } optind++; } } /* * Take care of the control file (-C Option) */ if (control_file) { char ctrl_path[MAXPGPATH+1]; char C_opt_path[MAXPGPATH+1]; char bkup_path[MAXPGPATH+1]; FILE *f1, *f2; int c; if (!gtmdata_D) { write_stderr(_("No -D option specified.\n")); exit(1); } if ((strcmp(gtm_app, "gtm") != 0) && (strcmp(gtm_app, "gtm_master") != 0)) { write_stderr(_("-C option is valid only for gtm.\n")); exit(1); } /* If there's already a control file, backup it to *.bak */ trim_last_slash(gtmdata_D); snprintf(ctrl_path, MAXPGPATH, "%s/%s", gtmdata_D, GTM_CONTROL_FILE); if ((f1 = fopen(ctrl_path, "r"))) { snprintf(bkup_path, MAXPGPATH, "%s/%s.bak", gtmdata_D, GTM_CONTROL_FILE); if (!(f2 = fopen(bkup_path, "w"))) { fclose(f1); write_stderr(_("Cannot open backup file, %s/%s.bak, %s\n"), gtmdata_D, GTM_CONTROL_FILE, strerror(errno)); exit(1); } while ((c = getc(f1)) != EOF) putc(c, f2); fclose(f1); fclose(f2); } /* Copy specified control file. */ snprintf(C_opt_path, MAXPGPATH, "%s/%s", gtmdata_D, control_file); if (!(f1 = fopen(ctrl_path, "w"))) { write_stderr(_("Cannot oopen control file, %s, %s\n"), ctrl_path, strerror(errno)); exit(1); } if (!(f2 = fopen(C_opt_path, "r"))) { fclose(f1); write_stderr(_("Cannot open -C option file, %s, %s\n"), C_opt_path, strerror(errno)); exit(1); } while ((c = getc(f2)) != EOF) putc(c, f1); fclose(f1); fclose(f2); } if (ctl_command == NO_COMMAND) { write_stderr(_("%s: no operation specified\n"), progname); do_advice(); exit(1); } gtm_data = getenv("GTMDATA"); if (gtm_data) { gtm_data = xstrdup(gtm_data); canonicalize_path(gtm_data); } if (!gtm_data) { write_stderr("%s: no GTM/GTM Proxy directory specified \n", progname); do_advice(); exit(1); } /* * pid files of gtm and gtm proxy are named differently * -Z option has also to be set for STOP_COMMAND * or gtm_ctl will not be able to find the correct pid_file */ if (!gtm_app) { write_stderr("%s: no launch option not specified\n", progname); do_advice(); exit(1); } if (strcmp(gtm_app,"gtm_proxy") != 0 && strcmp(gtm_app, "gtm_standby") != 0 && strcmp(gtm_app,"gtm") != 0) { write_stderr(_("%s: launch option incorrect\n"), progname); do_advice(); exit(1); } /* Check if GTM Proxy ID is set, this is not necessary when stopping */ if (ctl_command == START_COMMAND || ctl_command == RESTART_COMMAND) { /* Rebuild option string to include Proxy ID */ if (strcmp(gtm_app, "gtm_proxy") == 0) { gtmdata_opt = (char *) pg_realloc(gtmdata_opt, strlen(gtmdata_opt) + 9); if (nodename) sprintf(gtmdata_opt, "%s -i %s ", gtmdata_opt, nodename); else sprintf(gtmdata_opt, "%s ", gtmdata_opt); } } if (!wait_set) { switch (ctl_command) { case RESTART_COMMAND: case START_COMMAND: case PROMOTE_COMMAND: case STATUS_COMMAND: do_wait = false; break; case STOP_COMMAND: do_wait = true; break; default: break; } } /* Build strings for pid file and option file */ if (strcmp(gtm_app,"gtm_proxy") == 0) { snprintf(pid_file, MAXPGPATH, "%s/gtm_proxy.pid", gtm_data); snprintf(gtmopts_file, MAXPGPATH, "%s/gtm_proxy.opts", gtm_data); snprintf(conf_file, MAXPGPATH, "%s/gtm_proxy.conf", gtm_data); } else if (strcmp(gtm_app,"gtm") == 0) { snprintf(pid_file, MAXPGPATH, "%s/gtm.pid", gtm_data); snprintf(gtmopts_file, MAXPGPATH, "%s/gtm.opts", gtm_data); snprintf(conf_file, MAXPGPATH, "%s/gtm.conf", gtm_data); } else if (strcmp(gtm_app,"gtm_standby") == 0) { snprintf(pid_file, MAXPGPATH, "%s/gtm.pid", gtm_data); snprintf(gtmopts_file, MAXPGPATH, "%s/gtm.opts", gtm_data); snprintf(conf_file, MAXPGPATH, "%s/gtm.conf", gtm_data); } if (ctl_command==STATUS_COMMAND) gtm_opts = xstrdup("-c"); switch (ctl_command) { case START_COMMAND: do_start(); break; case STOP_COMMAND: do_stop(); break; case PROMOTE_COMMAND: do_promote(); break; case RESTART_COMMAND: do_restart(); break; case STATUS_COMMAND: do_status(); break; case RECONNECT_COMMAND: do_reconnect(); break; default: break; } exit(0); }