Skip to main content

A simple "health monitor" script

One of the new functions available in SyncJS as of AFT! v4.0.0 (and higher) is TcpConnect, which performs a quick connect/disconnect cycle to any desired host on any desired port, and returns whether or not the connection was successful (that's whether or not the server listening on that port on the specified host is up).

This is ideal to build very simple "health monitor" scripts, here's an example:

var host    = "your.host.com";
var port    = 443;
var alertTo = "alerts@yourdomain.com";
var alertFrom = "aft-monitor@yourdomain.com";

// TcpConnect returns true if the TCP handshake succeeds within the timeout.
// We use a 5-second timeout (5000 ms).
if (TcpConnect(host, port, 5000)) {
    Log.Info(host + ":" + port + " is reachable, all good");
} else {
    var subject = "ALERT: " + host + ":" + port + " is unreachable";
    var body    = "The TCP connectivity check for " + host + ":" + port + " failed.\n" +
                  "The host may be down, the port may be blocked, or the network may be unreachable.\n\n" +
                  "Please investigate immediately.";

    Log.Error(host + ":" + port + " is unreachable, sending alert to " + alertTo);

    if (!SendMail(alertFrom, alertTo, subject, body)) {
        Log.Error("alert email could not be sent, check the SMTP configuration in AFT! settings");
    }
}

Simple. Nothing fancy. If the host is up, it just logs a line, if the host is down it emails you to let you know.

As of v4.0.0, AFT! is no longer just file-transfer as code, with the power and flexibility of SyncJS you can do a lot more than in the past.