]> andersk Git - moira.git/blob - lib/critical.c
01c90baa1f055300b122169f894764872cfc4036
[moira.git] / lib / critical.c
1 /* $Id $
2  *
3  * Log and send a zephyrgram about any critical errors.
4  *
5  * (c) Copyright 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
11 #ifdef ZEPHYR
12 /* need to include before moira.h, which includes krb_et.h, because
13    zephyr.h is broken */
14 #include <zephyr/zephyr.h>
15 /* zephyr.h doesn't prototype this */
16 extern Code_t ZSendNotice(ZNotice_t *notice, Z_AuthProc cert_routine);
17 #endif
18
19 #include <mit-copyright.h>
20 #include <moira.h>
21 #include <moira_site.h>
22
23 #include <string.h>
24 #ifdef SYSLOG
25 #include <syslog.h>
26 #endif
27
28 RCSID("$Header$");
29
30 /* mode to create the file with */
31 #define LOGFILEMODE     0644
32
33 extern char *whoami;
34
35 /* This routine sends a class MOIRA zephyrgram of specified instance
36  * and logs to a special logfile the message passed to it via msg
37  * and args in printf format.  *** It expects the global variable
38  * whoami to be defined and contain the name of the calling program.
39  */
40
41 void critical_alert(char *instance, char *msg, ...)
42 {
43   FILE *crit;                   /* FILE for critical log file */
44   char buf[BUFSIZ];             /* Holds the formatted message */
45   va_list ap;
46
47   va_start(ap, msg);
48   vsprintf(buf, msg, ap);
49   va_end(ap);
50
51   /* Send zephyr notice */
52   send_zgram(instance, buf);
53
54   /* Log message to critical file */
55   if ((crit = fopen(CRITERRLOG, "a")))
56     {
57       time_t t;
58       char *time_s;
59
60       time(&t);
61       time_s = ctime(&t) + 4;
62       time_s[strlen(time_s) - 6] = '\0';
63
64       fprintf(crit, "%s <%ld> %s\n", time_s, (long)getpid(), buf);
65       fclose(crit);
66     }
67
68   com_err(whoami, 0, buf);
69 }
70
71
72
73 /* Sends a zephyrgram of class "MOIRA", instance as a parameter.  Ignores
74  * errors while sending message.
75  */
76
77 void send_zgram(char *inst, char *msg)
78 {
79 #ifdef ZEPHYR
80   ZNotice_t znotice;
81
82   memset(&znotice, 0, sizeof(znotice));
83   znotice.z_kind = UNSAFE;
84   znotice.z_class = "MOIRA";
85   znotice.z_class_inst = inst;
86   znotice.z_default_format = "MOIRA $instance on $fromhost:\n $message\n";
87   ZInitialize();
88   znotice.z_message = msg;
89   znotice.z_message_len = strlen(msg) + 1;
90   znotice.z_opcode = "";
91   znotice.z_recipient = "";
92   ZSendNotice(&znotice, ZNOAUTH);
93 #endif
94 #ifdef SYSLOG
95   {
96     char buf[512];
97     sprintf(buf, "MOIRA: %s %s", inst, msg);
98     syslog(LOG_ERR, buf);
99   }
100 #endif
101 }
This page took 0.112492 seconds and 3 git commands to generate.