Finished changes in logwatch subagent to read form logparser_ap folder.
authorTatjana Dubrovica <zev@radensolutions.com>
Thu, 21 Apr 2016 15:25:09 +0000 (18:25 +0300)
committerTatjana Dubrovica <zev@radensolutions.com>
Thu, 21 Apr 2016 15:33:05 +0000 (18:33 +0300)
include/nms_agent.h
src/agent/core/nxagentd.cpp
src/agent/core/policy.cpp
src/agent/subagents/logwatch/logwatch.cpp
src/agent/subagents/logwatch/logwatch.h
webui/webapp/ObjectBrowser/src/org/netxms/ui/eclipse/objectbrowser/api/ObjectAdapter.java
webui/webapp/PolicyManager/plugin.xml
webui/webapp/PolicyManager/src/org/netxms/ui/eclipse/policymanager/actions/CreateAgentPolicyLogParser.java [new file with mode: 0644]
webui/webapp/PolicyManager/src/org/netxms/ui/eclipse/policymanager/propertypages/ParserFile.java [new file with mode: 0644]

index 8faad03..b90d5c7 100644 (file)
 #define AGENT_POLICY_CONFIG      1
 #define AGENT_POLICY_LOG_PARSER  2
 
+/**
+ * Agent log parser policy folder
+ */
+#define LOGPARSER_AP_FOLDER _T("logparser_ap")
+
 /**
  * Error codes
  */
index be88773..b77a9da 100644 (file)
@@ -146,7 +146,7 @@ TCHAR g_szConfigServer[MAX_DB_STRING] = _T("not_set");
 TCHAR g_szRegistrar[MAX_DB_STRING] = _T("not_set");
 TCHAR g_szListenAddress[MAX_PATH] = _T("*");
 TCHAR g_szConfigIncludeDir[MAX_PATH] = AGENT_DEFAULT_CONFIG_D;
-TCHAR g_szLogParserDirectory = "";
+TCHAR g_szLogParserDirectory[MAX_PATH] = _T("");
 TCHAR g_masterAgent[MAX_PATH] = _T("not_set");
 TCHAR g_szSNMPTrapListenAddress[MAX_PATH] = _T("*");
 UINT16 g_wListenPort = AGENT_LISTEN_PORT;
@@ -732,7 +732,7 @@ BOOL Initialize()
    TCHAR tail = g_szDataDirectory[_tcslen(g_szDataDirectory) - 1];
        _sntprintf(g_szLogParserDirectory, MAX_PATH, _T("%s%s%s"), g_szDataDirectory,
                   ((tail != '\\') && (tail != '/')) ? FS_PATH_SEPARATOR : _T(""),
-              _T("logparser_ap") FS_PATH_SEPARATOR);
+              LOGPARSER_AP_FOLDER FS_PATH_SEPARATOR);
    DebugPrintf(INVALID_INDEX, 6, _T("Log parser policy directory: %s"), g_szLogParserDirectory);
        CreateFolder(g_szLogParserDirectory);
 
