There is a config file, the structure is not very convenient:

[/ deployment / test_host / CISCO_2]

AdminInterface = ClassName = com.Process Description = 2016-11-01 ABS-222 Link = / deployment / test_host / CISCO_1 MBeanServerConnection = service: jmx: rmi: //10.0.0.0/jndi/rmi: //0.0.0.0/jmxconnector QueryInterface = SecureMBeanServerConnection = service: jmx: rmi: //0.0.0.0.202/jndi/rmi: //0.0.0.0: 39189 / jmxconnector

[/ deployment / test_host / CISCO_2 / Aggregator]

FileNameTemplate = '% scheme / CISCO_'yyyyMMddHHmmss' % FileName % _% fileseq'

[/ deployment / test_host / CISCO_2 / Aggregator / 00]

[/ deployment / test_host / CISCO_2 / Properties]

MAXRESTARTTIMES = 100000000 PROCESSARGS = -l 4 STARTUPMODE = Automatic

[/ deployment / test_host / CISCO_3]

AdminInterface = ClassName = com.Process Description = 2016-11-01 ABS-222 Link = / deployment / test_host / CISCO_1 MBeanServerConnection = service: jmx: rmi: //10.0.0.0/jndi/rmi: //0.0.0.0/jmxconnector QueryInterface = SecureMBeanServerConnection = service: jmx: rmi: //0.0.0.0.202/jndi/rmi: //0.0.0.0: 39189 / jmxconnector

[/ deployment / test_host / CISCO_3 / Aggregator]

FileNameTemplate = '% scheme / CISCO3_'yyyyMMddHHmmss' % FileName % _% fileseq'

You need to pull out from it everything that is within a particular branch, for example / deployment / test_host / CISCO_2 - including, it starts with / deployment / test_host / CISCO_2 and before the new agent [/ deployment / test_host / CISCO_3]

I still do not understand how best to do it. If only the agent's name is filtered, then how to pull out those lines that go between the paths of agent names (for example, FileNameTemplate ...)

  • And this config is generally working? I mean, he has a very strange format in terms of dellimiters. Are properties separated by line breaks? Because it seems that the AdminInterface property AdminInterface value, is the set property ClassName , Description but then does ABS-222 break in, does it refer to a descriptor? - Ninazu
  • Config worker. It is divided and the transfer of the line apparently, now looked through the entire config. The specific agent configuration is between the first [/ deployment / test_host / CISCO_2] and the first of the next agent [/ deployment / test_host / CISCO_3 / Aggregator] - Dima Kuzmin

1 answer 1

It only remains to filter the array keys by the prefix you need.

 $lines = explode("\n", $configText); $params = []; $currentKey = null; foreach ($lines as $row) { if (empty($row)) { continue; } elseif (preg_match('/^\s*\[(.*?)\]\s*$/', $row, $matches)) { $currentKey = trim($matches[1]); $params[$currentKey] = []; } elseif (preg_match_all('/(\w+)\=/', $row, $matches)) { $values = preg_split('/(\w+)\=/', $row); unset($values[0]); $values = array_map('trim', $values); $params[$currentKey] = array_combine($matches[1], $values); } else { throw new Exception('Unexpected scenario'); } } print_r($params); 

Well, so you can handle the keys represented as folders

 $tree = []; foreach ($params as $key => $values) { $arr = &$tree; $keys = explode('/', $key); unset($keys[0]); foreach ($keys as $key) { $arr = &$arr[$key]; } $arr = $values; } print_r($tree);