示例#1
0
        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);
        }
示例#2
0
        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);
        }