agent restart issues restart command to all connected external subagents
authorVictor Kirhenshtein <victor@netxms.org>
Tue, 1 Aug 2017 09:00:37 +0000 (12:00 +0300)
committerVictor Kirhenshtein <victor@netxms.org>
Tue, 1 Aug 2017 09:00:37 +0000 (12:00 +0300)
include/nms_cscp.h
src/agent/core/extagent.cpp
src/agent/core/master.cpp
src/agent/core/nxagentd.cpp
src/agent/core/nxagentd.h
src/libnetxms/nxcp.cpp

index de78551..4e8dd6f 100644 (file)
@@ -586,6 +586,7 @@ typedef struct
 #define CMD_CLOSE_CHANNEL                 0x015F
 #define CMD_CREATE_OBJECT_ACCESS_SNAPSHOT 0x0160
 #define CMD_UNBIND_AGENT_TUNNEL           0x0161
+#define CMD_RESTART                       0x0162
 
 #define CMD_RS_LIST_REPORTS            0x1100
 #define CMD_RS_GET_REPORT              0x1101
index ec51669..6660f6f 100644 (file)
@@ -1,6 +1,6 @@
 /* 
 ** NetXMS multiplatform core agent
-** Copyright (C) 2003-2013 Victor Kirhenshtein
+** Copyright (C) 2003-2017 Victor Kirhenshtein
 **
 ** This program is free software; you can redistribute it and/or modify
 ** it under the terms of the GNU General Public License as published by
@@ -130,13 +130,20 @@ void ExternalSubagent::connect(HPIPE hPipe)
  */
 void ExternalSubagent::shutdown()
 {
-       NXCPMessage msg;
-       msg.setCode(CMD_SHUTDOWN);
-       msg.setId(m_requestId++);
+       NXCPMessage msg(CMD_SHUTDOWN, m_requestId++);
        sendMessage(&msg);
 }
 
 /**
+ * Send restart command
+ */
+void ExternalSubagent::restart()
+{
+   NXCPMessage msg(CMD_RESTART, m_requestId++);
+   sendMessage(&msg);
+}
+
+/**
  * Get list of supported parameters
  */
 NETXMS_SUBAGENT_PARAM *ExternalSubagent::getSupportedParameters(UINT32 *count)
@@ -846,8 +853,23 @@ void ShutdownExtSubagents()
        {
                if (s_subagents.get(i)->isConnected())
                {
-         DebugPrintf(1, _T("Sending SHUTDOWN command to external subagent %s"), s_subagents.get(i)->getName());
+         nxlog_debug(1, _T("Sending SHUTDOWN command to external subagent %s"), s_subagents.get(i)->getName());
          s_subagents.get(i)->shutdown();
                }
        }
 }
+
+/**
+ * Restart all connected external subagents
+ */
+void RestartExtSubagents()
+{
+   for(int i = 0; i < s_subagents.size(); i++)
+   {
+      if (s_subagents.get(i)->isConnected())
+      {
+         nxlog_debug(1, _T("Sending RESTART command to external subagent %s"), s_subagents.get(i)->getName());
+         s_subagents.get(i)->restart();
+      }
+   }
+}
index 618da66..e272336 100644 (file)
@@ -1,6 +1,6 @@
 /*
 ** NetXMS multiplatform core agent
-** Copyright (C) 2003-2013 Victor Kirhenshtein
+** Copyright (C) 2003-2017 Victor Kirhenshtein
 **
 ** This program is free software; you can redistribute it and/or modify
 ** it under the terms of the GNU General Public License as published by
 #include "nxagentd.h"
 
 /**
+ * Restart agent
+ */
+LONG RestartAgent();
+
+/**
  * Handler for CMD_GET_PARAMETER command
  */
 static void H_GetParameter(NXCPMessage *pRequest, NXCPMessage *pMsg)
@@ -168,6 +173,11 @@ THREAD_RESULT THREAD_CALL MasterAgentListener(void *arg)
                   ThreadSleep(10);
                   exit(0);
                   break;
+               case CMD_RESTART:
+                  RestartAgent();
+                  ThreadSleep(10);
+                  exit(0);
+                  break;
                                        default:
                                                response.setField(VID_RCC, ERR_UNKNOWN_COMMAND);
                                                break;
index d397e54..b45d6eb 100644 (file)
@@ -482,34 +482,46 @@ static THREAD_RESULT THREAD_CALL ShutdownThread(void *pArg)
 
 #endif   /* _WIN32 */
 
-
 /**
  * Restart agent
  */
