def test_markov_chain(self): tr1 = t.Transition(0, 1, pck.ProbCheck(1.0)) tr2 = t.Transition(1, 2, pck.ProbCheck(1.0)) mc = t.MarkovChain([tr1, tr2]) state1 = DummyState(0) state2 = DummyState(1) mc.apply_on(state1, 1) mc.apply_on(state2, 1) self.assertEqual(state1.state, 1) self.assertEqual(state2.state, 2) mc.apply_on(state1, 1) mc.apply_on(state2, 1) self.assertEqual(state1.state, 2) self.assertEqual(state2.state, 2)
def _create_event_triggers(self): triggers = self._config["triggers"] sup_tr = triggers["support"] user_support = ev.Trigger( prob_check=pck.ProbCheck(sup_tr["get_called"]), node_check=dck.AnyDepCheck(dck.NeqCheck("run")), event_builder=ev.EventBuilder( "User complained for poor web service")) webs1 = ev.Trigger( node_check=dck.OrCheck( dck.EqCheck("stop"), dck.DepCheck(dck.EqCheck("off")), dck.DepCheck(dck.DepCheck(dck.EqCheck("off")))), prob_check=pck.NoProbCheck(), event_builder=ev.EventBuilder("Application is down")) webs2 = ev.Trigger( node_check=dck.EqCheck("slow"), prob_check=pck.NoProbCheck(), event_builder=ev.EventBuilder("Application is slow")) host = ev.Trigger( node_check=dck.OrCheck(dck.EqCheck("off"), dck.DepCheck(dck.EqCheck("off"))), prob_check=pck.NoProbCheck(), event_builder=ev.EventBuilder("Host is unreachable or down")) switch = ev.Trigger( node_check=dck.EqCheck("off"), prob_check=pck.NoProbCheck(), event_builder=ev.EventBuilder("Switch is unreachable or down")) return { "switch": switch, "host": host, "web_service": [webs1, webs2], "support": user_support }
def test_prob_check(self): pc = pck.ProbCheck(0.5) i = 0 while i < 30: if pc(0): break i += 1 self.assertTrue(i < 30)
def test_transition(self): tr = t.Transition(0, 1, pck.ProbCheck(1.0)) state = DummyState(0) self.assertTrue(tr(state, 1)) self.assertEqual(state.state, 1) state = DummyState(2) self.assertFalse(tr(state, 1)) self.assertEqual(state.state, 2)
def test_prob_check_interpolate(self): pc = pck.ProbCheck({0: 0.0, 1: 0.0, 24: 1.0}) self.assertFalse(pc(0)) self.assertFalse(pc(1)) self.assertTrue(pc(24)) i = 0 while i < 30: if pc(12): break i += 1 self.assertTrue(i < 30)
def _create_trigger(self, prob, state, event_msg): """Aux function to create an event trigger :param prob: float between 0 and 1 -- probability of the event being triggered :param state: str -- State where this event can be triggered :param event_msg: str -- message that will be sent for this event """ return events.Trigger(prob_check=pck.ProbCheck(prob), node_check=dck.EqCheck(state), event_builder=events.EventBuilder(event_msg))
def _create_markov_chain_model(self): """Defines the Markov chain transitions. The transition will happen with the probability defined by ProbCheck, which will be evaluated each server_sleep_in_seconds time (defined by config). The values are quite low because the time when these are evaluated is the same as when the traffic is evaluated. Over time, the probability accumulation is much higher, though. """ tr_stop_normal = tr.Transition(from_state=STATE_STOP, to_state=STATE_NORMAL, prob_check=pck.ProbCheck(0.8)) tr_normal_stop = tr.Transition(from_state=STATE_NORMAL, to_state=STATE_STOP, prob_check=pck.ProbCheck(0.001)) tr_normal_attack = tr.Transition(from_state=STATE_NORMAL, to_state=STATE_ATTACK, prob_check=pck.ProbCheck(0.0001)) tr_attack_normal = tr.Transition(from_state=STATE_ATTACK, to_state=STATE_NORMAL, prob_check=pck.ProbCheck(0.2)) return tr.MarkovChain([ tr_stop_normal, tr_normal_stop, tr_normal_attack, tr_attack_normal ])
def _create_markov_chain_models(self): transitions = self._config["transitions"] # Switch Transitions sw_tr = transitions["switch"] tr1 = tr.Transition(from_state="on", to_state="off", prob_check=pck.ProbCheck(sw_tr["on=>off"])) tr2 = tr.Transition(from_state="off", to_state="on", prob_check=pck.ProbCheck(sw_tr["off=>on"])) switch_mc = tr.MarkovChain([tr1, tr2]) # Host Transitions hs_tr = transitions["host"] tr1 = tr.Transition(from_state="on", to_state="off", prob_check=pck.ProbCheck(hs_tr["on=>off"])) tr2 = tr.Transition(from_state="off", to_state="on", prob_check=pck.ProbCheck(hs_tr["off=>on"])) host_mc = tr.MarkovChain([tr1, tr2]) # Web service Transitions ws_tr = transitions["web_service"] tr1 = tr.Transition(from_state="run", to_state="stop", prob_check=pck.NoProbCheck(), deps_check=dck.EqCheck("off")) tr2 = tr.Transition(from_state="slow", to_state="stop", prob_check=pck.NoProbCheck(), deps_check=dck.EqCheck("off")) tr3 = tr.Transition(from_state="slow", to_state="run", prob_check=pck.NoProbCheck(), deps_check=dck.AndCheck( dck.EqCheck("on"), dck.DepCheck(dck.EqCheck("off")))) tr4 = tr.Transition(from_state="run", to_state="slow", prob_check=pck.ProbCheck(ws_tr["run=>slow"]), deps_check=dck.AndCheck( dck.EqCheck("on"), dck.DepCheck(dck.EqCheck("on")))) tr5 = tr.Transition(from_state="slow", to_state="run", prob_check=pck.ProbCheck(ws_tr["slow=>run"]), deps_check=dck.AndCheck( dck.EqCheck("on"), dck.DepCheck(dck.EqCheck("on")))) tr6 = tr.Transition(from_state="stop", to_state="run", prob_check=pck.ProbCheck(ws_tr["stop=>run"]), deps_check=dck.EqCheck("on")) webs_mc = tr.MarkovChain([tr1, tr2, tr3, tr4, tr5, tr6]) # User support markov chain sup_mc = tr.MarkovChain([]) return { "switch": switch_mc, "host": host_mc, "web_service": webs_mc, "support": sup_mc }