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