monitor.c 1.84 KB
#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;
}