/*****************************************************************************/ /* sMONitor.c Pronounced: ess-monitor, ess-mon-ee-tar, ess-moan-ar-ter ... whatever A CGI executable, browser viewable, VMS MONITOR utility executing in a PTD subprocess with the VT100/ANSI screen sequences converted into HTML. Up to six (pragmatic number only) such MONITOR teeminals can be displayed on a single browser page, demonstrating the ability to do just that. And of course, once set up, a particular configuration can be bookmarked. Also see SCREPER.C code. As with all applications providing insights into systems, potentially to external parties, sMONitor requires server authorisation. If access for the great unwashed is desired, define the logical name SMONITOR_REMOTE_USER to anything at all. HOW IT WORKS ------------ This was really just an exercise in screen-scraping the output of a "regular" command-line application onto a browser HTML page. The script uses JavaScript and the HTML DOM to build and populate that page. The script has two essential modes of operation (ignoring the form page). 1) build the browser page with display element 2) populate the display element with output from the CLI application The /build/ mode provides a browser page with the basic HTML/CSS/JavaScript resources. Inside that page an XMLHttpRequest() initiates the execution of another instance of the script this time with a query-string. The query-string /populate=1/ initiates the mode where the script uses the pseudo-terminal driver (PTD) to create a terminal session and then spawn a subprocess attached to that terminal (see SCREPER.C). In that subprocess the CLI application (in this case HTTPDmon) is invoked and its output to the pseudo-terminal processed (scraped) to be HTML-escaped and otherwise munged for representative display in the display element. This munged output is transmitted back to the browser XMLHttpRequest() in these chunks. Each chunk is parsed from the total output and displayed as that chunk. COPYRIGHT --------- Copyright (C) 2021 Mark G.Daniel Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. VERSION LOG ----------- 01-AUG-2021 MGD initial */ /*****************************************************************************/ #define SOFTWAREVN "v1.0.0" #define SOFTWARENM "SMONITOR" #ifdef __ALPHA # define SOFTWAREID SOFTWARENM " " SOFTWAREVN " AXP" #endif #ifdef __ia64 # define SOFTWAREID SOFTWARENM " " SOFTWAREVN " IA64" #endif #ifdef __x86_64 # define SOFTWAREID SOFTWARENM " " SOFTWAREVN " X86" #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "screper.h" #define FI_LI "SHTTPMON",__LINE__ #ifndef UINT64PTR /* mainly to allow easy use of the __unaligned directive */ #define UINTPTR __unaligned unsigned int* #define ULONGPTR __unaligned unsigned long* #define USHORTPTR __unaligned unsigned short* #define UINT64PTR __unaligned uint64* #define INT64PTR __unaligned int64* #endif #define FONT_DEFAULT 11 #define INTERVAL_DEFAULT 3 #define MON_MAX 2*3 int begun, inspect, fontSize; char *CgiQueryString, *CgiRemoteUser, *CgiRequestUri, *CgiScriptName, *CgiServerSoftware; static char ScriptName [64], SyiNodeName [16]; char *MonClass [] = { "CLUSTER", "DECNET", "DISK", "DLOCK", "IO", "FCP", "LOCK", "FILE_SYSTEM_CACHE", "MODES", "MSCP_SERVER", "PAGE", "PROCESSES", "RLOCK", "RMS", "SCS", "STATES", "SYSTEM", "TIMER", "TRANSACTION", "VECTOR", NULL }; char *MonQual [] = { "/ALL", "/AVERAGE", "/CURRENT", "/MAXIMUM", "/MINIMUM", "/TOPBIO", "/TOPCPU", "/TOPDIO", "/TOPEXEC", "/TOPFAULT", "/TOPKERNEL", "/TOPSUPER", "/TOPUSER", NULL }; char *MonStyle = "\n"; /* prototypes */ void sMonitorBuild (); int sMonitorCount (); void sMonitorExit (int, int, char*); int sMonitorNumber (int); void sMonitorPage (); void sMonitorPopulate (); void sMonitorTerminal (int); void sMonitorSelect (); int sMonitorUrlDecode (char*); /*****************************************************************************/ /* */ int main (int argc, char *argv[]) { static unsigned long JpiPidItem = JPI$_PID; static unsigned long SyiNodeNameItem = SYI$_NODENAME; static $DESCRIPTOR (SyiNodeNameDsc, SyiNodeName); int interval, number, page; ushort slen; char *cptr, *sptr; /*********/ /* begin */ /*********/ if (argc > 1) { if (!strncasecmp (argv[1], "/VERSION", 4)) fprintf (stdout, "%s\n%s\n", SOFTWAREID, ScreperDo(NULL, "-version")); exit (SS$_NORMAL); } lib$getsyi (&SyiNodeNameItem, 0, &SyiNodeNameDsc, &slen, 0, 0); SyiNodeName[slen] = '\0'; if (!(CgiServerSoftware = getenv ("WWW_SERVER_SOFTWARE"))) CgiServerSoftware = getenv ("SERVER_SOFTWARE"); if (!CgiServerSoftware) sMonitorExit (SS$_NORMAL, __LINE__, NULL); if (cptr = getenv ("SMONITOR_INSPECT")) inspect = atol (cptr); if (!(CgiRequestUri = getenv ("WWW_REQUEST_URI"))) if (!(CgiRequestUri = getenv ("REQUEST_URI"))) CgiRequestUri = ""; if (!(CgiScriptName = getenv ("WWW_SCRIPT_NAME"))) if (!(CgiScriptName = getenv ("SCRIPT_NAME"))) CgiScriptName = ""; if (!(CgiQueryString = getenv ("WWW_QUERY_STRING"))) if (!(CgiQueryString = getenv ("QUERY_STRING"))) CgiQueryString = ""; CgiQueryString = strdup (CgiQueryString); sMonitorUrlDecode (CgiQueryString); strcpy (ScriptName, "sMONitor"); if (!(CgiRemoteUser = getenv ("SMONITOR_REMOTE_USER"))) if (!(CgiRemoteUser = getenv ("WWW_REMOTE_USER"))) if (!(CgiRemoteUser = getenv ("REMOTE_USER"))) CgiRemoteUser = ""; if (!*CgiRemoteUser) { fprintf (stdout, "Status: 403 Forbidden\r\n\ Content-Type: text/html\r\n\ \r\n\ \n\ \n\ \n\ \n\ %s:: %s\n\ %s\ \n\ \n\
authorization failure
\n\ \n\ \n", SOFTWAREID, SyiNodeName, ScriptName, MonStyle); sMonitorExit (SS$_NORMAL, __LINE__, NULL); } if (*CgiQueryString) { if (!(stdout = freopen ("SYS$OUTPUT", "w", stdout, "ctx=bin"))) sMonitorExit (vaxc$errno, __LINE__, NULL); fontSize = FONT_DEFAULT; if (cptr = strstr (CgiQueryString, "font=")) fontSize = atoi(cptr+5); if (fontSize < 8 || fontSize >= 18) fontSize = FONT_DEFAULT; if (strstr (CgiQueryString, "populate=")) sMonitorPopulate (); else sMonitorPage (); } else sMonitorSelect (); sMonitorExit (SS$_NORMAL, __LINE__, NULL); } /*****************************************************************************/ /* Provide a form controlling the various MONITOR parameters. */ void sMonitorSelect () { static unsigned long SyiClusterMember; static unsigned long SyiClusterMemberItem = SYI$_CLUSTER_MEMBER; static char *selected = " checked"; int idx, count, font = 11, interval = 6; char *cptr, *dptr, *sptr; /*********/ /* begin */ /*********/ lib$getsyi (&SyiClusterMemberItem, &SyiClusterMember, 0, 0, 0, 0); begun = 1; fprintf (stdout, "Status: 200 OK\r\n\ Content-Type: text/html\r\n\ Script-Control: X-stream-mode=1\r\n\ %s\ \r\n\ \n\ \n\ \n\ \n\ %s:: %s\n\ %s\ \n\ \n\

