Last active
June 27, 2021 18:33
-
-
Save markusbuchholz/3fe5b5279623e5c54420c95f1d0a3814 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//g++ cpu_collector_json.cpp -o cpu_collector -lboost_system | |
//communication | |
#include <boost/asio.hpp> | |
#include <iostream> | |
#include "json.hpp" | |
#include <stdlib.h> | |
#include <time.h> | |
//system data collector | |
#include <iostream> | |
#include <vector> | |
#include <thread> | |
#include <chrono> | |
#include <string> | |
#include <iostream> | |
#include <fstream> | |
#include <sstream> | |
#include <iomanip> // std::setprecision | |
const std::string LINUX_PROC_FOLDER = "/proc/"; | |
const std::string LINUX_CPU_LOAD_AVG_FILE = "/loadavg"; | |
std::string cpuAvgLoadFile = LINUX_PROC_FOLDER + LINUX_CPU_LOAD_AVG_FILE; | |
using namespace boost::asio; | |
using namespace boost::asio::ip; | |
using json = nlohmann::json; | |
//----------------------------------------------------------- | |
std::tuple<float, float, float> callCPUcheck() | |
{ | |
std::ifstream infile; | |
infile.open(cpuAvgLoadFile); | |
std::string line; | |
float cpuavg1{0}; | |
float cpuavg3{0}; | |
float cpuavg15{0}; | |
if (std::getline(infile, line)) | |
{ | |
std::istringstream iss(line); | |
if (iss) | |
{ | |
iss >> cpuavg1 >> cpuavg3 >> cpuavg15; | |
} | |
} | |
return std::make_tuple(cpuavg1, cpuavg3, cpuavg15); | |
} | |
//----------------------------------------------------------- | |
std::string getData(tcp::socket &socket) | |
{ | |
boost::asio::streambuf buf; | |
boost::asio::read_until(socket, buf, "\n"); | |
std::string data = buffer_cast<const char *>(buf.data()); | |
return data; | |
} | |
//----------------------------------------------------------- | |
void sendData(tcp::socket &socket, const std::string &message) | |
{ | |
write(socket, | |
buffer(message + "\n")); | |
} | |
//----------------------------------------------------------- | |
int main(int argc, char *argv[]) | |
{ | |
std::tuple<float, float, float> cpuAVG; | |
io_service io_service; | |
// socket creation | |
ip::tcp::socket client_socket(io_service); | |
//here we choose the location the date to be sent (local 127.0.0.1 or remote: X.X.X.X - your Cloud server, here 9999 is a port) | |
client_socket.connect(tcp::endpoint(address::from_string("127.0.0.1"), 9999)); | |
std::string usr_name, reply, response; | |
usr_name = "avgCPUcollector"; | |
sendData(client_socket, usr_name); | |
while (true) | |
{ | |
cpuAVG = callCPUcheck(); | |
std::setprecision(3); | |
std::map<std::string, float> c_map{{"avg_1", std::get<0>(cpuAVG)}, {"avg_5", std::get<1>(cpuAVG)}, {"avg_15", std::get<2>(cpuAVG)}}; | |
json j_map(c_map); | |
auto s4 = j_map.dump(); | |
// Fetching response | |
response = getData(client_socket); | |
// Popping last character "\n" | |
response.pop_back(); | |
// Validating if the connection has to be closed | |
if (response == "exit") | |
{ | |
std::cout << "Connection terminated" << std::endl; | |
break; | |
} | |
std::cout << "Server: " << response << std::endl; | |
reply = s4; | |
sendData(client_socket, reply); | |
if (reply == "exit") | |
break; | |
std::this_thread::sleep_for(std::chrono::seconds(1)); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment