diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d3cc020..1a86dcb6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,7 @@ endif () # set PKTMINERG_MAJOR_VERSION, PKTMINERG_MINOR_VERSION, etc. set(PKTMINERG_MAJOR_VERSION "0") set(PKTMINERG_MINOR_VERSION "7") -set(PKTMINERG_PATCH_VERSION "1") +set(PKTMINERG_PATCH_VERSION "3") set(PKTMINERG_VERSION_STRING "${PKTMINERG_MAJOR_VERSION}.${PKTMINERG_MINOR_VERSION}.${PKTMINERG_PATCH_VERSION}") if (WIN32) diff --git a/src/daemonManager.cpp b/src/daemonManager.cpp index 86da2873..4f68ca43 100644 --- a/src/daemonManager.cpp +++ b/src/daemonManager.cpp @@ -68,6 +68,8 @@ void DaemonManager::killRunningPktg() { LOG(ERROR) << strErr; report_.addPacketAgentLogs("ERROR", strErr); } + clearCgroupfolder(pid); + clearCgroupfolder(agentPid_); agentPid_ = 0; } } @@ -85,6 +87,35 @@ void getActiveInstanceNames(std::set & names) { return; } +void DaemonManager::clearCgroupfolder(pid_t pid) { + std::string path1("/cgroup/"); + std::string path2("/sys/fs/cgroup/"); + + std::vector pathToRemove; + + pathToRemove.push_back(path1+"cpu/pid-"+std::to_string(pid)); + pathToRemove.push_back(path2+"cpu/pid-"+std::to_string(pid)); + pathToRemove.push_back(path1+"memory/pid-"+std::to_string(pid)); + pathToRemove.push_back(path2+"memory/pid-"+std::to_string(pid)); + + for(auto pStr:pathToRemove) { + boost::filesystem::path p(pStr); + if (boost::filesystem::exists(p)) { + if (rmdir(pStr.c_str()) == -1) { + output_buffer = boost::str(boost::format("Fail to remove path:%1%,%2% ")%pStr%strerror(errno)); + ctx_.log(output_buffer, log4cpp::Priority::ERROR); + LOG(ERROR) << output_buffer; + } else { + output_buffer = boost::str(boost::format("Successfully remove path:%1%")%pStr); + ctx_.log(output_buffer, log4cpp::Priority::INFO); + LOG(INFO) << output_buffer; + + } + } + } + return; +} + void DaemonManager::getDaemonImpl() { std::lock_guard lock{mtx_}; std::shared_ptr curl(::curl_easy_init(), [](CURL *curl) { @@ -179,7 +210,7 @@ void DaemonManager::getDaemonImpl() { startPA(agent_, result); - if(agent_.SyncIntervalIsSet() != agent_.SyncIntervalIsSet() && agent_.getSyncInterval() > 0) { + if(agent_.SyncIntervalIsSet() && agent_.getSyncInterval() > 0) { int periodUs = agent_.getSyncInterval() * TICK_US_SEC; timer_tasks_cancel(tasks_, getDaemonTid_); getDaemonTid_ = timer_tasks_push(tasks_, getDaemon, this, periodUs); @@ -200,16 +231,17 @@ void DaemonManager::getDaemonImpl() { ctx_.log(str, log4cpp::Priority::ERROR); LOG(ERROR) << str; report_.addPacketAgentLogs("ERROR", str); + clearCgroupfolder(agentPid_); + zmqPortAvlPush(zmqPort_); } - std::stringstream result; - + std::stringstream result; startPA(agent_, result); } if (agentPid_ != 0) report_.setPid(agentPid_); - if (agentPid_ != 0 && agent_.startTimeIsSet()) { + if (agentPid_ != 0 && agent_.startTimeIsSet() && report_.getPacketAgentMetrics()) { report_.getPacketAgentMetrics()->setStartTime(agent_.getStartTime()); } std::stringstream jsonS; @@ -441,11 +473,9 @@ std::string DaemonManager::createParams(std::shared_ptr commandStr; std::string str; commandStr.push_back("pktminerg"); @@ -454,7 +484,15 @@ int DaemonManager::startPA(io::swagger::server::model::Agent& body, std::strings if (datas.size() == 0) { return -1; } - + std::set names; + try{ + getActiveInstanceNames(names); + } + catch (...) { + std::string str = boost::str(boost::format("Can't get active instance.")); + ctx_.log(str, log4cpp::Priority::INFO); + LOG(INFO) << str; + } uint64_t buffSize; if(body.getMemLimit() == 0) { buffSize = 256; @@ -466,15 +504,19 @@ int DaemonManager::startPA(io::swagger::server::model::Agent& body, std::strings } else if (data->interfaceNamesIsSet()) { stratigies += data->getInterfaceNames().size(); } else if (data->instanceNamesIsSet()) { + instanceCheck_ = true; - stratigies += data->getInstanceNames().size(); + for (auto & i: data->getInstanceNames()) { + if(std::find(names.begin(), names.end(), i) != names.end()) { + stratigies += 1; + } + } } } if (stratigies == 0 || stratigies > body.getMemLimit()) { return -1; } - buffSize = body.getMemLimit()/stratigies; } @@ -485,7 +527,6 @@ int DaemonManager::startPA(io::swagger::server::model::Agent& body, std::strings result << error.toJson(); return -1; } - int noOfData = 0; //get buffer size for (auto &data: datas){ @@ -534,8 +575,7 @@ int DaemonManager::startPA(io::swagger::server::model::Agent& body, std::strings } } else if (data->instanceNamesIsSet()) { - std::set names; - getActiveInstanceNames(names); + bool needRestart = false; if (checkProcessRunning()) { for (auto & item: instanceStatus) { @@ -1024,6 +1064,7 @@ bool DaemonManager::delAgent() { LOG(ERROR) << strErr; report_.addPacketAgentLogs("ERROR", strErr); } + clearCgroupfolder(agentPid_); agentPid_ = 0; zmqPortAvlPush(zmqPort_); return true; @@ -1050,17 +1091,6 @@ DaemonManager::DaemonManager(const boost::program_options::variables_map &vm, ti daemon_.setNodeName(nodeName); } - if(vm_.count("includingNICs")) { - std::vector nics; - boost::split(nics, vm_["includingNICs"].as(), boost::is_any_of(",")); - if (0 == daemon_.filterNics(nics)) { - ctx_.log("No nics left after filtering, please check the config of \"includingNICs\"", log4cpp::Priority::ERROR); - std::cerr << "No nics left after filtering, please check the config of \"includingNICs\"" << std::endl; - timer_tasks_stop(tasks_); - return; - } - } - //set podname (and name space) for sidecard mode if(vm_.count("podname")) { char* podName = getenv(vm_["podname"].as().c_str()); @@ -1098,7 +1128,7 @@ DaemonManager::DaemonManager(const boost::program_options::variables_map &vm, ti } } - daemon_.setClientVersion("0.7.0"); + daemon_.setClientVersion("0.7.3"); split(strs, SUPPORT_API_VERSIONS, boost::algorithm::is_any_of(",")); for (const auto& str:strs) { @@ -1176,6 +1206,19 @@ void DaemonManager::scanNetworkInterfaceImpl() { { ctx_.log("scanNetworkInterface changed ", log4cpp::Priority::INFO); LOG(INFO) << "scanNetworkInterface changed "; + daemon_.updateNics(); + if(vm_.count("includingNICs")) { + std::vector nics; + boost::split(nics, vm_["includingNICs"].as(), boost::is_any_of(",")); + if (0 == daemon_.filterNics(nics)) { + ctx_.log("No nics left after filtering, please check the config of \"includingNICs\"", log4cpp::Priority::ERROR); + std::cerr << "No nics left after filtering, please check the config of \"includingNICs\"" << std::endl; + timer_tasks_stop(tasks_); + return; + } + + } + if(!daemonReg()) interfaces_ = interfaces; } diff --git a/src/daemonManager.h b/src/daemonManager.h index 3401142d..129d8ecb 100644 --- a/src/daemonManager.h +++ b/src/daemonManager.h @@ -15,6 +15,8 @@ #include #include #include +#include +#include #include "zmq.hpp" #include @@ -24,11 +26,11 @@ #include #include #include -#include #include #include #include #include +#include #include "glog/logging.h" #include "log4cpp/Category.hh" @@ -124,6 +126,8 @@ class DaemonManager void killRunningPktg(); + void clearCgroupfolder(pid_t pid); + private: const boost::program_options::variables_map& vm_; std::unordered_set interfaces_; diff --git a/src/pktminerg.cpp b/src/pktminerg.cpp index 9e9d21f1..ff1da4a2 100644 --- a/src/pktminerg.cpp +++ b/src/pktminerg.cpp @@ -94,10 +94,14 @@ static std::string getProccssIdWithContainer(const std::string &containerId, Log fp=popen(cmd.c_str(),"r"); fgets(buffer,sizeof(buffer),fp); pclose(fp); -#endif + if (strlen(buffer) == 0) { + std::string output_buffer = std::string("Can't get pid for the containerId:") + id; + ctx.log(output_buffer, log4cpp::Priority::ERROR); + std::cerr << StatisLogContext::getTimeString() << output_buffer << std::endl; return std::string(); - } + } +#endif return std::string(buffer, strlen(buffer) - 1); } @@ -665,7 +669,14 @@ Allowed options for each interface:"); uint16_t daemon_zmq_port = vm["control"].as(); if (daemon_zmq_port) { if (agent_control_plane == nullptr) { - agent_control_plane = std::make_shared(ctx, daemon_zmq_port); + try { + agent_control_plane = std::make_shared(ctx, daemon_zmq_port); + } catch (zmq::error_t& e) { + output_buffer = std::string("Can not bind daemon zmq port:") + std::to_string(daemon_zmq_port) + "," +e.what(); + ctx.log(output_buffer, log4cpp::Priority::ERROR); + std::cerr << output_buffer << std::endl; + return 1; + } } update_status = 1; } diff --git a/src/restful/model/Daemon.cpp b/src/restful/model/Daemon.cpp index 09db811e..b32ef437 100644 --- a/src/restful/model/Daemon.cpp +++ b/src/restful/model/Daemon.cpp @@ -171,8 +171,6 @@ namespace io { m_PlatformId = ""; m_PlatformIdIsSet = false; - - nics = getNetworkInterfaces(); } Daemon::~Daemon() { @@ -608,6 +606,10 @@ namespace io { } return nics.size(); } + int Daemon::updateNics() { + nics.clear(); + nics = getNetworkInterfaces(); + } } } } diff --git a/src/restful/model/Daemon.h b/src/restful/model/Daemon.h index 48d50456..dec136d4 100644 --- a/src/restful/model/Daemon.h +++ b/src/restful/model/Daemon.h @@ -167,6 +167,7 @@ class Daemon int filterNics(std::vector & nicNames); void setLogFileContext(LogFileContext& ctx) {m_ctx = ctx;}; + int updateNics(); protected: int64_t m_Id;