Пример #1
0
static void substitute(Mail_T *m, Event_T e) {
  char timestamp[STRLEN];

  ASSERT(m && e);

  Util_replaceString(&(*m)->from,    "$HOST", Run.localhostname);
  Util_replaceString(&(*m)->subject, "$HOST", Run.localhostname);
  Util_replaceString(&(*m)->message, "$HOST", Run.localhostname);
  
  Util_getRFC822Date((time_t *)&e->collected.tv_sec, timestamp, STRLEN);
  Util_replaceString(&(*m)->subject, "$DATE", timestamp);
  Util_replaceString(&(*m)->message, "$DATE", timestamp);

  Util_replaceString(&(*m)->subject, "$SERVICE", Event_get_source_name(e));
  Util_replaceString(&(*m)->message, "$SERVICE", Event_get_source_name(e));

  Util_replaceString(&(*m)->subject, "$EVENT", Event_get_description(e));
  Util_replaceString(&(*m)->message, "$EVENT", Event_get_description(e));

  Util_replaceString(&(*m)->subject, "$DESCRIPTION", NVLSTR(Event_get_message(e)));
  Util_replaceString(&(*m)->message, "$DESCRIPTION", NVLSTR(Event_get_message(e)));

  Util_replaceString(&(*m)->subject, "$ACTION", Event_get_action_description(e));
  Util_replaceString(&(*m)->message, "$ACTION", Event_get_action_description(e));
}
Пример #2
0
static void substitute(Mail_T m, Event_T e) {
  char timestamp[STRLEN];

  ASSERT(m && e);

  Util_replaceString(&m->from,    "$HOST", Run.system->name);
  Util_replaceString(&m->subject, "$HOST", Run.system->name);
  Util_replaceString(&m->message, "$HOST", Run.system->name);

  Time_string(e->collected.tv_sec, timestamp);
  Util_replaceString(&m->subject, "$DATE", timestamp);
  Util_replaceString(&m->message, "$DATE", timestamp);

  Util_replaceString(&m->subject, "$SERVICE", Event_get_source_name(e));
  Util_replaceString(&m->message, "$SERVICE", Event_get_source_name(e));

  Util_replaceString(&m->subject, "$EVENT", Event_get_description(e));
  Util_replaceString(&m->message, "$EVENT", Event_get_description(e));

  Util_replaceString(&m->subject, "$DESCRIPTION", NVLSTR(Event_get_message(e)));
  Util_replaceString(&m->message, "$DESCRIPTION", NVLSTR(Event_get_message(e)));

  Util_replaceString(&m->subject, "$ACTION", Event_get_action_description(e));
  Util_replaceString(&m->message, "$ACTION", Event_get_action_description(e));
}
Пример #3
0
/*
 * Setup the environment with special MONIT_xxx variables. The program
 * executed may use such variable for various purposes.
 */
static void set_monit_environment(Service_T S, command_t C, Event_T E, const char *date) {
        setenv("MONIT_DATE", date, 1);
        setenv("MONIT_SERVICE", S->name, 1);
        setenv("MONIT_HOST", Run.system->name, 1);
        setenv("MONIT_EVENT", E ? Event_get_description(E) : C == S->start ? "Started" : C == S->stop ? "Stopped" : "No Event", 1);
        setenv("MONIT_DESCRIPTION", E ? Event_get_message(E) : C == S->start ? "Started" : C == S->stop ? "Stopped" : "No Event", 1);
        if (S->type == TYPE_PROCESS) {
                putenv(Str_cat("MONIT_PROCESS_PID=%d", Util_isProcessRunning(S, FALSE)));
                putenv(Str_cat("MONIT_PROCESS_MEMORY=%ld", S->inf->priv.process.mem_kbyte));
                putenv(Str_cat("MONIT_PROCESS_CHILDREN=%d", S->inf->priv.process.children));
                putenv(Str_cat("MONIT_PROCESS_CPU_PERCENT=%d", S->inf->priv.process.cpu_percent));
        }
}
Пример #4
0
/*
 * Setup the environment with special MONIT_xxx variables. The program
 * executed may use such variable for various purposes.
 */
