ServerMonitor is a C++14 command line program for determining if a server or a service on a server is available (up) or not (down). It can monitor using four methods:
- HTTP(s) - the server must respond or redirect with a 200 status (see example below to customize)
- Port - the server must be listening on this port and accept a connection
- Ping - the server must respond to a ping
- Custom Command - a command can be run to provide custom logic to determine if a server is running. Exit code 0 is up, and anything else is down.
ServerMonitor checks each server in parallel, so the total time to check all servers will only be as long as the slowest server, instead of the total of all monitor times.
The program is ran like so:
ServerMonitor <input_config.json> <output_status.json>
There are only two command line arguments:
-
Input config - this is a JSON file with all configuration options specified. See below for examples.
-
Output status - after checking all servers, a JSON status report will get generated, which can then be consumed by any program that can read JSON. See status.html as an example static HTML page that renders the JSON file via jQuery.
To monitor Apple's website:
{
"servers": [
{
"name": "Apple Website",
"url": "http://apple.com"
}
]
}
To monitor GitHub's SSH port:
{
"servers": [
{
"name": "GitHub SSH",
"host": "github.com",
"port": 22
}
]
}
To monitor Google.com via ping:
{
"servers": [
{
"name": "Google",
"ping": "google.com"
}
]
}
To monitor via a custom command, for example SSH into a server and check for a file's existance:
{
"servers": [
{
"name": "My Server",
"cmd": "ssh my.server test -f myfile.txt"
}
]
}
To monitor a website with a custom HTTP status other than 200:
{
"servers": [
{
"name": "My Login Page",
"url": "http://example.com",
"httpStatus": 401
}
]
}
Currently the above examples don't provide any type of notification of when a server goes up or down. For this you must use the actions
key. There are two types of actions: Command and Email:
The most basic action is to run a command. For example, to display a notification in macOS:
{
"actions": {
"notif": {
"cmd": "osascript -e 'display notification \"Server is {{STATUS}}\" with title \"{{name}}\"'"
}
},
"servers": [
{
"name": "Apple Website",
"url": "http://apple.com",
"action": "notif"
}
]
}
Emails can be sent using SMTP:
{
"actions": {
"email": {
"smtp_host": "smtp.example.com:587",
"smtp_user": "user",
"smtp_password": "password",
"from": "servermonitor@example.com",
"to": "you@example.com",
"subject": "ServerMonitor: {{name}} is {{STATUS}}",
"body_down": "Error: {{error}}\nDate: {{date}}\n",
"body_up": "Date: {{date}}\n"
}
},
"servers": [
{
"name": "Apple Website",
"url": "http://apple.com",
"action": "email"
}
]
}
body_down
and body_up
are used to separate the body message for when a server is up or down.
As used above, actions can use the following case-sensitive variables:
Variable | Description |
---|---|
{{name}} | Server name |
{{status}} | "up" or "down" |
{{Status}} | "Up" or "Down" |
{{STATUS}} | "UP" or "DOWN" |
{{error}} | Error message |
{{date}} | Date when monitor started |
Advanced options can be set at the root level (along side actions
and servers
) and/or overridden for each individual server (except date_format
).
Option | Type | Description | Default |
---|---|---|---|
timeout | Integer | The timeout in seconds to wait for a response. | 5 |
verifypeer | Boolean | Enable or disable CURL's VERIFYPEER option. Useful for websites with self-signed or expired SSL certificates. | true |
date_format | String | The format used for dates (global only). See strftime. | %Y-%m-%d %I:%M:%S %p |
Example for overriding the timeout for all servers to 30 seconds:
{
"timeout": 30,
"servers": [
{
"name": "Apple Website",
"url": "http://apple.com"
}
]
}
Example for disabling peer verification for a single server:
{
"servers": [
{
"name": "Apple Secure Website",
"url": "https://apple.com",
"verifpeer": false
}
]
}
Dependencies:
- CMake 3.1 or later
- CURL
First make sure you cloned recursively:
git clone --recursive https://github.com/ProsoftEngineering/ServerMonitor.git
Then run make
.
Below are sample configurations for running ServerMonitor every minute.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.prosofteng.servermonitor</string>
<key>ProgramArguments</key>
<array>
<string>/Users/you/ServerMonitor/ServerMonitor</string>
<string>/Users/you/ServerMonitor/config.json</string>
<string>/Users/you/ServerMonitor/status.json</string>
</array>
<key>StartInterval</key>
<integer>60</integer>
</dict>
</plist>
Save to ~/Library/LaunchAgents/com.prosofteng.servermonitor.plist
and activate via:
launchctl load ~/Library/LaunchAgents/com.prosofteng.servermonitor.plist
* * * * * ~/ServerMonitor/ServerMonitor ~/ServerMonitor/config.json ~/ServerMonitor/status.json