main.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #include <iostream>
  2. #include "cwmp.nsmap"
  3. #include "httppost.h"
  4. #include "soapcwmpService.h"
  5. #include "plugin/threads.h"
  6. #include <vector>
  7. #include <algorithm>
  8. #include <signal.h>
  9. #include <thread>
  10. #include <fstream>
  11. #include "influxdb.h"
  12. //#include <InfluxDB.h>
  13. //#include <InfluxDBFactory.h>
  14. int port = 8000;
  15. int POST_handler(struct soap *soap);
  16. struct http_post_handlers http_post_handlers[] = {
  17. {"POST", POST_handler},
  18. {NULL}
  19. };
  20. void handler(int s) {
  21. printf("Sassan Caught SIGPIPE....\n");
  22. }
  23. void *process_request(void *arg) {
  24. cwmpService *service = (cwmpService *) arg;
  25. THREAD_DETACH(THREAD_ID);
  26. if (service) {
  27. service->serve();
  28. service->destroy(); /* clean up */
  29. delete service;
  30. }
  31. return NULL;
  32. }
  33. //<editor-fold desc="zbx_keys">
  34. /*
  35. Searching for: InternetGatewayDevice.DeviceInfo.SoftwareVersion & Find Result: 18
  36. Searching for: InternetGatewayDevice.DeviceInfo.UpTime & Find Result: 21
  37. Searching for: InternetGatewayDevice.DeviceInfo.X_TGT_IMSI & Find Result: 22
  38. Searching for: InternetGatewayDevice.DeviceInfo.MemoryStatus.Free & Find Result: 28
  39. Searching for: InternetGatewayDevice.DeviceInfo.ProcessStatus.CPUUsage & Find Result: 29
  40. Searching for: InternetGatewayDevice.Time.CurrentLocalTime & Find Result: 229
  41. Searching for: InternetGatewayDevice.WANDevice.1.X_TGT_WANNetConfigInfo.SINR1 & Find Result: 464
  42. Searching for: InternetGatewayDevice.WANDevice.1.X_TGT_WANNetConfigInfo.SINR2 & Find Result: 465
  43. Searching for: InternetGatewayDevice.WANDevice.1.X_TGT_WANNetConfigInfo.EnodeBId & Find Result: 469
  44. Searching for: InternetGatewayDevice.WANDevice.1.X_TGT_WANNetConfigInfo.CellId & Find Result: 470
  45. Searching for: InternetGatewayDevice.WANDevice.1.WANEthernetInterfaceConfig.Stats.BytesSent & Find Result: 542
  46. Searching for: InternetGatewayDevice.WANDevice.1.WANEthernetInterfaceConfig.Stats.BytesReceived & Find Result: 543
  47. Searching for: InternetGatewayDevice.WANDevice.1.WANEthernetInterfaceConfig.Stats.PacketsSent & Find Result: 544
  48. Searching for: InternetGatewayDevice.WANDevice.1.WANEthernetInterfaceConfig.Stats.PacketsReceived & Find Result: 545
  49. Searching for: InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANIPConnection.1.Uptime & Find Result: 553
  50. Searching for: InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANIPConnection.1.ExternalIPAddress & Find Result: 557
  51. Searching for: InternetGatewayDevice.WEB_GUI.Network.LTE_Setting.Status.RSRP1 & Find Result: 580
  52. Searching for: InternetGatewayDevice.WEB_GUI.Network.LTE_Setting.Status.RSRP2 & Find Result: 581
  53. Searching for: InternetGatewayDevice.WEB_GUI.Network.LTE_Setting.Status.TX_Power & Find Result: 585
  54. Searching for: InternetGatewayDevice.WEB_GUI.Network.LTE_Setting.Status.DL_MCS & Find Result: 591
  55. Searching for: InternetGatewayDevice.WEB_GUI.Network.LTE_Setting.Status.UL_MCS & Find Result: 592
  56. Searching for: InternetGatewayDevice.WEB_GUI.Overview.DeviceInfo.IMSI & Find Result: 612
  57. Searching for: InternetGatewayDevice.WEB_GUI.Overview.DeviceInfo.IMEI & Find Result: 613
  58. */
  59. std::string zbx_keys[] = {"InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANIPConnection.1.Uptime",
  60. "InternetGatewayDevice.WEB_GUI.Network.LTE_Setting.Status.UL_MCS",
  61. "InternetGatewayDevice.WEB_GUI.Network.LTE_Setting.Status.TX_Power",
  62. "InternetGatewayDevice.DeviceInfo.SoftwareVersion",
  63. "InternetGatewayDevice.WANDevice.1.X_TGT_WANNetConfigInfo.SINR2",
  64. "InternetGatewayDevice.WANDevice.1.X_TGT_WANNetConfigInfo.SINR1",
  65. "InternetGatewayDevice.WEB_GUI.Network.LTE_Setting.Status.RSRP2",
  66. "InternetGatewayDevice.WEB_GUI.Network.LTE_Setting.Status.RSRP1",
  67. "InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANIPConnection.1.ExternalIPAddress",
  68. "InternetGatewayDevice.WEB_GUI.Overview.DeviceInfo.IMSI",
  69. "InternetGatewayDevice.WEB_GUI.Overview.DeviceInfo.IMEI",
  70. "InternetGatewayDevice.WANDevice.1.X_TGT_WANNetConfigInfo.EnodeBId",
  71. "InternetGatewayDevice.DeviceInfo.MemoryStatus.Free",
  72. "InternetGatewayDevice.WANDevice.1.X_TGT_WANNetConfigInfo.EnodeBId",
  73. "InternetGatewayDevice.WEB_GUI.Network.LTE_Setting.Status.DL_MCS",
  74. "InternetGatewayDevice.Time.CurrentLocalTime",
  75. "InternetGatewayDevice.DeviceInfo.ProcessStatus.CPUUsage",
  76. "InternetGatewayDevice.WANDevice.1.X_TGT_WANNetConfigInfo.CellId",
  77. "InternetGatewayDevice.WANDevice.1.WANEthernetInterfaceConfig.Stats.BytesSent",
  78. "InternetGatewayDevice.WANDevice.1.WANEthernetInterfaceConfig.Stats.BytesReceived",
  79. "InternetGatewayDevice.WANDevice.1.WANEthernetInterfaceConfig.Stats.PacketsReceived",
  80. "InternetGatewayDevice.WANDevice.1.WANEthernetInterfaceConfig.Stats.PacketsSent",
  81. "InternetGatewayDevice.DeviceInfo.UpTime",
  82. "InternetGatewayDevice.DeviceInfo.X_TGT_IMSI"
  83. };
  84. //</editor-fold>
  85. void ZBX_Send(char **Name, char **Value, char **imsi) {
  86. if (!strcmp(*Name, "InternetGatewayDevice.WEB_GUI.Network.LTE_Setting.Status.DL_MCS") == 0 or
  87. !strcmp(*Name, "InternetGatewayDevice.WEB_GUI.Network.LTE_Setting.Status.UL_MCS") == 0) {
  88. if (!strcmp(*Value, "QPSK")) {
  89. *Value = "4";
  90. } else if (!strcmp(*Value, "QAM8")) {
  91. *Value = "8";
  92. } else if (!strcmp(*Value, "QAM16")) {
  93. *Value = "16";
  94. } else if (!strcmp(*Value, "QAM32")) {
  95. *Value = "32";
  96. } else if (!strcmp(*Value, "QAM64")) {
  97. *Value = "64";
  98. }
  99. std::ofstream zbx_data;
  100. time_t second;
  101. second = time(NULL);
  102. zbx_data.open("/var/tmp/zbx_sender_data", std::ios_base::app);
  103. zbx_data << "IMSI-" << *imsi << " " << *Name << " " << second << " \"" << *Value << "\"" << std::endl;
  104. }
  105. }
  106. int main() {
  107. signal(SIGPIPE, handler);
  108. std::cout << "Hello, World!" << std::endl;
  109. cwmpService service(SOAP_XML_INDENT);
  110. service.soap->send_timeout = service.soap->recv_timeout = 5;
  111. service.soap->transfer_timeout = 30;
  112. service.soap->keep_alive = 1;
  113. service.soap->tcp_keep_alive = 1;
  114. service.soap->bind_flags |= SO_REUSEADDR;
  115. service.soap->bind_flags |= SO_REUSEPORT;
  116. service.soap->bind_flags |= MSG_NOSIGNAL;
  117. soap_init2(service.soap, SOAP_IO_KEEPALIVE, SOAP_IO_KEEPALIVE);
  118. SOAP_SOCKET m = service.bind(NULL, port, 1); /* master socket */
  119. if (soap_valid_socket(m)) {
  120. while (soap_valid_socket(service.accept())) {
  121. THREAD_TYPE tid;
  122. void *arg = (void *) service.copy();
  123. if (arg)
  124. while (THREAD_CREATE(&tid, (void *(*)(void *)) process_request, arg))
  125. sleep(1);
  126. }
  127. }
  128. service.soap_stream_fault(std::cerr);
  129. service.destroy(); /* clean up */
  130. return 0;
  131. }
  132. int POST_handler(struct soap *soap) {
  133. // struct _cwmp__GetParameterValues *res = soap_new__cwmp__GetParameterValues(soap, -1);
  134. // struct ParameterNames *list = soap_new_ParameterNames(soap, -1);
  135. const char *buf;
  136. size_t len;
  137. buf = soap_http_get_body(soap, &len);
  138. // std::cerr<<"Sassan3 ID: "<<soap->header->cwmp__SupportedCWMPVersions<<std::endl;
  139. // std::cerr<<"Debug1: "<<len<<std::endl;
  140. // std::cerr<<"Debug2: "<<buf<<std::endl;
  141. /* if HTTP has a content type header or a HTTP body is present then return 404 */
  142. if (len == 0) {
  143. std::cerr << "Sassan 204...: " << buf << std::endl;
  144. return 204; // HTTP 400 Bad Request
  145. // }else {
  146. // std::cerr<<"Debug0: "<<buf<<std::endl;
  147. // std::cerr<<"Debug1: "<<len<<std::endl;
  148. // std::cerr<<"Debug2: "<<buf<<std::endl;
  149. }
  150. // if (soap->http_content || soap_http_has_body(soap))
  151. // return 404;
  152. // (void)soap_end_recv(soap);
  153. // /* populate the response with one array item */
  154. // if (res && list)
  155. // {
  156. // list->__size = 1;
  157. // if ((list->__ptrstring = (char**)soap_malloc(soap, list->__size * sizeof(char*))) != NULL)
  158. // {
  159. // list->__ptrstring[0] = soap_strdup(soap, "...");
  160. // res->ParameterNames = list;
  161. // }
  162. // }
  163. // /* send response */
  164. // soap->encodingStyle = "";
  165. // soap_serializeheader(soap);
  166. // soap_serialize__cwmp__GetParameterValues(soap, res);
  167. // if (soap_begin_count(soap))
  168. // return soap->error;
  169. // if (soap->mode & SOAP_IO_LENGTH)
  170. // {
  171. // if (soap_envelope_begin_out(soap)
  172. // || soap_putheader(soap)
  173. // || soap_body_begin_out(soap)
  174. // || soap_put__cwmp__GetParameterValues(soap, res, "cwmp:GetParameterValues", "")
  175. // || soap_body_end_out(soap)
  176. // || soap_envelope_end_out(soap))
  177. // return soap->error;
  178. // };
  179. // if (soap_end_count(soap)
  180. // || soap_response(soap, SOAP_OK)
  181. // || soap_envelope_begin_out(soap)
  182. // || soap_putheader(soap)
  183. // || soap_body_begin_out(soap)
  184. // || soap_put__cwmp__GetParameterValues(soap, res, "cwmp:GetParameterValues", "")
  185. // || soap_body_end_out(soap)
  186. // || soap_envelope_end_out(soap)
  187. // || soap_end_send(soap))
  188. // return soap->error;
  189. // return soap_closesock(soap);
  190. }
  191. int cwmpService::GetParameterNames(struct _cwmp__GetParameterNames *cwmp__GetParameterNames,
  192. struct _cwmp__GetParameterNamesResponse *cwmp__GetParameterNamesResponse) { return SOAP_OK; }
  193. int cwmpService::GetParameterValues(struct _cwmp__GetParameterValues *cwmp__GetParameterValues,
  194. struct _cwmp__GetParameterValuesResponse *cwmp__GetParameterValuesResponse) { return SOAP_OK; }
  195. int cwmpService::SetParameterValues(struct _cwmp__SetParameterValues *cwmp__SetParameterValues,
  196. struct _cwmp__SetParameterValuesResponse *cwmp__SetParameterValuesResponse) { return SOAP_OK; }
  197. int cwmpService::SetParameterAttributes(
  198. struct _cwmp__SetParameterAttributes *cwmp__SetParameterAttributes,
  199. struct _cwmp__SetParameterAttributesResponse *cwmp__SetParameterAttributesResponse) { return SOAP_OK; }
  200. int cwmpService::GetParameterAttributes(struct _cwmp__GetParameterAttributes *cwmp__GetParameterAttributes,
  201. struct _cwmp__GetParameterAttributesResponse *cwmp__GetParameterAttributesResponse) { return SOAP_OK; }
  202. int cwmpService::Download(struct _cwmp__Download *cwmp__Download,
  203. struct _cwmp__DownloadResponse *cwmp__DownloadResponse) { return SOAP_OK; }
  204. int cwmpService::Upload(
  205. struct _cwmp__Upload *cwmp__Upload,
  206. struct _cwmp__UploadResponse *cwmp__UploadResponse) { return SOAP_OK; }
  207. int cwmpService::AddObject(
  208. struct _cwmp__AddObject *cwmp__AddObject,
  209. struct _cwmp__AddObjectResponse *cwmp__AddObjectResponse) { return SOAP_OK; }
  210. int cwmpService::DeleteObject(
  211. struct _cwmp__DeleteObject *cwmp__DeleteObject,
  212. struct _cwmp__DeleteObjectResponse *cwmp__DeleteObjectResponse) { return SOAP_OK; }
  213. int cwmpService::GetRPCMethods(
  214. struct _cwmp__GetRPCMethods *cwmp__GetRPCMethods,
  215. struct _cwmp__GetRPCMethodsResponse *cwmp__GetRPCMethodsResponse) { return SOAP_OK; }
  216. int cwmpService::GetParameterValuesResponse(
  217. struct _cwmp__GetParameterValuesResponse *cwmp__GetParameterValuesResponse) { return SOAP_OK; }
  218. int cwmpService::Inform(struct _cwmp__Inform *cwmp__Inform,
  219. struct _cwmp__InformResponse *cwmp__InformResponse) {
  220. std::cerr << "Inform Recieved..." << cwmp__Inform->DeviceId->SerialNumber << std::endl;
  221. char *imsi;
  222. auto timestamp = std::chrono::high_resolution_clock::now();
  223. std::vector<std::thread> th;
  224. // int indexes[24];
  225. std::vector<int> ind;
  226. for (int i = 0 ; i < cwmp__Inform->ParameterList->__size; ++i) {
  227. if (!strcmp(cwmp__Inform->ParameterList->__ptrParameterValueStruct[i]->Name,
  228. "InternetGatewayDevice.DeviceInfo.X_TGT_IMSI")) {
  229. imsi = cwmp__Inform->ParameterList->__ptrParameterValueStruct[i]->Value;
  230. }
  231. std::string cpe = cwmp__Inform->DeviceId->SerialNumber;
  232. auto j = std::find(std::begin(zbx_keys), std::end(zbx_keys),
  233. cwmp__Inform->ParameterList->__ptrParameterValueStruct[i]->Name);
  234. if (j != std::end(zbx_keys)) {
  235. th.push_back(std::thread(ZBX_Send, &cwmp__Inform->ParameterList->__ptrParameterValueStruct[i]->Name,
  236. &cwmp__Inform->ParameterList->__ptrParameterValueStruct[i]->Value, &imsi));
  237. ind.push_back(i);
  238. }
  239. }
  240. for (auto &t : th) {
  241. t.join();
  242. }
  243. std::ofstream influx_data;
  244. time_t second;
  245. second = time(NULL);
  246. influx_data.open("/var/tmp/influxdb_data", std::ios_base::app);
  247. influx_data << "tr069,imsi=" << imsi << " " ;
  248. try {
  249. for (std::vector<int>::iterator it = ind.begin(); it != ind.end(); ++it){
  250. influx_data << cwmp__Inform->ParameterList->__ptrParameterValueStruct[*it]->Name << "=\""
  251. << cwmp__Inform->ParameterList->__ptrParameterValueStruct[*it]->Value << "\",";
  252. }
  253. influx_data<<std::endl;
  254. }catch (std::exception e){std::cerr<<e.what()<<std::endl;}
  255. return SOAP_OK;
  256. }