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