예제 #1
0
int main(int argc, char** argv)
{
  setlocale(LC_ALL, "");
  TBSYS_LOGGER.setFileName("ups_admin.log", true);
  TBSYS_LOG(INFO, "ups_admin start==================================================");
  ob_init_memory_pool();

  CmdLineParam clp;
  parse_cmd_line(argc, argv, clp);

  timeout = clp.timeout?: timeout;

  MockClient client;
  init_mock_client(clp.serv_addr, clp.serv_port, clp.login_type, client);

  PageArena<char> allocer;
  int rc = 0;

  if (0 == strcmp("apply", clp.cmd_type))
  {
    apply(clp.ini_fname, allocer, client);
  }
  else if (0 == strcmp("get_clog_status", clp.cmd_type))
  {
    get_clog_status(client);
  }
  else if (0 == strcmp("get_max_log_seq", clp.cmd_type))
  {
    get_max_log_seq(client);
  }
  else if (0 == strcmp("get_clog_cursor", clp.cmd_type))
  {
    get_clog_cursor(client);
  }
  else if (0 == strcmp("get_clog_master", clp.cmd_type))
  {
    get_clog_master(client);
  }
  else if (0 == strcmp("get_log_sync_delay_stat", clp.cmd_type))
  {
    get_log_sync_delay_stat(client);
  }
  else if (0 == strcmp("get", clp.cmd_type))
  {
    get(clp.ini_fname, allocer, client, clp.version_range,clp.expected_result_fname,
      clp.schema_fname);
  }
  else if (0 == strcmp("param_get", clp.cmd_type))
  {
    param_get(clp.ini_fname, client);
  }
  else if (0 == strcmp("scan", clp.cmd_type))
  {
    scan(clp.ini_fname, allocer, client, clp.version_range,clp.expected_result_fname,
      clp.schema_fname);
  }
  else if (0 == strcmp("total_scan", clp.cmd_type))
  {
    total_scan(clp.ini_fname, allocer, client, clp.version_range);
  }
  else if (0 == strcmp("minor_freeze", clp.cmd_type))
  {
    minor_freeze(client);
  }
  else if (0 == strcmp("major_freeze", clp.cmd_type))
  {
    major_freeze(client);
  }
  else if (0 == strcmp("fetch_schema", clp.cmd_type))
  {
    fetch_schema(client, clp.timestamp);
  }
  else if (0 == strcmp("get_sstable_range_list", clp.cmd_type))
  {
    get_sstable_range_list(client, clp.timestamp, clp.session_id);
  }
  else if (0 == strcmp("drop", clp.cmd_type))
  {
    drop(client);
  }
  else if (0 == strcmp("dump_memtable", clp.cmd_type))
  {
    dump_memtable(clp.ini_fname, client);
  }
  else if (0 == strcmp("dump_schemas", clp.cmd_type))
  {
    dump_schemas(client);
  }
  else if (0 == strcmp("force_fetch_schema", clp.cmd_type))
  {
    force_fetch_schema(client);
  }
  else if (0 == strcmp("reload_conf", clp.cmd_type))
  {
    reload_conf(clp.ini_fname, client);
  }
  else if (0 == strcmp("memory_watch", clp.cmd_type))
  {
    memory_watch(client);
  }
  else if (0 == strcmp("memory_limit", clp.cmd_type))
  {
    memory_limit(clp.memory_limit, clp.memtable_limit, client);
  }
  else if (0 == strcmp("priv_queue_conf", clp.cmd_type))
  {
    priv_queue_conf(clp.priv_queue_conf, client);
  }
  else if (0 == strcmp("clear_active_memtable", clp.cmd_type))
  {
    clear_active_memtable(client);
  }
  else if (0 == strcmp("get_last_frozen_version", clp.cmd_type))
  {
    get_last_frozen_version(client);
  }
  else if (0 == strcmp("fetch_ups_stat_info", clp.cmd_type))
  {
    fetch_ups_stat_info(client);
  }
  else if (0 == strcmp("get_bloomfilter", clp.cmd_type))
  {
    get_bloomfilter(client, clp.timestamp);
  }
  else if (0 == strcmp("store_memtable", clp.cmd_type))
  {
    store_memtable(client, clp.timestamp);
  }
  else if (0 == strcmp("erase_sstable", clp.cmd_type))
  {
    erase_sstable(client);
  }
  else if (0 == strcmp("load_new_store", clp.cmd_type))
  {
    load_new_store(client);
  }
  else if (0 == strcmp("reload_all_store", clp.cmd_type))
  {
    reload_all_store(client);
  }
  else if (0 == strcmp("reload_store", clp.cmd_type))
  {
    reload_store(client, clp.timestamp);
  }
  else if (0 == strcmp("umount_store", clp.cmd_type))
  {
    umount_store(client, clp.ini_fname);
  }
  else if (0 == strcmp("force_report_frozen_version", clp.cmd_type))
  {
    force_report_frozen_version(client);
  }
  else if (0 == strcmp("switch_commit_log", clp.cmd_type))
  {
    switch_commit_log(client);
  }
  else if (0 == strcmp("get_table_time_stamp", clp.cmd_type))
  {
    get_table_time_stamp(client, clp.timestamp);
  }
  else if (0 == strcmp("disable_memtable_checksum", clp.cmd_type))
  {
    disable_memtable_checksum(client);
  }
  else if (0 == strcmp("enable_memtable_checksum", clp.cmd_type))
  {
    enable_memtable_checksum(client);
  }
  else if (0 == strcmp("immediately_drop_memtable", clp.cmd_type))
  {
    immediately_drop_memtable(client);
  }
  else if (0 == strcmp("delay_drop_memtable", clp.cmd_type))
  {
    delay_drop_memtable(client);
  }
  else if (0 == strcmp("minor_load_bypass", clp.cmd_type))
  {
    rc = minor_load_bypass(client);
  }
  else if (0 == strcmp("major_load_bypass", clp.cmd_type))
  {
    rc = major_load_bypass(client);
  }
  else if(0 == strcmp("list_sessions", clp.cmd_type))
  {
    client.list_sessions(timeout);
  }
  else if(0 == strcmp("kill_session", clp.cmd_type))
  {
    client.kill_session(timeout, clp.session_id);
  }
  else if (0 == strcmp("change_log_level", clp.cmd_type))
  {
    change_log_level(client, clp.log_level);
  }
  else if (0 == strcmp("get_slave_ups_info", clp.cmd_type))
  {
    get_slave_ups_info(client);
  }
  else if (0 == strcmp("print_scanner", clp.cmd_type))
  {
    print_scanner(clp.ini_fname);
  }
  else if (0 == strcmp("print_schema", clp.cmd_type))
  {
    print_schema(clp.ini_fname);
  }
  else if (0 == strcmp("sql_query", clp.cmd_type))
  {
    execute_sql(client, clp.sql_query);
  }
  else if (0 == strcmp("ups_show_sessions", clp.cmd_type))
  {
    ups_show_sessions(client);
  }
  else if (0 == strcmp("ups_kill_session", clp.cmd_type))
  {
    ups_kill_session(static_cast<uint32_t>(clp.session_id), client);
  }
  else
  {
    print_usage();
  }

  if (!clp.quickly_exit)
  {
    client.destroy();
    TBSYS_LOG(INFO, "ups_admin end==================================================");
    return rc;
  }
  else
  {
    TBSYS_LOG(INFO, "ups_admin killed==================================================");
    fflush(stdout);
    fflush(stderr);
    _exit(rc);
  }
}
예제 #2
0
파일: upsmon.c 프로젝트: ThomasKurz/nut
int main(int argc, char *argv[])
{
    const char	*prog = xbasename(argv[0]);
    int	i, cmd = 0, checking_flag = 0;

    printf("Network UPS Tools %s %s\n", prog, UPS_VERSION);

    /* if no configuration file is specified on the command line, use default */
    configfile = xmalloc(SMALLBUF);
    snprintf(configfile, SMALLBUF, "%s/upsmon.conf", confpath());
    configfile = xrealloc(configfile, strlen(configfile) + 1);

    run_as_user = xstrdup(RUN_AS_USER);

    while ((i = getopt(argc, argv, "+Dhic:f:pu:VK46")) != -1) {
        switch (i) {
        case 'c':
            if (!strncmp(optarg, "fsd", strlen(optarg)))
                cmd = SIGCMD_FSD;
            if (!strncmp(optarg, "stop", strlen(optarg)))
                cmd = SIGCMD_STOP;
            if (!strncmp(optarg, "reload", strlen(optarg)))
                cmd = SIGCMD_RELOAD;

            /* bad command name given */
            if (cmd == 0)
                help(argv[0]);
            break;
        case 'D':
            nut_debug_level++;
            break;
        case 'f':
            free(configfile);
            configfile = xstrdup(optarg);
            break;
        case 'h':
            help(argv[0]);
            break;
        case 'K':
            checking_flag = 1;
            break;
        case 'p':
            use_pipe = 0;
            break;
        case 'u':
            free(run_as_user);
            run_as_user = xstrdup(optarg);
            break;
        case 'V':
            /* just show the banner */
            exit(EXIT_SUCCESS);
        case '4':
            opt_af = AF_INET;
            break;
        case '6':
            opt_af = AF_INET6;
            break;
        default:
            help(argv[0]);
            break;
        }
    }

    if (cmd) {
        sendsignal(prog, cmd);
        exit(EXIT_SUCCESS);
    }

    /* otherwise, we are being asked to start.
     * so check if a previous instance is running by sending signal '0'
     * (Ie 'kill <pid> 0') */
    if (sendsignal(prog, 0) == 0) {
        printf("Fatal error: A previous upsmon instance is already running!\n");
        printf("Either stop the previous instance first, or use the 'reload' command.\n");
        exit(EXIT_FAILURE);
    }

    argc -= optind;
    argv += optind;

    open_syslog(prog);

    loadconfig();

    if (checking_flag)
        exit(check_pdflag());

    if (shutdowncmd == NULL)
        printf("Warning: no shutdown command defined!\n");

    /* we may need to get rid of a flag from a previous shutdown */
    if (powerdownflag != NULL)
        clear_pdflag();
    /* FIXME (else): POWERDOWNFLAG is not defined!!
     * => fallback to a default value */

    if (totalpv < minsupplies) {
        printf("\nFatal error: insufficient power configured!\n\n");

        printf("Sum of power values........: %d\n", totalpv);
        printf("Minimum value (MINSUPPLIES): %d\n", minsupplies);

        printf("\nEdit your upsmon.conf and change the values.\n");
        exit(EXIT_FAILURE);
    }

    if (nut_debug_level < 1) {
        background();
    } else {
        upsdebugx(1, "debug level is '%d'", nut_debug_level);
    }

    /* only do the pipe stuff if the user hasn't disabled it */
    if (use_pipe) {
        struct passwd	*new_uid = get_user_pwent(run_as_user);

        /* === root parent and unprivileged child split here === */
        start_pipe();

        /* write the pid file now, as we will soon lose root */
        writepid(prog);

        become_user(new_uid);
    } else {
        upslogx(LOG_INFO, "Warning: running as one big root process by request (upsmon -p)");

        writepid(prog);
    }

    /* prep our signal handlers */
    setup_signals();

    /* reopen the log for the child process */
    closelog();
    open_syslog(prog);

    while (exit_flag == 0) {
        utype_t	*ups;

        /* check flags from signal handlers */
        if (userfsd)
            forceshutdown();

        if (reload_flag)
            reload_conf();

        for (ups = firstups; ups != NULL; ups = ups->next)
            pollups(ups);

        recalc();

        /* make sure the parent hasn't died */
        if (use_pipe)
            check_parent();

        /* reap children that have exited */
        waitpid(-1, NULL, WNOHANG);

        sleep(sleepval);
    }

    upslogx(LOG_INFO, "Signal %d: exiting", exit_flag);
    upsmon_cleanup();

    exit(EXIT_SUCCESS);
}