"Fossies" - the Fresh Open Source Software Archive

Member "xymon-4.3.30/xymonnet/beastat.c" (23 Jul 2019, 9857 Bytes) of package /linux/privat/xymon-4.3.30.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "beastat.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 4.3.29_vs_4.3.30.

    1 /*----------------------------------------------------------------------------*/
    2 /* Xymon monitor BEA statistics tool.                                         */
    3 /*                                                                            */
    4 /* This is used to collect statistics from a BEA Weblogic server              */
    5 /*                                                                            */
    6 /* Copyright (C) 2004-2011 Henrik Storner <henrik@hswn.dk>                    */
    7 /*                                                                            */
    8 /* This program is released under the GNU General Public License (GPL),       */
    9 /* version 2. See the file "COPYING" for details.                             */
   10 /*                                                                            */
   11 /*----------------------------------------------------------------------------*/
   12 
   13 static char rcsid[] = "$Id: beastat.c 8069 2019-07-23 15:29:06Z jccleaver $";
   14 
   15 #include <stdio.h>
   16 #include <string.h>
   17 #include <unistd.h>
   18 #include <stdlib.h>
   19 #include <limits.h>
   20 #include <errno.h>
   21 #include <ctype.h>
   22 #include <sys/types.h>
   23 #include <sys/stat.h>
   24 
   25 #include <pcre.h>
   26 
   27 #include "libxymon.h"
   28 #include "version.h"
   29 
   30 typedef struct bea_idx_t {
   31         char *idx;
   32         struct bea_idx_t *next;
   33 } bea_idx_t;
   34 
   35 static bea_idx_t *bea_idxhead = NULL;
   36 static char msgline[MAX_LINE_LEN];
   37 static int statuscolor = COL_GREEN;
   38 
   39 /* Set with environment or command-line options */
   40 static char *location = "";     /* XYMONNETWORK value */
   41 static int testuntagged = 0;
   42 static int default_port = 161;
   43 static char *default_community = "public";
   44 static int extcmdtimeout = 30;
   45 
   46 static void find_idxes(char *buf, char *searchstr)
   47 {
   48         bea_idx_t *idxwalk;
   49         char *bol, *eoln, *idxval;
   50 
   51         /* If we've done it before, clear out the old indexes */
   52         while (bea_idxhead) {
   53                 idxwalk = bea_idxhead;
   54                 bea_idxhead = bea_idxhead->next;
   55                 xfree(idxwalk->idx);
   56                 xfree(idxwalk);
   57         }
   58         bea_idxhead = NULL;
   59 
   60         bol = buf;
   61         while ((bol = strstr(bol, searchstr)) != NULL) {
   62                 idxval = NULL;
   63                 bol++;
   64                 eoln = strchr(bol, '\n');
   65                 if (eoln) *eoln = '\0';
   66                 bol = strchr(bol, '=');
   67                 if (bol) bol = strchr(bol, '\"');
   68                 if (bol) idxval = bol+1;
   69                 if (bol) bol = strchr(bol+1, '\"');
   70                 if (bol) {
   71                         *bol = '\0';
   72                         idxwalk = (bea_idx_t *)malloc(sizeof(bea_idx_t));
   73                         idxwalk->idx = strdup(idxval);
   74                         idxwalk->next = bea_idxhead;
   75                         bea_idxhead = idxwalk;
   76                         *bol = '\"';
   77                 }
   78                 if (eoln) *eoln = '\n';
   79         }
   80 }
   81 
   82 int wanted_host(void *host, char *netstring)
   83 {
   84     char *netlocation = xmh_item(host, XMH_NET);
   85 
   86     return ((strlen(netstring) == 0) ||                                /* No XYMONNETWORK = do all */
   87         (netlocation && (strcmp(netlocation, netstring) == 0)) ||  /* XYMONNETWORK && matching NET: tag */
   88         (testuntagged && (netlocation == NULL)));                  /* No NET: tag for this host */
   89 }
   90 
   91 char *getstring(char *databuf, char *beaindex, char *key)
   92 {
   93     static char result[4096];
   94     char keystr[4096];
   95     char *p, *eol;
   96 
   97     *result = '\0';
   98 
   99     snprintf(keystr, sizeof(keystr), "\nBEA-WEBLOGIC-MIB::%s.\"%s\"", key, beaindex);
  100     p = strstr(databuf, keystr);
  101     if (!p) {
  102         /* Might be at the very beginning of the buffer (with no \n first) */
  103         if (strncmp(databuf, keystr+1, strlen(keystr)-1) == 0) p = databuf;
  104     }
  105     else {
  106         p++;
  107     }
  108 
  109     if (p) {
  110         eol = strchr(p, '\n');
  111         if (eol) *eol = '\0';
  112         strncpy(result, p, sizeof(result));
  113         if (eol) *eol = '\n';
  114     }
  115 
  116     return result;
  117 }
  118 
  119 char *jrockitems[] = {
  120     "jrockitRuntimeIndex",
  121     "jrockitRuntimeParent",
  122     "jrockitRuntimeFreeHeap",
  123     "jrockitRuntimeUsedHeap",
  124     "jrockitRuntimeTotalHeap",
  125     "jrockitRuntimeFreePhysicalMemory",
  126     "jrockitRuntimeUsedPhysicalMemory",
  127     "jrockitRuntimeTotalPhysicalMemory",
  128     "jrockitRuntimeTotalNumberOfThreads",
  129     "jrockitRuntimeNumberOfDaemonThreads",
  130     "jrockitRuntimeTotalNurserySize",
  131     NULL
  132 };
  133 
  134 char *qitems[] = {
  135     "executeQueueRuntimeIndex",
  136     "executeQueueRuntimeName",
  137     "executeQueueRuntimeParent",
  138     "executeQueueRuntimeExecuteThreadCurrentIdleCount",
  139     "executeQueueRuntimePendingRequestCurrentCount",
  140     "executeQueueRuntimeServicedRequestTotalCount",
  141     NULL
  142 };
  143 
  144 void send_data(void *host, char *beadomain, char *databuf, char **items)
  145 {
  146     bea_idx_t *idxwalk;
  147     strbuffer_t *msgbuf;
  148     char *p;
  149     int i;
  150 
  151     msgbuf = newstrbuffer(0);
  152 
  153         for (idxwalk = bea_idxhead; (idxwalk); idxwalk = idxwalk->next) {
  154         snprintf(msgline, sizeof(msgline), "data %s.bea\n\n", commafy(xmh_item(host, XMH_HOSTNAME)));
  155         addtobuffer(msgbuf, msgline);
  156 
  157         if (beadomain && *beadomain) {
  158             snprintf(msgline, sizeof(msgline), "DOMAIN:%s\n", beadomain);
  159             addtobuffer(msgbuf, msgline);
  160         }
  161 
  162         for (i=0; (items[i]); i++) {
  163             p = getstring(databuf, idxwalk->idx, items[i]);
  164             snprintf(msgline, sizeof(msgline), "%s\n", p);
  165             addtobuffer(msgbuf, msgline);
  166         }
  167 
  168         sendmessage(STRBUF(msgbuf), NULL, XYMON_TIMEOUT, NULL);
  169         clearstrbuffer(msgbuf);
  170     }
  171 
  172     freestrbuffer(msgbuf);
  173 }
  174 
  175 int main(int argc, char *argv[])
  176 {
  177         void *hwalk;
  178     int argi;
  179     strbuffer_t *statusmsg, *jrockout, *qout;
  180 
  181     for (argi = 1; (argi < argc); argi++) {
  182         if ((strcmp(argv[argi], "--help") == 0)) {
  183             printf("beastat version %s\n\n", VERSION);
  184             printf("Usage:\n%s [--debug] [--no-update] [--port=SNMPPORT] [--community=SNMPCOMMUNITY]\n", 
  185                 argv[0]);
  186             exit(0);
  187         }
  188         else if ((strcmp(argv[argi], "--version") == 0)) {
  189             printf("beastat version %s\n", VERSION);
  190             exit(0);
  191         }
  192         else if ((strcmp(argv[argi], "--debug") == 0)) {
  193             debug = 1;
  194         }
  195         else if ((strcmp(argv[argi], "--no-update") == 0)) {
  196             dontsendmessages = 1;
  197         }
  198         else if (argnmatch(argv[argi], "--timeout=")) {
  199             char *p = strchr(argv[argi], '=');
  200             extcmdtimeout = atoi(p+1);
  201         }
  202         else if (argnmatch(argv[argi], "--port=")) {
  203             char *p = strchr(argv[argi], '=');
  204             default_port = atoi(p+1);
  205         }
  206         else if (argnmatch(argv[argi], "--community=")) {
  207             char *p = strchr(argv[argi], '=');
  208             default_community = strdup(p+1);
  209         }
  210     }
  211 
  212         load_hostnames(xgetenv("HOSTSCFG"), "netinclude", get_fqdn());
  213         if (first_host() == NULL) {
  214                 errprintf("Cannot load file %s\n", xgetenv("HOSTSCFG"));
  215                 return 1;
  216         }
  217 
  218     if (xgetenv("XYMONNETWORK") && (strlen(xgetenv("XYMONNETWORK")) > 0)) 
  219         location = strdup(xgetenv("XYMONNETWORK"));
  220     else if (xgetenv("BBLOCATION") && (strlen(xgetenv("BBLOCATION")) > 0))
  221         location = strdup(xgetenv("BBLOCATION"));
  222 
  223     init_timestamp();
  224     combo_start();
  225     statusmsg = newstrbuffer(0);
  226     jrockout = newstrbuffer(0);
  227     qout = newstrbuffer(0);
  228 
  229     for (hwalk = first_host(); (hwalk); hwalk = next_host(hwalk, 0)) {
  230         char *tspec = xmh_custom_item(hwalk, "bea=");
  231         char *snmpcommunity = default_community;
  232         char *beadomain = "";
  233         int snmpport = default_port;
  234         char *p;
  235         char pipecmd[4096];
  236         int jrockres, qres;
  237 
  238         clearstrbuffer(statusmsg);
  239         clearstrbuffer(jrockout);
  240         clearstrbuffer(qout);
  241 
  242         /* Check if we have a "bea" test for this host, and it is a host we want to test */
  243                 if (!tspec || !wanted_host(hwalk, location)) continue;
  244 
  245         /* Parse the testspec: bea=[SNMPCOMMUNITY@]BEADOMAIN[:SNMPPORT] */
  246         tspec = strdup(tspec+strlen("bea="));
  247 
  248         p = strchr(tspec, ':');
  249         if (p) {
  250             *p = '\0';
  251             snmpport = atoi(p+1);
  252         }
  253 
  254         p = strchr(tspec, '@');
  255         if (p) {
  256             *p = '\0';
  257             snmpcommunity = strdup(tspec);
  258             beadomain = strdup(p+1);
  259         }
  260         else {
  261             beadomain = strdup(tspec);
  262         }
  263 
  264         /* Prepare for the host status */
  265         statuscolor = COL_GREEN;
  266 
  267         /* Setup the snmpwalk pipe-command for jrockit stats */
  268         snprintf(pipecmd, sizeof(pipecmd), "snmpwalk -m BEA-WEBLOGIC-MIB -c %s@%s -v 1 %s:%d enterprises.140.625.302.1",
  269             snmpcommunity, beadomain, xmh_item(hwalk, XMH_IP), snmpport);
  270         jrockres = run_command(pipecmd, NULL, jrockout, 0, extcmdtimeout);
  271         if (jrockres == 0) {
  272             find_idxes(STRBUF(jrockout), "BEA-WEBLOGIC-MIB::jrockitRuntimeIndex.");
  273             send_data(hwalk, beadomain, STRBUF(jrockout), jrockitems);
  274         }
  275         else {
  276             if (statuscolor < COL_YELLOW) statuscolor = COL_YELLOW;
  277             snprintf(msgline, sizeof(msgline), "Could not retrieve BEA jRockit statistics from %s:%d domain %s (code %d)\n",
  278                 xmh_item(hwalk, XMH_IP), snmpport, beadomain, jrockres);
  279             addtobuffer(statusmsg, msgline);
  280         }
  281 
  282         /* Setup the snmpwalk pipe-command for executeQueur stats */
  283         snprintf(pipecmd, sizeof(pipecmd), "snmpwalk -m BEA-WEBLOGIC-MIB -c %s@%s -v 1 %s:%d enterprises.140.625.180.1",
  284             snmpcommunity, beadomain, xmh_item(hwalk, XMH_IP), snmpport);
  285         qres = run_command(pipecmd, NULL, qout, 0, extcmdtimeout);
  286         if (qres == 0) {
  287             find_idxes(STRBUF(qout), "BEA-WEBLOGIC-MIB::executeQueueRuntimeIndex.");
  288             send_data(hwalk, beadomain, STRBUF(qout), qitems);
  289         }
  290         else {
  291             if (statuscolor < COL_YELLOW) statuscolor = COL_YELLOW;
  292             snprintf(msgline, sizeof(msgline), "Could not retrieve BEA executeQueue statistics from %s:%d domain %s (code %d)\n",
  293                 xmh_item(hwalk, XMH_IP), snmpport, beadomain, qres);
  294             addtobuffer(statusmsg, msgline);
  295         }
  296 
  297         /* FUTURE: Have the statuscolor/statusmsg be updated to check against thresholds */
  298         /* Right now, the "bea" status is always green */
  299         init_status(statuscolor);
  300         snprintf(msgline, sizeof(msgline), "status %s.%s %s %s\n\n", commafy(xmh_item(hwalk, XMH_HOSTNAME)), "bea", colorname(statuscolor), timestamp);
  301         addtostatus(msgline);
  302         if (STRBUFLEN(statusmsg) == 0) addtobuffer(statusmsg, "All BEA monitors OK\n");
  303         addtostrstatus(statusmsg);
  304         finish_status();
  305     }
  306 
  307     combo_end();
  308     freestrbuffer(statusmsg);
  309     freestrbuffer(jrockout);
  310     freestrbuffer(qout);
  311 
  312     return 0;
  313 }
  314