sMONitor

\n\
\n\ \n", ScreperDo(NULL,"-csp"), SOFTWAREID, SyiNodeName, ScriptName, MonStyle, CgiScriptName); /* prefix allows to differentiate between LOCK, DLOCK, RLOCK */ fprintf (stdout, "\n"); fprintf (stdout, ""); for (count = 1; count <= MON_MAX; count++) fprintf (stdout, "", count); fprintf (stdout, "\n"); fprintf (stdout, "\n", MON_MAX); for (idx = 0; cptr = MonClass[idx]; idx++) { fprintf (stdout, "\n"); for (count = 1; count <= MON_MAX; count++) { if (!strcmp(cptr,"RMS")) sptr = " disabled"; else if (count == 1 && !strcmp(cptr,"SYSTEM")) sptr = " checked"; else sptr = ""; fprintf (stdout, "\n", cptr, count, sptr, cptr); } fprintf (stdout, "\n"); } fprintf (stdout, "\n", MON_MAX); for (idx = 0; cptr = MonQual[idx]; idx++) { fprintf (stdout, "\n"); for (count = 1; count <= MON_MAX; count++) fprintf (stdout, "\n", cptr, count, cptr); fprintf (stdout, "\n"); } fprintf (stdout, "\n", MON_MAX); fprintf (stdout, "\n"); for (count = 1; count <= MON_MAX; count++) fprintf (stdout, "\n", count == 1 ? " " : " qual", count == 1 ? "/NODE= " : "", count, SyiClusterMember ? "" : " disabled"); fprintf (stdout, "\n"); fprintf (stdout, "\n", MON_MAX); fprintf (stdout, "\n\ \n\ \ \n\ \n", interval == 2 ? selected : "", interval == 4 ? selected : "", interval == 6 ? selected : "", interval == 10 ? selected : "", interval == 20 ? selected : "", font == 8 ? selected : "", font == 10 ? selected : "", font == 11 ? selected : "", font == 12 ? selected : "", font == 14 ? selected : ""); fprintf (stdout, "
%d
 %s
