]>
Commit | Line | Data |
---|---|---|
fa59b86f | 1 | /* $Id$ |
96d806a4 | 2 | * |
3 | * Log and send a zephyrgram about any critical errors. | |
babbc197 | 4 | * |
7ac48069 | 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>. | |
96d806a4 | 8 | */ |
9 | ||
7ac48069 | 10 | |
ea0caf4a | 11 | #ifdef HAVE_ZEPHYR |
7ac48069 | 12 | /* need to include before moira.h, which includes krb_et.h, because |
13 | zephyr.h is broken */ | |
96d806a4 | 14 | #include <zephyr/zephyr.h> |
7ac48069 | 15 | /* zephyr.h doesn't prototype this */ |
16 | extern Code_t ZSendNotice(ZNotice_t *notice, Z_AuthProc cert_routine); | |
2f4a1cd0 | 17 | #endif |
7ac48069 | 18 | |
19 | #include <mit-copyright.h> | |
20 | #include <moira.h> | |
21 | #include <moira_site.h> | |
22 | ||
d5aa7842 | 23 | #include <stdio.h> |
ea0caf4a | 24 | #include <stdlib.h> |
7ac48069 | 25 | #include <string.h> |
ea0caf4a | 26 | #ifndef HAVE_ZEPHYR |
2f4a1cd0 | 27 | #include <syslog.h> |
28 | #endif | |
ea0caf4a | 29 | #include <time.h> |
7ac48069 | 30 | |
31 | RCSID("$Header$"); | |
96d806a4 | 32 | |
96d806a4 | 33 | /* mode to create the file with */ |
34 | #define LOGFILEMODE 0644 | |
35 | ||
5a77c71e | 36 | extern char *whoami; |
37 | ||
8defc06b | 38 | /* This routine sends a class MOIRA zephyrgram of specified instance |
955e02e2 | 39 | * and logs to a special logfile the message passed to it via msg |
40 | * and args in printf format. *** It expects the global variable | |
41 | * whoami to be defined and contain the name of the calling program. | |
955e02e2 | 42 | */ |
43 | ||
7ac48069 | 44 | void critical_alert(char *instance, char *msg, ...) |
96d806a4 | 45 | { |
a3bbcc2b | 46 | FILE *crit; |
47 | char *buf; | |
7ac48069 | 48 | va_list ap; |
a3bbcc2b | 49 | long start; |
96d806a4 | 50 | |
5eaef520 | 51 | /* Log message to critical file */ |
52 | if ((crit = fopen(CRITERRLOG, "a"))) | |
96d806a4 | 53 | { |
5eaef520 | 54 | time_t t; |
55 | char *time_s; | |
3f6b08ec | 56 | |
5eaef520 | 57 | time(&t); |
58 | time_s = ctime(&t) + 4; | |
59 | time_s[strlen(time_s) - 6] = '\0'; | |
3f6b08ec | 60 | |
a3bbcc2b | 61 | fprintf(crit, "%s <%ld>", time_s, (long)getpid()); |
62 | start = ftell(crit); | |
63 | va_start(ap, msg); | |
64 | vfprintf(crit, msg, ap); | |
65 | va_end(ap); | |
66 | fprintf(crit, "\n"); | |
67 | ||
68 | buf = malloc(ftell(crit) - start); | |
5eaef520 | 69 | fclose(crit); |
a3bbcc2b | 70 | |
71 | if (buf) | |
72 | { | |
73 | va_start(ap, msg); | |
74 | vsprintf(buf, msg, ap); | |
75 | va_end(ap); | |
76 | ||
77 | send_zgram(instance, buf); | |
78 | com_err(whoami, 0, buf); | |
79 | ||
80 | free(buf); | |
81 | } | |
96d806a4 | 82 | } |
923cef2a | 83 | |
a3bbcc2b | 84 | if (!crit || !buf) |
85 | { | |
86 | send_zgram(instance, "Couldn't format critical syslog!"); | |
87 | com_err(whoami, 0, "Couldn't format critical syslog!"); | |
88 | } | |
96d806a4 | 89 | } |
90 | ||
91 | ||
8defc06b | 92 | /* Sends a zephyrgram of class "MOIRA", instance as a parameter. Ignores |
96d806a4 | 93 | * errors while sending message. |
94 | */ | |
95 | ||
7ac48069 | 96 | void send_zgram(char *inst, char *msg) |
96d806a4 | 97 | { |
ea0caf4a | 98 | #ifdef HAVE_ZEPHYR |
5eaef520 | 99 | ZNotice_t znotice; |
100 | ||
101 | memset(&znotice, 0, sizeof(znotice)); | |
102 | znotice.z_kind = UNSAFE; | |
103 | znotice.z_class = "MOIRA"; | |
104 | znotice.z_class_inst = inst; | |
105 | znotice.z_default_format = "MOIRA $instance on $fromhost:\n $message\n"; | |
106 | ZInitialize(); | |
107 | znotice.z_message = msg; | |
108 | znotice.z_message_len = strlen(msg) + 1; | |
109 | znotice.z_opcode = ""; | |
110 | znotice.z_recipient = ""; | |
111 | ZSendNotice(&znotice, ZNOAUTH); | |
ea0caf4a | 112 | #else |
113 | char *buf; | |
114 | ||
115 | buf = malloc(9 + strlen(inst) + strlen(msg)); | |
a3bbcc2b | 116 | if (buf) |
117 | { | |
118 | sprintf(buf, "MOIRA: %s %s", inst, msg); | |
119 | syslog(LOG_ERR, buf); | |
120 | free(buf); | |
121 | } | |
2f4a1cd0 | 122 | #endif |
96d806a4 | 123 | } |