Générateur de conf Nagios

Brouillon

object configuration files

Voir :

Notes supervision Nagios

http://nagios.manubulon.com/traduction/docs14fr/xodtemplate.html#service

https://docs-older.centreon.com/docs/centreon-engine/en/latest/user/configuration/basics/object_definitions.html

https://github.com/NagiosEnterprises/nagioscore/tree/master/sample-config/template-object

Voir aussi :

https://github.com/pynag/pynag

MK Livestatus

NDOUtils

https://github.com/mclarkson/check_nagios_config

https://github.com/shriasi/nagiosconfigtocsv/tree/main

https://pypi.org/project/nagioscheck/#description

Fichiers plats à garder en statique :

commands.cfg

contacts.cfg

localhost.cfg

templates.cfg

timeperiods.cfg

Fichier à supprimer ou à regénérer :

printer.cfg

switch.cfg

windows.cfg

Types d'objets à générer :

host

hostgroup

service

servicegroup ?

Dans le conf par défaut nous avons les templates suivants pour les hosts

generic-host

linux-server

windows-server

generic-printer

generic-switch

et pour les services :

generic-service

local-service (utiliser que dans localhost.cfg)

Proposition de format YAML

--- hosts: - host_name: "srv1" use: "linux-server" alias: "Server 1" address: "192.168.1.10" - host_name: "hplj2605dn" use: "generic-printer" alias: "HP LaserJet 2605dn" address: "192.168.1.30" hostgroups: "network-printers" # Member of hostgroup - host_name: "winserver" use: "windows-server" # Template contains "hostgroups windows-servers" alias: "My Windows Server" address: "192.168.1.2" hostgroups: - hostgroup_name: "linux-servers" alias: "Linux Servers" # members: "srv1" # Member of hostgroup # hostgroup_members: - hostgroup_name: "network-printers" alias: "Network Printers" - hostgroup_name: "windows-servers" alias: "Windows Servers" services: - service_description: "SSH" use: "generic-service" #hostgroup: "linux-remote-servers" hostgroup_name: "generic-remote-hosts" check_command: "check_ssh" - service_description: "NSClient++ Version" use: "generic-service" host_name: "winserver" check_command: "check_nt!CLIENTVERSION" - service_description: Memory hostgroup_name: "linux-servers" check_command: "check_centreon_snmp_linux_mem!80!90" max_check_attempts: 1 normal_check_interval: 1 retry_check_interval: 1 check_period: "24x7" notification_interval: 2000 notification_period: "24x7" notification_options: "w,c,r" contact_groups: "support" event_handler: "trigger_memory"

Entrées :

printer.yaml

switch.yaml

windows.yaml

linux.yaml

Sorties :

printer.cfg

switch.cfg

windows.cfg

linux.cfg

On ajoute un template au fichier templates.cfg

templates.cfg

#define host {
#
# name tpl-host-windows
# use windows-server
# hostgroups windows-hosts
# register 0
# }
define host {
name linux-remote-servers
use linux-server
hostgroups linux-hosts
register 0
}

Il faudra créer les hostgroups associer (à faire à partir de YAML)

linux.cfg

define hostgroup {
hostgroup_name linux-hosts
alias Linux Hosts
}

windows.cfg

define hostgroup {
hostgroup_name windows-hosts
alias Windows Hosts
}

Changer la définition des hosts use linux-server use linux-remote-servers

templates2.cfg

############################################################################### # # HOST TEMPLATES # ############################################################################### #define host { # # name tpl-host-generic # use generic-host # hostgroups generic-remote-hosts # register 0 #} define host { name tpl-host-linux use linux-server hostgroups linux-hosts register 0 } define host { name tpl-host-windows use windows-server #hostgroups windows-servers hostgroups windows-hosts register 0 } define host { name tpl-host-printer use generic-printer hostgroups printer-hosts register 0 } define host { name tpl-host-network use generic-switch hostgroups network-hosts register 0 }

$ tree
.
├── README.md
├── in
│ ├── commands.cfg
│ ├── contacts.cfg
│ ├── localhost.cfg
│ ├── printer.yaml
│ ├── templates.cfg
│ ├── templates2.cfg
│ ├── timeperiods.cfg
│ └── windows.yaml
├── out
├── play-templating-nagios-config.yml
├── roles
│ └── nagios_mk_objects_definition
│ └── tasks
│ └── main.yml
└── template.jinja

play-templating-nagios-config.yml

#! /usr/bin/env ansible-playbook --- - name: Generate Nagios objects definition conf files gather_facts: false hosts: localhost vars: line: "{{ lookup('env', 'LINE') }}" pre_tasks: - name: Assert check environment vars are correctly defined ansible.builtin.assert: that: - line is defined and line != '' msg: "Env var must be defined" tasks: - name: Copy cfg files ansible.builtin.copy: src: "{{ item }}" dest: "out/{{ item | basename }}" mode: "0644" with_fileglob: in/*.cfg - name: Loop on each yaml file ansible.builtin.include_role: name: nagios_mk_objects_definition vars: nagios_mk_objects_definition_config_file: "{{ item }}" with_fileglob: in/*.yaml

roles/nagios_mk_objects_definition/tasks/main.yml

--- - name: Include yaml config files ansible.builtin.include_vars: file: "{{ nagios_mk_objects_definition_config_file }}" - name: Templating jinja files ansible.builtin.template: src: template.jinja dest: "out/{{ nagios_mk_objects_definition_config_file | basename | replace('.yaml', '.cfg') }}" mode: "0644"