- Added signal handlers to agent
authorVictor Kirhenshtein <victor@netxms.org>
Fri, 7 Jan 2005 19:38:59 +0000 (19:38 +0000)
committerVictor Kirhenshtein <victor@netxms.org>
Fri, 7 Jan 2005 19:38:59 +0000 (19:38 +0000)
- Solaris subagent: added parameter System.ProcessCount
- Solaris subagent: fixed bug in process name matching

src/agent/core/log.cpp
src/agent/core/messages.mc
src/agent/core/nxagentd.cpp
src/agent/subagents/sunos/main.cpp
src/agent/subagents/sunos/process.cpp

index a20216b..f461eb1 100644 (file)
@@ -122,7 +122,10 @@ static void WriteLogToFile(char *szMessage)
    loc = localtime(&t);
    strftime(szBuffer, 32, "[%d-%b-%Y %H:%M:%S]", loc);
    if (m_hLogFile != NULL)
+       {
       fprintf(m_hLogFile, "%s %s", szBuffer, szMessage);
+               fflush(m_hLogFile);
+       }
    if (!(g_dwFlags & AF_DAEMON))
       printf("%s %s", szBuffer, szMessage);
 
index b302e3d..be1a105 100644 (file)
@@ -164,4 +164,10 @@ Language=English
 Unable to create temporary file to hold process output: %1
 .
 
+MessageId=
+SymbolicName=MSG_SIGNAL_RECEIVED
+Language=English
+Signal %1 received
+.
+
 ;#endif
index 9bb2642..c80c07c 100644 (file)
 
 #include "nxagentd.h"
 
-#ifdef _WIN32
+#if defined(_WIN32)
 #include <conio.h>
-#endif
-
-#ifdef _NETWARE
+#elif defined(_NETWARE)
 #include <screen.h>
+#else
+#include <signal.h>
 #endif
 
 
@@ -243,6 +243,33 @@ static void WriteSubAgentMsg(int iLevel, TCHAR *pszMsg)
 }
 
 
+//
+// Signal handler for UNIX platforms
+//
+
+#ifndef _WIN32
+
+void OnSignal(int iSignal)
+{
+       WriteLog(MSG_SIGNAL_RECEIVED, EVENTLOG_WARNING_TYPE, "d", iSignal);
+       switch(iSignal)
+       {
+               case SIGTERM:
+               case SIGINT:
+                       ConditionSet(m_hCondShutdown);
+                       break;
+               case SIGSEGV:
+                       exit(5);
+                       break;
+               default:
+                       break;
+       }
+}
+
+#endif
+
+
+
 //
 // Initialization routine
 //
@@ -587,6 +614,11 @@ int main(int argc, char *argv[])
                   perror("Unable to setup itself as a daemon");
                   iExitCode = 4;
                }
+                               // Setup signal handlers
+                               for(int i = 0; i < 32; i++)
+                                       signal(i, SIG_IGN);
+                               signal(SIGTERM, OnSignal);
+                               signal(SIGSEGV, OnSignal);
 #endif
             if (iExitCode == 0)
             {
index badf42c..91d30d6 100644 (file)
@@ -30,7 +30,9 @@
 LONG H_DiskInfo(char *pszParam, char *pArg, char *pValue);
 LONG H_NetIfList(char *pszParam, char *pArg, NETXMS_VALUES_LIST *pValue);
 LONG H_ProcessCount(char *pszParam, char *pArg, char *pValue);
+LONG H_ProcessInfo(char *pszParam, char *pArg, char *pValue);
 LONG H_ProcessList(char *pszParam, char *pArg, NETXMS_VALUES_LIST *pValue);
+LONG H_SysProcCount(char *pszParam, char *pArg, char *pValue);
 
 
 //
@@ -51,7 +53,8 @@ static NETXMS_SUBAGENT_PARAM m_parameters[] =
    { "Disk.Free(*)", H_DiskInfo, (char *)DISK_FREE },
    { "Disk.Total(*)", H_DiskInfo, (char *)DISK_TOTAL },
    { "Disk.Used(*)", H_DiskInfo, (char *)DISK_USED },
-   { "Process.Count(*)", H_ProcessCount, NULL }
+   { "Process.Count(*)", H_ProcessCount, NULL },
+   { "System.ProcessCount", H_SysProcCount, NULL }
 };
 static NETXMS_SUBAGENT_ENUM m_enums[] =
 {
index 96f5f94..cebcf52 100644 (file)
@@ -53,22 +53,15 @@ static int ProcFilter(const struct dirent *pEnt)
 
 //
 // Read process list from /proc filesystem
+// Returns only processes with names matched to pszPattern
+// (currently with strcmp) ot, if pszPattern is NULL, all
+// processes in the system.
 //
 
-static int ProcRead(PROC_ENT **pEnt, char *szPatern)
+static int ProcRead(PROC_ENT **pEnt, char *pszPattern)
 {
        struct dirent **pNameList;
        int nCount, nFound;
-       char *pPatern;
-
-       if (szPatern != NULL)
-       {
-               pPatern = szPatern;
-       }
-       else
-       {
-               pPatern = "";
-       }
 
        nFound = -1;
        if (pEnt != NULL)
@@ -112,7 +105,7 @@ static int ProcRead(PROC_ENT **pEnt, char *szPatern)
 
                                if (read(hFile, &psi, sizeof(psinfo_t)) == sizeof(psinfo_t))
                                {
-                                       if (strstr(psi.pr_fname, pPatern) != NULL)
+                                       if ((pszPattern == NULL) || (!strcmp(psi.pr_fname, CHECK_NULL_EX(pszPattern))))
                                        {
                                                if (pEnt != NULL)
                                                {
@@ -145,7 +138,7 @@ LONG H_ProcessList(char *pszParam, char *pArg, NETXMS_VALUES_LIST *pValue)
        int i, nProc;
        char szBuffer[256];
 
-       nProc = ProcRead(&pList, "");
+       nProc = ProcRead(&pList, NULL);
        if (nProc != -1)
        {
                for(i = 0; i < nProc; i++)
@@ -187,6 +180,26 @@ LONG H_ProcessCount(char *pszParam, char *pArg, char *pValue)
 }
 
 
+//
+// Handler for System.ProcessCount parameter
+//
+
+LONG H_SysProcCount(char *pszParam, char *pArg, char *pValue)
+{
+       int nRet = SYSINFO_RC_ERROR;
+       int nCount;
+
+       nCount = ProcRead(NULL, NULL);
+       if (nCount >= 0)
+       {
+               ret_int(pValue, nCount);
+               nRet = SYSINFO_RC_SUCCESS;
+       }
+
+       return nRet;
+}
+
+
 //
 // Handler for Process.XXX parameters
 //