/core/src/LsHelper.cpp

https://github.com/BillTheBest/realopinsight-workstation · C++ · 161 lines · 119 code · 19 blank · 23 comment · 13 complexity · ab5d00bec81671ea84e9cb106583bbdf MD5 · raw file

  1. /*
  2. * LsHelper.cpp
  3. # ------------------------------------------------------------------------ #
  4. # Copyright (c) 2010-2014 Rodrigue Chakode (rodrigue.chakode@gmail.com) #
  5. # Last Update: 23-03-2014 #
  6. # #
  7. # This file is part of RealOpInsight (http://RealOpInsight.com) authored #
  8. # by Rodrigue Chakode <rodrigue.chakode@gmail.com> #
  9. # #
  10. # RealOpInsight is free software: you can redistribute it and/or modify #
  11. # it under the terms of the GNU General Public License as published by #
  12. # the Free Software Foundation, either version 3 of the License, or #
  13. # (at your option) any later version. #
  14. # #
  15. # The Software is distributed in the hope that it will be useful, #
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of #
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  18. # GNU General Public License for more details. #
  19. # #
  20. # You should have received a copy of the GNU General Public License #
  21. # along with RealOpInsight. If not, see <http://www.gnu.org/licenses/>. #
  22. #--------------------------------------------------------------------------#
  23. */
  24. #include "LsHelper.hpp"
  25. #include "utilsCore.hpp"
  26. #include "utilsCore.hpp"
  27. #include "RawSocket.hpp"
  28. #include "JsonHelper.hpp"
  29. #include <iostream>
  30. #include <QDir>
  31. #include <QScriptValueIterator>
  32. LsHelper::LsHelper(const QString& host, int port)
  33. : m_socketHandler(new RawSocket(host, port))
  34. {
  35. }
  36. LsHelper::~LsHelper()
  37. {
  38. delete m_socketHandler;
  39. }
  40. int LsHelper::setupSocket(void)
  41. {
  42. if (m_socketHandler->setupSocket()) {
  43. m_lastError = m_socketHandler->lastError();
  44. return -1;
  45. }
  46. return 0;
  47. }
  48. QByteArray LsHelper::prepareRequestData(const QString& host, ReqTypeT requestType)
  49. {
  50. QString data = "";
  51. switch(requestType) {
  52. case LsHelper::Host:
  53. data = "GET hosts\n"
  54. "Columns: name state last_state_change check_command plugin_output groups\n"
  55. "OutputFormat: json\n";
  56. break;
  57. case LsHelper::Service:
  58. data = "GET services\n"
  59. "Columns: host_name service_description state last_state_change check_command plugin_output host_groups\n"
  60. "OutputFormat: json\n";
  61. break;
  62. default:
  63. break;
  64. }
  65. if (! host.isEmpty()) {
  66. QString filterPattern;
  67. switch(requestType) {
  68. case LsHelper::Host:
  69. filterPattern = "Filter: name = %1\n"
  70. "Filter: host_groups ~ %1\n"
  71. "Or: 2\n";
  72. break;
  73. case LsHelper::Service:
  74. filterPattern = "Filter: host_name = %1\n"
  75. "Filter: host_groups ~ %1\n"
  76. "Or: 2\n";
  77. break;
  78. default:
  79. break;
  80. }
  81. data.append(filterPattern.arg(host));
  82. }
  83. return ngrt4n::toByteArray(data.append("\n"));
  84. }
  85. int LsHelper::loadChecks(const QString& host, ChecksT& checks)
  86. {
  87. checks.clear();
  88. if (makeRequest(prepareRequestData(host, LsHelper::Host), checks) != 0)
  89. return -1;
  90. return makeRequest(prepareRequestData(host, LsHelper::Service), checks);
  91. }
  92. int LsHelper::makeRequest(const QByteArray& data, ChecksT& checks)
  93. {
  94. if (m_socketHandler->makeRequest(data) != 0) {
  95. m_lastError = m_socketHandler->lastError();
  96. return -1;
  97. }
  98. parseResult(checks);
  99. return 0;
  100. }
  101. void LsHelper::parseResult(ChecksT& checks)
  102. {
  103. JsonHelper json(m_socketHandler->lastResult());
  104. QScriptValueIterator entryIter(json.data());
  105. while (entryIter.hasNext()) {
  106. entryIter.next();
  107. if (entryIter.flags() & QScriptValue::SkipInEnumeration) continue;
  108. QScriptValueIterator fieldIter(entryIter.value());
  109. QStringList fields;
  110. fields.clear();
  111. while (fieldIter.hasNext()) {
  112. fieldIter.next();
  113. if (fieldIter.flags() & QScriptValue::SkipInEnumeration) continue;
  114. fields.push_back(fieldIter.value().toString());
  115. }
  116. CheckT check;
  117. switch( fields.size() ) {
  118. case 6: // host
  119. check.id = check.host = fields[0].toStdString();
  120. check.status = fields[1].toInt();
  121. check.last_state_change = fields[2].toStdString();
  122. check.check_command = fields[3].toStdString();
  123. check.alarm_msg = fields[4].toStdString();
  124. check.host_groups = fields[5].toStdString();
  125. break;
  126. case 7: // service
  127. check.host = fields[0].toStdString();
  128. check.id = ID_PATTERN.arg(check.host.c_str(), fields[1]).toLower().toStdString();
  129. check.status = fields[2].toInt();
  130. check.last_state_change = fields[3].toStdString();
  131. check.check_command = fields[4].toStdString();
  132. check.alarm_msg = fields[5].toStdString();
  133. check.host_groups = fields[6].toStdString();
  134. break;
  135. default:
  136. qDebug()<< "unexpected entry: "<< entryIter.value().toString();
  137. continue;
  138. break;
  139. }
  140. checks.insert(std::pair<std::string, CheckT>(check.id, check));
  141. }
  142. }