servermonitoring

A basic monitoring configuration for Monit, running on a server. Notifications will be sent via email.

Requirements:

  • monit monit
  • A Mail Transfer Agent (MTA) installed on the computer, and listening to port 25. While technically, you could add user names and passwords for SMTP servers to the configuration, we would not recommend to keep credentials scattered across configuration files, unless you’re forced to.

This basic configuration monitors a few essentials:

  • System load
  • Usage and ownership of the root mount
  • Usage and ownership of the /data mount
  • Status of the SSH service, including a restart, if necessary

Substitute your own notification emails address for notifications@example.com, adapt the script to your context, and save it under /etc/monit/conf.d/servermonitoring. Then, restart the Monit service.

The Monit website has helpful howtos for monitoring many more things.

To download this script, first hover with your mouse over the listing below and press »Open code in new window«. Copying and pasting the colored and formatted listing below, as-is, won’t work.
# Globals
# ===
set mailserver localhost
set alert notifications@example.com


# Watchdogs
# ===

# System
check system $HOST
    if loadavg (1min) per core > 2 for 5 cycles then alert
    if loadavg (5min) per core > 1.5 for 10 cycles then alert
    if memory usage > 80% for 4 cycles then alert
    if swap usage > 60% for 4 cycles then alert
    if cpu usage (user) > 30% for 4 cycles then alert
    if cpu usage (system) > 40% for 4 cycles then alert
    if cpu usage (wait) > 30% for 4 cycles then alert


# File systems

check filesystem "myserver /" with path /
    if changed fsflags then alert
    if space usage > 85% for 2 cycles then alert
    if inode usage > 50% then alert
    if failed uid "root" then alert
    if failed gid "root" then alert
    if service time > 500 milliseconds for 3 times within 5 cycles then alert

check filesystem "myserver /data" with path /data
    if changed fsflags then alert
    if space usage > 85% for 2 cycles then alert
    if space usage < 40% then alert # Mass deletion alert
    if inode usage > 50% then alert
    if failed uid "root" then alert
    if failed gid "root" then alert
    if service time > 500 milliseconds for 3 times within 5 cycles then alert

# Networking

check network "myserver LAN" with interface eno1
    if failed link then alert
    if changed link then alert
    if saturation > 90% then alert
    if download > 10 MB/s then alert
    if total uploaded > 10 GB in last hour then alert


# SSHD

check process "myserver sshd" with pidfile /var/run/sshd.pid
   start program  "/usr/bin/systemctl start ssh.service"
   stop program  "/usr/bin/systemctl stop ssh.service"
   if failed host 127.0.0.1 port 22 protocol ssh then restart

Image Credits:
Papirus icon for Terminal (modified) | GNU General Public License, version 3

Licensing:
This content is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
For your attributions to us please use the word »tuxwise«, and the link https://tuxwise.net.