monitor.c
1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdlib.h> /* for system() */
#include <sys/wait.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <stdarg.h> /* for ellipse handling */
#include "include/signalHandling.h"
#include "include/monitor.h"
#define MONITORCMD "/usr/bin/monitor"
#define MONITORTYPE "app"
#define MONITORNAME "virtualitem.logreceiver"
const char severity[][10] = {
"info",
"warning",
"critical",
"failure"
};
int
monitor(
unsigned int sev,
const char * pattern,
const char * message
) {
char monCall[1024];
int ret;
snprintf(monCall, 1023,
"%s %s %s.%s.%s \"%s\"",
MONITORCMD,
severity[sev],
MONITORTYPE,
MONITORNAME,
pattern,
message);
ret = system(monCall);
if (WIFSIGNALED(ret)) {
switch (WTERMSIG(ret)) {
case SIGINT:
case SIGQUIT:
syslog(LOG_INFO, "interrupted in monitor call");
doShutdown=1;
}
}
if (-1 == ret || 0 != WEXITSTATUS(ret)) {
syslog(LOG_ERR, "call monitoring failed: %s", strerror(errno));
}
return ret;
}
/* this handles simple %d and %s replacements,
* complexer stuff must be prepared via snprintf
* the complete message should not extend 1024
* Bytes, else it will be truncated silently */
int
syslogMonitor(
unsigned int logLvl,
unsigned int sev,
const char * pattern,
const char * message,
...
) {
va_list args;
char buffer[1025];
int maxBuf = sizeof(buffer)/sizeof(buffer[0]);
memset(buffer, 0, maxBuf);
va_start(args, message);
vsnprintf(buffer, 1024, message, args);
va_end(args);
syslog(logLvl, "%s", buffer);
monitor(sev, pattern, buffer);
return 0;
}