"Fossies" - the Fresh Open Source Software Archive

Member "netxms-3.8.166/src/agent/subagents/mqtt/main.cpp" (23 Feb 2021, 5050 Bytes) of package /linux/misc/netxms-3.8.166.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "main.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 3.6.300_vs_3.7.95.

    1 /*
    2  ** MQTT subagent
    3  ** Copyright (C) 2017-2020 Raden Solutions
    4  **
    5  ** This program is free software; you can redistribute it and/or modify
    6  ** it under the terms of the GNU General Public License as published by
    7  ** the Free Software Foundation; either version 2 of the License, or
    8  ** (at your option) any later version.
    9  **
   10  ** This program is distributed in the hope that it will be useful,
   11  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  ** GNU General Public License for more details.
   14  **
   15  ** You should have received a copy of the GNU General Public License
   16  ** along with this program; if not, write to the Free Software
   17  ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   18  **
   19  **/
   20 #include "mqtt_subagent.h"
   21 #include <netxms-version.h>
   22 
   23 /**
   24  * Registered brokers
   25  */
   26 static ObjectArray<MqttBroker> s_brokers(8, 8, Ownership::True);
   27 static Mutex s_brokersLock;
   28 
   29 /**
   30  * Handler for MQTT.Brokers table
   31  */
   32 static LONG H_BrokersTable(const TCHAR *param, const TCHAR *arg, Table *value, AbstractCommSession *session)
   33 {
   34    value->addColumn(_T("GUID"), DCI_DT_STRING, _T("GUID"), true);
   35    value->addColumn(_T("HOSTNAME"), DCI_DT_STRING, _T("Hostname"));
   36    value->addColumn(_T("PORT"), DCI_DT_UINT, _T("Port"));
   37    value->addColumn(_T("LOGIN"), DCI_DT_STRING, _T("Login"));
   38    value->addColumn(_T("IS_LOCAL"), DCI_DT_STRING, _T("Is local"));
   39    value->addColumn(_T("TOPICS"), DCI_DT_UINT, _T("Topics"));
   40    value->addColumn(_T("IS_CONNECTED"), DCI_DT_UINT, _T("Is connected"));
   41 
   42    s_brokersLock.lock();
   43    for(int i = 0; i < s_brokers.size(); i++)
   44    {
   45       value->addRow();
   46       MqttBroker *b = s_brokers.get(i);
   47       value->set(0, b->getGuid().toString());
   48       value->set(1, b->getHostname());
   49       value->set(2, b->getPort());
   50       value->set(3, CHECK_NULL_EX_A(b->getLogin()));
   51       value->set(4, b->isLocallyConfigured() ? 1 : 0);
   52       value->set(5, b->getTopicCount());
   53       value->set(6, b->isConnected() ? 1 : 0);
   54    }
   55    s_brokersLock.unlock();
   56    return SYSINFO_RC_SUCCESS;
   57 }
   58 
   59 /**
   60  * Add brokers and parameters from config
   61  */
   62 static void RegisterBrokers(StructArray<NETXMS_SUBAGENT_PARAM> *parameters, Config *config)
   63 {
   64    ObjectArray<ConfigEntry> *brokers = config->getSubEntries(_T("/MQTT/Brokers"), _T("*"));
   65    if (brokers != NULL)
   66    {
   67       for(int i = 0; i < brokers->size(); i++)
   68       {
   69          MqttBroker *b = MqttBroker::createFromConfig(brokers->get(i), parameters);
   70          if (b != NULL)
   71          {
   72             s_brokers.add(b);
   73          }
   74          else
   75          {
   76             AgentWriteLog(NXLOG_WARNING, _T("MQTT: cannot add broker %s definition from config"), brokers->get(i)->getName());
   77          }
   78       }
   79       delete brokers;
   80    }
   81    nxlog_debug(3, _T("MQTT: %d parameters added from configuration"), parameters->size());
   82 }
   83 
   84 /**
   85  * Initialize subagent
   86  */
   87 static bool SubAgentInit(Config *config)
   88 {
   89    mosquitto_lib_init();
   90 
   91    int major, minor, rev;
   92    mosquitto_lib_version(&major, &minor, &rev);
   93    nxlog_debug(2, _T("MQTT: using libmosquitto %d.%d.%d"), major, minor, rev);
   94 
   95    // Start network loops
   96    for(int i = 0; i < s_brokers.size(); i++)
   97       s_brokers.get(i)->startNetworkLoop();
   98 
   99    return true;
  100 }
  101 
  102 /**
  103  * Shutdown subagent
  104  */
  105 static void SubAgentShutdown()
  106 {
  107    // Stop network loops
  108    for(int i = 0; i < s_brokers.size(); i++)
  109       s_brokers.get(i)->stopNetworkLoop();
  110 
  111    mosquitto_lib_cleanup();
  112    nxlog_debug(2, _T("MQTT subagent shutdown completed"));
  113 }
  114 
  115 /**
  116  * Command handler
  117  */
  118 static bool CommandHandler(UINT32 command, NXCPMessage *request, NXCPMessage *response, AbstractCommSession *session)
  119 {
  120    switch(command)
  121    {
  122       case CMD_CONFIGURE_MQTT_BROKER:
  123          return true;
  124       case CMD_REMOVE_MQTT_BROKER:
  125          return true;
  126       case CMD_ADD_MQTT_TOPIC:
  127          return true;
  128       case CMD_REMOVE_MQTT_TOPIC:
  129          return true;
  130       default:
  131          return false;
  132    }
  133 }
  134 
  135 /**
  136  * Provided tables
  137  */
  138 static NETXMS_SUBAGENT_TABLE s_tables[] =
  139 {
  140    { _T("MQTT.Brokers"), H_BrokersTable, NULL, _T("GUID"), _T("MQTT: configured brokers") }
  141 };
  142 
  143 /**
  144  * Subagent information
  145  */
  146 static NETXMS_SUBAGENT_INFO m_info =
  147 {
  148     NETXMS_SUBAGENT_INFO_MAGIC,
  149     _T("MQTT"), NETXMS_VERSION_STRING,
  150     SubAgentInit, SubAgentShutdown, CommandHandler, NULL,
  151     0, NULL,    // parameters
  152     0, NULL,        // lists
  153     sizeof(s_tables) / sizeof(NETXMS_SUBAGENT_TABLE), s_tables,
  154     0, NULL,        // actions
  155     0, NULL     // push parameters
  156 };
  157 
  158 /**
  159  * Entry point for NetXMS agent
  160  */
  161 DECLARE_SUBAGENT_ENTRY_POINT(MQTT)
  162 {
  163    StructArray<NETXMS_SUBAGENT_PARAM> parameters;
  164 
  165    RegisterBrokers(&parameters, config);
  166 
  167    m_info.numParameters = parameters.size();
  168    m_info.parameters = MemCopyArray(parameters.getBuffer(), parameters.size());
  169 
  170     *ppInfo = &m_info;
  171     return true;
  172 }
  173 
  174 #ifdef _WIN32
  175 
  176 /**
  177  * DLL entry point
  178  */
  179 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
  180 {
  181    if (dwReason == DLL_PROCESS_ATTACH)
  182       DisableThreadLibraryCalls(hInstance);
  183     return TRUE;
  184 }
  185 
  186 #endif