-static LONG H_RestartAgent(const TCHAR *action, StringList *args, const TCHAR *data, AbstractCommSession *session)
+LONG RestartAgent()
 {
-       DebugPrintf(1, _T("H_RestartAgent() called"));
+       nxlog_debug(1, _T("RestartAgent() called"));
 
-   TCHAR szCmdLine[4096], szPlatformSuffixOption[MAX_PSUFFIX_LENGTH + 16];
+       RestartExtSubagents();
 
+   TCHAR platformSuffixOption[MAX_PSUFFIX_LENGTH + 16];
        if (g_szPlatformSuffix[0] != 0)
        {
-               _sntprintf(szPlatformSuffixOption, MAX_PSUFFIX_LENGTH + 16, _T("-P \"%s\" "), g_szPlatformSuffix);
+               _sntprintf(platformSuffixOption, MAX_PSUFFIX_LENGTH + 16, _T("-P \"%s\" "), g_szPlatformSuffix);
+       }
+       else
+       {
+          platformSuffixOption[0] = 0;
+       }
+
+       const TCHAR *configSection = g_config->getAlias(_T("agent"));
+       TCHAR configSectionOption[256];
+       if ((configSection != NULL) && (*configSection != 0))
+       {
+          _sntprintf(configSectionOption, 256, _T("-G %s "), configSection);
        }
        else
        {
-               szPlatformSuffixOption[0] = 0;
+          configSectionOption[0] = 0;
        }
 
+   TCHAR szCmdLine[4096];
 #ifdef _WIN32
-   _sntprintf(szCmdLine, 4096, _T("\"%s\" -c \"%s\" -n \"%s\" -e \"%s\" %s%s%s%s%s-D %d %s-X %u"), s_executableName,
+   _sntprintf(szCmdLine, 4096, _T("\"%s\" -c \"%s\" -n \"%s\" -e \"%s\" %s%s%s%s%s-D %d %s%s-X %u"), s_executableName,
               g_szConfigFile, g_windowsServiceName, g_windowsEventSourceName,
                                  (g_dwFlags & AF_DAEMON) ? _T("-d ") : _T(""),
               (g_dwFlags & AF_HIDE_WINDOW) ? _T("-H ") : _T(""),
                                  (g_dwFlags & AF_CENTRAL_CONFIG) ? _T("-M ") : _T(""),
                                  (g_dwFlags & AF_CENTRAL_CONFIG) ? g_szConfigServer : _T(""),
                                  (g_dwFlags & AF_CENTRAL_CONFIG) ? _T(" ") : _T(""),
-                                 s_debugLevel, szPlatformSuffixOption,
+                                 s_debugLevel, platformSuffixOption, configSectionOption,
               (g_dwFlags & AF_DAEMON) ? 0 : GetCurrentProcessId());
        DebugPrintf(1, _T("Restarting agent with command line '%s'"), szCmdLine);
 
@@ -549,12 +561,12 @@ static LONG H_RestartAgent(const TCHAR *action, StringList *args, const TCHAR *d
    }
    return dwResult;
 #else
-   _sntprintf(szCmdLine, 4096, _T("\"%s\" -c \"%s\" %s%s%s%s-D %d %s-X %lu"), s_executableName,
+   _sntprintf(szCmdLine, 4096, _T("\"%s\" -c \"%s\" %s%s%s%s-D %d %s%s-X %lu"), s_executableName,
               g_szConfigFile, (g_dwFlags & AF_DAEMON) ? _T("-d ") : _T(""),
                                  (g_dwFlags & AF_CENTRAL_CONFIG) ? _T("-M ") : _T(""),
                                  (g_dwFlags & AF_CENTRAL_CONFIG) ? g_szConfigServer : _T(""),
                                  (g_dwFlags & AF_CENTRAL_CONFIG) ? _T(" ") : _T(""),
-                                 (int)s_debugLevel, szPlatformSuffixOption,
+                                 (int)s_debugLevel, platformSuffixOption, configSectionOption,
               (unsigned long)s_pid);
        DebugPrintf(1, _T("Restarting agent with command line '%s'"), szCmdLine);
    return ExecuteCommand(szCmdLine, NULL, NULL);
@@ -562,6 +574,14 @@ static LONG H_RestartAgent(const TCHAR *action, StringList *args, const TCHAR *d
 }
 
 /**
+ * Handler for Agent.Restart action
+ */
+static LONG H_RestartAgent(const TCHAR *action, StringList *args, const TCHAR *data, AbstractCommSession *session)
+{
+   return RestartAgent();
+}
+
+/**
  * This function writes message from subagent to agent's log
  */
 static void WriteSubAgentMsg(int logLevel, int debugLevel, const TCHAR *pszMsg)
index 4bc7ced..18a7dc0 100644 (file)
@@ -257,6 +257,7 @@ public:
        void listTables(NXCPMessage *msg, UINT32 *baseId, UINT32 *count);
        void listTables(StringList *list);
    void shutdown();
+   void restart();
 };
 
 /**
@@ -594,6 +595,7 @@ bool SendMessageToPipe(HPIPE hPipe, NXCP_MESSAGE *msg);
 bool SendMessageToMasterAgent(NXCPMessage *msg);
 bool SendRawMessageToMasterAgent(NXCP_MESSAGE *msg);
 void ShutdownExtSubagents();
+void RestartExtSubagents();
 
 void RegisterApplicationAgent(const TCHAR *name);
 UINT32 GetParameterValueFromAppAgent(const TCHAR *name, TCHAR *buffer);
index e336758..c4564fc 100644 (file)
@@ -391,10 +391,11 @@ TCHAR LIBNETXMS_EXPORTABLE *NXCPMessageCodeName(WORD code, TCHAR *pszBuffer)
       _T("CMD_CHANNEL_DATA"),
       _T("CMD_CLOSE_CHANNEL"),
       _T("CMD_CREATE_OBJECT_ACCESS_SNAPSHOT"),
-      _T("CMD_UNBIND_AGENT_TUNNEL")
+      _T("CMD_UNBIND_AGENT_TUNNEL"),
+      _T("CMD_RESTART")
    };
 
-   if ((code >= CMD_LOGIN) && (code <= CMD_UNBIND_AGENT_TUNNEL))
+   if ((code >= CMD_LOGIN) && (code <= CMD_RESTART))
    {
       _tcscpy(pszBuffer, pszMsgNames[code - CMD_LOGIN]);
    }