]> andersk Git - moira.git/blob - lib/critical.c
declare whoami
[moira.git] / lib / critical.c
1 /* $Header$
2  *
3  * Log and send a zephyrgram about any critical errors.
4  */
5
6 #include <stdio.h>
7 #include <sys/file.h>
8 #include <zephyr/zephyr.h>
9
10
11 /* log file for critical events that require human intervention */
12 #define CRITERRLOG      "/u1/sms/critical.log"
13
14 /* mode to create the file with */
15 #define LOGFILEMODE     0644
16
17 extern char *whoami;
18
19
20 void critical_alert(instance, msg, args)
21   char *instance;               /* Instance for zephyr gram */
22   char *msg;                    /* printf format message */
23                                 /* args = arguements, printf style */
24   /* This routine sends a class SMS zephyrgram of specified instance
25      and logs to a special logfile the message passed to it via msg
26      and args in printf format.  *** It expects the global variable
27      whoami to be defined and contain the name of the calling program. */
28   /* Note: The part of this code that process the variable arguements
29      was stolen from sprintf(). */
30 {
31     FILE _bufstr;               /* For _doprnt() */
32     FILE *crit;                 /* FILE for critical log file */
33     char buf[BUFSIZ];           /* Holds the formatted message */
34
35     /* Put the fully formatted message into buf */
36     _bufstr._flag = _IOWRT + _IOSTRG;
37     _bufstr._ptr = buf;
38     _bufstr._cnt = BUFSIZ;
39     _doprnt(msg, &args, &_bufstr);
40     putc('\0', &_bufstr);
41
42     /* Send zephyr notice */
43     send_zgram(instance, buf);
44
45     /* Log message to critical file */
46     if ((crit = fopen(CRITERRLOG, "a")) != (FILE *)NULL) 
47     {
48         long t;
49         char  *time_s;
50
51         time(&t);
52         time_s = ctime(&t) + 4;
53         time_s[strlen(time_s)-6] = '\0';
54
55         fprintf(crit, "%s <%d> %s\n", time_s, getpid(), buf);
56         fclose(crit);
57     }
58
59     com_err(whoami, 0, buf);
60 }
61
62
63
64 /* Sends a zephyrgram of class "SMS", instance as a parameter.  Ignores
65  * errors while sending message.
66  */
67
68 send_zgram(inst, msg)
69 char *inst;
70 char *msg;
71 {
72     ZNotice_t znotice;
73
74     bzero (&znotice, sizeof (znotice));
75     znotice.z_kind = UNSAFE;
76     znotice.z_class = "SMS";
77     znotice.z_class_inst = inst;
78     znotice.z_default_format = "SMS $instance:\n $message\n";
79     (void) ZInitialize ();
80     znotice.z_message = msg;
81     znotice.z_message_len = strlen(msg) + 1;
82     znotice.z_opcode = "";
83     znotice.z_recipient = "";
84     ZSendNotice(&znotice, ZNOAUTH);
85 }
This page took 0.06101 seconds and 5 git commands to generate.