added support for CMD_CHECK_NETWORK_SERVICE
authorAlex Kirhenshtein <alk@netxms.org>
Fri, 28 Jan 2005 02:50:32 +0000 (02:50 +0000)
committerAlex Kirhenshtein <alk@netxms.org>
Fri, 28 Jan 2005 02:50:32 +0000 (02:50 +0000)
suported:
ssh: host/port req.
pop3: host/port/request string req. request string format: "login:password"

.gitattributes
src/agent/subagents/portCheck/main.cpp
src/agent/subagents/portCheck/main.h [new file with mode: 0644]
src/agent/subagents/portCheck/net.cpp
src/agent/subagents/portCheck/net.h
src/agent/subagents/portCheck/pop3.cpp
src/agent/subagents/portCheck/pop3.h
src/agent/subagents/portCheck/ssh.cpp
src/agent/subagents/portCheck/ssh.h

index 3e8e908..7cc288a 100644 (file)
@@ -186,6 +186,7 @@ src/agent/subagents/ping/ping.dsp -text
 src/agent/subagents/ping/ping.dsw -text
 src/agent/subagents/portCheck/Makefile.am -text
 src/agent/subagents/portCheck/main.cpp -text
+src/agent/subagents/portCheck/main.h -text
 src/agent/subagents/portCheck/net.cpp -text
 src/agent/subagents/portCheck/net.h -text
 src/agent/subagents/portCheck/pop3.cpp -text
index 59011e9..ea03c1b 100644 (file)
@@ -1,13 +1,85 @@
-/* $Id: main.cpp,v 1.2 2005-01-19 13:42:47 alk Exp $ */
+/* $Id: main.cpp,v 1.3 2005-01-28 02:50:32 alk Exp $ */
 
 #include <nms_common.h>
 #include <nms_agent.h>
+#include <nxclapi.h>
+#include <nxcscpapi.h>
 
+
+#include "main.h"
 #include "net.h"
 #include "pop3.h"
 #include "ssh.h"
 
 //
+// Command handler
+//
+BOOL CommandHandler(DWORD dwCommand, CSCPMessage *pRequest, CSCPMessage *pResponce)
+{
+       BOOL bHandled = TRUE;
+       WORD wType, wPort, wIpProto;
+       DWORD dwAddress;
+       char szRequest[1024 * 10];
+       char szResponce[1024 * 10];
+       DWORD nRet;
+       
+       if (dwCommand != CMD_CHECK_NETWORK_SERVICE)
+       {
+               return FALSE;
+       }
+
+       wType = pRequest->GetVariableShort(VID_SERVICE_TYPE);
+       wPort = pRequest->GetVariableShort(VID_IP_PORT);
+       dwAddress = pRequest->GetVariableLong(VID_IP_ADDRESS);
+       pRequest->GetVariableStr(VID_SERVICE_REQUEST, szRequest, sizeof(szRequest));
+       pRequest->GetVariableStr(VID_SERVICE_RESPONCE, szResponce, sizeof(szResponce));
+
+       switch(wType)
+       {
+       case NETSRV_CUSTOM:
+               // unsupported for now
+               bHandled = FALSE;
+               break;
+       case NETSRV_SSH:
+                       nRet = CheckSSH(NULL, dwAddress, wPort, NULL, NULL);
+
+                       pResponce->SetVariable(VID_RCC, nRet);
+               break;
+       case NETSRV_POP3:
+               {
+                       char *pUser, *pPass;
+                       nRet = PC_ERR_PARAM;
+
+                       pUser = szRequest;
+                       pPass = strchr(szRequest, ':');
+                       if (pPass != NULL)
+                       {
+                               *pPass = 0;
+                               pPass++;
+
+                               nRet = CheckPOP3(NULL, dwAddress, wPort, pUser, pPass);
+                       }
+
+                       pResponce->SetVariable(VID_RCC, nRet);
+               }
+               break;
+       case NETSRV_SMTP:
+               bHandled = FALSE;
+               break;
+       case NETSRV_FTP:
+               bHandled = FALSE;
+               break;
+       case NETSRV_HTTP:
+               break;
+       default:
+               bHandled = FALSE;
+               break;
+       }
+
+       return bHandled;
+}
+
+//
 // Subagent information
 //
 
