/** * Prints a event description into the given buffer. * @param E Event object * @param B StringBuffer object */ static void status_event(Event_T E, StringBuffer_T B) { struct timeval *tv = Event_get_collected(E); StringBuffer_append(B, "<event>" "<collected_sec>%ld</collected_sec>" "<collected_usec>%ld</collected_usec>" "<service>%s</service>" "<type>%d</type>" "<id>%ld</id>" "<state>%d</state>" "<action>%d</action>" "<message><![CDATA[%s]]></message>", tv->tv_sec, (long)tv->tv_usec, Event_get_id(E) == Event_Instance ? "Monit" : Event_get_source_name(E), Event_get_source_type(E), Event_get_id(E), Event_get_state(E), Event_get_action(E), Event_get_message(E)); Service_T s = Event_get_source(E); if (s && s->token) StringBuffer_append(B, "<token>%s</token>", s->token); StringBuffer_append(B, "</event>"); }
/** * Update the partialy handled event in the global queue * @param E An event object * @param file_name File name */ static void _queueUpdate(Event_T E, const char *file_name) { int version = EVENT_VERSION; Action_Type action = Event_get_action(E); boolean_t rv; ASSERT(E); ASSERT(E->flag != Handler_Succeeded); if (! file_checkQueueDirectory(Run.eventlist_dir)) { LogError("Aborting event - cannot access the directory %s\n", Run.eventlist_dir); return; } DEBUG("Updating event in the queue file %s for later delivery\n", file_name); FILE *file = fopen(file_name, "w"); if (! file) { LogError("Aborting event - cannot open the event file %s -- %s\n", file_name, STRERROR); return; } /* write event structure version */ if (! (rv = file_writeQueue(file, &version, sizeof(int)))) goto error; /* write event structure */ if (! (rv = file_writeQueue(file, E, sizeof(*E)))) goto error; /* write source */ if (! (rv = file_writeQueue(file, E->source->name, strlen(E->source->name) + 1))) goto error; /* write message */ if (! (rv = file_writeQueue(file, E->message, E->message ? strlen(E->message) + 1 : 0))) goto error; /* write event action */ if (! (rv = file_writeQueue(file, &action, sizeof(Action_Type)))) goto error; error: fclose(file); if (! rv) { LogError("Aborting event - unable to update event information to %s\n", file_name); if (unlink(file_name) < 0) LogError("Failed to remove event file '%s' -- %s\n", file_name, STRERROR); } }
/** * Prints a event description into the given buffer. * @param E Event object * @param B StringBuffer object */ static void status_event(Event_T E, StringBuffer_T B) { StringBuffer_append(B, "<event>" "<collected_sec>%lld</collected_sec>" "<collected_usec>%ld</collected_usec>" "<service>%s</service>" "<type>%d</type>" "<id>%ld</id>" "<state>%d</state>" "<action>%d</action>" "<message><![CDATA[", (long long)E->collected.tv_sec, (long)E->collected.tv_usec, E->id == Event_Instance ? "Monit" : E->source->name, E->type, E->id, E->state, Event_get_action(E)); _escapeCDATA(B, E->message); StringBuffer_append(B, "]]></message>"); if (E->source->token) StringBuffer_append(B, "<token>%s</token>", E->source->token); StringBuffer_append(B, "</event>"); }
/** * Get a textual description of actual event action. For instance if the * event type is possitive Event_Nonexist, the textual description of * failed state related action is "restart". Likewise if the event type is * negative Event_Checksumthe textual description of recovery related action * is "alert" and so on. * @param E An event object * @return A string describing the event type in clear text. If the * event type is not found NULL is returned. */ const char *Event_get_action_description(Event_T E) { ASSERT(E); return actionnames[Event_get_action(E)]; }
/** * Add the partialy handled event to the global queue * @param E An event object */ static void _queueAdd(Event_T E) { ASSERT(E); ASSERT(E->flag != Handler_Succeeded); if (! file_checkQueueDirectory(Run.eventlist_dir)) { LogError("Aborting event - cannot access the directory %s\n", Run.eventlist_dir); return; } if (! file_checkQueueLimit(Run.eventlist_dir, Run.eventlist_slots)) { LogError("Aborting event - queue over quota\n"); return; } /* compose the file name of actual timestamp and service name */ char file_name[PATH_MAX]; snprintf(file_name, PATH_MAX, "%s/%lld_%lx", Run.eventlist_dir, (long long)Time_now(), (long unsigned)E->source->name); LogInfo("Adding event to the queue file %s for later delivery\n", file_name); FILE *file = fopen(file_name, "w"); if (! file) { LogError("Aborting event - cannot open the event file %s -- %s\n", file_name, STRERROR); return; } boolean_t rv; /* write event structure version */ int version = EVENT_VERSION; if (! (rv = file_writeQueue(file, &version, sizeof(int)))) goto error; /* write event structure */ if (! (rv = file_writeQueue(file, E, sizeof(*E)))) goto error; /* write source */ if (! (rv = file_writeQueue(file, E->source->name, strlen(E->source->name) + 1))) goto error; /* write message */ if (! (rv = file_writeQueue(file, E->message, E->message ? strlen(E->message) + 1 : 0))) goto error; /* write event action */ Action_Type action = Event_get_action(E); if (! (rv = file_writeQueue(file, &action, sizeof(Action_Type)))) goto error; error: fclose(file); if (! rv) { LogError("Aborting event - unable to save event information to %s\n", file_name); if (unlink(file_name) < 0) LogError("Failed to remove event file '%s' -- %s\n", file_name, STRERROR); } else { if (! (Run.flags & Run_HandlerInit) && E->flag & Handler_Alert) Run.handler_queue[Handler_Alert]++; if (! (Run.flags & Run_HandlerInit) && E->flag & Handler_Mmonit) Run.handler_queue[Handler_Mmonit]++; } }