]> andersk Git - moira.git/blobdiff - clients/mrcheck/mrcheck.c
Make mrcl_connect authenticate for us rather than doing it seperately.
[moira.git] / clients / mrcheck / mrcheck.c
index 50c50a371b92a21c7e0dcbb210c7363bc581e156..6dfb9ccb675754f4c31f0ba33838ef8d31ece292 100644 (file)
-/*
- * Verify that all SMS updates are successful
+/* $Id$
  *
- * Copyright 1988 by the Massachusetts Institute of Technology. For copying
- * and distribution information, see the file "mit-copyright.h". 
+ * Verify that all Moira updates are successful
  *
- * $Source$
- * $Header$
- * $Author$
+ * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology.
+ * For copying and distribution information, please see the file
+ * <mit-copyright.h>.
  */
 
-#ifndef lint
-static char *rcsid_chsh_c = "$Header$";
-#endif lint
+#include <mit-copyright.h>
+#include <moira.h>
+#include <moira_site.h>
+#include <mrclient.h>
 
 #include <stdio.h>
-#include <sms.h>
-#include <sms_app.h>
-#include "mit-copyright.h"
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
 
-char *malloc(), *rindex(), *strsave();
+#define argis(a, b) (!strcmp(*arg + 1, a) || !strcmp(*arg + 1, b))
 
-static int status;
-static char *whoami;
+RCSID("$Header$");
 
+char *atot(char *itime);
+int process_server(int argc, char **argv, void *sqv);
+void disp_svc(char **argv, char *msg);
+int process_host(int argc, char **argv, void *sqv);
+void disp_sh(char **argv, char *msg);
+void usage(void);
 
-char *atot(itime)
-char *itime;
+char *whoami;
+static int count = 0;
+static time_t now;
+
+struct service {
+  char name[17];
+  char update_int[10];
+};
+
+
+/* turn an ascii string containing the number of seconds since the epoch
+ * into an ascii string containing the corresponding time & date
+ */
+
+char *atot(char *itime)
 {
-    int time;
-    char *ct, *ctime();
+  time_t time;
+  char *ct;
 
-    time = atoi(itime);
-    ct = ctime(&time);
-    ct[24] = 0;
-    return(&ct[4]);
+  time = atoi(itime);
+  ct = ctime(&time);
+  ct[24] = 0;
+  return &ct[4];
 }
 
-gserv(argc, argv, sq)
-int argc;
-char **argv;
-struct save_queue *sq;
+
+/* Decide if the server has an error or not.  Also, save the name and
+ * interval for later use.
+ */
+
+int process_server(int argc, char **argv, void *sqv)
 {
-    char *tmp;
+  struct service *s;
+  struct save_queue *sq = sqv;
+
+  if (atoi(argv[SVC_ENABLE]))
+    {
+      s = malloc(sizeof(struct service));
+      strcpy(s->name, argv[SVC_SERVICE]);
+      strcpy(s->update_int, argv[SVC_INTERVAL]);
+      sq_save_data(sq, s);
+    }
 
-    tmp = strsave(atot(argv[4]));
-    printf("Service %s, error %s: %s\n\tlast success %s, last try %s\n",
-          argv[0], argv[9], argv[10], tmp, atot(argv[5]));
-    free(tmp);
-    return(SMS_CONT);
+  if (atoi(argv[SVC_HARDERROR]) && atoi(argv[SVC_ENABLE]))
+    disp_svc(argv, "Error needs to be reset\n");
+  else if (atoi(argv[SVC_HARDERROR]) ||
+          (!atoi(argv[SVC_ENABLE]) && atoi(argv[SVC_DFCHECK])))
+    disp_svc(argv, "Should this be enabled?\n");
+  else if (atoi(argv[SVC_ENABLE]) &&
+          60 * atoi(argv[SVC_INTERVAL]) + 86400 + atoi(argv[SVC_DFCHECK])
+          < now)
+    disp_svc(argv, "Service has not been updated\n");
+
+  return MR_CONT;
 }
 
