Optimize Vim for Icinga2

Author: @mamoru
Original: https://monitoring-portal.org/t/optimize-vim-for-icinga2/5270


I interact with Icinga2 conf files exclusively through Vim, since the machine hosting the master instance doesn’t have a graphical environment. Recently I learned about some Vim features that I thought could make my life easier, and wanted to share what I’ve come up with so far.

Validate your config without leaving Vim

Add this to your .vimrc:

au BufNewFile,BufRead /etc/icinga2/*.conf set makeprg=icinga2\ daemon \-C
    \ | set  efm=%ELocation:\ in\ %f:\ %l:%c-%.%#,%C%f(%l):,%C%f(%l):%.%#,%C%p,%.%#critical/%.%#:\ %m,%-G%.%#

If you think that string is intense, I agree :slight_smile:

With this, you can simply run :make! in Vim and it will drop you to the shell, show the output of the validation, and when you come back you can view any errors with :copen in a more concise way.

Do all that with just one keystroke

Add this to your .vimrc:

nnoremap <F5> :w<CR> :make!<CR> :cw<CR>

From normal mode, hit F5 (change to your preference) and it will run the validation, return you to the editor, and open the Quickfix window if and only if there were any errors (and it if was open, but you fixed the errors, it will close the Quickfix window).

Extras

Two plugins that I’ve found really help with catching errors and working faster:

  • github.com/luochen1990/rainbow - Changes colors of ([{}]) to assist with keeping track of nested objects–makes it much to easier to find your missing }.
  • github.com/scrooloose/nerdtree - An in-editor file browser that makes it very easy for me to get around my rather complicated directory tree under zones.d. Especially useful for putting a Service definition side by side with a new Host definition or something.

For the curious, error messages will be displayed in Vim like this:

|| Attribute 'addess' does not exist.
hosts.conf|23 col 3 error|
|| 1 error

Compare to:

[2018-12-06 22:37:52 -0500] information/cli: Icinga application loader (version: r2.10.1-1)
[2018-12-06 22:37:52 -0500] information/cli: Loading configuration file(s).
[2018-12-06 22:37:52 -0500] information/ConfigItem: Committing config item(s).
[2018-12-06 22:37:52 -0500] information/ApiListener: My API identity: icinga2dir.test
[2018-12-06 22:37:52 -0500] critical/config: Error: Attribute 'addess' does not exist.
Location: in /etc/icinga2/conf.d/hosts.conf: 23:3-23:22
/etc/icinga2/conf.d/hosts.conf(21):
/etc/icinga2/conf.d/hosts.conf(22):   /* Specify the address attributes for checks e.g. `ssh` or `http`. */
/etc/icinga2/conf.d/hosts.conf(23):   addess = "127.0.0.1"
                                      ^^^^^^^^^^^^^^^^^^^^
/etc/icinga2/conf.d/hosts.conf(24):   address6 = "::1"
/etc/icinga2/conf.d/hosts.conf(25):

[2018-12-06 22:37:52 -0500] critical/config: 1 error

Would love to hear any other Vim tips the community has!

2 Likes