" " %s
%s" "
 
\n\ \n\ \n\ \n\ \ \ \ \ \ \ \
Interval:2461020
\n\
\n\ \n\ \n\ \n\ \ \ \ \ \ \ \
Font:810111214
\n\
\n\

  \ \n\

\n"); fprintf (stdout, "\n\ \n"); sMonitorExit (SS$_NORMAL, __LINE__, NULL); } /*****************************************************************************/ /* A page with multiple terminal screen outputs, each in an iframe. If a single screen then sMonitorTerminal() provides that directly. */ void sMonitorPage () { int number; char *cptr; void *scrptr = NULL; /*********/ /* begin */ /*********/ if ((number = sMonitorCount()) <= 1) { sMonitorTerminal (1); return; } number = 0; if (cptr = strstr (CgiQueryString, "&number=")) number = atoi (cptr+8); if (number >= 1 && number <= MON_MAX) { sMonitorTerminal (number); return; } scrptr = ScreperInit (); begun = 1; fprintf (stdout, "Status: 200 OK\r\n\ Content-Type: text/html\r\n\ Script-Control: X-stream-mode=1\r\n\ %s\ \r\n\ \n\ \n\ \n\ \n\ %s:: %s\n\ %s\ %s\ \n\ \n", ScreperDo(scrptr,"-csp"), SOFTWAREID, SyiNodeName, ScriptName, ScreperDo(scrptr,"-resize"), MonStyle); fprintf (stdout, "
\n"); for (number = 1; number <= MON_MAX; number++) if (sMonitorNumber (number)) fprintf (stdout, "\n", number, CgiRequestUri, number); fprintf (stdout, "
\n\ %s\ \n\ \n", ScreperDo(scrptr,"-pause")); sMonitorExit (SS$_NORMAL, __LINE__, NULL); } /*****************************************************************************/ /* A page with the embedded terminal screen output. If multiple concurrent monitors then this will be in an iframe. */ void sMonitorTerminal (int number) { void *scrptr = NULL; /*********/ /* begin */ /*********/ begun = 1; scrptr = ScreperInit (); fprintf (stdout, "Status: 200 OK\r\n\ Content-Type: text/html\r\n\ Script-Control: X-stream-mode=1\r\n\ %s\ \r\n\ \n\ \n\ \n\ \n\ %s:: %s\n\ %s\ %s\ \n\ \n\ %s\ \n\ \n", ScreperDo (scrptr, "-csp"), SOFTWAREID, SyiNodeName, ScriptName, ScreperDo (scrptr, "-css"), ScreperDo (scrptr, "-javascript"), ScreperDo (scrptr, "-screen")); sMonitorExit (SS$_NORMAL, __LINE__, NULL); } /*****************************************************************************/ /* Spawn the command subprocess and provide the HTML-ified, scraped terminal screen output back to the embedded virtual terminal. */ void sMonitorPopulate () { int cnt, idx, interval, number, status; char *aptr, *cptr, *rptr, *sptr; char scratch [64], scrdo [512]; void *scrptr = NULL; /*********/ /* begin */ /*********/ begun = 1; interval = INTERVAL_DEFAULT; if (cptr = strstr (CgiQueryString, "interval=")) { interval = atoi(cptr+9); if (interval < 2 || interval > 60) interval = INTERVAL_DEFAULT; } number = 0; if (cptr = strstr (CgiQueryString, "number=")) number = atoi (cptr+7); if (number < 1 || number > MON_MAX) number = 1; sptr += sprintf (sptr = scrdo, "-inspect=%d -utility=\"%s\" -page=24 -font=%d -snapshot=%d -noecho " "-dcl=set terminal /nobroadcast\r\n", inspect, "sMONitor", fontSize, (interval * 10) - 5); sptr += sprintf (sptr, "MONITOR "); for (idx = cnt = 0; cptr = MonClass[idx]; idx++) { /* need to differentiate between LOCK, DLOCK, RLOCK */ sprintf (scratch, "&%s=%d", cptr, number); if (strstr (CgiQueryString, scratch)) sptr += sprintf (sptr, "%s%s", cnt++ ? "," : "", MonClass[idx]); } if (!cnt) strcat (sptr, "SYSTEM"); while (*sptr) sptr++; for (idx = 0; cptr = MonQual[idx]; idx++) { sprintf (scratch, "&%s=%d", cptr, number); if (strstr (CgiQueryString, scratch)) sptr += sprintf (sptr, " %s", MonQual[idx]); } sprintf (scratch, "&NODE%d=", number); if (cptr = strstr (CgiQueryString, scratch)) { while (*cptr && *cptr != '=') cptr++; if (*cptr) cptr++; for (aptr = cptr; *cptr && *cptr != '&'; cptr++); if (cptr - aptr > 0) sptr += sprintf (sptr, " /NODE=%*.*s", cptr-aptr, cptr-aptr, aptr); } sptr += sprintf (sptr, " /INTERVAL=%d", interval); /* facilitate development - no referer then is populate=1 directly */ if (!(rptr = getenv ("WWW_HTTP_REFERER"))) rptr = getenv ("HTTP_REFERER"); scrptr = ScreperInit (); if (inspect && rptr) fprintf (stdout, "Status: 200 OK\r\n\ Content-Type: text/plain\r\n\ Script-Control: X-record-mode=1\r\n\ %s\ \r\n", ScreperDo(scrptr,"-csp")); else fprintf (stdout, "Content-Type: text/html\r\n\ Script-Control: X-stream-mode=1\r\n\ Script-Control: X-timeout-output=none\r\n\ Script-Control: X-content-encoding-gzip=0\r\n\ %s\ \r\n", ScreperDo(scrptr,"-csp")); if (inspect) printf ("|%s|\n|%s|\n", CgiQueryString, scrdo); fflush (stdout); if (!rptr) /* no referer then is populate=1 directly */ fprintf (stdout, "\n\n%s\n
\n",
               ScreperDo (scrptr, "-css"));
 
   sptr = ScreperDo (scrptr, scrdo);
   if (*(USHORTPTR)sptr == '%X')
      status = strtol (sptr+2, NULL, 16);
   else
      status = SS$_BUGCHECK;

   sMonitorExit (status, __LINE__, sptr);
}