@@ -27,7 +99,8 @@ static NETXMS_SUBAGENT_INFO m_info =
    NETXMS_SUBAGENT_INFO_MAGIC,
        "portCheck",
        NETXMS_VERSION_STRING,
-       NULL, NULL,
+       NULL, // unload handler
+       &CommandHandler,
        sizeof(m_parameters) / sizeof(NETXMS_SUBAGENT_PARAM),
        m_parameters,
        sizeof(m_enums) / sizeof(NETXMS_SUBAGENT_ENUM),
@@ -49,6 +122,9 @@ extern "C" BOOL NxSubAgentInit(NETXMS_SUBAGENT_INFO **ppInfo)
 /*
 
 $Log: not supported by cvs2svn $
+Revision 1.2  2005/01/19 13:42:47  alk
++ ServiceCheck.SSH(host[, port]) Added
+
 Revision 1.1.1.1  2005/01/18 18:38:54  alk
 Initial import
 
diff --git a/src/agent/subagents/portCheck/main.h b/src/agent/subagents/portCheck/main.h
new file mode 100644 (file)
index 0000000..5898984
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef __MAIN__H__
+#define __MAIN__H__
+
+enum
+{
+       PC_ERR_NONE,
+       PC_ERR_PARAM,
+       PC_ERR_CONNECT,
+       PC_ERR_HANDSHAKE,
+};
+
+#endif // __MAIN__H__
index a3ec1ab..41b5611 100644 (file)
@@ -1,11 +1,11 @@
-/* $Id: net.cpp,v 1.1.1.1 2005-01-18 18:38:54 alk Exp $ */
+/* $Id: net.cpp,v 1.2 2005-01-28 02:50:32 alk Exp $ */
 
 #include <nms_common.h>
 #include <nms_agent.h>
 
 #include "net.h"
 
