public static MonitHandlerType HandleMmonit(Event_T E) { var rv = MonitHandlerType.Handler_Mmonit; if (MonitWindowsAgent.Run.mmonits == null || (E != null && !E.state_changed)) { return(MonitHandlerType.Handler_Succeeded); } foreach (var C in MonitWindowsAgent.Run.mmonits) { var xml = Xml.StatusXml(E, E != null ? MonitLevelType.Level_Summary : MonitLevelType.Level_Full, 2, "TODO: LOCALHOST"); var request = sendData(C, xml); if (request != null) { if (!checkData(request)) { Logger.Log.Error("Pushing Data to M/Monit had an Error!"); } else { rv = MonitHandlerType.Handler_Succeeded; } } } return(rv); }
/// <summary> /// Events the get action. /// </summary> /// <param name="E">The e.</param> /// <returns></returns> public static MonitActionType GetAction(Event_T E) { Action_T A = null; switch (E.state) { case MonitStateType.State_Succeeded: case MonitStateType.State_ChangedNot: A = E.action.succeeded; break; case MonitStateType.State_Failed: case MonitStateType.State_Changed: case MonitStateType.State_Init: A = E.action.failed; break; default: Logger.Log.ErrorFormat("Invalid event state: {0}", E.state); return(MonitActionType.Action_Ignored); } if (A == null) { return(MonitActionType.Action_Ignored); } /* In the case of passive mode we replace the description of start, stop or restart action for alert action, because these actions are passive in this mode */ return((E.mode == MonitMonitorModeType.Monitor_Passive && ((A.id == MonitActionType.Action_Start) || (A.id == MonitActionType.Action_Stop) || (A.id == MonitActionType.Action_Restart))) ? MonitActionType.Action_Alert : A.id); }
/// <summary> /// Get a textual description of actual event type. /// </summary> /// <param name="E">An event object</param> /// <returns>A string describing the event type in clear text. If the event type is not found NULL is returned.</returns> public static string GetDescription(Event_T E) { var et = MonitEventTable.Event_Table; foreach (var ete in et) { if (E.id == ete.id) { switch (E.state) { case MonitStateType.State_Succeeded: return(ete.description_succeeded); case MonitStateType.State_Failed: return(ete.description_failed); case MonitStateType.State_Init: return(ete.description_failed); case MonitStateType.State_Changed: return(ete.description_changed); case MonitStateType.State_ChangedNot: return(ete.description_changednot); default: break; } } } return(null); }
public static Service_T GetSource(Event_T E) { Service_T S = null; if ((S = Util.GetService(E.source)) == null) { Logger.Log.Error(string.Format("Service {0} not found in monit configuration", E.source)); } return(S); }
/// <summary> /// TODO /// Event_queue_updates the specified e. /// </summary> /// <param name="E">The e.</param> /// <param name="file_name">The file_name.</param> private static void Event_queue_update(Event_T E, string file_name) { var version = Event_T.EVENT_VERSION; var action = GetAction(E); bool rv; // 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); var file = File.OpenWrite(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, E->source ? strlen(E->source) + 1 : 0))) // 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); // } }
public static bool CheckState(Event_T E, MonitStateType S) { var count = 0; var state = (S == MonitStateType.State_Succeeded || S == MonitStateType.State_ChangedNot) ? MonitStateType.State_Succeeded : MonitStateType.State_Failed; /* translate to 0/1 class */ Action_T action; Service_T service; long flag; if ((service = GetSource(E)) == null) { return(true); } /* Only true failed/changed state condition can change the initial state */ if (state == MonitStateType.State_Succeeded && E.state == MonitStateType.State_Init && (service.error & (int)E.id) == 0) { return(false); } action = state == MonitStateType.State_Succeeded ? E.action.succeeded : E.action.failed; /* Compare as many bits as cycles able to trigger the action */ for (var i = 0; i < action.cycles; i++) { /* Check the state of the particular cycle given by the bit position */ flag = (E.state_map >> i) & 0x1; /* Count occurences of the posted state */ if (flag == (int)state) { count++; } } /* the internal instance and action events are handled as changed any time since we need to deliver alert whenever it occurs */ if (E.id == MonitEventType.Event_Instance || E.id == MonitEventType.Event_Action || (count >= action.count && (S != E.state || S == MonitStateType.State_Changed))) { E.state_map = (int)state; // Restart state map on state change, so we'll not flicker on multiple-failures condition (next state change requires full number of cycles to pass) return(true); } return(false); }
private static void substitute(Mail_T m, Event_T e) { m.from = m.from.Replace("$HOST", MonitWindowsAgent.Run.system[0].name); m.subject = m.subject.Replace("$HOST", MonitWindowsAgent.Run.system[0].name); m.message = m.message.Replace("$HOST", MonitWindowsAgent.Run.system[0].name); m.subject = m.subject.Replace("$DATE", Timing.GetTimestamp(e.collected).ToString()); m.message = m.message.Replace("$DATE", Timing.GetTimestamp(e.collected).ToString()); m.subject = m.subject.Replace("$SERVICE", e.source); m.message = m.message.Replace("$SERVICE", e.source); m.subject = m.subject.Replace("$EVENT", Event.GetDescription(e)); m.message = m.message.Replace("$EVENT", Event.GetDescription(e)); m.subject = m.subject.Replace("$DESCRIPTION", e.message); //NVLSTR(e.message)); m.message = m.message.Replace("$DESCRIPTION", e.message); //NVLSTR(e.message)); m.subject = m.subject.Replace("$ACTION", MonitWindowsAgent.actionnames[(int)Event.GetAction(e)]); m.message = m.message.Replace("$ACTION", MonitWindowsAgent.actionnames[(int)Event.GetAction(e)]); }
public static string StatusXml(Event_T E, MonitLevelType L, int V, string myip) { var centerString = new StringBuilder(); if (V == 2) { centerString.Append("<services>"); } foreach (var S in MonitWindowsAgent.servicelist_conf) { statusService(S, centerString, L, V); } if (V == 2) { centerString.Append("</services><servicegroups>"); foreach (var SG in MonitWindowsAgent.servicegrouplist) { var serviceGroupXmlBuilder = new OutputBuilder(Resources.ServiceGroupXml); var serviceGroupString = new StringBuilder(); foreach (var sgm in SG.members) { serviceGroupString.AppendFormat("<service>{0}</service>", sgm.name); } serviceGroupXmlBuilder.ReplaceAllTags(SG.name, serviceGroupString.ToString()); centerString.Append(serviceGroupXmlBuilder.GetResult()); } centerString.Append("</servicegroups>"); } if (E != null) { var eventXmlBuilder = new OutputBuilder(Resources.EventXml); var timeval = E.collected; var source = Event.GetSource(E); eventXmlBuilder.ReplaceAllTags(Timing.GetTimestamp(timeval), Timing.GetUsec(timeval), E.id == MonitEventType.Event_Instance ? "Monit" : E.source, (int)E.type, (int)E.id, (int)E.state, (int)Event.GetAction(E), _escapeCDATA(E.message), (source != null && !string.IsNullOrEmpty(source.token)) ? string.Format("<token>{0}</token>", source.token) : ""); centerString.Append(eventXmlBuilder.GetResult()); } var headerFooterXmlBuilder = new OutputBuilder(Resources.HeaderFooterXml); var run = MonitWindowsAgent.Run; var systemInfo = MonitWindowsAgent.systeminfo; var headString = new StringBuilder(); if (V == 2) { headString.AppendFormat("<monit id=\"{0}\" incarnation=\"{1}\" version=\"{2}\"><server>", run.id, Timing.GetTimestamp(run.incarnation), MonitWindowsAgent.VERSION); } else { headString.AppendFormat( "<monit><server><id>{0}</id><incarnation>{1}</incarnation><version>{2}</version>", run.id, Timing.GetTimestamp(run.incarnation), MonitWindowsAgent.VERSION); } var httpdBuilder = new StringBuilder(); if (MonitWindowsAgent.Run.httpd != null) { var httpd = MonitWindowsAgent.Run.httpd; httpdBuilder.AppendFormat("<httpd><address>{0}</address><port>{1}</port><ssl>{2}</ssl></httpd>", httpd.address, httpd.port, httpd.ssl ? "1" : "0"); if (httpd.credentials != null && httpd.credentials.Count > 0) { httpdBuilder.AppendFormat( "<credentials><username>{0}</username><password>{1}</password></credentials>", httpd.credentials[0].uname, httpd.credentials[0].passwd); } } headerFooterXmlBuilder.ReplaceAllTags(headString.ToString(), "5", run.polltime, run.startdelay, run.system[0].name, run.controlfile, httpdBuilder.ToString(), systemInfo.uname.sysname, systemInfo.uname.release, systemInfo.uname.version, systemInfo.uname.machine, systemInfo.cpus, systemInfo.mem_kbyte_max, systemInfo.swap_kbyte_max, centerString.ToString()); return(headerFooterXmlBuilder.GetResult()); }
/** * Notify registred users about the event * @param E An Event object * @return If failed, return Handler_Alert flag or Handler_Succeeded if succeeded */ public static MonitHandlerType HandleAlert(Event_T E) { var rv = MonitHandlerType.Handler_Succeeded; var s = Event.GetSource(E); if (s == null) { Logger.Log.Error("Aborting alert\n"); return(rv); } if (s.maillist != null || MonitWindowsAgent.Run.maillist != null) { var list = new List <Mail_T>(); /* * Build a mail-list with local recipients that has registered interest * for this event. */ foreach (var m in s.maillist) { if ( /* particular event notification type is allowed for given recipient */ Event.IsEventSet((int)m.events, E.id) && ( /* 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 == MonitStateType.State_Failed && m.reminder > 0 && E.count % m.reminder == 0) ) ) { var tmp = new Mail_T(); copy_mail(tmp, m); substitute(tmp, E); escape(tmp); list.Insert(0, tmp); Logger.Log.DebugFormat("{0} notification is sent to {1}", Event.GetDescription(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. */ foreach (var m in MonitWindowsAgent.Run.maillist) { var skip = false; foreach (var n in s.maillist) { if (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 */ Event.IsEventSet((int)m.events, E.id) && ( /* 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 == MonitStateType.State_Failed && m.reminder > 0 && E.count % m.reminder == 0) ) ) { var tmp = new Mail_T(); copy_mail(tmp, m); substitute(tmp, E); escape(tmp); list.Insert(0, tmp); Logger.Log.DebugFormat("{0} notification is sent to {1}", Event.GetDescription(E), m.to); } } if (list != null) { if (Sendmail.Send(list)) { rv = MonitHandlerType.Handler_Alert; } } } return(rv); }
/// <summary> /// TODO /// Events the queue add. /// </summary> /// <param name="E">The e.</param> private static void eventQueueAdd(Event_T E) { // 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); // 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, E->source ? strlen(E->source) + 1 : 0))) // 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.handler_init && E->flag & Handler_Alert) // Run.handler_queue[Handler_Alert]++; // if (! Run.handler_init && E->flag & Handler_Mmonit) // Run.handler_queue[Handler_Mmonit]++; // } }
/// <summary> /// TODO /// Handles the action. /// </summary> /// <param name="E">The e.</param> /// <param name="A">a.</param> private static void handleAction(Event_T E, Action_T A) { Service_T s; E.flag = MonitHandlerType.Handler_Succeeded; if (A.id == MonitActionType.Action_Ignored) { return; } /* Alert and mmonit event notification are common actions */ E.flag |= MMonit.HandleMmonit(E); E.flag |= Alert.HandleAlert(E); /* In the case that some subhandler failed, enqueue the event for * partial reprocessing */ if (E.flag != MonitHandlerType.Handler_Succeeded) { if (!string.IsNullOrEmpty(MonitWindowsAgent.Run.eventlist_dir)) { eventQueueAdd(E); } else { Logger.Log.Error("Aborting event"); } } if ((s = GetSource(E)) == null) { Logger.Log.Error("Event action handling aborted"); return; } /* Action event is handled already. For Instance events * we don't want actions like stop to be executed * to prevent the disabling of system service monitoring */ if (A.id == MonitActionType.Action_Alert || E.id == MonitEventType.Event_Instance) { } if (A.id == MonitActionType.Action_Exec) { Logger.Log.InfoFormat("'{0}'", s.name); //spawn(s, A.exec, E); } else { if (s.actionratelist != null && (A.id == MonitActionType.Action_Start || A.id == MonitActionType.Action_Restart)) { s.nstart++; } if (s.mode == MonitMonitorModeType.Monitor_Passive && (A.id == MonitActionType.Action_Start || A.id == MonitActionType.Action_Stop || A.id == MonitActionType.Action_Restart)) { return; } Control.ControlService(s.name, A.id); } }
/// <summary> /// Handles the event. /// </summary> /// <param name="S">The s.</param> /// <param name="E">The e.</param> private static void handleEvent(Service_T S, Event_T E) { /* We will handle only first succeeded event, recurrent succeeded events * or insufficient succeeded events during failed service state are * ignored. Failed events are handled each time. */ if (!E.state_changed && (E.state == MonitStateType.State_Succeeded || E.state == MonitStateType.State_ChangedNot || ((E.state_map & 0x1) ^ 0x1) == 1)) { Logger.Log.DebugFormat("'{0}' {1}%s", S.name, E.message); return; } if (!string.IsNullOrEmpty(E.message)) { /* In the case that the service state is initializing yet and error * occured, log it and exit. Succeeded events in init state are not * logged. Instance and action events are logged always with priority * info. */ if (E.state != MonitStateType.State_Init || (E.state_map & 0x1) == 1) { if (E.state == MonitStateType.State_Succeeded || E.state == MonitStateType.State_ChangedNot || E.id == MonitEventType.Event_Instance || E.id == MonitEventType.Event_Action) { Logger.Log.InfoFormat("'{0}' {1}", S.name, E.message); } else { Logger.Log.ErrorFormat("'{0}' {1}", S.name, E.message); } } if (E.state == MonitStateType.State_Init) { return; } } if (E.state == MonitStateType.State_Failed || E.state == MonitStateType.State_Changed) { if (E.id != MonitEventType.Event_Instance && E.id != MonitEventType.Event_Action) { // We are not interested in setting error flag for instance and action events S.error |= (int)E.id; /* The error hint provides second dimension for error bitmap and differentiates between failed/changed event states (failed=0, chaged=1) */ if (E.state == MonitStateType.State_Changed) { S.error_hint |= (int)E.id; } else { S.error_hint &= ~(int)E.id; } } handleAction(E, E.action.failed); } else { S.error &= ~(int)E.id; handleAction(E, E.action.succeeded); } /* Possible event state change was handled so we will reset the flag. */ E.state_changed = false; }
public static void Post(Service_T service, MonitEventType id, MonitStateType state, EventAction_T action, string s, params object[] args) { var message = string.Format(s, args); Event_T E = null; if (service.eventlist != null) { service.eventlist.ForEach(e => { if (e.action == action && e.id == id) { e.collected = DateTime.UtcNow; e.state_map <<= 1; e.state_map |= ((state == MonitStateType.State_Succeeded || state == MonitStateType.State_ChangedNot) ? 0 : 1); e.message = message; E = e; } }); } if (E == null) { if (state == MonitStateType.State_Succeeded || state == MonitStateType.State_ChangedNot) { Logger.Log.DebugFormat("'{0}' {1}", service.name, message); return; } E = new Event_T(); E.id = id; E.collected = DateTime.UtcNow; E.source = service.name; E.mode = service.mode; E.type = service.type; E.state = MonitStateType.State_Init; E.state_map = 1; E.action = action; E.message = message; if (service.eventlist == null) { service.eventlist = new List <Event_T>(); } service.eventlist.Insert(0, E); } E.state_changed = CheckState(E, state); if (E.state_changed) { E.state = state; E.count = 1; } else { E.count++; } handleEvent(service, E); }