/*****************************************************************************/
/*
By parsing the query string, return true or false if the specified terminal
number is present.
*/

int sMonitorNumber (int number)

{
   int  idx;
   char  *cptr;
   char  class [64];

   /*********/
   /* begin */
   /*********/

   for (idx = 0; cptr = MonClass[idx]; idx++)
   {
      sprintf (class, "&%s=%d", cptr, number);
      if (strstr (CgiQueryString, class)) return (1);
   }
   return (0);
}

/*****************************************************************************/
/*
By parsing the query string, return the number of individual monitor terminals.
*/

int sMonitorCount ()

{
   int  count, number;

   /*********/
   /* begin */
   /*********/

   count = 0;
   for (number = 1; number <= MON_MAX; number++)
      if (sMonitorNumber (number)) count++;
   return (count);
}

/*****************************************************************************/
/*
Perform in-place URL decode.  Perform strdup() before calling to retain
original then free() when finished using, as required.
*/

int sMonitorUrlDecode (char *string)

{
   uint  ch;
   char  *cptr, *sptr;

   /*********/
   /* begin */
   /*********/

   for (cptr = sptr = string; *cptr; cptr++)
   {
      if (*cptr == '+')
         *sptr++ = ' ';
      else
      if (*cptr == '%')
      {
         *sptr = '\0';
         cptr++;
         if (*(cptr+1))
         {
            if (sscanf (cptr, "%02x", &ch) < 1) return (0);
            *sptr++ = ch;
         }
         else
            return (0);
         cptr++;
      }
      else
         *sptr++ = *cptr;
   }
   *sptr = '\0';
   return (sptr - string);
}

/*****************************************************************************/
/*
Provide some WATCHable exit information.
*/

void sMonitorExit (int status, int line, char* sptr)

{
   /*********/
   /* begin */
   /*********/

   if (!begun)
      fputs ("Status: 200 OK\r\n\
Content-Type: text/plain\r\n\
Script-Control: X-record-mode=1\r\n\
\r\n", stdout);

   fprintf (stdout, "\n\n",
            line, status, sptr ? " " : "", sptr ? sptr : "");

   exit (status);
}

/*****************************************************************************/