javapythonganglia

How to read ganglia information from other application?


I have managed to install and configure Ganglia on my cluster. I do not want to just see all performance data on ganglia web interface but instead I want to read cluster information from other application (application may be Java or Python based). I am not able to find if it is possible or not.

Is there any API to read Ganglia data?

To test Ganglia I used telnet master 8649 and Ganglia showed me nice XML text on my console. But how do I do the same thing using Java or Python? I can definitely connect to 8649 using sockets but after that do I need to send something to Ganglia daemons?


Solution

  • I can help you to get an insight on this. But before that I must tell you, I am not a Java programmer, rather I am a C/C++ programmer. So, it means I can let you know, how things work in ganglia and you can find equivalent methods in Java/ Python to rewrite the codes you want.

    Please be informed that there is no API in ganglia to achieve what you want to.

    First consider below set up of ganglia to understand properly:

    ganglia minimal setup

    GS1 and GS2 are collecting system metrics and pushing them to GM. So, according to your question, if you want to collect all such metrics by your own Java/ Python based application, then you may have to install the application on the Master server (i.e Replace GS with your own application).

    GS1 and GS2 sends all collected metrics either by UDP unicast channel or UDP multicast channel. It is recommended that UDP unicast should be enabled in every gmond.conf for easier scalability.

    I wouldn't discuss much on GS1 and GS2 as your question is more about replacing GM with your own tool.

    GM uses two important libraries heavily to establish a UDP connection and translate data into its own readable format. They are APR (Apache Portable Runtime) to establish UDP connection and perform related activities and XDR (External Data Representation) to send data across networks and perform RPC.

    You need to find APR and XDR equivalent libraries in Java and Python first. XDR is already available in Java and APR could be replaced by your own basic implementation to perform inter-network operations (i.e., create UDP socket, etc).

    Open gmond.c source file of ganglia and go to line 1436. You will find a C function:

    static void process_udp_recv_channel(const apr_pollfd_t *desc, apr_time_t now).

    This function basically performs "UDP connection establishment" and "data translation into readable format" activities.

    The call flow of the above function is shown below:
    Call flow

    Now, let's expand the function at line 1436 to understand more.

    The first argument in this function carries network parameters such as IP, Port, etc. The structure is expanded below. You can find similar object in Java also.

    struct apr_pollfd_t {
        apr_pool_t *p;              /**< associated pool */
        apr_datatype_e desc_type;   /**< descriptor type */
        apr_int16_t reqevents;      /**< requested events */
        apr_int16_t rtnevents;      /**< returned events */
        apr_descriptor desc;        /**< @see apr_descriptor */
        void *client_data;          /**< allows app to associate context */
    };
    

    The second parameter has nothing to do, if SFLOW is disabled.

    So, Start with creating a APR pool, UDP connection, etc.

      socket         = desc->desc.s;
      channel       = desc->client_data;
    
      apr_pool_create(&p, global_context);
      status = apr_socket_addr_get(&remotesa, APR_LOCAL, socket);
    
      status = apr_sockaddr_info_get(&remotesa, NULL, remotesa->family, remotesa->port, 0, p);
    
      /* Grab the data */
      status = apr_socket_recvfrom(remotesa, socket, 0, buf, &len);
      if(status != APR_SUCCESS)
        {
          apr_pool_destroy(p);
          return;
        }  
    
      apr_sockaddr_ip_buffer_get(remoteip, 256, remotesa);
    
      /* Check the ACL */
      if(Ganglia_acl_action( channel->acl, remotesa) != GANGLIA_ACCESS_ALLOW)
        {
          apr_pool_destroy(p);
          return;
        }
    

    All declarations of variable can be found in the beginning of the function expanded (line 1439 to 1456).

    Then, create XDR stream:

    xdrmem_create(&x, buf, max_udp_message_len, XDR_DECODE);
    

    Flush the data of the struct which saves metadata and metrics value:

    memset( &fmsg, 0, sizeof(Ganglia_metadata_msg));
    memset( &vmsg, 0, sizeof(Ganglia_value_msg));
    

    fmsg (Ganglia_metadata_msg) and vmsg (Ganglia_value_msg) struct definitions can be found in gm_protocol.h header file. Re-write them in Java.

    Then, figure out if the message received is "metadata" or "metrics values".

    xdr_Ganglia_msg_formats(&x, &id); // this function is located in the source file gm_protocol_xdr.c and this file is generated by rpcgen.
    

    Note: rpcgen is a rpc compiler and its explanation can be found in this question.

    Note: Here is the link for gm_protocol_xdr.c.

    Here, id is an enum and its declaration is shown below:

    enum Ganglia_msg_formats {
        gmetadata_full = 128,
        gmetric_ushort = 128 + 1,
        gmetric_short = 128 + 2,
        gmetric_int = 128 + 3,
        gmetric_uint = 128 + 4,
        gmetric_string = 128 + 5,
        gmetric_float = 128 + 6,
        gmetric_double = 128 + 7,
        gmetadata_request = 128 + 8,
    };
    typedef enum Ganglia_msg_formats Ganglia_msg_formats;
    

    Based on the value of id, you can determine what kind of value the packets have. For this purpose, this function calls an another function (which is in fact generated by rpcgen) to determine the kind of value the packet has and if found, it translates it to human readable format too.

    The function is:

    xdr_Ganglia_value_msg(&x, &vmsg);
    

    You can find the full expansion of this function in gm_protocol_xdr.c from line 275.

    After that you can do whatever you would like with these packets.

    In the end, you must free all XDR variables and APR pools allocated.

    I hope this gives you a fair idea to start with your own application.