-int NetConnectTCP(char *szHost, unsigned short nPort)
+int NetConnectTCP(char *szHost, DWORD dwAddr, unsigned short nPort)
 {
        int nSocket;
 
@@ -16,7 +16,14 @@ int NetConnectTCP(char *szHost, unsigned short nPort)
 
                sa.sin_family = AF_INET;
                sa.sin_port = htons(nPort);
-               sa.sin_addr.s_addr = inet_addr(szHost);
+               if (szHost != NULL)
+               {
+                       sa.sin_addr.s_addr = inet_addr(szHost);
+               }
+               else
+               {
+                       sa.sin_addr.s_addr = htonl(dwAddr);
+               }
                
                if (connect(nSocket, (struct sockaddr*)&sa, sizeof(sa)) < 0)
                {
@@ -47,5 +54,11 @@ void NetClose(int nSocket)
 /*
 
 $Log: not supported by cvs2svn $
+Revision 1.1.1.1  2005/01/18 18:38:54  alk
+Initial import
+
+implemented:
+       ServiceCheck.POP3(host, user, password) - connect to host:110 and try to login
+
 
 */
index efe3e48..9de60e5 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: net.h,v 1.1.1.1 2005-01-18 18:38:54 alk Exp $ */
+/* $Id: net.h,v 1.2 2005-01-28 02:50:32 alk Exp $ */
 
 #ifndef __NET__H__
 #define __NET__H__
@@ -9,7 +9,7 @@ enum
        PROTOCOL_TCP
 };
 
-int NetConnectTCP(char *, unsigned short);
+int NetConnectTCP(char *, DWORD, unsigned short);
 int NetRead(int, char *, int);
 int NetWrite(int, char *, int);
 void NetClose(int);
@@ -20,5 +20,11 @@ void NetClose(int);
 /*
 
 $Log: not supported by cvs2svn $
+Revision 1.1.1.1  2005/01/18 18:38:54  alk
+Initial import
+
+implemented:
+       ServiceCheck.POP3(host, user, password) - connect to host:110 and try to login
+
 
 */
index bf175f9..d7a4fd9 100644 (file)
@@ -1,9 +1,11 @@
-/* $Id: pop3.cpp,v 1.1.1.1 2005-01-18 18:38:54 alk Exp $ */
+/* $Id: pop3.cpp,v 1.2 2005-01-28 02:50:32 alk Exp $ */
 
 #include <nms_common.h>
 #include <nms_agent.h>
 
+#include "main.h"
 #include "net.h"
+#include "pop3.h"
 
 LONG H_CheckPOP3(char *pszParam, char *pArg, char *pValue)
 {
@@ -23,12 +25,31 @@ LONG H_CheckPOP3(char *pszParam, char *pArg, char *pValue)
                return SYSINFO_RC_ERROR;
        }
 
-       nSd = NetConnectTCP(szHost, 110);
+       if (CheckPOP3(szHost, 0, 110, szUser, szPassword) == 0)
+       {
+               ret_int(pValue, 1);
+       }
+       else
+       {
+               ret_int(pValue, 0);
+       }
+
+       return nRet;
+}
+
+int CheckPOP3(char *szAddr, DWORD dwAddr, short nPort, char *szUser, char *szPass)
+{
+       int nRet = 0;
+       int nSd;
+
+       nSd = NetConnectTCP(szAddr, dwAddr, nPort);
        if (nSd > 0)
        {
                char szBuff[512];
                char szTmp[128];
 
+               nRet = PC_ERR_HANDSHAKE;
+
 #define CHECK_OK ((NetRead(nSd, szBuff, sizeof(szBuff)) > 3) \
                                && (strncmp(szBuff, "+OK", 3) == 0))
 
@@ -39,12 +60,12 @@ LONG H_CheckPOP3(char *pszParam, char *pArg, char *pValue)
                        {
                                if (CHECK_OK)
                                {
-                                       snprintf(szTmp, sizeof(szTmp), "PASS %s\r\n", szPassword);
+                                       snprintf(szTmp, sizeof(szTmp), "PASS %s\r\n", szPass);
                                        if (NetWrite(nSd, szTmp, strlen(szTmp)) > 0)
                                        {
                                                if (CHECK_OK)
                                                {
-                                                       bIsOk = true;
+                                                       nRet = PC_ERR_NONE;
                                                }
                                        }
                                }
@@ -53,7 +74,10 @@ LONG H_CheckPOP3(char *pszParam, char *pArg, char *pValue)
 
                NetClose(nSd);
        }
-       ret_int(pValue, bIsOk ? 1 : 0);
+       else
+       {
+               nRet = PC_ERR_CONNECT;
+       }
 
        return nRet;
 }
@@ -62,5 +86,11 @@ LONG H_CheckPOP3(char *pszParam, char *pArg, char *pValue)
 /*
 
 $Log: not supported by cvs2svn $
+Revision 1.1.1.1  2005/01/18 18:38:54  alk
+Initial import
+
+implemented:
+       ServiceCheck.POP3(host, user, password) - connect to host:110 and try to login
+
 
 */
index 58cce7a..472c684 100644 (file)
@@ -1,9 +1,10 @@
-/* $Id: pop3.h,v 1.1.1.1 2005-01-18 18:38:54 alk Exp $ */
+/* $Id: pop3.h,v 1.2 2005-01-28 02:50:32 alk Exp $ */
 
 #ifndef __POP3__H__
 #define __POP3__H__
 
 LONG H_CheckPOP3(char *, char *, char *);
+int CheckPOP3(char *, DWORD, short, char *, char *);
 
 #endif // __POP3__H__
 
@@ -11,5 +12,11 @@ LONG H_CheckPOP3(char *, char *, char *);
 /*
 
 $Log: not supported by cvs2svn $
+Revision 1.1.1.1  2005/01/18 18:38:54  alk
+Initial import
+
+implemented:
+       ServiceCheck.POP3(host, user, password) - connect to host:110 and try to login
+
 
 */
