Thank you, PipoCanaja.
I tried your IF suggestion and found I do not understand Laravel Blade or PHP well enough to succeed without help.
The intended algorithm was:
- Create wanted_info as empty array
- Search $value[‘sysDescr’] for Cisco IOS Software
- If found add sensor_descr and state_descr to wanted_info
- Search $value[‘sysDescr’] for RouterOS
- If found add sensor_descr and state_descr to wanted_info
- As found necessary, search $value[‘sysDescr’] for other device types of interest
- If found add wanted info names to wanted_info
- If wanted_info empty
- Use default template output lines
- Else
- Loop over wanted_info and output each $value[]
I failed to implement that, probably because of trying to create variables in a Laravel template rather than in a Laravel controller (I do not want to mess with code installed by LibreNMS).
https://stackoverflow.com/questions/13002626/how-to-set-variables-in-a-laravel-blade-template?rq=1 suggested creating variables in a Laravel template is contrary to how Laravel should be used, has ugly workarounds and has changed with Laravel releases.
Please give me some pointers on how implement the intended algorithm. My current template:
{{ $alert->title }}
@if ($alert->faults) Faults:
@foreach ($alert->faults as $key => $value)
@php
$wanted_info = array();
@endphp
@if (preg_match("/Cisco IOS Software/i", $value["sysObjectID"]))
Matched Cisco IOS Software
@php
$wanted_info[] = "sensor_descr";
$wanted_info[] = "state_descr";
@endphp
@elseif (preg_match("/RouterOS/i", $value["sysObjectID"]))
Matched RouterOS
@php
$wanted_info[] = "sensor_descr";
$wanted_info[] = "state_descr";
@endphp
@endif
@php
$max_index = count($wanted_info);
@endphp
Max index A: $max_index
Max index B: {{ $max_index }}
Max index C: {{ count($wanted_info) }}
@if (count($wanted_info) == 0))
@foreach ($alert->faults as $key => $value)
{{ $key }}: {{ $value['string'] }}
@endforeach
@else
@for ($i = 0; $i < count($wanted_info); $i++)
{{ $wanted[i] }}: {{ $value[wanted[i]] }}
@endfor
@endif
@endforeach
@endif
@if ($alert->state == 0) Time elapsed: {{ $alert->elapsed }} @endif
Timestamp: {{ $alert->timestamp }}
Rule: @if ($alert->name) {{ $alert->name }} @else {{ $alert->rule }} @endif