monit-ls is a Node.js CLI script designed to fetch status information from Monit hosts and present it in various formats, including human-readable command-line output, JSON, and HTML.
The script uses ejs for HTML templating. A basic HTML template is embedded within the script. For more complex HTML reports, you can create a template.ejs file in the same directory as index.js, and the script will use that instead.
Features:
- Multiple Monit Hosts: Query status from one or more Monit instances.
- Flexible Configuration: Specify Monit host URLs directly via command-line arguments or through a JSON configuration file.
- Error Handling: Gracefully handles unreachable Monit hosts or API errors.
Output Formats:
- Table: A formatted ASCII spreadsheet for easy readability in the terminal.
- JSON: A structured JSON object suitable for programmatic consumption.
- HTML: A basic HTML report for web-based viewing.
The monit-ls script can be executed directly from the project directory, or via the monitls command.
You can specify Monit hosts in 3 ways. Provide a comma-separated list of Monit URLs. Include authentication credentials (username:password) directly in the URL if required.
| Option | Description |
|---|---|
-h, --hosts hosts |
Comma-separated list of Monit URLs. (e.g. http://user:pass@host:2812) |
-c, --config path |
Path to a JSON config file containing the list of hosts. |
-f, --format format |
Output format: json, table, html. |
-o, --output path |
File path to save the output. |
-t, --template path |
Path to a custom EJS template for HTML reports. |
-w, --wait timeout |
Number of seconds to wait for a response from each host. |
- Via the first argument:
monitls http://user:pass@monit-host1:2812,http://monit-host2:2812- Via the
--hostsoption:
monitls --hosts http://user:pass@monit-host1:2812,http://monit-host2:2812- Using a configuration file with the
--configoption:
- Create a JSON file (e.g.,
config.json) with ahostsarray. - A custom EJS template path may also be supplied via a
templatestring.
monitls --config ~/path/to/config.jsonconfig.json example:
{
"hosts": [
"http://user:pass@monit-host1:2812",
"http://monit-host2:2812"
],
"template": "~/path/to/custom_template.ejs",
"wait": 5
}Use the --format option to choose the output format. The default is table.
monitls --hosts http://user:pass@monit-host:2812 --format tableExample output:
┌───────────┬─────────┬──────┬────────┬──────────────┐
│ Host │ Service │ Type │ Status │ Uptime/Usage │
├───────────┼─────────┼──────┼────────┼──────────────┤
│ test-host │ apache2 │ 3 │ OK │ Up: 500s │
├───────────┼─────────┼──────┼────────┼──────────────┤
│ │ rootfs │ 5 │ OK │ Disk: 45.0% │
└───────────┴─────────┴──────┴────────┴──────────────┘
monitls --hosts http://user:pass@monit-host:2812 --format jsonExample output (truncated):
[
{
"url": "http://user:pass@monit-host:2812",
"success": true,
"data": {
"$": { ... },
"server": { ... },
"platform": { ... },
"service": [ { ... }, { ... } ]
}
}
]monitls --hosts http://user:pass@monit-host:2812 --format htmlThis will output HTML content to the console. To save it to a file, use the --output option.
Use the --output <path> option to save the generated output to a specified file.
monitls --hosts http://user:pass@monit-host:2812 --format html --output monit_report.html