index 1b6e671..4143e60 100644 (file)
@@ -1,18 +1,19 @@
-/* $Id: ssh.cpp,v 1.1 2005-01-19 13:42:47 alk Exp $ */
+/* $Id: ssh.cpp,v 1.2 2005-01-28 02:50:32 alk Exp $ */
 
 #include <nms_common.h>
 #include <nms_agent.h>
 
+#include "main.h"
 #include "net.h"
+#include "ssh.h"
 
 LONG H_CheckSSH(char *pszParam, char *pArg, char *pValue)
 {
        LONG nRet = SYSINFO_RC_SUCCESS;
+
        char szHost[256];
        char szPort[256];
        unsigned short nPort;
-       int nSd;
-       bool bIsOk = false;
 
    NxGetParameterArg(pszParam, 1, szHost, sizeof(szHost));
    NxGetParameterArg(pszParam, 2, szPort, sizeof(szPort));
@@ -21,18 +22,38 @@ LONG H_CheckSSH(char *pszParam, char *pArg, char *pValue)
        {
                return SYSINFO_RC_ERROR;
        }
+
        nPort = (unsigned short)atoi(szPort);
        if (nPort == 0)
        {
                nPort = 22;
        }
 
-       nSd = NetConnectTCP(szHost, nPort);
+       if (CheckSSH(szHost, 0, nPort, NULL, NULL) == 0)
+       {
+               ret_int(pValue, 1);
+       }
+       else
+       {
+               ret_int(pValue, 0);
+       }
+
+       return nRet;
+}
+
+int CheckSSH(char *szAddr, DWORD dwAddr, short nPort, char *szUser, char *szPass)
+{
+       int nRet = 0;
+       int nSd;
+
+       nSd = NetConnectTCP(szAddr, dwAddr, nPort);
        if (nSd > 0)
        {
                char szBuff[512];
                char szTmp[128];
 
+               nRet = PC_ERR_HANDSHAKE;
+
                if (NetRead(nSd, szBuff, sizeof(szBuff)) >= 8)
                {
                        int nMajor, nMinor;
@@ -43,14 +64,17 @@ LONG H_CheckSSH(char *pszParam, char *pArg, char *pValue)
                                                nMajor, nMinor);
                                if (NetWrite(nSd, szTmp, strlen(szTmp)) > 0)
                                {
-                                       bIsOk = true;
+                                       nRet = PC_ERR_NONE;
                                }
                        }
                }
 
                NetClose(nSd);
        }
-       ret_int(pValue, bIsOk ? 1 : 0);
+       else
+       {
+               nRet = PC_ERR_CONNECT;
+       }
 
        return nRet;
 }
@@ -59,5 +83,8 @@ LONG H_CheckSSH(char *pszParam, char *pArg, char *pValue)
 /*
 
 $Log: not supported by cvs2svn $
+Revision 1.1  2005/01/19 13:42:47  alk
++ ServiceCheck.SSH(host[, port]) Added
+
 
 */
index 21ff57f..01393ab 100644 (file)
@@ -1,9 +1,10 @@
-/* $Id: ssh.h,v 1.1 2005-01-19 13:42:47 alk Exp $ */
+/* $Id: ssh.h,v 1.2 2005-01-28 02:50:32 alk Exp $ */
 
 #ifndef __SSH__H__
 #define __SSH__H__
 
 LONG H_CheckSSH(char *, char *, char *);
+int CheckSSH(char *, DWORD, short, char *, char *);
 
 #endif // __SSH__H__
 
@@ -11,5 +12,8 @@ LONG H_CheckSSH(char *, char *, char *);
 /*
 
 $Log: not supported by cvs2svn $
+Revision 1.1  2005/01/19 13:42:47  alk
++ ServiceCheck.SSH(host[, port]) Added
+
 
 */