Customising the alert templates to get them looking just how I wanted took me a fair bit of time so I thought I’d share my alert templates and some of the alerts I use with them as they might be of use for others.
I see there is a thread from 2017 like this already however it predates the change to the alert template syntax in 2018 (?) so I thought it was probably better to keep this separate.
My templates started life as some of the examples in the documentation but have diverged quite a bit as I’ve learnt more about the template syntax. I’ve actually managed to get my alerts down to using just three templates - Default, (used by no ICMP response, no SNMP response and Device Rebooted) Disk Space Alert and Services alerts.
They are all written and tested with HTML email alerts disabled, and are well formatted for both email and Pushover notifications. So here they are:
Default Alert Template:
Operating System: {{ LibreNMS\Config::getOsSetting($alert->os, 'text') }} {{ $alert->version }}
@if ($alert->hardware)
Hardware: {{ $alert->hardware }}
@endif
@if ($alert->location)
Location: {{ $alert->location }}
@endif
Device URL: http://librenms.yourdomain.local/device/device={{ $alert->hostname }}/
{{ $alert->title }}
Severity: {{ $alert->severity }}
@if ($alert->state == 0)
Time elapsed: {{ $alert->elapsed }}
@endif
Timestamp: {{ $alert->timestamp }}
Unique-ID: {{ $alert->uid }}
The Device URL link above gives an easy way to click a link in an email or push notification alert (I use Pushover) which will take you directly to the device page without resorting to formatting all alerts as HTML. So you should change the hostname to match your server or delete the entire line if you don’t want it. The other templates also have this line.
Low Disk Space Alert Template:
Device Name: {{ $alert->sysName }}
Operating System: {{ LibreNMS\Config::getOsSetting($alert->os, 'text') }} {{ $alert->version }}
@if ($alert->hardware)
Hardware: {{ $alert->hardware }}
@endif
@if ($alert->location)
Location: {{ $alert->location }}
@endif
Device URL: http://librenms.yourdomain.local/device/device={{ $alert->hostname }}/
{{ $alert->title }}
Severity: {{ $alert->severity }}
@if ($alert->state == 0)
Time elapsed: {{ $alert->elapsed }}
@endif
Timestamp: {{ $alert->timestamp }}
Unique-ID: {{ $alert->uid }}
@foreach ($alert->faults as $key => $value)
Drive: {{ $value['storage_descr'] }}
Disk Utilization: {{ $value['storage_perc'] }}%
Disk Size: {{ number_format($value['storage_size']/1073741824,2) }} GB
Disk Free: {{ number_format($value['storage_free']/1073741824,2) }} GB
@endforeach
Disk space is stored internally in bytes - not very useful, so I use number_format() to convert to Gigabytes (proper base 1024 ones ) rounded to two decimal places. You can adjust the number of decimal places by changing the number after the comma.
Service Warning Alert Template:
Device Name: {{ $alert->sysName }}
Operating System: {{ LibreNMS\Config::getOsSetting($alert->os, 'text') }} {{ $alert->version }}
@if ($alert->hardware)
Hardware: {{ $alert->hardware }}
@endif
@if ($alert->location)
Location: {{ $alert->location }}
@endif
Device URL: http://librenms.yourdomain.local/device/device={{ $alert->hostname }}/
{{ $alert->title }}
Severity: {{ $alert->severity }}
Rule: @if ($alert->name)
{{ $alert->name }}
@else
{{ $alert->rule }}
@endif
@if ($alert->state == 0)
Time elapsed: {{ $alert->elapsed }}
@endif
Timestamp: {{ $alert->timestamp }}
Unique-ID: {{ $alert->uid }}
@if ($alert->faults)
Faults:
@foreach ($alert->faults as $key => $value)
#{{ $key }}: Service: {{ $value['service_ip'] }}
Description: {{ $value['service_desc'] }}
Message: {{ $value['service_message'] }}
@endforeach
@endif
Not a lot to say about this one - it enumerates all the individual service faults in a nice readable format.
All of these templates use the same boilerplate Alert Title and Recovery Title:
Alert Title:
Alert for device {{ $alert->sysName }} - @if ($alert->name) {{ $alert->name }} @else {{ $alert->rule }} @endif
Recovery Title:
Device {{ $alert->sysName }} recovered from @if ($alert->name) {{ $alert->name }} @else {{ $alert->rule }} @endif
Hope these are of use to someone as while there is quite a bit of documentation for the alert system there are still some significant holes in the template documentation, getting the formatting tidy is a bit fiddly (especially when using @if) and there is a lot that can be done that isn’t really documented.