Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Machine data grabber
0003  *
0004  * @file machine.c
0005  */
0006 /* -----------------------------------------------------------------------------
0007  * Enduro/X Middleware Platform for Distributed Transaction Processing
0008  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0009  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0012  * See LICENSE file for full text.
0013  * -----------------------------------------------------------------------------
0014  * AGPL license:
0015  *
0016  * This program is free software; you can redistribute it and/or modify it under
0017  * the terms of the GNU Affero General Public License, version 3 as published
0018  * by the Free Software Foundation;
0019  *
0020  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0021  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0022  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0023  * for more details.
0024  *
0025  * You should have received a copy of the GNU Affero General Public License along 
0026  * with this program; if not, write to the Free Software Foundation, Inc.,
0027  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0028  *
0029  * -----------------------------------------------------------------------------
0030  * A commercial use license is available from Mavimax, Ltd
0031  * contact@mavimax.com
0032  * -----------------------------------------------------------------------------
0033  */
0034 #include <stdio.h>
0035 #include <stdlib.h>
0036 #include <string.h>
0037 #include <errno.h>
0038 #include <regex.h>
0039 #include <utlist.h>
0040 #include <unistd.h>
0041 #include <signal.h>
0042 #include <fcntl.h>
0043 
0044 #include <ndebug.h>
0045 #include <atmi.h>
0046 #include <atmi_int.h>
0047 #include <typed_buf.h>
0048 #include <ndrstandard.h>
0049 #include <ubf.h>
0050 #include <Exfields.h>
0051 #include <Excompat.h>
0052 #include <ubfutil.h>
0053 #include <sys_unix.h>
0054 #include <gencall.h>
0055 #include "tpadmsv.h"
0056 #include "expr.h"
0057 /*---------------------------Externs------------------------------------*/
0058 /*---------------------------Macros-------------------------------------*/
0059 /*---------------------------Enums--------------------------------------*/
0060 /*---------------------------Typedefs-----------------------------------*/
0061     
0062 /**
0063  * Image of the machine
0064  */
0065 typedef struct 
0066 {
0067     char lmid[MAXTIDENT+1];   /**< cluster node id                              */
0068     long curaccessers;        /**< current accessers on machine server+clt+ctx  */
0069     long curclients;          /**< current number of client contexts            */
0070     long curconv;             /**< current number of open conversations         */
0071     char state[3+1];          /**< state information of the matchine (conn)     */
0072 } ndrx_adm_machine_t;
0073 
0074 /**
0075  * Client class infos mapping table
0076  */
0077 expublic ndrx_adm_elmap_t ndrx_G_machine_map[] =
0078 {  
0079     /* Driving of the Preparing: */
0080     {TA_LMID,                   TPADM_EL(ndrx_adm_machine_t, lmid)}
0081     ,{TA_CURACCESSERS,          TPADM_EL(ndrx_adm_machine_t, curaccessers)}
0082     ,{TA_CURCLIENTS,            TPADM_EL(ndrx_adm_machine_t, curclients)}
0083     ,{TA_CURCONV,               TPADM_EL(ndrx_adm_machine_t, curconv)}
0084     ,{TA_STATE,                 TPADM_EL(ndrx_adm_machine_t, state)}
0085     ,{BBADFLDID}
0086 };
0087 
0088 /*---------------------------Globals------------------------------------*/
0089 /*---------------------------Statics------------------------------------*/
0090 
0091 exprivate ndrx_adm_cursors_t *M_cursnew;
0092 exprivate int M_idx = 0;    /**< Current growlist index */
0093 /*---------------------------Prototypes---------------------------------*/
0094 
0095 /**
0096  * Fill any cluster data here, filter by bridge processes
0097  * @param reply
0098  * @param reply_len
0099  * @return 
0100  */
0101 exprivate int ndrx_adm_machine_proc_list(command_reply_t *reply, size_t reply_len)
0102 {
0103     command_reply_ppm_t * ppm_info = (command_reply_ppm_t*)reply;
0104     int ret = EXSUCCEED;
0105     
0106     if (NDRXD_CALL_TYPE_PM_PPM!=reply->msg_type)
0107     {
0108         /* not payload */
0109         goto out;
0110     }
0111     
0112     NDRX_LOG(log_debug, "ppm out: [%s]", ppm_info->binary_name);
0113     if (ppm_info->flags & SRV_KEY_FLAGS_BRIDGE)
0114     {
0115         ndrx_adm_machine_t mach;
0116         memset(&mach, 0, sizeof(mach));
0117 
0118         mach.curaccessers = EXFAIL;
0119         mach.curclients = EXFAIL;
0120         mach.curconv = EXFAIL;
0121         snprintf(mach.lmid, sizeof(mach.lmid), "%hd", ppm_info->nodeid);
0122 
0123         /* Check the state */
0124         if (ppm_info->flags & SRV_KEY_FLAGS_CONNECTED)
0125         {
0126             NDRX_STRCPY_SAFE(mach.state, "ACT");
0127         }
0128         else if (NDRXD_PM_RUNNING_OK==ppm_info->state)
0129         {
0130             NDRX_STRCPY_SAFE(mach.state, "PEN");
0131         }
0132         else
0133         {
0134             NDRX_STRCPY_SAFE(mach.state, "INA");
0135         }
0136 
0137         if (EXSUCCEED!=ndrx_growlist_add(&M_cursnew->list, (void *)&mach, M_idx))
0138         {
0139             NDRX_LOG(log_error, "Growlist failed - out of memory?");
0140             EXFAIL_OUT(ret);
0141         }
0142 
0143         NDRX_LOG(log_debug, "Machine/Node [%s] state %s added", mach.lmid, mach.state);
0144         M_idx++;
0145     }
0146     
0147 out:
0148     return ret;
0149 }
0150 
0151 /**
0152  * Read machine data
0153  * @param clazz class name
0154  * @param cursnew this is new cursor domain model
0155  * @param flags not used
0156  */
0157 expublic int ndrx_adm_machine_get(char *clazz, ndrx_adm_cursors_t *cursnew, long flags)
0158 {
0159     int ret = EXSUCCEED;
0160     ndrx_adm_machine_t mach;
0161     
0162     string_list_t* qlist = NULL;
0163     string_list_t* elt = NULL;
0164     
0165     int typ;
0166     /* init cursor */
0167     M_idx = 0;
0168     
0169     ndrx_growlist_init(&cursnew->list, 100, sizeof(ndrx_adm_machine_t));
0170     
0171     M_cursnew = cursnew;
0172     cursnew->map = ndrx_G_machine_map;
0173     
0174     if (EXSUCCEED!=ndrx_adm_list_call(ndrx_adm_machine_proc_list, 
0175             NDRXD_COM_XAPPM_RQ, NDRXD_COM_XAPPM_RP, ndrx_get_G_atmi_conf()->ndrxd_q_str))
0176     {
0177         NDRX_LOG(log_error, "Failed to call PPM");
0178         EXFAIL_OUT(ret);
0179     }
0180     
0181     /* Add local machine too. */
0182     memset(&mach, 0, sizeof(mach));
0183     
0184     snprintf(mach.lmid, sizeof(mach.lmid), "%ld", tpgetnodeid());
0185     NDRX_STRCPY_SAFE(mach.state, "ACT");
0186     
0187     qlist = ndrx_sys_mqueue_list_make(G_atmi_env.qpath, &ret);
0188     
0189     if (EXSUCCEED!=ret)
0190     {
0191         NDRX_LOG(log_error, "posix queue listing failed!");
0192         EXFAIL_OUT(ret);
0193     }
0194     
0195     /* get the usage states / check queues... */
0196     LL_FOREACH(qlist,elt)
0197     {
0198         /* parse the queue..., extract clients.. */
0199         
0200         /* if not print all, then skip this queue */
0201         if (0!=strncmp(elt->qname, 
0202                 G_atmi_env.qprefix_match, G_atmi_env.qprefix_match_len))
0203         {
0204             continue;
0205         }
0206         
0207         typ = ndrx_q_type_get(elt->qname);
0208         
0209         switch (typ)
0210         {
0211             case NDRX_QTYPE_CLTRPLY:
0212                 mach.curclients++;
0213                 mach.curaccessers++;
0214                 break;
0215             case NDRX_QTYPE_SRVRPLY:
0216                 mach.curaccessers++;
0217                 break;
0218             case NDRX_QTYPE_CONVINIT:
0219                 mach.curconv++;
0220                 break;
0221         }
0222     }
0223     
0224     if (EXSUCCEED!=ndrx_growlist_add(&M_cursnew->list, (void *)&mach, M_idx))
0225     {
0226         NDRX_LOG(log_error, "Growlist failed - out of memory?");
0227         EXFAIL_OUT(ret);
0228     }
0229 
0230     NDRX_LOG(log_debug, "Local Machine/Node [%s] state %s added "
0231             "curclients=%ld curaccessers=%ld curconv=%ld", 
0232             mach.lmid, mach.state, mach.curclients, mach.curaccessers, mach.curconv);
0233     M_idx++;
0234     
0235 out:
0236     
0237     if (NULL!=qlist)
0238     {
0239         ndrx_string_list_free(qlist);
0240     }
0241 
0242     if (EXSUCCEED!=ret)
0243     {
0244         ndrx_growlist_free(&M_cursnew->list);
0245     }
0246 
0247     return ret;
0248 }
0249 
0250 /* vim: set ts=4 sw=4 et smartindent: */