Exemplo n.º 1
0
int main(int argc, char* argv[])
{
  int32_t i;
  int ret = TFS_SUCCESS;
  bool directly = false;
  bool set_log_level = false;

  // analyze arguments
  while ((i = getopt(argc, argv, "s:r:k:l:nihv")) != EOF)
  {
    switch (i)
    {
      case 'n':
        set_log_level = true;
        break;
      case 's':
        nsip = optarg;
        break;
      case 'r':
        rc_addr = optarg;
        break;
      case 'k':
        krs_addr = optarg;
        break;
      case 'l':
        lrs_addr = optarg;
      case 'i':
        directly = true;
        break;
      case 'v':
      case 'h':
      default:
        usage(argv[0]);
        return TFS_ERROR;
    }
  }

  if (set_log_level)
  {
    TBSYS_LOGGER.setLogLevel("ERROR");
  }

  if (NULL == nsip && NULL == rc_addr & NULL == krs_addr && NULL == lrs_addr)
  {
    usage(argv[0]);
    return TFS_ERROR;
  }

  gstreamer.set_packet_factory(&gfactory);
  NewClientManager::get_instance().initialize(&gfactory, &gstreamer);

  if (lrs_addr != NULL)
  {
    if (rc_addr == NULL)
    {
      usage(argv[0]);
      return TFS_ERROR;
    }
    // kv meta use RcClientImpl client
    strcpy(app_key, default_app_key);
    g_meta_type = META_LIFECYCLE;
  }
  else if (krs_addr != NULL)
  {
    if (rc_addr == NULL)
    {
      usage(argv[0]);
      return TFS_ERROR;
    }
    strcpy(app_key, default_app_key);
    g_meta_type = META_KV;
  }
  else if (rc_addr != NULL)
  {
    strcpy(app_key, default_app_key);
    g_meta_type = META_NAME;
  }
  else if (nsip != NULL)
  {
    g_tfs_client = TfsClientImplV2::Instance();
    // ret = g_tfs_client->initialize(nsip, DEFAULT_BLOCK_CACHE_TIME, 1000, false);
    ret = g_tfs_client->initialize(nsip);
    if (ret != TFS_SUCCESS)
    {
      fprintf(stderr, "init tfs client fail, ret: %d\n", ret);
      return ret;
    }
    g_meta_type = META_RAW;
  }

  init();

  if (optind >= argc)
  {
    signal(SIGINT, sign_handler);
    signal(SIGTERM, sign_handler);
    main_loop();
  }
  else // has other params
  {
    int32_t i = 0;
    if (directly)
    {
      for (i = optind; i < argc; i++)
      {
        do_cmd(argv[i]);
      }
    }
    else
    {
      VSTRING param;
      for (i = optind; i < argc; i++)
      {
        param.clear();
        param.push_back(argv[i]);
        cmd_batch_file(param);
      }
    }
  }
  if (g_tfs_client != NULL)
  {
    g_tfs_client->destroy();
  }
  return TFS_SUCCESS;
}
Exemplo n.º 2
0
int main(int argc,char** argv)
{
  //TODO readline
  int32_t i;
  string ns_ip_port_1;
  string ns_ip_port_2;
  bool directly = false;
  while ((i = getopt(argc, argv, "s:m:ih")) != EOF)
  {
    switch (i)
    {
      case 's':
        ns_ip_port_2 = optarg;
        break;
      case 'm':
        ns_ip_port_1 = optarg;
        g_need_cmp = true;
        break;
      case 'i':
        directly = true;
        break;
      case 'h':
      default:
        usage(argv[0]);
    }
  }

  if (ns_ip_port_2.empty() || (g_need_cmp && ns_ip_port_1.empty()))
  {
    fprintf(stderr, "please input nameserver ip and port.\n");
    usage(argv[0]);
  }

  TBSYS_LOGGER.setLogLevel("error");

  init();

  gstreamer.set_packet_factory(&gfactory);
  NewClientManager::get_instance().initialize(&gfactory, &gstreamer);

  if (!g_need_cmp)
  {
    g_show_info.set_ns_ip(ns_ip_port_2);
  }
  else
  {
    g_cmp_info.set_ns_ip(ns_ip_port_1, ns_ip_port_2);
  }

  if (optind >= argc)
  {
    signal(SIGINT, sign_handler);
    signal(SIGTERM, sign_handler);
    main_loop();
  }
  else
  {
    if (directly)
    {
      for (i = optind; i < argc; i++)
      {
        do_cmd(argv[i]);
      }
    }
    else
    {
      usage(argv[0]);
    }
  }
}
Exemplo n.º 3
0
int main(int argc, char* argv[])
{
  int32_t i;
  bool directly = false;
  bool set_log_level = false;

  // analyze arguments
  while ((i = getopt(argc, argv, "s:k:ih")) != EOF)
  {
    switch (i)
    {
      case 'n':
        set_log_level = true;
        break;
      case 'k':
        krs_addr = optarg;
        break;
      case 'i':
        directly = true;
        break;
      case 'h':
      default:
        usage(argv[0]);
        return TFS_ERROR;
    }
  }

  if (set_log_level)
  {
    TBSYS_LOGGER.setLogLevel("ERROR");
  }

  if (NULL == krs_addr)
  {
    usage(argv[0]);
    return TFS_ERROR;
  }

  gstreamer.set_packet_factory(&gfactory);
  NewClientManager::get_instance().initialize(&gfactory, &gstreamer);

  if (krs_addr != NULL)
  {
    new_server_id = Func::get_host_ip(krs_addr);
  }

  init();

  if (optind >= argc)
  {
    signal(SIGINT, sign_handler);
    signal(SIGTERM, sign_handler);
    main_loop();
  }
  else // has other params
  {
    int32_t i = 0;
    if (directly)
    {
      for (i = optind; i < argc; i++)
      {
        do_cmd(argv[i]);
      }
    }
    else
    {
      VSTRING param;
      for (i = optind; i < argc; i++)
      {
        param.clear();
        param.push_back(argv[i]);
        cmd_batch_file(param);
      }
    }
  }
  return TFS_SUCCESS;
}
Exemplo n.º 4
0
int main(int argc, char *argv[])
{
  gstreamer.set_packet_factory(&gfactory);

  tfs::common::NewClientManager::get_instance().initialize(&gfactory, &gstreamer);

  int i;

  std::string old_server;
  std::string new_server;
  std::string log_file_name;
  std::string input_file_name;
  while ((i = getopt(argc, argv, "i:o:n:l:")) != EOF)
  {
    switch (i)
    {
      case 'i':
        input_file_name = optarg;
        break;
      case 'o':
        old_server = optarg;
        break;
      case 'n':
        new_server = optarg;
        break;
      case 'l':
        log_file_name = optarg;
        break;
      default:
        usage(argv[0]);
        return TFS_ERROR;
    }
  }
  cout << input_file_name  << "  -" << old_server << "  -" << new_server << "  -" << log_file_name << endl;
  if (input_file_name.empty() || log_file_name.empty())
  {
    usage(argv[0]);
    return TFS_ERROR;
  }
  TBSYS_LOGGER.setFileName(log_file_name.c_str());
  TBSYS_LOGGER.setLogLevel("info");
  signal(SIGINT, sign_handler);

  NameMetaClientImpl old_client;
  if( TFS_SUCCESS != old_client.initialize( old_server.c_str() ) )
  {
    return TFS_ERROR;
  }
  TBSYS_LOG(DEBUG, "old_client initialize SUCCESS");

  old_server_id = Func::get_host_ip(old_server.c_str());
  new_server_id = Func::get_host_ip(new_server.c_str());
  string tfs_name = "/";
  int32_t ret = TFS_SUCCESS;
  FILE *s_fd = NULL;
  s_fd = fopen(input_file_name.c_str(), "r");
  if (NULL == s_fd)
  {
    printf(" open file %s for read error\n", input_file_name.c_str());
    ret =  TFS_ERROR;
  }
  char buff[128];
  while(TFS_SUCCESS == ret && 1 != stop)
  {
    if (NULL == fgets(buff, 128, s_fd))
    {
      break;
    }
    buff[127] = 0;
    char *p = NULL;
    const char DLIMER = ',';
    p = strchr(buff, DLIMER);
    if (NULL == p)
    {
      TBSYS_LOG(ERROR, "err input line %s", buff);
      continue;
    }
    app_id = -1;
    uid = -1;
    *p = '\0';
    app_id = strtoll(buff, NULL, 10);
    uid = strtoll(p + 1, NULL, 10);
    if (app_id <= 0 || uid <= 0)
    {
      *p = DLIMER;
      TBSYS_LOG(ERROR, "err input line %s", buff);
      continue;
    }
    TBSYS_LOG(INFO, "check app_id %ld uid %ld", app_id, uid);
    ret = check(old_client, tfs_name);
  }
  fclose(s_fd);
  s_fd = NULL;
  return 0;
}
Exemplo n.º 5
0
 RcStat::RcStat()
   :app_id_(0), oper_type_(0), order_by_(0), is_json_(false)
 {
   gstreamer.set_packet_factory(&gfactory);
   NewClientManager::get_instance().initialize(&gfactory, &gstreamer);
 }
