How to configure the email alert (SMTP config) while exit 2 code (error occured) for service check

I Created a simple service check bash script in all the agents in /opt/sensu/ path
service check script is an example to test the SMTP config for exit code

#!/bin/bash

# Check if nginx service is running
if systemctl is-active --quiet nginx; then
    echo "nginx service is running"
    exit 0  # Exit with status code 0 indicating OK
else
    echo "nginx service is not running"
    exit 2  # Exit with status code 2 indicating Critical
fi

from workstation i apply below yaml for creating checks and add the email-handler for sending the alerts to email

sensuctl create -f nginx_check.yml
api_version: core/v2
type: CheckConfig
metadata:
  name: nginx-check
  namespace: default
spec:
  command: "/opt/sensu/nginx_check.sh"
  subscriptions:
    - system_test
  interval: 5
  publish: true
  handlers:
  - email-handler

then i created email_handler.yaml for the same

sensuctl create -f email_handler.yaml
api_version: core/v2
type: Handler
metadata:
  name: email-handler
  namespace: default
spec:
  type: pipe
  command: "sensu-email-handler --authMethod 'none' \
--mail-from='xxxx@xxxxx.com' --mail-to='yyyy@yyyyy.com' \
--smtp-address='xx.xx.xx.xx' --smtp-port=25"
  timeout: 5
  runtime_assets:
    - email-handler

and added the plugins

sensuctl asset add sensu/sensu-email-handler -r email-handler

so can anyone tell me what addtional steps needs to be done in agent or workstation or in backend for the same
any

Hey @sensu-tester, :wave:

Welcome to the community! Looking at your configuration, I only see one major thing that is not explicitly noted, which will be essential to consider: the agent subscription.

Sensu Subscription: Each Sensu agent must be configured with the subscription that matches one specified in your check configuration (system_test). This should be set in the agent’s configuration file or passed as a flag when starting the agent:

subscriptions:
  - system_test

You may already have this set, but I don’t see it explicitly noted in the provided configuration.

You may also want to consider adding some of the Sensu built-in filtering for the email handler, like:

filters:
  - is_incident
  - not_silenced

The next steps are validating your (correctly) obfuscated email handler configuration and then testing. Will you let us know if you encounter any difficulties there?

One additional note: while your script seems fine, there is an asset that can accomplish the same task:

Sensu Processes Check

I hope that’s helpful; let us know how things go.

Hello @justinhenderson ,
Thank you for the clarification on the above issue, with all the pertinent details provided.
Yes i added the subscriptions to all the available agents

[root@test1 sensu]# sensuctl entity list
   ID     Class    OS               Subscriptions                        Last Seen            
──────── ─────── ─────── ──────────────────────────────────── ────────────────────────────────
  test1   agent   linux   system_test,webserver,entity:test1   2024-04-19 07:20:31 +0000 UTC  
  test2   agent   linux   system_test,webserver,entity:test2   2024-04-19 07:20:27 +0000 UTC  

And after successfully receiving the alert, I also added the filter in handlers

Additionally, there is one thing I would like to ask you

  1. If we are using sensu plugins to add the checks
    then in command section we are using below syntax
command: check-disk-usage --warning 80.0 --critical 90.0

and if we are using custom script (shell script here )
then we are giving below commands

command: "/opt/sensu/check_cpu_utilisation.sh 75 90"

and this
‘/opt/sensu/check_cpu_utilisation.sh’ script should be present in this path in every agent servers,

so is there any way that we can put custom script in one place and we are giving path of the scripts in that command section …
so we dont need to be put that custom scripts in every agent servers.

THank You

Hey @sensu-tester,

I’m glad to hear things are working. :+1: For your next question, adding custom scripts to each agent would be a good task for a configuration management tool. One of the significant benefits of using Sensu Assets is with one command, the binary will be installed on the agent right when it’s needed. So, one option is to use Sensu Assets where you can rather than custom scripts.

Your CPU check script can be accomplished similarly with an asset available on Bonsai.

If you’d like a more comprehensive, cross-platform performance check, consider using the Sensu System Check. This combines several checks into one, with which you can alert as you see fit: CPU, Memory, Swap, Disk, System Load, Host uptime, and Host processes. You can use all or just a few in one check, and that asset will show up on any agent with the check and subscription.

I’d recommend looking at Bonsai for more plugins that may be helpful for your needs.

Let me know if that is helpful! :grin:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.