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