Exemplo n.º 6
0
  namespace tools
  {
    static int32_t server_update_interval = 10;
    using namespace std;
    static tfs::message::MessageFactory gfactory;
    static tfs::common::BasePacketStreamer gstreamer;

    RcStat::RcStat()
      :app_id_(0), oper_type_(0), order_by_(0), is_json_(false)
    {
      gstreamer.set_packet_factory(&gfactory);
      NewClientManager::get_instance().initialize(&gfactory, &gstreamer);
    }

    RcStat::~RcStat()
    {
    }

    inline std::string& RcStat::trim_space(std::string &s)
    {
      s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
      s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());

      return s;
    }

    void RcStat::split_string_to_vector(const std::string& vec_str, const std::string& pattern, std::vector<std::string>& vec)
    {
      if (vec_str.size() == 0 || pattern.size() == 0)
      {
        return;
      }
      std::string::size_type pos, size, pattern_size;
      std::string tmp_str = vec_str + pattern;

      size = tmp_str.size();
      pattern_size = pattern.size();

      for(std::string::size_type i = 0; i < size; i++)
      {
        pos = tmp_str.find(pattern, i);
        if (pos < size)
        {
          std::string s = tmp_str.substr(i, pos-i);
          vec.push_back(trim_space(s));

          i = pos + pattern_size -1 ;
        }
      }
    }


    int RcStat::initialize(int argc, char **argv)
    {
      int32_t i = 0;

      if (argc < 2)
      {
        fprintf(stderr, "Usage: %s -r 127.0.0.1:6202 -a 0 -t 2\n", argv[0]);
        exit(1);
      }

      string str_rc_ips;

      while ((i = getopt(argc, argv,"r:a:t:o:ij")) != EOF)
      {
        switch (i)
        {
          /* rc server address */
          case 'r':
            str_rc_ips = optarg;
            break;
            /* app_id */
          case 'a':
            app_id_ = atoi(optarg);
            break;
          case 'o':
            order_by_ = atoi(optarg);
            break;
          case 'i':
            server_update_interval = atoi(optarg);
            if (server_update_interval <= 0)
            {
              server_update_interval = 10;
            }
            break;
            /* type  */
          case 't':
            oper_type_ = atoi(optarg);
            break;
          case 'j':
            is_json_ = true;
            break;
          default:
            fprintf(stderr, "Usage: %s -a 127.0.0.1:6202 -a 0\n", argv[0]);
            return EXIT_SUCCESS;
        }
      }

      if (str_rc_ips.empty())
      {
        fprintf(stderr, "Usage: %s -a 127.0.0.1:6202\n", argv[0]);
      }
      else
      {
        split_string_to_vector(str_rc_ips, ",",rc_ips_vec_);
      }

      return TFS_SUCCESS;
    }

    //int RcStat::parse_rcs_stat(const std::vector<RspRcStatMessage *> rsp_rcstat_msg_vec)
    void RcStat::parse_rcs_stat(const RspRcStatMessage* rsp_rcstat_msg)
    {
      int64_t key;

      const common::AppOperInfoMap& app_oper_info_map = rsp_rcstat_msg->get_stat_info();
      if (app_oper_info_map.size() > 0)
      {
        AppOperInfoMapConstIter mit = app_oper_info_map.begin();
        for (; mit != app_oper_info_map.end(); ++mit)
        {
          key = mit->second.oper_app_id_ << 16 | mit->second.oper_type_;

          std::map<int64_t, AppOperInfo>::iterator it = appoper_result_map_.find(key);
          if (it == appoper_result_map_.end())
          {
            appoper_result_map_.insert(make_pair(key, mit->second));
          }
          else
          {
            AppOperInfo& app_oper_info = it->second;
            if(app_oper_info.oper_app_id_ == mit->second.oper_app_id_ && app_oper_info.oper_type_ == mit->second.oper_type_)
            {
              app_oper_info.oper_times_ += mit->second.oper_times_;
              app_oper_info.oper_size_ += mit->second.oper_size_;
              app_oper_info.oper_rt_ += mit->second.oper_rt_;
              app_oper_info.oper_succ_ += mit->second.oper_succ_;
            }
            else
            {
              TBSYS_LOG(ERROR, "rcserver data inconsistent.");
            }
          }
        }
      }
    }

    int RcStat::show_rcs_stat()
    {
      int32_t ret = TFS_SUCCESS;
      std::multimap<int64_t, AppOperInfo> order_appoper_result_map;
      std::multimap<int64_t, AppOperInfo>::iterator it = appoper_result_map_.begin();
      int64_t count_per_sec, succ_count_per_sec;
      if (is_json_)
      {
        order_by_ = 0; //use json format, order is no matter
      }
      for (; it != appoper_result_map_.end(); ++it)
      {
        count_per_sec = it->second.oper_times_ /server_update_interval + 1;
        succ_count_per_sec = it->second.oper_succ_ /server_update_interval + 1;
        it->second.oper_size_ = it->second.oper_size_ / it->second.oper_times_;
        it->second.oper_rt_= it->second.oper_rt_ / it->second.oper_times_;
        it->second.oper_times_ = count_per_sec;
        it->second.oper_succ_ = succ_count_per_sec;

        if (order_by_ == 1)
        {
          order_appoper_result_map.insert(make_pair(it->second.oper_times_, it->second));
        }
        else if (order_by_ == 2)
        {
          order_appoper_result_map.insert(make_pair(it->second.oper_size_, it->second));
        }
        else if (order_by_ == 3)
        {
          order_appoper_result_map.insert(make_pair(it->second.oper_rt_, it->second));
        }
      }

      std::multimap<int64_t, AppOperInfo> *tmp_appoper_result_map = NULL;
      if (order_by_ == 1 || order_by_ == 2 || order_by_ == 3)
      {
        tmp_appoper_result_map = &order_appoper_result_map;
      }
      else
      {
        tmp_appoper_result_map = &appoper_result_map_;
      }

      it = tmp_appoper_result_map->begin();
      if (is_json_)
      {
        int last_app_id = -1;
        bool have_done_oper[3];
        have_done_oper[0] = have_done_oper[1] = have_done_oper[2] = true;
        fprintf(stdout, "{\n");
        for (; it != tmp_appoper_result_map->end(); ++it)
        {
          if (it->second.oper_app_id_ <= 0 ) continue;
          bool first_line_in_app = last_app_id != it->second.oper_app_id_;
          if (first_line_in_app)
          {
            if (last_app_id != -1)
            {
              fill_nodata_col(have_done_oper);
              fprintf(stdout, "\n},\n");
              fprintf(stdout, "{\n");
            }
            last_app_id = it->second.oper_app_id_;
            fprintf(stdout, "\"app_id\":\"%d\"", last_app_id);
            have_done_oper[0] = have_done_oper[1] = have_done_oper[2] = false;
          }

          switch (it->second.oper_type_)
          {
            case 1:
              fprintf(stdout, ",\"read_times\":%ld, \"read_bytes\":%ld, \"read_rt\":%ld",
                  it->second.oper_times_, it->second.oper_size_, it->second.oper_rt_);
              have_done_oper[0] = true;
              break;
            case 2:
              fprintf(stdout, ",\"write_times\":%ld, \"write_bytes\":%ld, \"write_rt\":%ld",
                  it->second.oper_times_, it->second.oper_size_, it->second.oper_rt_);
              have_done_oper[1] = true;
              break;
            case 3:
              fprintf(stdout, ",\"rm_times\":%ld, \"rm_bytes\":%ld, \"rm_rt\":%ld",
                  it->second.oper_times_, it->second.oper_size_, it->second.oper_rt_);
              have_done_oper[2] = true;
              break;
            default:
              break;
          }
        }
        fill_nodata_col(have_done_oper);
        fprintf(stdout, "\n}\n");
      }
      else
      {
        for (; it != tmp_appoper_result_map->end(); ++it)
        {
          fprintf(stdout, "%d,%d,%ld,%ld,%ld,%ld\n", it->second.oper_app_id_, it->second.oper_type_, \
              it->second.oper_times_, it->second.oper_size_, it->second.oper_rt_, succ_count_per_sec);
        }
      }

      return ret;
    }

    int RcStat::get_rcs_stat()
    {
      int ret = TFS_SUCCESS;
      uint64_t server_id = 0;

      std::vector<string>::const_iterator iter = rc_ips_vec_.begin();
      for (; iter != rc_ips_vec_.end(); ++iter)
      {
        string rc_ip = *iter;
        server_id = Func::get_host_ip(rc_ip.c_str());

        tbnet::Packet *ret_msg = NULL;
        ReqRcStatMessage req_rcstat_msg;
        NewClient* client = NewClientManager::get_instance().create_client();

        req_rcstat_msg.set_app_id(app_id_);
        req_rcstat_msg.set_oper_type(oper_type_);

        if ((ret = send_msg_to_server(server_id, client, &req_rcstat_msg, ret_msg)) == TFS_SUCCESS)
        {
          if (ret_msg->getPCode() != RSP_RC_REQ_STAT_MESSAGE)
          {
            TBSYS_LOG(ERROR, "cant't get response message from rcserver.");
            ret = TFS_ERROR;
          }
          else
          {
            RspRcStatMessage *rsp_rcstat_msg = dynamic_cast<RspRcStatMessage*>(ret_msg);
            parse_rcs_stat(rsp_rcstat_msg);
          }
        }
        else
        {
          TBSYS_LOG(ERROR, "ERROR:send_msg_to_server error. ret: %d.\n", ret);
        }
        NewClientManager::get_instance().destroy_client(client);
      }

      show_rcs_stat();

      return ret;
    }
  }
