]> andersk Git - moira.git/blob - clients/mrcheck/mrcheck.c
8d64a42fabf7e620b20f0845f6e821fd1c707f58
[moira.git] / clients / mrcheck / mrcheck.c
1 /* $Id$
2  *
3  * Verify that all Moira updates are successful
4  *
5  * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology.
6  * For copying and distribution information, please see the file
7  * <mit-copyright.h>.
8  */
9
10 #include <mit-copyright.h>
11 #include <moira.h>
12 #include <moira_site.h>
13 #include <mrclient.h>
14
15 #include <sys/time.h>
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <time.h>
21
22 RCSID("$Header$");
23
24 char *atot(char *itime);
25 int process_server(int argc, char **argv, void *sqv);
26 void disp_svc(char **argv, char *msg);
27 int process_host(int argc, char **argv, void *sqv);
28 void disp_sh(char **argv, char *msg);
29 void usage(void);
30
31 char *whoami;
32 static int count = 0;
33 static struct timeval now;
34
35 struct service {
36   char name[17];
37   char update_int[10];
38 };
39
40
41 /* turn an ascii string containing the number of seconds since the epoch
42  * into an ascii string containing the corresponding time & date
43  */
44
45 char *atot(char *itime)
46 {
47   time_t time;
48   char *ct;
49
50   time = atoi(itime);
51   ct = ctime(&time);
52   ct[24] = 0;
53   return &ct[4];
54 }
55
56
57 /* Decide if the server has an error or not.  Also, save the name and
58  * interval for later use.
59  */
60
61 int process_server(int argc, char **argv, void *sqv)
62 {
63   struct service *s;
64   struct save_queue *sq = sqv;
65
66   if (atoi(argv[SVC_ENABLE]))
67     {
68       s = malloc(sizeof(struct service));
69       strcpy(s->name, argv[SVC_SERVICE]);
70       strcpy(s->update_int, argv[SVC_INTERVAL]);
71       sq_save_data(sq, s);
72     }
73
74   if (atoi(argv[SVC_HARDERROR]) && atoi(argv[SVC_ENABLE]))
75     disp_svc(argv, "Error needs to be reset\n");
76   else if (atoi(argv[SVC_HARDERROR]) ||
77            (!atoi(argv[SVC_ENABLE]) && atoi(argv[SVC_DFCHECK])))
78     disp_svc(argv, "Should this be enabled?\n");
79   else if (atoi(argv[SVC_ENABLE]) &&
80            60 * atoi(argv[SVC_INTERVAL]) + 86400 + atoi(argv[SVC_DFCHECK])
81            < now.tv_sec)
82     disp_svc(argv, "Service has not been updated\n");
83
84   return MR_CONT;
85 }
86
87
88 /* Format the information about a service. */
89
90 void disp_svc(char **argv, char *msg)
91 {
92   char *tmp = strdup(atot(argv[SVC_DFGEN]));
93
94   printf("Service %s Interval %s %s/%s/%s %s\n",
95          argv[SVC_SERVICE], argv[SVC_INTERVAL],
96          atoi(argv[SVC_ENABLE]) ? "Enabled" : "Disabled",
97          atoi(argv[SVC_INPROGRESS]) ? "InProgress" : "Idle",
98          atoi(argv[SVC_HARDERROR]) ? "Error" : "NoError",
99          atoi(argv[SVC_HARDERROR]) ? argv[SVC_ERRMSG] : "");
100   printf("  Generated %s; Last checked %s\n", tmp, atot(argv[SVC_DFCHECK]));
101   printf("  Last modified by %s at %s with %s\n",
102          argv[SVC_MODBY], argv[SVC_MODTIME], argv[SVC_MODWITH]);
103   printf(" * %s\n", msg);
104   count++;
105   free(tmp);
106 }
107
108
109 /* Decide if the host has an error or not. */
110
111 int process_host(int argc, char **argv, void *sqv)
112 {
113   struct service *s = NULL;
114   struct save_queue *sq = sqv, *sq1;
115   char *update_int = NULL;
116
117   for (sq1 = sq->q_next; sq1 != sq; sq1 = sq1->q_next)
118     {
119       if ((s = (struct service *)sq1->q_data) &&
120           !strcmp(s->name, argv[SH_SERVICE]))
121         break;
122     }
123   if (s && !strcmp(s->name, argv[SH_SERVICE]))
124     update_int = s->update_int;
125
126   if (atoi(argv[SH_HOSTERROR]) && atoi(argv[SH_ENABLE]))
127     disp_sh(argv, "Error needs to be reset\n");
128   else if (atoi(argv[SH_HOSTERROR]) ||
129            (!atoi(argv[SH_ENABLE]) && atoi(argv[SH_LASTTRY])))
130     disp_sh(argv, "Should this be enabled?\n");
131   else if (atoi(argv[SH_ENABLE]) && update_int &&
132            60 * atoi(update_int) + 86400 + atoi(argv[SH_LASTSUCCESS])
133            < now.tv_sec)
134     disp_sh(argv, "Host has not been updated\n");
135
136   return MR_CONT;
137 }
138
139
140 /* Format the information about a host. */
141
142 void disp_sh(char **argv, char *msg)
143 {
144   char *tmp = strdup(atot(argv[SH_LASTTRY]));
145
146   printf("Host %s:%s %s/%s/%s/%s/%s %s\n",
147          argv[SH_SERVICE], argv[SH_MACHINE],
148          atoi(argv[SH_ENABLE]) ? "Enabled" : "Disabled",
149          atoi(argv[SH_SUCCESS]) ? "Success" : "Failure",
150          atoi(argv[SH_INPROGRESS]) ? "InProgress" : "Idle",
151          atoi(argv[SH_OVERRIDE]) ? "Override" : "Normal",
152          atoi(argv[SH_HOSTERROR]) ? "Error" : "NoError",
153          atoi(argv[SH_HOSTERROR]) ? argv[SH_ERRMSG] : "");
154   printf("  Last try %s; Last success %s\n", tmp, atot(argv[SH_LASTSUCCESS]));
155   printf("  Last modified by %s at %s with %s\n",
156          argv[SH_MODBY], argv[SH_MODTIME], argv[SH_MODWITH]);
157   printf(" * %s\n", msg);
158   count++;
159   free(tmp);
160 }
161
162
163
164 int main(int argc, char *argv[])
165 {
166   char *args[2], buf[BUFSIZ];
167   struct save_queue *sq;
168   int status;
169   int auth_required = 1;
170
171   if ((whoami = strrchr(argv[0], '/')) == NULL)
172     whoami = argv[0];
173   else
174     whoami++;
175
176   if (argc == 2 && !strcmp(argv[1], "-noauth"))
177     auth_required = 0;
178   else if (argc > 1)
179     usage();
180
181   if (mrcl_connect(NULL, NULL, 2, 0) != MRCL_SUCCESS)
182     exit(2);
183   status = mr_auth("mrcheck");
184   if (status && auth_required)
185     {
186       sprintf(buf, "\nAuthorization failure -- run \"kinit\" and try again");
187       goto punt;
188     }
189
190   gettimeofday(&now, 0);
191   sq = sq_create();
192
193   /* Check services first */
194   args[0] = "*";
195   if ((status = mr_query("get_server_info", 1, args, process_server, sq)) &&
196       status != MR_NO_MATCH)
197     com_err(whoami, status, " while getting servers");
198
199   args[1] = "*";
200   if ((status = mr_query("get_server_host_info", 2, args, process_host, sq)) &&
201       status != MR_NO_MATCH)
202     com_err(whoami, status, " while getting servers");
203
204   if (!count)
205     printf("Nothing has failed at this time\n");
206   else
207     printf("%d thing%s ha%s failed at this time\n", count,
208            count == 1 ? "" : "s", count == 1 ? "s" : "ve");
209
210   mr_disconnect();
211   exit(0);
212
213 punt:
214   com_err(whoami, status, buf);
215   mr_disconnect();
216   exit(1);
217 }
218
219 void usage(void)
220 {
221   fprintf(stderr, "Usage: %s [-noauth]\n", whoami);
222   exit(1);
223 }
This page took 0.039661 seconds and 3 git commands to generate.