tech:generateur_de_conf_nagios
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| tech:generateur_de_conf_nagios [2025/04/21 14:21] – Jean-Baptiste | tech:generateur_de_conf_nagios [2025/11/20 10:00] (Version actuelle) – Jean-Baptiste | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| < | < | ||
| - | {{tag> | + | {{tag> |
| # Générateur de conf Nagios | # Générateur de conf Nagios | ||
| - | ** Brouillon ** | ||
| object configuration files | object configuration files | ||
| Ligne 63: | Ligne 62: | ||
| alias: | alias: | ||
| address: | address: | ||
| + | __comment: | ||
| + | __lines: | ||
| - host_name: | - host_name: | ||
| Ligne 91: | Ligne 92: | ||
| use: " | use: " | ||
| # | # | ||
| - | hostgroup_name: | + | hostgroup_name: |
| check_command: | check_command: | ||
| Ligne 130: | Ligne 131: | ||
| '' | '' | ||
| - | ~~~ | + | ~~~perl |
| #define host { | #define host { | ||
| # | # | ||
| - | # name windows-tpl | + | # name tpl-host-windows |
| # use windows-server | # use windows-server | ||
| # hostgroups | # hostgroups | ||
| Ligne 152: | Ligne 153: | ||
| '' | '' | ||
| - | ~~~ | + | ~~~perl |
| define hostgroup { | define hostgroup { | ||
| hostgroup_name | hostgroup_name | ||
| Ligne 160: | Ligne 161: | ||
| '' | '' | ||
| - | ~~~ | + | ~~~perl |
| - | #define hostgroup { | + | define hostgroup { |
| - | # | + | |
| - | # | + | hostgroup_name |
| - | # | + | alias Windows Hosts |
| - | #} | + | } |
| ~~~ | ~~~ | ||
| Ligne 176: | Ligne 177: | ||
| + | ## templates2.cfg | ||
| + | |||
| + | ~~~perl | ||
| + | |||
| + | |||
| + | ############################################################################### | ||
| + | # | ||
| + | # HOST TEMPLATES | ||
| + | # | ||
| + | ############################################################################### | ||
| + | |||
| + | |||
| + | #define host { | ||
| + | # | ||
| + | # name tpl-host-generic | ||
| + | # use | ||
| + | # hostgroups | ||
| + | # register | ||
| + | #} | ||
| + | |||
| + | |||
| + | define host { | ||
| + | |||
| + | name tpl-host-linux | ||
| + | use | ||
| + | hostgroups | ||
| + | register | ||
| + | } | ||
| + | |||
| + | |||
| + | define host { | ||
| + | |||
| + | name tpl-host-windows | ||
| + | use | ||
| + | # | ||
| + | hostgroups | ||
| + | register | ||
| + | } | ||
| + | |||
| + | |||
| + | define host { | ||
| + | |||
| + | name tpl-host-printer | ||
| + | use | ||
| + | hostgroups | ||
| + | register | ||
| + | } | ||
| + | |||
| + | |||
| + | define host { | ||
| + | |||
| + | name tpl-host-network | ||
| + | use | ||
| + | hostgroups | ||
| + | register | ||
| + | } | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | -------------- | ||
| + | |||
| + | ## | ||
| + | |||
| + | ~~~ | ||
| + | $ 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 | ||
| + | │ | ||
| + | │ | ||
| + | └── template.jinja | ||
| + | ~~~ | ||
| + | |||
| + | '' | ||
| + | ~~~yaml | ||
| + | #! / | ||
| + | |||
| + | --- | ||
| + | |||
| + | - name: Generate Nagios objects definition conf files | ||
| + | gather_facts: | ||
| + | hosts: localhost | ||
| + | |||
| + | vars: | ||
| + | line: "{{ lookup(' | ||
| + | |||
| + | 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: " | ||
| + | |||
| + | - name: Copy cfg files | ||
| + | ansible.builtin.copy: | ||
| + | src: "{{ item }}" | ||
| + | dest: " | ||
| + | mode: " | ||
| + | with_fileglob: | ||
| + | |||
| + | - name: Loop on each yaml file | ||
| + | ansible.builtin.include_role: | ||
| + | name: nagios_mk_objects_definition | ||
| + | vars: | ||
| + | nagios_mk_objects_definition_config_file: | ||
| + | with_fileglob: | ||
| + | ~~~ | ||
| + | |||
| + | '' | ||
| + | ~~~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: " | ||
| + | mode: " | ||
| + | ~~~ | ||
| + | |||
| + | '' | ||
| + | ~~~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/ | ||
| + | |||
| + | |||
| + | Avantage : | ||
| + | * Une seule configuration central qui peut être déclinée sur l' | ||
| + | * Evite d' | ||
| + | * 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/ | ||
| + | in/ | ||
| + | |||
| + | Chanque fichier yaml générera un fichier cfg. | ||
| + | Ex | ||
| + | in/ | ||
| + | in/ | ||
| + | |||
| + | |||
| + | 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 '' | ||
| + | 2. Lancer la commande ci-dessous. (Exemple avec la ligne 12) | ||
| + | 3. Copier les fichiers out/ | ||
| + | 4. S' | ||
| + | 5. Vérifier la syntaxe (voir plus bas) | ||
| + | |||
| + | Exemple pour la ligne 12 | ||
| + | ~~~bash | ||
| + | env LINE=12 ./ | ||
| + | ~~~ | ||
| + | |||
| + | Le dossier out/ | ||
| + | < | ||
| + | |||
| + | Une fois les nouveaux fichiers en place il est recommandé de les vérifier | ||
| + | ~~~bash | ||
| + | / | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Compatibilité | ||
| + | |||
| + | Compatible Nagios 3 et Nagios 4 | ||
| + | |||
| + | |||
| + | ## Limites | ||
| + | Pour le moment seuls les types d' | ||
| + | * host | ||
| + | * hostgroup | ||
| + | * service | ||
| + | Cepandent si d' | ||
tech/generateur_de_conf_nagios.1745238089.txt.gz · Dernière modification : de Jean-Baptiste
