golang网站和REST API监控告警插件StatusOK的使用
Golang网站和REST API监控告警插件StatusOK的使用
StatusOK简介
StatusOK是一个用于监控网站和API的工具,当你的服务器宕机或响应时间超过预期时,它会通过Slack或电子邮件通知你。
简单版本使用
步骤1:编写config.json配置文件
{
"notifications":{
"mail":{
"smtpHost":"smtp.gmail.com",
"port":587,
"username":"yourmailid@gmail.com",
"password":"your gmail password",
"from":"yourmailid@gmail.com",
"to":"destemailid@gmail.com"
}
},
"requests":[
{
"url":"http://mywebsite.com",
"requestType":"GET",
"checkEvery":30,
"responseTime":800
}
]
}
确保开启Gmail的"允许不够安全的应用"选项。
步骤2:下载并运行StatusOK
下载二进制文件后,在终端运行以下命令:
$ ./statusok --config config.json
要在后台运行,添加&
:
$ ./statusok --config config.json &
停止进程:
$ jobs
$ kill %jobnumber
完整版本使用InfluxDB
你可以将数据保存到InfluxDB,并使用Grafana查看响应时间随时间的变化。
以下是一个监控REST API的配置示例:
{
"url":"http://mywebsite.com/v1/data",
"requestType":"POST",
"headers":{
"Authorization":"Bearer ac2168444f4de69c27d6384ea2ccf61a49669be5a2fb037ccc1f",
"Content-Type":"application/json"
},
"formParams":{
"description":"sanath test",
"url":"http://google.com"
},
"checkEvery":30,
"responseCode":200,
"responseTime":800
},
{
"url":"http://mywebsite.com/v1/data",
"requestType":"GET",
"headers":{
"Authorization":"Bearer ac2168444f4de69c27d6384ea2ccf61a49669be5a2fb037ccc1f",
},
"urlParams":{
"name":"statusok"
},
"checkEvery":300,
"responseCode":200,
"responseTime":800
},
{
"url":"http://something.com/v1/data",
"requestType":"DELETE",
"formParams":{
"name":"statusok"
},
"checkEvery":300,
"responseCode":200,
"responseTime":800
}
数据库支持
目前仅支持InfluxDB 0.9.3+,你也可以添加数据到你自己的数据库。
通知方式
支持以下通知方式:
- Slack
- SMTP电子邮件
- Mailgun
- HTTP端点
- Dingding
Docker运行方式
普通Docker运行
docker run -d -v /path/to/config/folder:/config sanathp/statusok
注意:配置文件夹必须包含名为config.json
的配置文件。
Docker Compose运行
准备docker-compose.yml文件:
version: '2'
services:
statusok:
build: sanathp/statusok
volumes:
- /path/to/config/folder:/config
depends_on:
- influxdb
influxdb:
image: tutum/influxdb:0.9
environment:
- PRE_CREATE_DB="statusok"
ports:
- 8083:8083
- 8086:8086
grafana:
image: grafana/grafana
ports:
- 3000:3000
然后运行:
docker-compose up
贡献
欢迎贡献代码。如果发现bug请创建issue,如果有新功能或修复请发送pull request。请确保编写测试用例。
许可证
Copyright 2015 Sanath Kumar Pasumarthy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License
更多关于golang网站和REST API监控告警插件StatusOK的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
更多关于golang网站和REST API监控告警插件StatusOK的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
StatusOK - Golang网站和REST API监控告警工具
StatusOK是一个用Go编写的轻量级监控工具,专门用于监控网站和REST API的可用性,并在服务出现问题时发送告警通知。
StatusOK主要功能
- 定期监控HTTP/HTTPS端点
- 支持GET、POST、PUT、DELETE等HTTP方法
- 验证响应状态码和响应内容
- 支持多种通知渠道:Slack、Email、Webhook等
- 可配置的检查频率
- 简单的配置方式
安装StatusOK
go get -u github.com/sanathp/statusok
或者直接下载预编译的二进制文件:
wget https://github.com/sanathp/statusok/releases/download/v1.0.0/statusok-v1.0.0-linux-amd64.tar.gz
tar -xvf statusok-v1.0.0-linux-amd64.tar.gz
基本配置示例
创建一个config.json
配置文件:
{
"notifications": {
"slack": {
"webhookUrl": "https://hooks.slack.com/services/YOUR/SLACK/WEBHOOK",
"channel": "#monitoring",
"username": "StatusOK Bot"
},
"email": {
"host": "smtp.gmail.com",
"port": 587,
"username": "your-email@gmail.com",
"password": "your-password",
"from": "your-email@gmail.com",
"to": ["alert-recipient@example.com"]
}
},
"requests": [
{
"url": "https://api.example.com/health",
"method": "GET",
"frequency": 60,
"expectedStatus": 200,
"expectedResponse": {
"status": "ok"
},
"requestHeaders": {
"Content-Type": "application/json"
},
"timeout": 5
}
]
}
运行StatusOK
statusok --config config.json
在Go程序中集成StatusOK
你也可以将StatusOK作为库集成到自己的Go程序中:
package main
import (
"log"
"github.com/sanathp/statusok"
"github.com/sanathp/statusok/notify"
)
func main() {
// 配置通知
slackConfig := notify.SlackConfig{
WebhookUrl: "https://hooks.slack.com/services/YOUR/SLACK/WEBHOOK",
Channel: "#monitoring",
Username: "StatusOK Bot",
}
emailConfig := notify.EmailConfig{
Host: "smtp.gmail.com",
Port: 587,
Username: "your-email@gmail.com",
Password: "your-password",
From: "your-email@gmail.com",
To: []string{"alert-recipient@example.com"},
}
// 配置监控请求
requests := []statusok.RequestConfig{
{
Url: "https://api.example.com/health",
Method: "GET",
Frequency: 60,
ExpectedStatus: 200,
ExpectedResponse: map[string]interface{}{"status": "ok"},
RequestHeaders: map[string]string{"Content-Type": "application/json"},
Timeout: 5,
},
}
// 创建监控器
monitor := statusok.NewMonitor(requests, []notify.Notifier{
notify.NewSlack(slackConfig),
notify.NewEmail(emailConfig),
})
// 启动监控
if err := monitor.Start(); err != nil {
log.Fatalf("Failed to start monitor: %v", err)
}
// 阻塞主线程
select {}
}
高级功能
- 自定义检查间隔:可以为每个端点设置不同的检查频率
- 响应验证:可以验证JSON响应中的特定字段
- 请求头设置:支持添加自定义请求头
- 超时设置:可以配置每个请求的超时时间
- 重试机制:在发送告警前可以配置重试次数
告警通知示例
当监控的端点出现问题时,StatusOK会发送类似以下内容的告警:
[CRITICAL] Request Failed
URL: https://api.example.com/health
Method: GET
Expected Status: 200
Actual Status: 500
Error: context deadline exceeded
Time: 2023-05-20 14:30:45
当服务恢复时,也会发送恢复通知。
总结
StatusOK是一个简单但功能强大的监控工具,特别适合监控REST API和网站的可用性。它的Go语言实现使其高效且资源占用低,适合在各种环境中部署。通过简单的JSON配置,你可以快速设置对多个端点的监控,并通过多种渠道接收告警。
对于更复杂的监控需求,你可能需要考虑Prometheus+Grafana等更全面的监控解决方案,但对于简单的API和网站监控,StatusOK提供了一个轻量级的选择。