|
| 1 | +--- |
| 2 | +title: Événements avec un check custom d'Agent |
| 3 | +kind: documentation |
| 4 | +further_reading: |
| 5 | + - link: developers/write_agent_check/ |
| 6 | + tag: Documentation |
| 7 | + text: Écrire un check custom d'Agent |
| 8 | +--- |
| 9 | +## Envoi |
| 10 | + |
| 11 | +Utilisez la fonction `event(<DICT_ÉVÉNEMENT>)` pour envoyer un événement depuis un check custom d'Agent : |
| 12 | + |
| 13 | +```text |
| 14 | +self.event( |
| 15 | + { |
| 16 | + "timestamp": <TIMESTAMP_EPOCH>, |
| 17 | + "event_type": "<NOM_ÉVÉNEMENT>", |
| 18 | + "msg_title": "<TITRE>", |
| 19 | + "msg_text": "<MESSAGE>", |
| 20 | + "aggregation_key": "<CLÉ_AGRÉGATION>", |
| 21 | + "alert_type": "<TYPE_ALERTE>", |
| 22 | + "source_type_name": "<TYPE_SOURCE>", |
| 23 | + "host": "<HOSTNAME>", |
| 24 | + "tags": ["<TAGS>"], |
| 25 | + "priority": "<PRIORITÉ>" |
| 26 | + } |
| 27 | +) |
| 28 | +``` |
| 29 | + |
| 30 | +Les types de données et de clés suivants sont disponibles dans le dictionnaire de l'événement : |
| 31 | + |
| 32 | +| Clé | Type | Obligatoire | Description | |
| 33 | +|--------------------|-----------------|----------|---------------------------------------------------------------| |
| 34 | +| `timestamp` | Nombre entier | Oui | Le timestamp epoch de l'événement | |
| 35 | +| `event_type` | Chaîne | Oui | Le nom de l'événement | |
| 36 | +| `msg_title` | Chaîne | Oui | Le titre de l'événement | |
| 37 | +| `msg_text` | Chaîne | Oui | Le corps de texte de l'événement | |
| 38 | +| `aggregation_key` | Chaîne | Non | La clé à utiliser pour agréger les événements | |
| 39 | +| `alert_type` | Chaîne | Non | `error`, `warning`, `success` ou `info` (valeur par défaut : `info`) | |
| 40 | +| `source_type_name` | Chaîne | Non | Le nom du type de source | |
| 41 | +| `host` | Chaîne | Non | Le hostname | |
| 42 | +| `tags` | Liste de chaînes | Non | La liste de tags associés à cet événement | |
| 43 | +| `priority` | Chaîne | Non | Indique la priorité de l'événement (`normal` ou `low`) | |
| 44 | + |
| 45 | +### Exemple |
| 46 | + |
| 47 | +Voici un exemple d'utilisation d'un check custom d'Agent permettant d'envoyer régulièrement un événement. Consultez la rubrique [Écrire un check custom d'Agent][1] pour en savoir plus. |
| 48 | + |
| 49 | +1. Créez un nouveau répertoire `event_example.d/` dans le dossier `conf.d/` à la racine du [répertoire de configuration de votre Agent][2]. |
| 50 | + |
| 51 | +2. Dans le dossier `event_example.d/`, créez un fichier de configuration intitulé `event_example.yaml` avec le contenu suivant : |
| 52 | + |
| 53 | + ```yaml |
| 54 | + instances: [{}] |
| 55 | + ``` |
| 56 | +
|
| 57 | +3. Accédez au dossier `checks.d/` dans le dossier parent de `conf.d/`. |
| 58 | +4. Dans ce dossier, créez un fichier de check custom `event_example.py` avec le contenu suivant : |
| 59 | + |
| 60 | + {{< code-block lang="python" filename="event_example.py" >}} |
| 61 | + from datadog_checks.base import AgentCheck |
| 62 | + |
| 63 | + __version__ = "1.0.0" |
| 64 | + |
| 65 | + class MyClass(AgentCheck): |
| 66 | + def check(self, instance): |
| 67 | + self.event( |
| 68 | + { |
| 69 | + "timestamp": time.time(), |
| 70 | + "event_type": "Error", |
| 71 | + "msg_title": "Exemple d'événement", |
| 72 | + "msg_text": "Voici un exemple d'événement provenant de Datadog.", |
| 73 | + "alert_type": "error", |
| 74 | + } |
| 75 | + ) |
| 76 | + {{< /code-block >}} |
| 77 | + |
| 78 | +5. [Redémarrez l'Agent][3]. |
| 79 | +6. Pour valider votre check, lancez la [commande status de l'Agent][4] et cherchez `event_example` dans la section Checks : |
| 80 | + |
| 81 | + ``` |
| 82 | + ========= |
| 83 | + Collector |
| 84 | + ========= |
| 85 | +
|
| 86 | + Running Checks |
| 87 | + ============== |
| 88 | +
|
| 89 | + (...) |
| 90 | +
|
| 91 | + event_example (1.0.0) |
| 92 | + --------------------- |
| 93 | + Instance ID: event_example:d884b5186b651429 [OK] |
| 94 | + Total Runs: 2 |
| 95 | + Metric Samples: Last Run: 0, Total: 0 |
| 96 | + Events: Last Run: 1, Total: 2 |
| 97 | + Service Checks: Last Run: 0, Total: 0 |
| 98 | + Average Execution Time : 0s |
| 99 | +
|
| 100 | + (...) |
| 101 | + ``` |
| 102 | + |
| 103 | +7. Il ne vous reste plus qu'à accéder à votre [flux d'événements Datadog][5] pour visualiser vos événements : |
| 104 | + |
| 105 | +{{< img src="developers/events/agent_check/event_stream_example.png" alt="exemple de flux d'événements" style="width:80%;">}} |
| 106 | + |
| 107 | +## Pour aller plus loin |
| 108 | + |
| 109 | +{{< partial name="whats-next/whats-next.html" >}} |
| 110 | + |
| 111 | + |
| 112 | +[1]: /fr/developers/write_agent_check |
| 113 | +[2]: /fr/agent/guide/agent-configuration-files/#agent-configuration-directory |
| 114 | +[3]: /fr/agent/guide/agent-commands/#restart-the-agent |
| 115 | +[4]: /fr/agent/guide/agent-commands/#agent-information |
| 116 | +[5]: https://app.datadoghq.com/event/stream |
0 commit comments