- Provide server statistics in text format as well
authorZdeněk Šustr <sustr4@cesnet.cz>
Wed, 30 May 2012 16:22:48 +0000 (16:22 +0000)
committerZdeněk Šustr <sustr4@cesnet.cz>
Wed, 30 May 2012 16:22:48 +0000 (16:22 +0000)
- Contruct stats list systematically
- Return error code if statistics cannot be retireved

org.glite.lb.server/src/lb_html.c
org.glite.lb.server/src/lb_html.h
org.glite.lb.server/src/lb_proto.c
org.glite.lb.server/src/server_stats.h

index 1711f59..acee5c9 100644 (file)
@@ -534,89 +534,46 @@ int edg_wll_WSDLOutput(edg_wll_Context ctx UNUSED_VAR, char **message, char *fil
        return 0;
 }
 
-int edg_wll_StatisticsToHTML(edg_wll_Context ctx, char **message) {
+int edg_wll_StatisticsToHTML(edg_wll_Context ctx, char **message, int text) {
         char *out;
+       char* times[SERVER_STATISTICS_COUNT];
+       int i;
+       char *out_tmp;
+       time_t *starttime;
+
+       for (i = 0; i < SERVER_STATISTICS_COUNT; i++)
+               if (starttime = edg_wll_ServerStatisticsGetStart(ctx, i)) 
+                       if (text) asprintf(&(times[i]), "%ld", *starttime);
+                       else times[i] = strdup((const char*)ctime(starttime));
+               else
+                       times[i] = NULL;
+
+       if (!text) asprintf(&out, "<HTML>\n<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n<TITLE>Server Usage Statistics</TITLE>\n<BODY>\n"
+               "<h2>LB Server Usage Statistics</h2>\n"
+               "%s"
+               "<table halign=\"left\">\n"
+               "<tr><td>Variable</td><td>Value</td><td>Measured from</td></tr>\n",
+               edg_wll_ServerStatisticsInTmp() ? "<b>WARNING: L&B statistics are stored in /tmp, please, configure L&B server to make them really persistent!</b><br/><br/>\n" : "");
+       else
+               asprintf(&out, "");
+
+       for (i = 0; i < SERVER_STATISTICS_COUNT; i++) {
+               asprintf(&out_tmp, text ? "%s%s=%i,%s\n" : "%s<tr><td>%s</td><td>%i</td><td>%s</td></tr>\n", out,
+                       text ? edg_wll_server_statistics_type_key[i] : edg_wll_server_statistics_type_title[i],
+                       edg_wll_ServerStatisticsGetValue(ctx, i),
+                       times[i]);
+               free(out);
+               out = out_tmp;
+       }
 
-        if (ctx->count_server_stats == 2 && !ctx->noAuth && !check_authz_policy_ctx(ctx, ADMIN_ACCESS))  
-        {
-                asprintf(&out,"<h2>LB Server Usage Statistics</h2>\n"
-                        "Only superusers can view server usage statistics on this particular server.\n");
-        }
-        else
-        {
-                char* times[SERVER_STATISTICS_COUNT];
-                int i;
-               char *head;
-                for (i = 0; i < SERVER_STATISTICS_COUNT; i++)
-                        if (edg_wll_ServerStatisticsGetStart(ctx, i))
-                                times[i] = strdup((const char*)ctime(edg_wll_ServerStatisticsGetStart(ctx, i)));
-                        else
-                                times[i] = 0;
-
-               if (edg_wll_ServerStatisticsInTmp())
-                        asprintf(&head,
-                        "<b>WARNING: L&B statistics are stored in /tmp, please, configure L&B server to make them really persistent!</b><br/><br/>\n");
-                else
-                        asprintf(&head, " ");
-
-                asprintf(&out,
-                        "<h2>LB Server Usage Statistics</h2>\n"
-                       "%s"
-                        "<table halign=\"left\">\n"
-                        "<tr><td>Variable</td><td>Value</td><td>Measured from</td></tr>\n"
-                        "<tr><td>gLite job regs</td><td>%i</td><td>%s</td></tr>\n"
-                        "<tr><td>PBS job regs</td><td>%i</td><td>%s</td></tr>\n"
-                        "<tr><td>Condor job regs</td><td>%i</td><td>%s</td></tr>\n"
-                        "<tr><td>CREAM job regs</td><td>%i</td><td>%s</td></tr>\n"
-                        "<tr><td>Sandbox regs</td><td>%i</td><td>%s</td></tr>\n"
-                        "<tr><td>Notification regs (legacy interface)</td><td>%i</td><td>%s</td></tr>\n"
-                        "<tr><td>Notification regs (msg interface)</td><td>%i</td><td>%s</td></tr>\n"
-                        "<tr><td>Job events</td><td>%i</td><td>%s</td></tr>\n"
-                        "<tr><td>Notifications sent (legacy)</td><td>%i</td><td>%s</td></tr>\n"
-                        "<tr><td>Notifications sent (msg)</td><td>%i</td><td>%s</td></tr>\n"
-                        "<tr><td>L&B protocol accesses</td><td>%i</td><td>%s</td></tr>\n"
-                        "<tr><td>WS queries</td><td>%i</td><td>%s</td></tr>\n"
-                        "<tr><td>HTML accesses</td><td>%i</td><td>%s</td></tr>\n"
-                        "<tr><td>Plain text accesses</td><td>%i</td><td>%s</td></tr>\n"
-                        "<tr><td>RSS accesses</td><td>%i</td><td>%s</td></tr>\n"
-                        "</table>\n",
-                       head,
-                       edg_wll_ServerStatisticsGetValue(ctx, SERVER_STATS_GLITEJOB_REGS),
-                        times[SERVER_STATS_GLITEJOB_REGS],
-                        edg_wll_ServerStatisticsGetValue(ctx, SERVER_STATS_PBSJOB_REGS),
-                        times[SERVER_STATS_PBSJOB_REGS],
-                        edg_wll_ServerStatisticsGetValue(ctx, SERVER_STATS_CONDOR_REGS),
-                        times[SERVER_STATS_CONDOR_REGS],
-                        edg_wll_ServerStatisticsGetValue(ctx, SERVER_STATS_CREAM_REGS),
-                        times[SERVER_STATS_CREAM_REGS],
-                        edg_wll_ServerStatisticsGetValue(ctx, SERVER_STATS_SANDBOX_REGS),
-                        times[SERVER_STATS_SANDBOX_REGS],
-                        edg_wll_ServerStatisticsGetValue(ctx, SERVER_STATS_NOTIF_LEGACY_REGS),
-                        times[SERVER_STATS_NOTIF_LEGACY_REGS],
-                        edg_wll_ServerStatisticsGetValue(ctx, SERVER_STATS_NOTIF_MSG_REGS),
-                        times[SERVER_STATS_NOTIF_MSG_REGS],
-                        edg_wll_ServerStatisticsGetValue(ctx, SERVER_STATS_JOB_EVENTS),
-                        times[SERVER_STATS_JOB_EVENTS],
-                        edg_wll_ServerStatisticsGetValue(ctx, SERVER_STATS_NOTIF_LEGACY_SENT),
-                        times[SERVER_STATS_NOTIF_LEGACY_SENT],
-                        edg_wll_ServerStatisticsGetValue(ctx, SERVER_STATS_NOTIF_MSG_SENT),
-                        times[SERVER_STATS_NOTIF_MSG_SENT],
-                        edg_wll_ServerStatisticsGetValue(ctx, SERVER_STATS_LBPROTO),
-                        times[SERVER_STATS_LBPROTO],
-                        edg_wll_ServerStatisticsGetValue(ctx, SERVER_STATS_WS_QUERIES),
-                        times[SERVER_STATS_WS_QUERIES],
-                        edg_wll_ServerStatisticsGetValue(ctx, SERVER_STATS_HTML_VIEWS),
-                        times[SERVER_STATS_HTML_VIEWS],
-                        edg_wll_ServerStatisticsGetValue(ctx, SERVER_STATS_TEXT_VIEWS),
-                        times[SERVER_STATS_TEXT_VIEWS],
-                        edg_wll_ServerStatisticsGetValue(ctx, SERVER_STATS_RSS_VIEWS),
-                        times[SERVER_STATS_RSS_VIEWS]
-                );
-
-                for (i = 0; i < SERVER_STATISTICS_COUNT; i++)
-                        free(times[i]);
-               free(head);
-        }
+       if (!text) {
+               asprintf(&out_tmp, "%s</table>\n</BODY>\n</HTML>", out);
+               free(out);
+               out = out_tmp;
+       }
+
+       for (i = 0; i < SERVER_STATISTICS_COUNT; i++)
+               free(times[i]);
 
         *message = out;
        return 0;
index 682662d..675059d 100644 (file)
@@ -33,6 +33,6 @@ int edg_wll_UserNotifsToHTML(edg_wll_Context ctx, char **notifids, char **messag
 int edg_wll_NotificationToHTML(edg_wll_Context ctx, notifInfo *ni, char **message);
 char *edg_wll_ErrorToHTML(edg_wll_Context,int);
 int edg_wll_FileTransferStatusToHTML(edg_wll_Context ctx, edg_wll_JobStat stat, char **message);
-int edg_wll_StatisticsToHTML(edg_wll_Context ctx, char **message);
+int edg_wll_StatisticsToHTML(edg_wll_Context ctx, char **message, int text);
 
 #endif /* GLITE_LB_HTML_H */
index e00ef67..9ed3fd5 100644 (file)
@@ -925,13 +925,19 @@ edg_wll_ErrorCode edg_wll_Proto(edg_wll_Context ctx,
                } else if (extra_opt == HTTP_EXTRA_OPTION_CONFIGURATION) {
                        // also browser-readable HTML version here?
                        isadm = ctx->noAuth || edg_wll_amIroot(ctx->peerName, ctx->fqans,&ctx->authz_policy);
-
                        edg_wll_ConfigurationToText(ctx, isadm, &message);
                        edg_wll_ServerStatisticsIncrement(ctx, SERVER_STATS_TEXT_VIEWS);
        /* GET /?stats*/
                } else if (extra_opt == HTTP_EXTRA_OPTION_STATS) {
-                       edg_wll_StatisticsToHTML(ctx, &message);
-                       edg_wll_ServerStatisticsIncrement(ctx, SERVER_STATS_HTML_VIEWS);
+                       isadm = ctx->noAuth || edg_wll_amIroot(ctx->peerName, ctx->fqans,&ctx->authz_policy);
+                       if ( !isadm && ctx->count_server_stats == 2 ) {
+                               ret = HTTP_UNAUTH;
+                               edg_wll_SetError(ctx, EPERM, "Only superusers can view server usage statistics on this particular server.");
+                       }
+                       else {
+                               edg_wll_StatisticsToHTML(ctx, &message, text);
+                               edg_wll_ServerStatisticsIncrement(ctx, SERVER_STATS_HTML_VIEWS);
+                       }
        /* GET [something else]: not understood */
                } else ret = HTTP_BADREQ;
                free(requestPTR); requestPTR = NULL;
index d05c4f6..152e0d3 100644 (file)
@@ -13,8 +13,6 @@ typedef struct _edg_wll_server_statistics{
        time_t start;
 } edg_wll_server_statistics;
 
-#define SERVER_STATISTICS_COUNT 15
-
 enum edg_wll_server_statistics_type{
        SERVER_STATS_GLITEJOB_REGS = 0,
        SERVER_STATS_PBSJOB_REGS,
@@ -30,8 +28,44 @@ enum edg_wll_server_statistics_type{
        SERVER_STATS_NOTIF_LEGACY_SENT,
        SERVER_STATS_NOTIF_MSG_SENT,
        SERVER_STATS_WS_QUERIES,
-       SERVER_STATS_LBPROTO
+       SERVER_STATS_LBPROTO,
+       SERVER_STATISTICS_COUNT
 };
+// Add new values in front of LAST and do not change order
+
+static const char *edg_wll_server_statistics_type_title[] = {
+       "gLite job regs",
+       "PBS job regs",
+       "Condor job regs",
+       "CREAM job regs",
+       "Sandbox regs",
+       "Job events",
+       "HTML accesses",
+       "Plain text accesses",
+       "RSS accesses",
+       "Notification regs (legacy interface)",
+       "Notification regs (msg interface)",
+       "Notifications sent (legacy)",
+       "Notifications sent (msg)",
+       "WS queries",
+       "L&B protocol accesses" };
+
+static const char *edg_wll_server_statistics_type_key[] = {
+       "glite_jobs",
+       "pbs_jobs",
+       "condor_jobs",
+       "cream_jobs",
+       "sb_ft_jobs",
+       "events",
+       "queries_html",
+       "queries_text",
+       "queries_rss",
+       "notif_regs_legacy",
+       "notif_regs_msg",
+       "notifs_legacy",
+       "notis_msg",
+       "queries_ws",
+       "queries_api" };
 
 int edg_wll_InitServerStatistics(edg_wll_Context ctx, char *prefix);