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