Exemplo n.º 7
0
int main(int argc, char* argv[])
{
  int i = 0;
  int iex = 0;

  // input option
  if (argc == 1)
  {
    usage(argv[0]);
    return TFS_ERROR;
  }
  while ((i = getopt(argc, argv, "d:i::")) != -1)
  {
    switch (i)
    {
    case 'i':
      iex = 1;
      break;
    case 'd':
      ds_ip = Func::get_host_ip(optarg);
      if (ds_ip == 0)
      {
        printf("ip or port is invalid, please try again.\n");
        return TFS_ERROR;
      }
      break;
    case ':':
      printf("missing -d");
      usage(argv[0]);
      break;
    default:
      usage(argv[0]);
      return TFS_ERROR;
    }
  }
  static tfs::message::MessageFactory factory;
  static tfs::common::BasePacketStreamer streamer;
  streamer.set_packet_factory(&factory);
  NewClientManager::get_instance().initialize(&factory, &streamer);

  init();

  if (optind >= argc)
  {
    signal(SIGINT, signal_handler);
    main_loop();
  }
  else
  {
    VSTRING param;
    int i = optind;
    if (iex)
    {
      printf("with i\n");
      for (i = optind; i < argc; i++)
      {
        param.clear();
        int cmd = parse_cmd(argv[i], param);
        switch_cmd(cmd, param);
      }
    }
    else
    {
      printf("without i\n");
      for (i = optind; i < argc; i++)
      {
        param.clear();
        param.push_back(argv[i]);
      }
    }
  }
  return TFS_SUCCESS;
}