commit
fbb75c3d35
@ -0,0 +1,8 @@
|
||||
/.idea
|
||||
/.idea/
|
||||
/.idea/*
|
||||
.idea
|
||||
.idea/
|
||||
.idea/*
|
||||
go.sum
|
||||
nogit.txt
|
||||
@ -0,0 +1,49 @@
|
||||
探测url的ssl证书过期时间 并发微信告警
|
||||
|
||||
## build on arm64
|
||||
```bash
|
||||
cd /root
|
||||
rm -rf remoteMonitor
|
||||
git clone https://git.sre.ink/go/remoteMonitor.git
|
||||
cd remoteMonitor
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 GO111MODULE=on go get github.com/shirou/gopsutil/v3/cpu@v3.21.12
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 GO111MODULE=on go get github.com/shirou/gopsutil/v3/disk@v3.21.12
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 GO111MODULE=on go mod tidy
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 GO111MODULE=on go build -v -a -o rtm main.go
|
||||
chmod +x rtm
|
||||
rm -rf /usr/bin/rtm
|
||||
mv rtm /usr/bin/
|
||||
rtm
|
||||
```
|
||||
|
||||
|
||||
## build
|
||||
```bash
|
||||
cd /root
|
||||
rm -rf remoteMonitor
|
||||
git clone https://git.sre.ink/go/remoteMonitor.git
|
||||
cd remoteMonitor
|
||||
GOPROXY="https://goproxy.cn,direct" go get github.com/shirou/gopsutil/v3/cpu@v3.21.12
|
||||
GOPROXY="https://goproxy.cn,direct" go get github.com/shirou/gopsutil/v3/disk@v3.21.12
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on GOPROXY="https://goproxy.cn,direct" go build -v -a -o rtm main.go
|
||||
chmod +x rtm
|
||||
rm -rf /usr/bin/rtm
|
||||
mv rtm /usr/bin/
|
||||
```
|
||||
|
||||
|
||||
## crontab
|
||||
```bash
|
||||
*/1 * * * * /usr/bin/rtm
|
||||
```
|
||||
|
||||
## usage
|
||||
```bash
|
||||
chmod +x /usr/bin/rtm
|
||||
rtm
|
||||
```
|
||||
|
||||
## build for arm64
|
||||
```bash
|
||||
CGO_ENABLED=0 GOARM=7 GOOS=linux GOARCH=arm64 GO111MODULE=on GOPROXY="https://goproxy.cn,direct" go build -v -a -o certmonitor main.go
|
||||
```
|
||||
@ -0,0 +1,23 @@
|
||||
module remoteMonitor
|
||||
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1449
|
||||
github.com/shirou/gopsutil/v3 v3.21.12
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/go-ole/go-ole v1.2.6 // indirect
|
||||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
|
||||
github.com/json-iterator/go v1.1.5 // indirect
|
||||
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.1 // indirect
|
||||
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
|
||||
github.com/tklauser/go-sysconf v0.3.9 // indirect
|
||||
github.com/tklauser/numcpus v0.3.0 // indirect
|
||||
github.com/yusufpapurcu/wmi v1.2.2 // indirect
|
||||
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c // indirect
|
||||
gopkg.in/ini.v1 v1.66.2 // indirect
|
||||
)
|
||||
@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"remoteMonitor/thirdpart"
|
||||
"remoteMonitor/util"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("获取主机信息")
|
||||
util.GetHostInfo()
|
||||
fmt.Println("获取CPU信息")
|
||||
|
||||
util.GetCpuInfo()
|
||||
fmt.Println("获取内存信息")
|
||||
|
||||
util.GetMemInfo()
|
||||
fmt.Println("获取系统负载")
|
||||
|
||||
util.GetSysLoad()
|
||||
fmt.Println("获取硬盘存储信息")
|
||||
util.GetDiskInfo()
|
||||
|
||||
if util.IsAlert {
|
||||
thirdpart.SendWechat(util.MSG)
|
||||
//thirdpart.SendToPhone("13152148384", "9999")
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,43 @@
|
||||
/**
|
||||
* https://next.api.aliyun.com/api-tools/sdk/Dysmsapi?spm=a2c4g.11186623.0.0.257a7218t3HRei&version=2017-05-25&language=go-tea
|
||||
*/
|
||||
|
||||
package thirdpart
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
|
||||
"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
|
||||
)
|
||||
|
||||
func SendToPhone(phone string, code string) bool {
|
||||
//调用阿里云短信接口发送短信 从配置中心拿配置项
|
||||
sign_name := "333333333333"
|
||||
template_code := "3333333333333333"
|
||||
app_key := "33333333333333333333333333"
|
||||
app_secret := "333333333333333333333333333333"
|
||||
region_id := "cn-hangzhou"
|
||||
|
||||
client, err := dysmsapi.NewClientWithAccessKey(region_id, app_key, app_secret)
|
||||
request := requests.NewCommonRequest()
|
||||
request.Method = "POST"
|
||||
request.Scheme = "https" // https | http
|
||||
request.Domain = "dysmsapi.aliyuncs.com"
|
||||
request.Version = "2017-05-25"
|
||||
request.ApiName = "SendSms"
|
||||
request.QueryParams["RegionId"] = region_id
|
||||
request.QueryParams["PhoneNumbers"] = phone //手机号
|
||||
request.QueryParams["SignName"] = sign_name //阿里云验证过的项目名 自己设置
|
||||
request.QueryParams["TemplateCode"] = template_code //阿里云的短信模板号 自己设置
|
||||
request.QueryParams["TemplateParam"] = "{\"code\":" + code + "}" //短信模板中的验证码内容。
|
||||
response, err := client.ProcessCommonRequest(request)
|
||||
fmt.Print(client.DoAction(request, response))
|
||||
// fmt.Print(response)
|
||||
if err != nil {
|
||||
fmt.Print(err.Error())
|
||||
return false
|
||||
}
|
||||
fmt.Printf("response is %#v\n", response)
|
||||
//json数据解析
|
||||
return true
|
||||
}
|
||||
@ -0,0 +1,114 @@
|
||||
package thirdpart
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
var WECOM_CID string = GetEnvDefault("WECOM_CID", "333333333333333333333")
|
||||
var WECOM_SECRET string = GetEnvDefault("WECOM_SECRET", "33333333333333333333333333")
|
||||
var WECOM_AID string = GetEnvDefault("WECOM_AID", "33333333333333")
|
||||
var WECOM_TOUID string = GetEnvDefault("WECOM_TOUID", "333333333333333333333")
|
||||
var REDIS_STAT string = GetEnvDefault("REDIS_STAT", "OFF")
|
||||
var REDIS_PASSWORD string = GetEnvDefault("REDIS_PASSWORD", "")
|
||||
var ctx = context.Background()
|
||||
|
||||
func GetEnvDefault(key, defVal string) string {
|
||||
val, ex := os.LookupEnv(key)
|
||||
if !ex {
|
||||
return defVal
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func praser_json(json_str string) map[string]interface{} {
|
||||
var wecom_response map[string]interface{}
|
||||
if string(json_str) != "" {
|
||||
err := json.Unmarshal([]byte(string(json_str)), &wecom_response)
|
||||
if err != nil {
|
||||
log.Println("生成json字符串错误")
|
||||
}
|
||||
}
|
||||
return wecom_response
|
||||
}
|
||||
|
||||
func get_token(corpid, app_secret string) string {
|
||||
resp, err := http.Get("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + app_secret)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
resp_data, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
token_response := praser_json(string(resp_data))
|
||||
return token_response["access_token"].(string)
|
||||
}
|
||||
|
||||
func post_msg(text_msg, msg_type, post_url string) string {
|
||||
type msg struct {
|
||||
Content string `json:"content"`
|
||||
}
|
||||
type JsonData struct {
|
||||
Touser string `json:"touser"`
|
||||
Agentid string `json:"agentid"`
|
||||
Msgtype string `json:"msgtype"`
|
||||
Text msg `json:"text"`
|
||||
Duplicate_check_interval int `json:"duplicate_check_interval"`
|
||||
}
|
||||
post_data := JsonData{
|
||||
Touser: WECOM_TOUID,
|
||||
Agentid: WECOM_AID,
|
||||
Msgtype: msg_type,
|
||||
Duplicate_check_interval: 600,
|
||||
Text: msg{Content: text_msg},
|
||||
}
|
||||
|
||||
post_json, _ := json.Marshal(post_data)
|
||||
log.Println(string(post_json))
|
||||
msg_req, err := http.NewRequest("POST", post_url, bytes.NewBuffer(post_json))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
msg_req.Header.Set("Content-Type", "application/json")
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(msg_req)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer msg_req.Body.Close()
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
return string(body)
|
||||
}
|
||||
|
||||
func IsZero(v interface{}) (bool, error) {
|
||||
t := reflect.TypeOf(v)
|
||||
if !t.Comparable() {
|
||||
return false, fmt.Errorf("type is not comparable: %v", t)
|
||||
}
|
||||
return v == reflect.Zero(t).Interface(), nil
|
||||
}
|
||||
|
||||
func SendWechat(msg string) {
|
||||
access_token := get_token(WECOM_CID, WECOM_SECRET)
|
||||
post_url := "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + access_token
|
||||
msg_type := "text"
|
||||
post_status := post_msg(msg, msg_type, post_url)
|
||||
log.Println(post_status)
|
||||
post_response := praser_json(string(post_status))
|
||||
log.Println(post_response)
|
||||
errcode := post_response["errcode"]
|
||||
_, err := IsZero(errcode)
|
||||
if err != nil {
|
||||
fmt.Printf("%v", err)
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetNetInfo(t *testing.T) {
|
||||
fmt.Println("获取网络信息")
|
||||
fmt.Println(GetNetInfo())
|
||||
|
||||
}
|
||||
Loading…
Reference in new issue