"Fossies" - the Fresh Open Source Software Archive

Member "xymon-4.3.30/web/hostlist.c" (23 Jul 2019, 3975 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 "hostlist.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 hostlist report generator.                                           */
    3 /*                                                                            */
    4 /* Copyright (C) 2007-2011 Henrik Storner <henrik@hswn.dk>                    */
    5 /*                                                                            */
    6 /* This program is released under the GNU General Public License (GPL),       */
    7 /* version 2. See the file "COPYING" for details.                             */
    8 /*                                                                            */
    9 /*----------------------------------------------------------------------------*/
   10 
   11 static char rcsid[] = "$Id: hostlist.c 8069 2019-07-23 15:29:06Z jccleaver $";
   12 
   13 #include <stdio.h>
   14 #include <ctype.h>
   15 #include <string.h>
   16 
   17 #include "libxymon.h"
   18 
   19 cgidata_t *cgidata = NULL;
   20 char *pagefilter = NULL;
   21 char *testfilter = "cpu";
   22 enum { SORT_IP, SORT_HOSTNAME } sortkey = SORT_HOSTNAME;
   23 char *fields = NULL;
   24 char csvchar = ',';
   25 
   26 void parse_query(void)
   27 {
   28     cgidata_t *cwalk;
   29 
   30     fields = strdup("XMH_HOSTNAME,XMH_IP");
   31     cwalk = cgidata;
   32     while (cwalk) {
   33         /*
   34          * cwalk->name points to the name of the setting.
   35          * cwalk->value points to the value (may be an empty string).
   36          */
   37 
   38         if (strcasecmp(cwalk->name, "page") == 0) {
   39             pagefilter = strdup(cwalk->value);
   40         }
   41         else if (strcasecmp(cwalk->name, "filter") == 0) {
   42             testfilter = strdup(cwalk->value);
   43         }
   44         else if (strcasecmp(cwalk->name, "sort") == 0) {
   45             if (strcasecmp(cwalk->value, "hostname") == 0)
   46                 sortkey = SORT_HOSTNAME;
   47             else if (strcasecmp(cwalk->value, "ip") == 0)
   48                 sortkey = SORT_IP;
   49         }
   50         else if (strncasecmp(cwalk->name, "XMH_", 4) == 0) {
   51             if (strcasecmp(cwalk->value, "on") == 0) {
   52                 fields = (char *)realloc(fields, strlen(fields) + strlen(cwalk->name) + 2);
   53                 strcat(fields, ",");
   54                 strcat(fields, cwalk->name);
   55             }
   56         }
   57         else if (strcasecmp(cwalk->name, "csvdelim") == 0) {
   58             csvchar = *(cwalk->value);
   59         }
   60 
   61         cwalk = cwalk->next;
   62     }
   63 }
   64 
   65 
   66 int main(int argc, char *argv[])
   67 {
   68     char *envarea = NULL;
   69     SBUF_DEFINE(req);
   70     char *board, *l;
   71     int argi, res;
   72     sendreturn_t *sres;
   73     char *cookie;
   74     pcre *dummy;
   75 
   76     init_timestamp();
   77     for (argi=1; (argi < argc); argi++) {
   78         if (argnmatch(argv[argi], "--env=")) {
   79             char *p = strchr(argv[argi], '=');
   80             loadenv(p+1, envarea);
   81         }
   82         else if (argnmatch(argv[argi], "--area=")) {
   83             char *p = strchr(argv[argi], '=');
   84             envarea = strdup(p+1);
   85         }
   86         else if (strcmp(argv[argi], "--debug") == 0) {
   87             debug = 1;
   88         }
   89     }
   90 
   91     cookie = get_cookie("pagepath");
   92     if (cookie) sethostenv_pagepath(cookie);
   93 
   94     cgidata = cgi_request();
   95     if (cgidata == NULL) {
   96         /* Present the query form */
   97         sethostenv("", "", "", colorname(COL_BLUE), NULL);
   98         printf("Content-type: %s\n\n", xgetenv("HTMLCONTENTTYPE"));
   99         showform(stdout, "hostlist", "hostlist_form", COL_BLUE, getcurrenttime(NULL), NULL, NULL);
  100         return 0;
  101     }
  102     parse_query();
  103 
  104     dummy = (testfilter ? compileregex(testfilter) : NULL);
  105     if (dummy == NULL) return 1; else freeregex(dummy);
  106     dummy = (pagefilter ? compileregex(pagefilter) : NULL);
  107     if (dummy == NULL) return 1; else freeregex(dummy);
  108 
  109     sres = newsendreturnbuf(1, NULL);
  110     SBUF_MALLOC(req, 1024 + strlen(fields) + strlen(testfilter) + strlen(pagefilter));
  111     snprintf(req, req_buflen, "xymondboard fields=%s test=%s page=%s",
  112         fields, testfilter, pagefilter);
  113     res = sendmessage(req, NULL, XYMON_TIMEOUT, sres);
  114     if (res != XYMONSEND_OK) return 1;
  115     board = getsendreturnstr(sres, 1);
  116     freesendreturnbuf(sres);
  117 
  118     printf("Content-type: text/csv\n\n");
  119     l = strtok(fields, ",");
  120     while (l) {
  121         printf("%s;", l);
  122         l = strtok(NULL, ",\n");
  123     }
  124     printf("\n");
  125 
  126     l = board;
  127     while (l && *l) {
  128         char *p;
  129         char *eoln = strchr(l, '\n');
  130         if (eoln) *eoln = '\0';
  131 
  132         do {
  133             p = strchr(l, '|');
  134             if (p) *p = csvchar;
  135         } while (p);
  136         printf("%s\n", l);
  137 
  138         if (eoln) l = eoln+1; else l = NULL;
  139     }
  140 
  141     return 0;
  142 }
  143