refactor: Improve yaml state discovery (#7221)

* feature: Improve yaml state discovery
Handle state values that are returned as strings instead of int
Synchronize state values for existing state translations so we can change them without creating a new translation and losing historical data
More extensive/verbose yaml discovery phpunit tests
dbBulkInsert, use the first entry instead of requiring the first entry to be at index 0

* Update sensor state documentation
re-order values for better readability
remove os check
Use snmpwalk_group since it is more flexible

* Add some more debug output in dynamic discovery
This commit is contained in:
Tony Murray
2017-09-03 13:58:39 -05:00
committed by Neil Lathwood
parent 5441bafc81
commit 7b262a6851
5 changed files with 180 additions and 96 deletions

View File

@@ -25,18 +25,17 @@
namespace LibreNMS\Tests;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;
use LibreNMS\Config;
use PHPUnit_Framework_ExpectationFailedException as PHPUnitException;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;
class YamlTest extends \PHPUnit_Framework_TestCase
{
public function testOSYaml()
{
global $config;
$pattern = $config['install_dir'] . '/includes/definitions/*.yaml';
$pattern = Config::get('install_dir') . '/includes/definitions/*.yaml';
foreach (glob($pattern) as $file) {
try {
$data = Yaml::parse(file_get_contents($file));
@@ -50,30 +49,52 @@ class YamlTest extends \PHPUnit_Framework_TestCase
}
}
public function testDiscoveryYaml()
/**
* @dataProvider listDiscoveryFiles
* @param $file
*/
public function testDiscoveryYaml($file)
{
global $config;
try {
$data = Yaml::parse(file_get_contents(Config::get('install_dir') . "/includes/definitions/discovery/$file"));
} catch (ParseException $e) {
throw new PHPUnitException("includes/definitions/discovery/$file Could not be parsed");
}
$pattern = $config['install_dir'] . '/includes/definitions/discovery/*.yaml';
foreach (glob($pattern) as $file) {
try {
$data = Yaml::parse(file_get_contents($file));
} catch (ParseException $e) {
throw new PHPUnitException("$file Could not be parsed");
}
foreach ($data['modules'] as $module => $sub_modules) {
foreach ($sub_modules as $type => $sub_module) {
$this->assertArrayHasKey('data', $sub_module, "$type is missing data key");
foreach ($sub_module['data'] as $sensor_index => $sensor) {
$this->assertArrayHasKey('oid', $sensor, "$type.data.$sensor_index is missing oid key");
if ($type !== 'pre-cache') {
$this->assertArrayHasKey('num_oid', $sensor, "$type.data.$sensor_index(${sensor['oid']}) is missing num_oid key");
$this->assertArrayHasKey('descr', $sensor, "$type.data.$sensor_index(${sensor['oid']}) is missing descr key");
}
foreach ($data['modules'] as $module => $sub_modules) {
foreach ($sub_modules as $type => $sub_module) {
foreach ($sub_module['data'] as $sensor) {
$this->assertArrayHasKey('oid', $sensor, $file);
if ($type !== 'pre-cache') {
$this->assertArrayHasKey('oid', $sensor, $file);
$this->assertArrayHasKey('num_oid', $sensor, $file);
$this->assertArrayHasKey('descr', $sensor, $file);
if ($type === 'state') {
$this->assertArrayHasKey('states', $sensor, "$type.data(${sensor['oid']}) is missing states key");
foreach ($sensor['states'] as $state_index => $state) {
$this->assertArrayHasKey('descr', $state, "$type.data.$sensor_index(${sensor['oid']}).states.$state_index is missing descr key");
$this->assertNotEmpty($state['descr'], "$type.data.$sensor_index(${sensor['oid']}).states.$state_index(${state['descr']}) descr must not be empty");
$this->assertArrayHasKey('graph', $state, "$type.data.$sensor_index(${sensor['oid']}).states.$state_index(${state['descr']}) is missing graph key");
$this->assertTrue($state['graph'] === 0 || $state['graph'] === 1, "$type.data.$sensor_index(${sensor['oid']}).states.$state_index(${state['descr']}) invalid graph value must be 0 or 1");
$this->assertArrayHasKey('value', $state, "$type.data.$sensor_index(${sensor['oid']}).states.$state_index(${state['descr']}) is missing value key");
$this->assertInternalType('int', $state['value'], "$type.data.$sensor_index(${sensor['oid']}).states.$state_index(${state['descr']}) value must be an int");
$this->assertArrayHasKey('generic', $state, "$type.data.$sensor_index(${sensor['oid']}).states.$state_index(${state['descr']}) is missing generic key");
$this->assertInternalType('int', $state['generic'], "$type.data.$sensor_index(${sensor['oid']}).states.$state_index(${state['descr']}) generic must be an int");
}
}
}
}
}
}
public function listDiscoveryFiles()
{
$pattern = Config::get('install_dir') . '/includes/definitions/discovery/*.yaml';
return array_map(function ($file) {
return array(basename($file));
}, glob($pattern));
}
}