{{tag>Nagios Ansible Jinja}} # Générateur de conf Nagios 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 ~~~yaml --- hosts: - host_name: "srv1" use: "linux-server" alias: "Server 1" address: "192.168.1.10" __comment: "Server One" __lines: [3, 12] - 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'' ~~~perl #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'' ~~~perl define hostgroup { hostgroup_name linux-hosts alias Linux Hosts } ~~~ ''windows.cfg'' ~~~perl define hostgroup { hostgroup_name windows-hosts alias Windows Hosts } ~~~ Changer la définition des hosts ''use linux-server'' ''use linux-remote-servers'' ## templates2.cfg ~~~perl ############################################################################### # # 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'' ~~~yaml #! /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: Mkdir out/L ansible.builtin.file: path: out/L{{ line }}/ state: directory mode: "0755" - name: Copy cfg files ansible.builtin.copy: src: "{{ item }}" dest: "out/L{{ line }}/{{ 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'' ~~~yaml --- - 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/L{{ line }}/{{ nagios_mk_objects_definition_config_file | basename | replace('.yaml', '.cfg') }}" mode: "0644" ~~~ ''template.jinja'' ~~~jinja ############################################################################### # # HOST DEFINITIONS # ############################################################################### {% for HOST in hosts %} {% if ( HOST.__lines is not defined ) or ( line in HOST.__lines ) %} {% if ( HOST.__comment[line] is defined ) or ( HOST.__comment is defined and HOST.__comment is not mapping ) %} # {{ HOST.__comment[line] | d(HOST.__comment) | d() }} {% endif %} define host { {% for k, v in HOST.items() %} {% if not k | regex_search('__') %} {% if ( v[line] is defined ) or ( v[line] is not defined and v is not mapping ) %} {{ k.ljust(20) }} {{ v[line] | d (v) }} {% endif %} {% endif %} {% endfor %} } {% endif %} {% endfor %} ############################################################################### # # HOST GROUP DEFINITIONS # ############################################################################### {% for HOSTGROUP in hostgroups %} {% if ( HOSTGROUP.__lines is not defined ) or ( line in HOSTGROUP.__lines ) %} {% if ( HOSTGROUP.__comment[line] is defined ) or ( HOSTGROUP.__comment is defined and HOSTGROUP.__comment is not mapping ) %} # {{ HOSTGROUP.__comment[line] | d(HOSTGROUP.__comment) | d() }} {% endif %} define hostgroup { {% for k, v in HOSTGROUP.items() %} {% if not k | regex_search('__') %} {% if ( v[line] is defined ) or ( v[line] is not defined and v is not mapping ) %} {{ k.ljust(20) }} {{ v[line] | d(v) }} {% endif %} {% endif %} {% endfor %} } {% endif %} {% endfor %} ############################################################################### # # SERVICE DEFINITIONS # ############################################################################### {% for SERVICE in services %} {% if ( SERVICE.__lines is not defined ) or ( line in SERVICE.__lines ) %} {% if ( SERVICE.__comment[line] is defined ) or ( SERVICE.__comment is defined and SERVICE.__comment is not mapping ) %} # {{ SERVICE.__comment[line] | d(SERVICE.__comment) | d() }} {% endif %} define service { {% for k, v in SERVICE.items() %} {% if not k | regex_search('__') %} {% if ( v[line] is defined ) or ( v[line] is not defined and v is not mapping ) %} {{ k.ljust(20) }} {{ v[line] | d(v) }} {% endif %} {% endif %} {% endfor %} } {% endif %} {% endfor %} ~~~ -------------- # README # Nagios object configuration files generator Cet outil sert à générer les fichiers de configuration Nagios (object configuration files) en prenant les données dans des fichiers YAML et de les exploiter grâce à un template JINJA. Les fichers .cfg en sortie seront à déposer dans nagios/etc/objects/ Avantage : * Une seule configuration central qui peut être déclinée sur l'ensemble des lignes * Evite d'avoir des disparités trop importantes d'une ligne à une autre * Permet de mieux voir et comprendre les désparités * Il devient facil et rapide de comparer la conf d'une ligne à une autre et donc les incohérences resortent mieux * En cas de création d'une nouvelle ligne il est pertinent et facile de partir du tronc commun * Plus besoin de merger la conf Nagios d'une ligne à une autre Inconvéniens: * Précaution à prendre dans le cas où les fichiers finaux seraient modifiés à la mains sur les serveurs Nagios. ## Usage Les fichiers en entrée sont dans in/ Ils sont de deux types: * cfg (nagios object configuration files). Ces fichiers ne seront pas transformés. Ces fichiers ne devraient pas ordinairement être modifiés. * yaml qui servirons à générer des fichiers .cfg Les fichiers cfg existant serons copiés tel quel Ex in/localhost.cfg => out/L12/localhost.cfg in/contacts.cfg => out/L12/contacts.cfg Chanque fichier yaml générera un fichier cfg. Ex in/printer.yaml => out/L12/printer.cfg in/servers.yaml => out/L12/servers.cfg Noter que chaque fichier cfg généré devra être inclue dans nagios.cfg pour être pris en compte par Nagios. Pour modifier la conf il faut : 1. Editer le fichier yaml approprié. Ex: servers.yaml. Prendre garde à la clause ''__lines'' pour restreindre la définition à une liste de ligne (défaut : toutes) 2. Lancer la commande ci-dessous. (Exemple avec la ligne 12) 3. Copier les fichiers out/L12/*.cfg dans nagios/etc/objects 4. S'assurer en cas de création d'un nouveau fichier yaml que celui-ce est bien appelé dans nagios.cfg 5. Vérifier la syntaxe (voir plus bas) Exemple pour la ligne 12 ~~~bash env LINE=12 ./play-templating-nagios-config.yml ~~~ Le dossier out/L/ contient tous les fichiers à placer dans nagios/etc/objects/ correspond au numéro de la ligne Une fois les nouveaux fichiers en place il est recommandé de les vérifier ~~~bash /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg ~~~ ## Compatibilité Compatible Nagios 3 et Nagios 4 ## Limites Pour le moment seuls les types d'objects ci-dessous sont pris en charge : * host * hostgroup * service Cepandent si d'autres types sévererait nécessaire il est aisé de modifier le fichier template.jinja