bozo: bosserver man page updates
[openafs.git] / src / bozo / smail-notifier.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12 #include <afs/stds.h>
13
14 #include <roken.h>
15
16 #include <afs/afsutil.h>
17
18 /*
19  * XXX CHANGE the following depedent stuff XXX
20  */
21 #define SENDMAIL        "/afs/cellname/fs/dev/localtools/dest/bin/rcs-sendmail"
22 /*
23  * Replace it with  a bboard (i.e. transarc.bosserver.auto-reports)
24  */
25 #define RECIPIENT       "foo@cellname"
26
27 #include "AFS_component_version_number.c"
28
29 main(argc, argv)
30      int argc;
31      char **argv;
32 {
33     struct stat tstat;
34     FILE *fin = stdin;
35     char buf[BUFSIZ], *bufp, *bufp1, *typep, *cmd, *bp;
36     afs_int32 code, c, fd, id, pflags = -1, len, core = 0, lastE = 0;
37     char comLine[60], coreName[40], name[40], lastErrorName[50];
38     afs_int32 pid = -1, lastExit = -1, lastSignal = -1, rsCount = -1;
39     afs_int32 procStarts = -1;
40     afs_int32 errorCode = -1, errorSignal = -1, goal = -1;
41     time_t procStartTime = -1, rsTime = -1, lastAnyExit = -1, lastErrorExit = -1;
42     char *timeStamp;
43
44     typep = (char *)malloc(50);
45     cmd = (char *)malloc(50);
46     bufp = bufp1 = (char *)malloc(1000);
47     while (fgets(buf, sizeof(buf), fin)) {
48         code = sscanf(buf, "%s %s\n", typep, cmd);
49         if (code < 2) {
50             continue;
51         }
52         if (!strcmp(typep, "BEGIN") && !strcmp(cmd, "bnode_proc")) {
53             while (fgets(buf, sizeof(buf), fin)) {
54                 code = sscanf(buf, "%s %s\n", typep, cmd);
55                 if (code < 2) {
56                     printf("**bnode_proc**: typed=%s, cmd=%s\n", typep, cmd);
57                     break;
58                 }
59                 if (!strcmp(typep, "comLine:"))
60                     strcpy(comLine, cmd);
61                 else if (!strcmp(typep, "coreName:"))
62                     strcpy(coreName, cmd);
63                 else if (!strcmp(typep, "pid:"))
64                     pid = atoi(cmd);
65 #ifdef  notdef
66                 else if (!strcmp(typep, "lastExit:"))
67                     lastExit = atoi(cmd);
68                 else if (!strcmp(typep, "lastSignal:"))
69                     lastSignal = atoi(cmd);
70 #endif
71                 else if (!strcmp(typep, "flags:"))
72                     pflags = atoi(cmd);
73                 else if (!strcmp(typep, "END")) {
74                     break;
75                 } else {
76                     printf
77                         ("Unexpected token %s in the bnode_proc (should be END)\n",
78                          typep);
79                     exit(1);
80                 }
81             }
82         } else if (!strcmp(typep, "BEGIN") && !strcmp(cmd, "bnode")) {
83             while (fgets(buf, sizeof(buf), fin)) {
84                 code = sscanf(buf, "%s %s\n", typep, cmd);
85                 if (code < 2) {
86                     printf("**bnode**: typed=%s, cmd=%s\n", typep, cmd);
87                     break;
88                 }
89                 if (!strcmp(typep, "name:"))
90                     strcpy(name, cmd);
91                 else if (!strcmp(typep, "rsTime:"))
92                     rsTime = atoi(cmd);
93                 else if (!strcmp(typep, "rsCount:"))
94                     rsCount = atoi(cmd);
95                 else if (!strcmp(typep, "procStartTime:"))
96                     procStartTime = atoi(cmd);
97                 else if (!strcmp(typep, "procStarts:"))
98                     procStarts = atoi(cmd);
99                 else if (!strcmp(typep, "lastAnyExit:"))
100                     lastAnyExit = atoi(cmd);
101                 else if (!strcmp(typep, "lastErrorExit:"))
102                     lastErrorExit = atoi(cmd);
103                 else if (!strcmp(typep, "errorCode:"))
104                     errorCode = atoi(cmd);
105                 else if (!strcmp(typep, "errorSignal:"))
106                     errorSignal = atoi(cmd);
107 /*
108                 else if (!strcmp(typep, "lastErrorName:"))
109                     strcpy(lastErrorName, cmd);
110 */
111                 else if (!strcmp(typep, "goal:"))
112                     goal = atoi(cmd);
113                 else if (!strcmp(typep, "END")) {
114                     break;
115                 } else {
116                     printf
117                         ("Unexpected token %s in the bnode (should be END)\n",
118                          typep);
119                     exit(1);
120                 }
121             }
122         } else {
123             printf("Unexpected token %s (should be BEGIN)\n", typep);
124             exit(1);
125         }
126     }
127     /*
128      * Now make up the text for the post
129      */
130     sprintf(buf, "/tmp/snote.%d", getpid());
131     fd = open(buf, O_RDWR | O_CREAT | O_TRUNC, 0600);
132     if (fd == -1) {
133         perror(buf);
134         printf("Unable to create temp file, %s\n", buf);
135         exit(1);
136     }
137     (void)sprintf(bufp, "Subject: Bosserver's automatic notification\n\n");
138     bufp += strlen(bufp);
139     (void)sprintf(bufp,
140                   "AUTOMATIC NOTIFICATION EVENT FOR AFS SERVER INSTANCE %s\n\n",
141                   name);
142     bufp += strlen(bufp);
143     (void)sprintf(bufp, "Server Process id was: %d\n", pid);
144     bufp += strlen(bufp);
145     (void)sprintf(bufp, "Server command line: %s\n", comLine);
146     bufp += strlen(bufp);
147     if (strcmp(coreName, "(null)"))
148         core = 1;
149     bp = comLine;
150     strcpy(bp, AFSDIR_SERVER_CORELOG_FILEPATH);
151     if (core) {
152         strcat(bp, coreName);
153         strcat(bp, ".");
154     }
155     strcat(bp, name);
156     if ((code = stat(bp, &tstat)) == 0) {
157         c = 1;
158         if ((lastAnyExit - tstat.st_ctime) > 300)       /* > 5 mins old */
159             c = 0;
160         core = 1;
161     } else
162         core = 0;
163     strcat(bp, " ");
164     (void)sprintf(bufp, "There is %score dump left %sfor this server\n",
165                   (core ? (c ? "a recent " : "an 'old' ") : "no "),
166                   (core ? bp : ""));
167     bufp += strlen(bufp);
168 #ifdef  notdef
169     (void)sprintf(bufp, "Last Exit code %d\n", lastExit);
170     bufp += strlen(bufp);
171     (void)sprintf(bufp, "Last Signal number %d\n", lastSignal);
172     bufp += strlen(bufp);
173 #endif
174     if (pflags == 1)
175         strcpy(bp, "PROCESS STARTED");
176     else if (pflags == 2)
177         strcpy(bp, "PROCESS EXITED");
178     else
179         strcpy(bp, "UNKNOWN");
180     (void)sprintf(bufp, "Process state %d (%s)\n", pflags, bp);
181     bufp += strlen(bufp);
182     timeStamp = ctime(&rsTime);
183     timeStamp[24] = 0;
184     (void)sprintf(bufp, "\nNumber of restarts since %s is %d\n", timeStamp,
185                   rsCount);
186     bufp += strlen(bufp);
187     if (procStartTime) {
188         timeStamp = ctime(&procStartTime);
189         timeStamp[24] = 0;
190         (void)sprintf(bufp,
191                       "Number of process restarts since the process started %s is %d\n",
192                       timeStamp, procStarts);
193     }
194     bufp += strlen(bufp);
195     if (lastAnyExit) {
196         timeStamp = ctime(&lastAnyExit);
197         timeStamp[24] = 0;
198         (void)sprintf(bufp, "Last time process exited for any reason: %s\n",
199                       timeStamp);
200     }
201     bufp += strlen(bufp);
202     if (lastErrorExit) {
203         timeStamp = ctime(&lastErrorExit);
204         timeStamp[24] = 0;
205         (void)sprintf(bufp, "Last time process exited unexpectedly: %s\n",
206                       timeStamp);
207     }
208     bufp += strlen(bufp);
209     (void)sprintf(bufp, "Last exit return code %d\n", errorCode);
210     bufp += strlen(bufp);
211     (void)sprintf(bufp, "Last process terminating signal %d\n", errorSignal);
212     bufp += strlen(bufp);
213 #ifdef  notdef
214     if (strcmp(lastErrorName, "(null)"))
215         lastE = 1;
216     if (lastE) {
217         (void)sprintf(bufp,
218                       "Short name of process that failed last in this bnode is: %s\n",
219                       lastErrorName);
220         bufp += strlen(bufp);
221     }
222 #endif
223     (void)sprintf(bufp, "The server is now %srunning\n",
224                   (goal ? "" : "not "));
225     bufp += strlen(bufp);
226     len = (int)(bufp - bufp1);
227     if (write(fd, bufp1, len) < 0) {
228         perror("Write");
229         exit(1);
230     }
231     close(fd);
232     /*
233      * Send the mail out
234      */
235     sprintf(bufp1, "%s %s -s TESTING < %s", SENDMAIL, RECIPIENT, buf);
236     code = system(bufp1);
237     unlink(buf);
238     exit(0);
239 }