static void set_monit_environment(Service_T S, command_t C, Event_T E, const char *date) {
        setenv("MONIT_DATE", date, 1);
        setenv("MONIT_SERVICE", S->name, 1);
        setenv("MONIT_HOST", Run.system->name, 1);
        setenv("MONIT_EVENT", E ? Event_get_description(E) : C == S->start ? "Started" : C == S->stop ? "Stopped" : "No Event", 1);
        setenv("MONIT_DESCRIPTION", E ? E->message : C == S->start ? "Started" : C == S->stop ? "Stopped" : "No Event", 1);
        switch (S->type) {
                case Service_Process:
                        putenv(Str_cat("MONIT_PROCESS_PID=%d", S->inf->priv.process.pid));
                        putenv(Str_cat("MONIT_PROCESS_MEMORY=%llu", (unsigned long long)((double)S->inf->priv.process.mem / 1024.)));
                        putenv(Str_cat("MONIT_PROCESS_CHILDREN=%d", S->inf->priv.process.children));
                        putenv(Str_cat("MONIT_PROCESS_CPU_PERCENT=%.1f", S->inf->priv.process.cpu_percent));
                        break;
                case Service_Program:
                        putenv(Str_cat("MONIT_PROGRAM_STATUS=%d", S->program->exitStatus));
                        break;
                default:
                        break;
        }
}
Пример #5
0
/**
 * Notify registred users about the event
 * @param E An Event object
 * @return If failed, return HANDLER_ALERT flag or HANDLER_SUCCEEDED if succeeded
 */
int handle_alert(Event_T E) {
  Service_T s;
  int rv = HANDLER_SUCCEEDED;

  ASSERT(E);

  s= Event_get_source(E);
  if(!s) {
    LogError("Aborting alert\n");
    return rv;
  }

  if(s->maillist || Run.maillist) {
    Mail_T m;
    Mail_T n;
    Mail_T list= NULL;
    /*
     * Build a mail-list with local recipients that has registered interest
     * for this event.
     */
    for(m= s->maillist; m; m= m->next) {
      
      if(
        /* particular event notification type is allowed for given recipient */
        IS_EVENT_SET(m->events, Event_get_id(E)) &&
        (
          /* state change notification is sent always */
          E->state_changed       ||
          /* in the case that the state is failed for more cycles we check
           * whether we should send the reminder */
          (E->state && m->reminder && E->count % m->reminder == 0)
        )
      )
      {
        Mail_T tmp= NULL;

        NEW(tmp);
        copy_mail(tmp, m);
        substitute(&tmp, E);
        replace_bare_linefeed(&tmp);
        tmp->next= list;
        list= tmp;

        DEBUG("%s notification is sent to %s\n", Event_get_description(E), m->to);

      }

    }

    /*
     * Build a mail-list with global recipients that has registered interest
     * for this event. Recipients which are defined in the service localy
     * overrides the same recipient events which are registered globaly.
     */
    for(m= Run.maillist; m; m= m->next) {
      int skip= FALSE;

      for(n= s->maillist; n; n= n->next) {
        if(IS(m->to, n->to)) {
          skip= TRUE;
          break;
        }
      }

      if(
        /* the local service alert definition has not overrided the global one */
        !skip &&
        /* particular event notification type is allowed for given recipient */
        IS_EVENT_SET(m->events, Event_get_id(E)) &&
        (
          /* state change notification is sent always */
          E->state_changed       ||
          /* in the case that the state is failed for more cycles we check
           * whether we should send the reminder */
          (E->state && m->reminder && E->count % m->reminder == 0)
        )
      )
      {

        Mail_T tmp= NULL;

        NEW(tmp);
        copy_mail(tmp, m);
        substitute(&tmp, E);
        replace_bare_linefeed(&tmp);
        tmp->next= list;
        list= tmp;

        DEBUG("%s notification is sent to %s\n", Event_get_description(E), m->to);

      }

    }

    if(list) {

      if(!sendmail(list))
        rv = HANDLER_ALERT;
      gc_mail_list(&list);

    }

  }

  return rv;

}