-ghost(argc, argv, sq)
-int argc;
-char **argv;
-struct save_queue *sq;
-{
-    char *tmp;
 
-    tmp = strsave(atot(argv[9]));
-    printf("Host %s:%s, error %s: %s\n\tlast success %s, last try %s\n",
-          argv[0], argv[1], argv[6], argv[7], tmp, atot(argv[8]));
-    free(tmp);
-    return(SMS_CONT);
+/* Format the information about a service. */
+
+void disp_svc(char **argv, char *msg)
+{
+  char *tmp = strdup(atot(argv[SVC_DFGEN]));
+
+  printf("Service %s Interval %s %s/%s/%s %s\n",
+        argv[SVC_SERVICE], argv[SVC_INTERVAL],
+        atoi(argv[SVC_ENABLE]) ? "Enabled" : "Disabled",
+        atoi(argv[SVC_INPROGRESS]) ? "InProgress" : "Idle",
+        atoi(argv[SVC_HARDERROR]) ? "Error" : "NoError",
+        atoi(argv[SVC_HARDERROR]) ? argv[SVC_ERRMSG] : "");
+  printf("  Generated %s; Last checked %s\n", tmp, atot(argv[SVC_DFCHECK]));
+  printf("  Last modified by %s at %s with %s\n",
+        argv[SVC_MODBY], argv[SVC_MODTIME], argv[SVC_MODWITH]);
+  printf(" * %s\n", msg);
+  count++;
+  free(tmp);
 }
 
-save_args(argc, argv, sq)
-int argc;
-char **argv;
-struct save_queue *sq;
+
+/* Decide if the host has an error or not. */
+
+int process_host(int argc, char **argv, void *sqv)
 {
-    sq_save_args(argc, argv, sq);
-    return(SMS_CONT);
+  struct service *s = NULL;
+  struct save_queue *sq = sqv, *sq1;
+  char *update_int = NULL;
+
+  for (sq1 = sq->q_next; sq1 != sq; sq1 = sq1->q_next)
+    {
+      if ((s = (struct service *)sq1->q_data) &&
+         !strcmp(s->name, argv[SH_SERVICE]))
+       break;
+    }
+  if (s && !strcmp(s->name, argv[SH_SERVICE]))
+    update_int = s->update_int;
+
+  if (atoi(argv[SH_HOSTERROR]) && atoi(argv[SH_ENABLE]))
+    disp_sh(argv, "Error needs to be reset\n");
+  else if (atoi(argv[SH_HOSTERROR]) ||
+          (!atoi(argv[SH_ENABLE]) && atoi(argv[SH_LASTTRY])))
+    disp_sh(argv, "Should this be enabled?\n");
+  else if (atoi(argv[SH_ENABLE]) && update_int &&
+          60 * atoi(update_int) + 86400 + atoi(argv[SH_LASTSUCCESS])
+          < now)
+    disp_sh(argv, "Host has not been updated\n");
+
+  return MR_CONT;
 }
 
-main(argc, argv)
-    char *argv[];
 
+/* Format the information about a host. */
+
+void disp_sh(char **argv, char *msg)
 {
-    char *args[6], buf[BUFSIZ], **service, **host;
-    struct save_queue *services, *hosts;
-    int count = 0, scream();
+  char *tmp = strdup(atot(argv[SH_LASTTRY]));
+
+  printf("Host %s:%s %s/%s/%s/%s/%s %s\n",
+        argv[SH_SERVICE], argv[SH_MACHINE],
+        atoi(argv[SH_ENABLE]) ? "Enabled" : "Disabled",
+        atoi(argv[SH_SUCCESS]) ? "Success" : "Failure",
+        atoi(argv[SH_INPROGRESS]) ? "InProgress" : "Idle",
+        atoi(argv[SH_OVERRIDE]) ? "Override" : "Normal",
+        atoi(argv[SH_HOSTERROR]) ? "Error" : "NoError",
+        atoi(argv[SH_HOSTERROR]) ? argv[SH_ERRMSG] : "");
+  printf("  Last try %s; Last success %s\n", tmp, atot(argv[SH_LASTSUCCESS]));
+  printf("  Last modified by %s at %s with %s\n",
+        argv[SH_MODBY], argv[SH_MODTIME], argv[SH_MODWITH]);
+  printf(" * %s\n", msg);
+  count++;
+  free(tmp);
+}
 
-    init_sms_err_tbl();
-    init_krb_err_tbl();
 
-    if ((whoami = rindex(argv[0], '/')) == NULL)
-       whoami = argv[0];
-    else
-       whoami++;
 
-    if (argc > 1) {
+int main(int argc, char *argv[])
+{
+  char *args[2], buf[BUFSIZ];
+  struct save_queue *sq;
+  int status;
+  int auth_required = 1;
+  char **arg = argv;
+  char *server = NULL;
+
+  if ((whoami = strrchr(argv[0], '/')) == NULL)
+    whoami = argv[0];
+  else
+    whoami++;
+
+  /* parse our command line options */
+  while (++arg - argv < argc)
+    {
+      if (**arg == '-')
+       {
+         if (argis("n", "noauth"))
+           auth_required = 0;
+         else if (argis("db", "database"))
+           {
+             if (arg - argv < argc - 1)
+               {
+                 ++arg;
+                 server = *arg;
+               }
+             else
+               usage();
+           }
+       }
+      else
        usage();
     }
 
-    status = sms_connect(SMS_SERVER);
-    if (status) {
-       (void) sprintf(buf, "\nConnection to the SMS server failed.");
-       goto punt;
-    }
+  if (mrcl_connect(server, "mrcheck", 2, auth_required) != MRCL_SUCCESS)
+    exit(2);
 
-    status = sms_auth("smscheck");
-    if (status) {
-       (void) sprintf(buf, "\nAuthorization failure -- run \"kinit\" \
-and try again");
-       goto punt;
-    }
+  now = time(NULL);
+  sq = sq_create();
 
-    services = sq_create();
-    args[0] = args[2] = "TRUE";
-    args[1] = "DONTCARE";
-    if ((status = sms_query("qualified_get_server", 3, args, save_args,
-                           (char *)services)) &&
-       status != SMS_NO_MATCH)
-      com_err(whoami, status, " while getting servers");
-
-    hosts = sq_create();
-    args[0] = "*";
-    args[1] = args[5] = "TRUE";
-    args[2] = args[3] = args[4] = "DONTCARE";
-    if ((status = sms_query("qualified_get_server_host", 6, args, save_args,
-                           (char *)hosts)) &&
-       status != SMS_NO_MATCH)
-      com_err(whoami, status, " while getting server/hosts");
-
-    while (sq_get_data(services, &service)) {
-       count++;
-       if (status = sms_query("get_server_info", 1, service, gserv, NULL))
-         com_err(whoami, status, " while getting info about service %s",
-                 service[0]);
-    }
+  /* Check services first */
+  args[0] = "*";
+  if ((status = mr_query("get_server_info", 1, args, process_server, sq)) &&
+      status != MR_NO_MATCH)
+    com_err(whoami, status, " while getting servers");
 
-    while (sq_get_data(hosts, &host)) {
-       count++; 
-       if (status = sms_query("get_server_host_info", 2, host, ghost, NULL))
-         com_err(whoami, status, " while getting info about host %s:%s",
-                 host[0], host[1]);
-   }
+  args[1] = "*";
+  if ((status = mr_query("get_server_host_info", 2, args, process_host, sq)) &&
+      status != MR_NO_MATCH)
+    com_err(whoami, status, " while getting servers");
 
-    if (!count)
-      strcpy(buf, "Nothing has failed at this time");
-    else
-      sprintf(buf, "%d thing%s ha%s failed at this time", count,
-             count == 1 ? "" : "s", count == 1 ? "s" : "ve");
-    puts(buf);
+  if (!count)
+    printf("Nothing has failed at this time\n");
+  else
+    printf("%d thing%s ha%s failed at this time\n", count,
+          count == 1 ? "" : "s", count == 1 ? "s" : "ve");
 
-    sms_disconnect();
-    exit(0);
+  mr_disconnect();
+  exit(0);
 
 punt:
-    com_err(whoami, status, buf);
-    sms_disconnect();
-    exit(1);
-}
-
-
-scream()
-{
-    com_err(whoami, status, "Update to SMS returned a value -- \
-programmer botch.\n");
-    sms_disconnect();
-    exit(1);
+  com_err(whoami, status, buf);
+  mr_disconnect();
+  exit(1);
 }
 
-usage()
+void usage(void)
 {
-    fprintf(stderr, "Usage: %s\n", whoami);
-    exit(1);
+  fprintf(stderr, "Usage: %s [-noauth] [-db|-database server[:port]]\n",
+         whoami);
+  exit(1);
 }
This page took 0.046887 seconds and 4 git commands to generate.