From 8c988c88044db6768bc7ee6bcd7db10a53227e9c Mon Sep 17 00:00:00 2001 From: root Date: Tue, 14 Nov 2017 20:11:13 +0800 Subject: [PATCH] Add gmonitor and gshow in C++ --- Main/gmonitor.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++ Main/gshow.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 Main/gmonitor.cpp create mode 100644 Main/gshow.cpp diff --git a/Main/gmonitor.cpp b/Main/gmonitor.cpp new file mode 100644 index 0000000..6d6297a --- /dev/null +++ b/Main/gmonitor.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include + +using namespace std; + +const string UrlEncode(const string& s) +{ + string ret; + unsigned char *ptr = (unsigned char *)s.c_str(); + ret.reserve(s.length()); + + for(int i=0;i=48) && (int(ptr[i])<=57)) + ret += ptr[i]; + else if((int(ptr[i])>=65) && (int(ptr[i])<=90)) + ret += ptr[i]; + else if((int(ptr[i])>=97) && (int(ptr[i])<=122)) + ret += ptr[i]; + else if(int(ptr[i])==32) + ret += '+'; + else + { + char buf[5]; + memset(buf,0,5); + snprintf(buf,5,"%%%X",ptr[i]); + ret.append(buf); + } + } + return ret; +} +size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid) +{ + string* str = dynamic_cast((string *)lpVoid); + if( NULL == str || NULL == buffer ) + return -1; + char* pData = (char*)buffer; + str->append(pData, size * nmemb); + return nmemb; +} +int +main(int argc, char * argv[]) +{ + cout << "argc: " << argc << endl; + cout << "ip: " << argv[1] << endl; + cout << "port: " << argv[2] << endl; + + string serverIP = string(argv[1]); + string serverPort = string(argv[2]); + const string strUrl = "http://"+serverIP+":"+serverPort+"/?operation=monitor"; + string res; + res.clear(); + CURLcode ret; + CURL* curl = curl_easy_init(); + if(NULL == curl) + return CURLE_FAILED_INIT; + curl_easy_setopt(curl, CURLOPT_URL, UrlEncode(strUrl).c_str()); + curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&res); + curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); + curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3); + curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3); + ret = curl_easy_perform(curl); + curl_easy_cleanup(curl); + cout<< res < +#include +#include +#include + +using namespace std; + +const string UrlEncode(const string& s) +{ + string ret; + unsigned char *ptr = (unsigned char *)s.c_str(); + ret.reserve(s.length()); + + for(int i=0;i=48) && (int(ptr[i])<=57)) + ret += ptr[i]; + else if((int(ptr[i])>=65) && (int(ptr[i])<=90)) + ret += ptr[i]; + else if((int(ptr[i])>=97) && (int(ptr[i])<=122)) + ret += ptr[i]; + else if(int(ptr[i])==32) + ret += '+'; + else + { + char buf[5]; + memset(buf,0,5); + snprintf(buf,5,"%%%X",ptr[i]); + ret.append(buf); + } + } + return ret; +} +size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid) +{ + string* str = dynamic_cast((string *)lpVoid); + if( NULL == str || NULL == buffer ) + return -1; + char* pData = (char*)buffer; + str->append(pData, size * nmemb); + return nmemb; +} +int +main(int argc, char * argv[]) +{ + cout << "argc: " << argc << endl; + cout << "ip: " << argv[1] << endl; + cout << "port: " << argv[2] << endl; + + string serverIP = string(argv[1]); + string serverPort = string(argv[2]); + const string strUrl = "http://"+serverIP+":"+serverPort+"/?operation=show"; + string res; + res.clear(); + CURLcode ret; + CURL* curl = curl_easy_init(); + if(NULL == curl) + return CURLE_FAILED_INIT; + curl_easy_setopt(curl, CURLOPT_URL, UrlEncode(strUrl).c_str()); + curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&res); + curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); + curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3); + curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3); + ret = curl_easy_perform(curl); + curl_easy_cleanup(curl); + cout<< "database: "<< res <