]> andersk Git - moira.git/blob - clients/mrcheck/mrcheck.c
Command line printer manipulation client, and build goo.
[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 <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <time.h>
19
20 #define argis(a, b) (!strcmp(*arg + 1, a) || !strcmp(*arg + 1, b))
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 time_t 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)
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)
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   char **arg = argv;
171   char *server = NULL;
172
173   if ((whoami = strrchr(argv[0], '/')) == NULL)
174     whoami = argv[0];
175   else
176     whoami++;
177
178   /* parse our command line options */
179   while (++arg - argv < argc)
180     {
181       if (**arg == '-')
182         {
183           if (argis("n", "noauth"))
184             auth_required = 0;
185           else if (argis("db", "database"))
186             {
187               if (arg - argv < argc - 1)
188                 {
189                   ++arg;
190                   server = *arg;
191                 }
192               else
193                 usage();
194             }
195         }
196       else
197         usage();
198     }
199
200   if (mrcl_connect(server, "mrcheck", 2, auth_required) != MRCL_SUCCESS)
201     exit(2);
202
203   now = time(NULL);
204   sq = sq_create();
205
206   /* Check services first */
207   args[0] = "*";
208   if ((status = mr_query("get_server_info", 1, args, process_server, sq)) &&
209       status != MR_NO_MATCH)
210     com_err(whoami, status, " while getting servers");
211
212   args[1] = "*";
213   if ((status = mr_query("get_server_host_info", 2, args, process_host, 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 void usage(void)
233 {
234   fprintf(stderr, "Usage: %s [-noauth] [-db|-database server[:port]]\n",
235           whoami);
236   exit(1);
237 }
This page took 0.052078 seconds and 5 git commands to generate.