Skip to content

Commit

Permalink
Merge pull request #169 from Icinga/fix/minimum-worst-state
Browse files Browse the repository at this point in the history
BpNode: Use worst state in minimum state calculation
  • Loading branch information
lippserd committed Jul 2, 2018
2 parents 28bed28 + fae5036 commit 024d537
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 17 deletions.
20 changes: 15 additions & 5 deletions library/Businessprocess/BpNode.php
Expand Up @@ -369,13 +369,23 @@ public function reCalculateState()
break;
default:
// MIN:
sort($sort_states);

// default -> unknown
$sort_state = 3 << self::SHIFT_FLAGS;

for ($i = 1; $i <= $this->operator; $i++) {
$sort_state = array_shift($sort_states);
if (count($sort_states) >= $this->operator) {
$actualGood = 0;
foreach ($sort_states as $s) {
if (($s >> self::SHIFT_FLAGS) === self::ICINGA_OK) {
$actualGood++;
}
}

if ($actualGood >= $this->operator) {
// condition is fulfilled
$sort_state = self::ICINGA_OK;
} else {
// worst state if not fulfilled
$sort_state = max($sort_states);
}
}
}
if ($sort_state & self::FLAG_DOWNTIME) {
Expand Down
81 changes: 69 additions & 12 deletions test/php/library/Businessprocess/Operators/MinOperatorTest.php
Expand Up @@ -49,41 +49,41 @@ public function testTwoOfTwoTimesCriticalAndUnknownAreAtLeastCritical()
);
}

public function testTwoOfCriticalAndWarningAndOkAreAtLeastWarning()
public function testTwoOfCriticalAndWarningAndOkAreAtLeastCritical()
{
$bp = $this->getBp();
$bp->setNodeState('b', 2);
$bp->setNodeState('c', 1);
$bp->setNodeState('d', 0);

$this->assertEquals(
'WARNING',
'CRITICAL',
$bp->getNode('a')->getStateName()
);
}

public function testTwoOfUnknownAndWarningAndCriticalAreAtLeastUnknown()
public function testTwoOfUnknownAndWarningAndCriticalAreAtLeastCritical()
{
$bp = $this->getBp();
$bp->setNodeState('b', 2);
$bp->setNodeState('c', 1);
$bp->setNodeState('d', 3);

$this->assertEquals(
'UNKNOWN',
'CRITICAL',
$bp->getNode('a')->getStateName()
);
}

public function testTwoOfTwoTimesWarningAndUnknownAreAtLeastWarning()
public function testTwoOfTwoTimesWarningAndUnknownAreAtLeastUnknown()
{
$bp = $this->getBp();
$bp->setNodeState('b', 0);
$bp->setNodeState('b', 3);
$bp->setNodeState('c', 1);
$bp->setNodeState('d', 1);

$this->assertEquals(
'WARNING',
'UNKNOWN',
$bp->getNode('a')->getStateName()
);
}
Expand All @@ -101,17 +101,74 @@ public function testTwoOfThreeTimesOkAreAtLeastOk()
);
}

public function testTenWithAllOk()
{
$bp = $this->getBp(10, 9, 0);

$this->assertEquals(
'OK',
$bp->getNode('a')->getStateName()
);
}

public function testTenWithOnlyTwoCritical()
{
$bp = $this->getBp(10, 8, 0);
$bp->setNodeState('b', 2);
$bp->setNodeState('c', 2);

$this->assertEquals(
'OK',
$bp->getNode('a')->getStateName()
);
}

public function testTenWithThreeCritical()
{
$bp = $this->getBp(10, 8, 0);
$bp->setNodeState('b', 2);
$bp->setNodeState('c', 2);
$bp->setNodeState('d', 2);

$this->assertEquals(
'CRITICAL',
$bp->getNode('a')->getStateName()
);
}

public function testTenWithThreeWarning()
{
$bp = $this->getBp(10, 8, 0);
$bp->setNodeState('b', 1);
$bp->setNodeState('c', 1);
$bp->setNodeState('d', 1);

$this->assertEquals(
'WARNING',
$bp->getNode('a')->getStateName()
);
}

/**
* @return BpConfig
*/
protected function getBp()
protected function getBp($count = 3, $min = 2, $defaultState = null)
{
$names = array();
$a = 97;
for ($i = 1; $i <= $count; $i++) {
$names[] = chr($a + $i);
}

$storage = new LegacyStorage($this->emptyConfigSection());
$expression = 'a = 2 of: b + c + d';
$expression = sprintf('a = %d of: %s', $min, join(' + ', $names));
$bp = $storage->loadFromString('dummy', $expression);
$bp->createBp('b');
$bp->createBp('c');
$bp->createBp('d');
foreach ($names as $n) {
$bp->createBp($n);
if ($defaultState !== null) {
$bp->setNodeState($n, $defaultState);
}
}

return $bp;
}
Expand Down

0 comments on commit 024d537

Please sign in to comment.