index e43da9e..094111c 100644 (file)
@@ -136,7 +136,7 @@ static UINT32 DeployLogParser(UINT32 session, const uuid& guid, NXCPMessage *msg
        int fh;
        UINT32 rcc;
 
-       _sntprintf(path, MAX_PATH, _T("%s.xml"), g_szLogParserDirectory
+       _sntprintf(path, MAX_PATH, _T("%s%s.xml"), g_szLogParserDirectory
                ,guid.toString(name));
 
        fh = _topen(path, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, S_IRUSR | S_IWUSR);
@@ -232,13 +232,11 @@ static UINT32 RemoveConfig(UINT32 session, const uuid& guid, NXCPMessage *msg)
  */
 static UINT32 RemoveLogParser(UINT32 session, const uuid& guid,  NXCPMessage *msg)
 {
-       TCHAR path[MAX_PATH], name[64], tail;
+       TCHAR path[MAX_PATH], name[64];
        UINT32 rcc;
 
-       tail = g_szConfigIncludeDir[_tcslen(g_szConfigIncludeDir) - 1];
-       _sntprintf(path, MAX_PATH, _T("%s%s%s.conf"), g_szConfigIncludeDir,
-                  ((tail != '\\') && (tail != '/')) ? FS_PATH_SEPARATOR : _T(""),
-              guid.toString(name));
+       _sntprintf(path, MAX_PATH, _T("%s%s.xml"), g_szLogParserDirectory
+               ,guid.toString(name));
 
        if (_tremove(path) != 0)
        {
index e661f25..98297b1 100644 (file)
@@ -216,6 +216,49 @@ static void AddParserFromConfig(const TCHAR *file)
        }
 }
 
+/**
+ * Add to logwatch everything inside logwatch policy folder
+ */
+static void AddLogwatchPolicyFiles()
+{
+   const TCHAR *dataDir = AgentGetDataDirectory();
+   TCHAR policyFolder[MAX_PATH];
+   TCHAR tail = dataDir[_tcslen(dataDir) - 1];
+       _sntprintf(policyFolder, MAX_PATH, _T("%s%s%s"), dataDir,
+                  ((tail != '\\') && (tail != '/')) ? FS_PATH_SEPARATOR : _T(""),
+              LOGPARSER_AP_FOLDER FS_PATH_SEPARATOR);
+
+   AgentWriteDebugLog(1, _T("AddLogwatchPolicyFiles(): Log parser policy directory: %s"), policyFolder);
+
+   _TDIR *dir = _topendir(policyFolder);
+   if (dir != NULL)
+   {
+      struct _tdirent *d;
+      while((d = _treaddir(dir)) != NULL)
+      {
+         if (!_tcscmp(d->d_name, _T(".")) || !_tcscmp(d->d_name, _T("..")))
+         {
+            continue;
+         }
+
+         TCHAR fullName[MAX_PATH];
+         _tcscpy(fullName, policyFolder);
+         _tcscat(fullName, FS_PATH_SEPARATOR);
+         _tcscat(fullName, d->d_name);
+
+         NX_STAT_STRUCT st;
+         if (CALL_STAT(fullName, &st) == 0)
+         {
+            if(S_ISREG(st.st_mode))
+            {
+               AddParserFromConfig(fullName);
+            }
+         }
+      }
+      _tclosedir(dir);
+   }
+}
+
 /**
  * Subagent initialization
  */
@@ -231,6 +274,7 @@ static BOOL SubagentInit(Config *config)
                for(int i = 0; i < parsers->getValueCount(); i++)
                        AddParserFromConfig(parsers->getValue(i));
        }
+   AddLogwatchPolicyFiles();
 
        // Create shutdown condition and start parsing threads
        s_shutdownCondition = ConditionCreate(TRUE);
index 1d7607f..7d28e6f 100644 (file)
@@ -27,5 +27,6 @@
 #include <nms_util.h>
 #include <nms_agent.h>
 #include <nxlpapi.h>
+#include <nxstat.h>
 
 #endif
index 7d2135d..8ac3911 100644 (file)
@@ -92,6 +92,7 @@ public class ObjectAdapter implements IWorkbenchAdapter
                                return Activator.getImageDescriptor("icons/policy_group.png"); //$NON-NLS-1$
                        case AbstractObject.OBJECT_AGENTPOLICY:
                        case AbstractObject.OBJECT_AGENTPOLICY_CONFIG:
+                       case AbstractObject.OBJECT_AGENTPOLICY_LOGPARSER:
                                return Activator.getImageDescriptor("icons/policy.png"); //$NON-NLS-1$
                        case AbstractObject.OBJECT_NETWORKMAP:
                                return Activator.getImageDescriptor("icons/netmap.png"); //$NON-NLS-1$
index 7d8bd4e..02661dc 100644 (file)
              </instanceof>
           </enabledWhen>
         </page>
+        <page
+              class="org.netxms.ui.eclipse.policymanager.propertypages.ParserFile"
+              id="org.netxms.ui.eclipse.policymanager.propertypages.ParserFile"
+              name="Log Parser File">
+           <enabledWhen>
+              <instanceof
+                    value="org.netxms.client.objects.AgentPolicyLogParser">
+              </instanceof>
+           </enabledWhen>
+        </page>
   </extension>
 
    <extension
                label="%action.label.CreatePolicyGrp"
                menubarPath="objectcreation">
          </action>
+         <action
+               class="org.netxms.ui.eclipse.policymanager.actions.CreateAgentPolicyLogParser"
+               enablesFor="1"
+               id="org.netxms.ui.eclipse.policymanager.popupActions.CreateAgentPolicyLogParser_PolicyRoot"
+               label="Create log parser policy(xml file)..."
+               menubarPath="objectcreation">
+         </action>
       </objectContribution>
       <objectContribution
             adaptable="false"
                label="%action.label.CreatePolicyGrp"
                menubarPath="objectcreation">
          </action>
+         <action
+               class="org.netxms.ui.eclipse.policymanager.actions.CreateAgentPolicyLogParser"
+               enablesFor="1"
+               id="org.netxms.ui.eclipse.policymanager.popupActions.CreateAgentPolicyLogParser_PolicyGroup"
+               label="Create log parser policy(xml file)..."
+               menubarPath="objectcreation">
+         </action>
       </objectContribution>
       <objectContribution
             adaptable="false"
diff --git a/webui/webapp/PolicyManager/src/org/netxms/ui/eclipse/policymanager/actions/CreateAgentPolicyLogParser.java b/webui/webapp/PolicyManager/src/org/netxms/ui/eclipse/policymanager/actions/CreateAgentPolicyLogParser.java
new file mode 100644 (file)
index 0000000..038978e
--- /dev/null
@@ -0,0 +1,96 @@
+/**
+ * NetXMS - open source network management system
+ * Copyright (C) 2003-2011 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+package org.netxms.ui.eclipse.policymanager.actions;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.TreeSelection;
+import org.eclipse.jface.window.Window;
+import org.eclipse.ui.IObjectActionDelegate;
+import org.eclipse.ui.IWorkbenchPart;
+import org.netxms.client.NXCObjectCreationData;
+import org.netxms.client.NXCSession;
+import org.netxms.client.objects.AbstractObject;
+import org.netxms.client.objects.PolicyGroup;
+import org.netxms.client.objects.PolicyRoot;
+import org.netxms.ui.eclipse.jobs.ConsoleJob;
+import org.netxms.ui.eclipse.objectbrowser.dialogs.CreateObjectDialog;
+import org.netxms.ui.eclipse.policymanager.Activator;
+import org.netxms.ui.eclipse.policymanager.Messages;
+import org.netxms.ui.eclipse.shared.ConsoleSharedData;
+
+/**
+ * @author Victor
+ *
+ */
+public class CreateAgentPolicyLogParser implements IObjectActionDelegate
+{
+       private IWorkbenchPart targetPart;
+       private AbstractObject currentObject;
+       
+       /* (non-Javadoc)
+        * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
+        */
+       @Override
+       public void setActivePart(IAction action, IWorkbenchPart targetPart)
+       {
+               this.targetPart = targetPart;
+       }
+
+       /* (non-Javadoc)
+        * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
+        */
+       @Override
+       public void run(IAction action)
+       {
+               final CreateObjectDialog dlg = new CreateObjectDialog(targetPart.getSite().getShell(), Messages.get().CreateAgentPolicyConfig_AgentPolicy_Config);
+               if (dlg.open() == Window.OK)
+               {
+                       final NXCSession session = (NXCSession)ConsoleSharedData.getSession();
+                       new ConsoleJob(Messages.get().CreateAgentPolicyConfig_JobName, targetPart, Activator.PLUGIN_ID, null) {
+                               @Override
+                               protected void runInternal(IProgressMonitor monitor) throws Exception
+                               {
+                                       NXCObjectCreationData cd = new NXCObjectCreationData(AbstractObject.OBJECT_AGENTPOLICY_LOGPARSER, dlg.getObjectName(), currentObject.getObjectId());
+                                       session.createObject(cd);
+                               }
+
+                               @Override
+                               protected String getErrorMessage()
+                               {
+                                       return Messages.get().CreateAgentPolicyConfig_JobError;
+                               }
+                       }.start();
+               }
+       }
+
+       /* (non-Javadoc)
+        * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
+        */
+       @Override
+       public void selectionChanged(IAction action, ISelection selection)
+       {
+               if (selection instanceof TreeSelection)
+               {
+                       currentObject = (AbstractObject)((TreeSelection)selection).getFirstElement();
+                       action.setEnabled((currentObject instanceof PolicyRoot) || (currentObject instanceof PolicyGroup));
+               }
+       }
+}
diff --git a/webui/webapp/PolicyManager/src/org/netxms/ui/eclipse/policymanager/propertypages/ParserFile.java b/webui/webapp/PolicyManager/src/org/netxms/ui/eclipse/policymanager/propertypages/ParserFile.java
new file mode 100644 (file)
index 0000000..fb40927
--- /dev/null
@@ -0,0 +1,156 @@
+/**
+ * NetXMS - open source network management system
+ * Copyright (C) 2003-2010 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+package org.netxms.ui.eclipse.policymanager.propertypages;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Text;
+import org.eclipse.ui.dialogs.PropertyPage;
+import org.netxms.client.NXCObjectModificationData;
+import org.netxms.client.NXCSession;
+import org.netxms.client.objects.AgentPolicyLogParser;
+import org.netxms.ui.eclipse.jobs.ConsoleJob;
+import org.netxms.ui.eclipse.policymanager.Activator;
+import org.netxms.ui.eclipse.policymanager.Messages;
+import org.netxms.ui.eclipse.shared.ConsoleSharedData;
+import org.netxms.ui.eclipse.tools.WidgetHelper;
+
+/**
+ * Property page for agent configuration policy
+ *
+ */
+public class ParserFile extends PropertyPage
+{
+       private Text textContent;
+       private String initialContent;
+       private AgentPolicyLogParser object;
+       
+       
+       /* (non-Javadoc)
+        * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
+        */
+       @Override
+       protected Control createContents(Composite parent)
+       {
+               Composite dialogArea = new Composite(parent, SWT.NONE);
+               
+               object = (AgentPolicyLogParser)getElement().getAdapter(AgentPolicyLogParser.class);
+               if (object == null)     // Paranoid check
+                       return dialogArea;
+               
+               GridLayout layout = new GridLayout();
+               layout.verticalSpacing = WidgetHelper.OUTER_SPACING;
+               layout.marginWidth = 0;
+               layout.marginHeight = 0;
+               //FormLayout layout = new FormLayout();
+      dialogArea.setLayout(layout);
+      
+               // File content
+      Label label = new Label(dialogArea, SWT.NONE);
+      label.setText(Messages.get().ConfigFile_File);
+      
+      initialContent = new String(object.getFileContent());
+      GridData gd = new GridData();
+      gd.grabExcessHorizontalSpace = true;
+      gd.grabExcessVerticalSpace = true;
+      gd.horizontalAlignment = GridData.FILL;
+      gd.verticalAlignment = GridData.FILL;
+      gd.heightHint = 0;
+      textContent = new Text(dialogArea, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
+      textContent.setText(object.getFileContent());
+      textContent.setLayoutData(gd);
+      
+               return dialogArea;
+       }
+       
+       
+       /**
+        * Apply changes
+        * 
+        * @param isApply true if update operation caused by "Apply" button
+        */
+       protected void applyChanges(final boolean isApply)
+       {
+               if (textContent.getText().equals(initialContent))
+                       return;         // Nothing to apply
+               
+               if (isApply)
+                       setValid(false);
+               
+               final String newContent = new String(textContent.getText());
+               new ConsoleJob(Messages.get().ConfigFile_JobName, null, Activator.PLUGIN_ID, null) {
+                       @Override
+                       protected void runInternal(IProgressMonitor monitor) throws Exception
+                       {
+                               if (object != null)
+                               {
+                                       NXCObjectModificationData md = new NXCObjectModificationData(object.getObjectId());
+                                       md.setConfigFileContent(newContent);
+                                       ((NXCSession)ConsoleSharedData.getSession()).modifyObject(md);
+                               }
+                               initialContent = newContent;
+                       }
+
+                       @Override
+                       protected void jobFinalize()
+                       {
+                               if (isApply)
+                               {
+                                  runInUIThread(new Runnable() {
+                  @Override
+                  public void run()
+                  {
+                     ParserFile.this.setValid(true);
+                  }
+               });
+                               }
+                       }
+
+                       @Override
+                       protected String getErrorMessage()
+                       {
+                               return Messages.get().ConfigFile_JobError;
+                       }
+               }.start();
+       }
+       
+       /* (non-Javadoc)
+        * @see org.eclipse.jface.preference.PreferencePage#performOk()
+        */
+       @Override
+       public boolean performOk()
+       {
+               applyChanges(false);
+               return true;
+       }
+
+       /* (non-Javadoc)
+        * @see org.eclipse.jface.preference.PreferencePage#performApply()
+        */
+       @Override
+       protected void performApply()
+       {
+               applyChanges(true);
+       }
+}