"Fossies" - the Fresh Open Source Software Archive

Member "xymon-4.3.30/web/csvinfo.c" (23 Jul 2019, 5440 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 "csvinfo.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 general CSV info viewer.                                             */
    3 /*                                                                            */
    4 /* This is a CGI script for a generic presentation of information stored in   */
    5 /* a comma-separated file (CSV), e.g. via an export from a spreadsheet or DB. */
    6 /* It is also used for the Xymon column-name links, to provide information    */
    7 /* about what each column header means and what kind of test is run.          */
    8 /*                                                                            */
    9 /* Copyright (C) 2003-2011 Henrik Storner <henrik@storner.dk>                 */
   10 /*                                                                            */
   11 /* This program is released under the GNU General Public License (GPL),       */
   12 /* version 2. See the file "COPYING" for details.                             */
   13 /*                                                                            */
   14 /*----------------------------------------------------------------------------*/
   15 
   16 static char rcsid[] = "$Id: csvinfo.c 8069 2019-07-23 15:29:06Z jccleaver $";
   17 
   18 #include <limits.h>
   19 #include <stdio.h>
   20 #include <string.h>
   21 #include <unistd.h>
   22 #include <stdlib.h>
   23 #include <ctype.h>
   24 
   25 #include "libxymon.h"
   26 
   27 #define MAXCOLUMNS 80
   28 
   29 char *srcdb = "hostinfo.csv";
   30 char *wantedname = "";
   31 int keycolumn = 0;
   32 char delimiter = ';';
   33 cgidata_t *cgidata = NULL;
   34 
   35 
   36 void errormsg(char *msg)
   37 {
   38         printf("Content-type: %s\n\n", xgetenv("HTMLCONTENTTYPE"));
   39         printf("<html><head><title>Invalid request</title></head>\n");
   40         printf("<body>%s</body></html>\n", msg);
   41         exit(1);
   42 }
   43 
   44 void parse_query(void)
   45 {
   46     cgidata_t *cwalk;
   47 
   48     cwalk = cgidata;
   49     while (cwalk) {
   50         /*
   51          * cwalk->name points to the name of the setting.
   52          * cwalk->value points to the value (may be an empty string).
   53          */
   54 
   55         if (strcasecmp(cwalk->name, "key") == 0) {
   56             wantedname = strdup(cwalk->value);
   57         }
   58         else if (strcasecmp(cwalk->name, "db") == 0) {
   59             char *val, *p;
   60 
   61             /* Don't allow any slashes in the db-name */
   62             val = cwalk->value;
   63             p = strrchr(val, '/');
   64             if (p) val = (p+1);
   65 
   66             srcdb = strdup(val);
   67         }
   68         else if (strcasecmp(cwalk->name, "column") == 0) {
   69             keycolumn = atoi(cwalk->value);
   70         }
   71         else if (strcasecmp(cwalk->name, "delimiter") == 0) {
   72             delimiter = *(cwalk->value);
   73         }
   74 
   75         cwalk = cwalk->next;
   76     }
   77 }
   78 
   79 
   80 int main(int argc, char *argv[])
   81 {
   82     FILE *db;
   83     char dbfn[PATH_MAX];
   84     strbuffer_t *inbuf;
   85     char *hffile = "info";
   86     int bgcolor = COL_BLUE;
   87     char *envarea = NULL;
   88 
   89     char *headers[MAXCOLUMNS];
   90     char *items[MAXCOLUMNS];
   91 
   92     int i, found;
   93     int argi;
   94 
   95     for (argi=1; (argi < argc); argi++) {
   96         if (argnmatch(argv[argi], "--env=")) {
   97             char *p = strchr(argv[argi], '=');
   98             loadenv(p+1, envarea);
   99         }
  100         else if (argnmatch(argv[argi], "--area=")) {
  101             char *p = strchr(argv[argi], '=');
  102             envarea = strdup(p+1);
  103         }
  104         else if (strcmp(argv[argi], "--debug") == 0) {
  105             debug = 1;
  106         }
  107         else if (argnmatch(argv[argi], "--hffile=")) {
  108             char *p = strchr(argv[argi], '=');
  109             hffile = strdup(p+1);
  110         }
  111         else if (argnmatch(argv[argi], "--color=")) {
  112             char *p = strchr(argv[argi], '=');
  113             bgcolor = parse_color(p+1);
  114         }
  115     }
  116 
  117     redirect_cgilog("csvinfo");
  118 
  119     cgidata = cgi_request();
  120     parse_query();
  121     if (strlen(wantedname) == 0) {
  122         errormsg("Invalid request");
  123         return 1;
  124     }
  125 
  126     snprintf(dbfn, sizeof(dbfn), "%s/etc/%s", xgetenv("XYMONHOME"), srcdb);
  127     db = fopen(dbfn, "r");
  128     if (db == NULL) {
  129         SBUF_DEFINE(msg);
  130 
  131         SBUF_MALLOC(msg, 30+strlen(htmlquoted(dbfn)));
  132         snprintf(msg, msg_buflen, "Cannot open sourcedb %s\n", htmlquoted(dbfn));
  133         errormsg(msg);
  134         return 1;
  135     }
  136 
  137     /* First, load the headers from line 1 of the sourcedb */
  138     memset(headers, 0, sizeof(headers));
  139     initfgets(db);
  140     inbuf = newstrbuffer(0);
  141     if (unlimfgets(inbuf, db)) {
  142         char *p1, *p2;
  143 
  144         for (i=0, p1=STRBUF(inbuf), p2=strchr(STRBUF(inbuf), delimiter); (p1 && p2 && strlen(p1)); i++,p1=p2+1,p2=strchr(p1, delimiter)) {
  145             *p2 = '\0';
  146             headers[i] = strdup(p1);
  147         }
  148     }
  149 
  150 
  151     /*
  152      * Pre-allocate the buffer space for the items - we weill be stuffing data
  153      * into these while scanning for the right item.
  154      */
  155     for (i=0; i<MAXCOLUMNS; i++) items[i] = malloc(MAX_LINE_LEN);
  156 
  157     found = 0;
  158     while (!found && unlimfgets(inbuf, db)) {
  159 
  160         char *p1, *p2;
  161 
  162         for (i=0; i<MAXCOLUMNS; i++) *(items[i]) = '\0';
  163 
  164         for (i=0, p1=STRBUF(inbuf), p2=strchr(STRBUF(inbuf), delimiter); (p1 && p2 && strlen(p1)); i++,p1=p2+1,p2=strchr(p1, delimiter)) {
  165             *p2 = '\0';
  166             strcpy(items[i], (strlen(p1) ? p1 : "&nbsp;"));
  167         }
  168 
  169         found = (strcasecmp(items[keycolumn], wantedname) == 0);
  170     }
  171     fclose(db);
  172     freestrbuffer(inbuf);
  173 
  174     if (!found) {
  175         errormsg("No match");
  176         return 1;
  177     }
  178 
  179 
  180     /*
  181      * Ready ... go build the webpage.
  182      */
  183     printf("Content-Type: %s\n\n", xgetenv("HTMLCONTENTTYPE"));
  184 
  185         /* It's ok with these hardcoded values, as they are not used for this page */
  186         sethostenv(wantedname, "", "", colorname(bgcolor), NULL);
  187         headfoot(stdout, hffile, "", "header", bgcolor);
  188 
  189     printf("<table align=center border=1 summary=\"Information table\">\n");
  190 
  191     for (i=0; (headers[i]); i++) {
  192         printf("<tr>\n");
  193         printf("  <th align=left>%s</th><td align=left valign=top>%s</td>\n", headers[i], items[i]);
  194         printf("</tr>\n");
  195     }
  196 
  197     printf("</table>\n");
  198         headfoot(stdout, hffile, "", "footer", bgcolor);
  199 
  200     return 0;
  201 }
  202