Ad Widget

Collapse

Suggest net.tcp.service.read

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • freak
    Member
    • Oct 2007
    • 52

    #1

    Suggest net.tcp.service.read

    Hi,

    I'd like to check for the string returned by a tcp connection, i.e. "SSH-1.99-OpenSSH_4.3" to check if sshd is actually running.
    Or even the a host's hostkey (i.e. "ssh-rsa AAAAB3Nza...LiPk==").

    I'm just wondering whether there is an existing function to use to read and return that answer string.

    Maybe someone can point me into a direction.

    Thanks
    Sebastian
  • freak
    Member
    • Oct 2007
    • 52

    #2
    Patch net.tcp.service.read (severely needs testing and code review)

    I wrote some code to just read the output of a TCP connection - using get_http_page as example..

    Again: I'm new to zabbix and I'm new to C, so please be patient and I'd appreciate some comments..

    This patch is based on trunk (rev. 4901):

    ./src/libs/zbxsysinfo/common/net.c
    PHP Code:
    diff --exclude=.svn -Nru trunk/src/libs/zbxsysinfo/common/net.c trunk-new/src/libs/zbxsysinfo/common/net.c
    --- trunk/src/libs/zbxsysinfo/common/net.c      2007-10-11 09:42:36.000000000 +0200
    +++ trunk-new/src/libs/zbxsysinfo/common/net.c  2007-10-22 14:06:53.000000000 +0200
    @@ -259,+259,41 @@
            return 
    SYSINFO_RET_FAIL;
     
    #endif /* not WINDOWS */
     
    }
    +
    +
    /* read from TCP connections */
    +int    tcp_read(       const char      *host,          /* hostname or IP to connect to */
    +                       unsigned short  port,           /* port to connect to */
    +                       const char      *request,       /* request string */
    +                       const char      *value_str,     /* value_str holds the result */
    +                       int             max_buf_len
    +               )
    +{
    +       
    zbx_sock_t      s;
    +       
    char    *buf,
    +               
    str_request[MAX_STRING_LEN];
    +       
    int     ret SYSINFO_RET_FAIL;
    +
    +       
    assert(value_str);
    +
    +       if(
    SUCCEED == (ret zbx_tcp_connect(&s,host,port)))
    +       {
    +               
    zbx_snprintf(str_request,sizeof(str_request),"%s",request);
    +               if(
    SUCCEED == (ret zbx_tcp_send_raw(&s,str_request)))
    +               {
    +                       if(
    SUCCEED == (ret zbx_tcp_recv(&s,&buf)))
    +                       {
    +                               
    zbx_rtrim(buf,"\n\r\0");
    +                               
    zbx_snprintf((char *) value_str,max_buf_len,"%s",buf);
    +                       }
    +               }
    +       }
    +       
    zbx_tcp_close(&s);
    +
    +       if(
    FAIL == ret)
    +       {
    +               
    zabbix_log(LOG_LEVEL_DEBUG"tcp_read error: %s:%s"hostport);
    +               return 
    SYSINFO_RET_FAIL;
    +       }
    +
    +       return 
    SYSINFO_RET_OK;
    +} 
    ./src/libs/zbxsysinfo/common/net.h
    PHP Code:
    diff --exclude=.svn -Nru trunk/src/libs/zbxsysinfo/common/net.h trunk-new/src/libs/zbxsysinfo/common/net.h
    --- trunk/src/libs/zbxsysinfo/common/net.h      2007-10-11 09:42:36.000000000 +0200
    +++ trunk-new/src/libs/zbxsysinfo/common/net.h  2007-10-22 11:00:10.000000000 +0200
    @@ -22,+22,@@
     
    #include "sysinfo.h"

     
    int    tcp_expect(const char   *hostunsigned short port, const char *request, const char *expect, const char *sendtocloseint *value_int);
    +
    int    tcp_read(const char *hostunsigned short port, const char *request, const char *value_strint max_buf_len);
     
    int    TCP_LISTEN(const char *cmd, const char *paramunsigned flagsAGENT_RESULT *result);
     
    int    CHECK_PORT(const char *cmd, const char *paramunsigned flagsAGENT_RESULT *result);
     
    int    CHECK_DNS(const char *cmd, const char *paramunsigned flagsAGENT_RESULT *result); 
    ./src/libs/zbxsysinfo/simple/simple.c
    PHP Code:
    diff --exclude=.svn -Nru trunk/src/libs/zbxsysinfo/simple/simple.c trunk-new/src/libs/zbxsysinfo/simple/simple.c
    --- trunk/src/libs/zbxsysinfo/simple/simple.c   2007-10-11 09:42:36.000000000 +0200
    +++ trunk-new/src/libs/zbxsysinfo/simple/simple.c       2007-10-22 16:41:12.000000000 +0200
    @@ -33,+33,@@
            {
            {
    "net.tcp.service",     CF_USEUPARAM,   CHECK_SERVICE,          0,      "ssh,127.0.0.1,22"},
            {
    "net.tcp.service.perf",CF_USEUPARAM,   CHECK_SERVICE_PERF,     0,      "ssh,127.0.0.1,22"},
    +       {
    "net.tcp.service.read",CF_USEUPARAM,   CHECK_SERVICE_READ,     0,      "ssh,127.0.0.1,22"},
            {
    0}
            };

    @@ -
    398,+399,67 @@
            return 
    ret;
     }

    +
    int    CHECK_SERVICE_READ(const char *cmd, const char *paramunsigned flagsAGENT_RESULT *result)
    +{
    +       
    unsigned short  port=0;
    +       
    char    service[MAX_STRING_LEN];
    +       
    char    host[MAX_STRING_LEN];
    +       
    char    str_port[MAX_STRING_LEN];
    +
    +       
    int     ret SYSINFO_RET_FAIL;
    +       
    char    value_str[MAX_STRING_LEN]; /* holds the service_read() result */
    +
    +       
    assert(result);
    +
    +       
    init_result(result);
    +
    +       
    /* Parameter Checking and Defaults */
    +       if(num_param(param)>3)
    +       {
    +               return 
    ret;
    +       }
    +
    +       if(
    get_param(param,1,service,MAX_STRING_LEN)!=0)
    +       {
    +               return 
    ret;
    +       }
    +
    +       
    /* no parameter found, set empty */
    +       if(get_param(param,2,host,MAX_STRING_LEN)!=0)
    +       {
    +               
    host[0]='\0';
    +       }
    +
    +       
    /* if empty, set default */
    +       if(host[0]=='\0')
    +       {
    +               
    strscpy(host,"127.0.0.1");
    +       }
    +
    +       if(
    get_param(param,3,str_port,MAX_STRING_LEN)!=0)
    +       {
    +               
    str_port[0]='\0';
    +       }
    +
    +       if(
    str_port[0]!='\0')
    +       {
    +               
    port atoi(str_port);
    +       }
    +       else
    +       {
    +               
    port 0;
    +       }
    +
    +       
    /* Predefined Services */
    +       /* SSH */
    +       if(strcmp(service,"ssh")==0)
    +       {
    +               if(
    port==0)     port=22;
    +               
    tcp_read(host,port,"",(char *) &value_str,255);
    +               
    SET_STR_RESULT(resultstrdup(value_str));
    +               
    ret SYSINFO_RET_OK;
    +       }
    +
    +       return 
    ret;
    +}

    ./src/libs/zbxsysinfo/simple/simple.h
    PHP Code:
    diff --exclude=.svn -Nru trunk/src/libs/zbxsysinfo/simple/simple.h trunk-new/src/libs/zbxsysinfo/simple/simple.h
    --- trunk/src/libs/zbxsysinfo/simple/simple.h   2007-10-11 09:42:36.000000000 +0200
    +++ trunk-new/src/libs/zbxsysinfo/simple/simple.h       2007-10-19 17:00:37.000000000 +0200
    @@ -23,+23,@@

     
    extern ZBX_METRIC      parameters_simple[];

    +
    int    CHECK_SERVICE_READ(const char *cmd, const char *paramunsigned flagsAGENT_RESULT *result);
     
    int    CHECK_SERVICE_PERF(const char *cmd, const char *paramunsigned flagsAGENT_RESULT *result);
     
    int    CHECK_SERVICE(const char *cmd, const char *paramunsigned flagsAGENT_RESULT *result); 
    Example:
    Now
    Code:
    zabbix_agentd -t net.tcp.service.read[ssh,localhost,22]
    gives me
    Code:
    net.tcp.service.read[ssh,localhost,22]        [s|SSH-1.99-OpenSSH_4.3]
    I was not sure what to use as max_buf_len so I just used 255 when calling
    Code:
    tcp_read(host,port,"",(char *) &value_str,255);
    (Using \[PHP\] instead of \[CODE\] for syntax highlighting purposes..)
    Last edited by freak; 23-10-2007, 11:43. Reason: using [php] instead of [code] for syntax highlighting

    Comment

    • tadams
      Junior Member
      • Jun 2011
      • 5

      #3
      I would very much like to see this to. I have proxies that the only way to detect they are running properly is to look at returned headers from webpages which are forced through the proxy.

      With this functionality, it would allow me to validate that the proxy is online.

      Comment

      • sire
        Senior Member
        • Jul 2010
        • 210

        #4
        Please register a feature request here https://support.zabbix.com/secure/Dashboard.jspa, so that Zabbix developers notice this.
        Regards,
        Sergey Syreskin

        Monitored hosts: 2646 / Active items: 23604 / Server performance: 765.74

        Temporary out of Zabbix business

        Comment

        Working...