BOOBEN is a Python script that notifies you when your system starts or stops, using your system's Mail Transfer Agent (MTA) to send out emails. Right out of the box, it includes useful info like server status, the time of the event, ZFS pool status, network interface details, and logs from journald and dmesg. Feel free to tweak it by adding what you need or removing what you don't. It's made to be easy to customize to your liking.
- Python 3
- A configured system MTA (like
sendmail) that respects/etc/aliases.
- Copy the script to
/usr/local/bin/booben. - Ensure the script is executable:
chmod +x /usr/local/bin/booben.
To automate email generation on system start and stop, configure a systemd service.
- Create a systemd service file
/etc/systemd/system/booben.servicewith the following content:
[Unit]
Description=Send mail on system start and stop
Wants=network-online.target
After=network.target network-online.target
[Service]
Type=oneshot
# Should be tweaked for your MTA
ExecStart=/bin/bash -c '/usr/local/bin/booben start | sendmail --aliases=/etc/aliases root'
ExecStop=/bin/bash -c '/usr/local/bin/booben stop | sendmail --aliases=/etc/aliases root'
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target- Reload systemd to recognize the new service:
sudo systemctl daemon-reload. - Enable the service:
sudo systemctl enable booben.service.
While the script is typically invoked by systemd, you can manually trigger email generation:
- For a start event:
/usr/local/bin/booben start. - For a stop event:
/usr/local/bin/booben stop.
Systemd handles the execution at system start and stop, piping the script's output to sendmail for email delivery.
Edit /etc/aliases to specify the notification email's recipient(s), directing emails intended for root to the appropriate address(es).
- This script generates email content but relies on your system's MTA to send the emails. Ensure your MTA is correctly configured.
- The use of gzip compression for logs helps reduce email size.
booben is designed with flexibility in mind, making it straightforward to customize the set of commands executed and the information fields included in the notification emails. Here's how you can customize booben to fit your specific needs:
The script consists of various classes representing different sections of the email body, including CommandSection for executing shell commands and capturing their output, and FileSection for attaching logs and other files. To customize the information included in the emails:
-
Add or Remove Sections: You can modify the
create_emailfunction to add or remove instances ofCommandSection,FileSection, or any custom sections you define. Each section is designed to capture specific pieces of information or system states. -
Customize Shell Commands: In the
CommandSectioninstances, change theshell_commandparameter to any shell command whose output you wish to include in the notification email. For example, replacing'zpool status -v'with another command like'df -h'to report disk space usage. -
Adjust Log Files: The
LogSectionclass is a specialized form ofFileSectionfor attaching log files. Modify thecommandparameter to change which logs are captured and compressed into gzip format for attachment.
To add a section reporting disk usage, you would insert the following line into the create_email function:
message_body.add_row('Disk Usage', CommandSection('df -h'))This line creates a new CommandSection that executes the df -h command, capturing its output in a readable format for inclusion in the email.
To attach a custom log file, you can create a new LogSection instance with the log file's path and desired filename for the attachment:
files.append(LogSection("errors.log", 'grep "error" /path/to/your/logfile'))This line appends a new LogSection to the files list, which reads and compresses the specified log file, attaching it to the email.
- When adding shell commands, ensure they can be executed without user interaction and complete in a reasonable amount of time.
- Review the commands to ensure they do not expose sensitive information unintendedly, as the email content will include their output.
By following these guidelines, you can tailor booben to meet your system monitoring and notification needs, ensuring you're always informed about the key aspects of your system's status at boot and shutdown.