2023-09-18 08:08:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-10-31 16:02:52 +00:00
|
|
|
"bytes"
|
2023-09-30 16:01:43 +00:00
|
|
|
"flag"
|
2023-10-01 16:10:11 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2023-09-30 16:01:43 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
2023-10-31 16:02:52 +00:00
|
|
|
"net/http"
|
2023-09-30 16:01:43 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
2023-10-01 16:10:11 +00:00
|
|
|
"tianji-reporter/utils"
|
|
|
|
"time"
|
2023-09-18 08:08:16 +00:00
|
|
|
)
|
|
|
|
|
2023-10-01 16:10:11 +00:00
|
|
|
type ReportData struct {
|
|
|
|
WorkspaceId string `json:"workspaceId"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Hostname string `json:"hostname"`
|
2023-10-01 16:17:36 +00:00
|
|
|
Timeout int `json:"timeout"` // if service receive after timeout second, its means client are offline
|
2023-10-01 16:10:11 +00:00
|
|
|
Payload utils.ReportDataPayload `json:"payload"`
|
|
|
|
}
|
|
|
|
|
2023-09-18 08:08:16 +00:00
|
|
|
var (
|
2024-01-08 18:06:32 +00:00
|
|
|
Mode = flag.String("mode", "http", "The send mode of report data, you can select: 'http' or 'udp', default is 'http'")
|
2023-09-30 16:01:43 +00:00
|
|
|
Url = flag.String("url", "", "The http url of tianji, for example: https://tianji.msgbyte.com")
|
|
|
|
WorkspaceId = flag.String("workspace", "", "The workspace id for tianji, this should be a uuid")
|
|
|
|
Name = flag.String("name", "", "The identification name for this machine")
|
2023-11-21 15:01:25 +00:00
|
|
|
Interval = flag.Int("interval", 5.0, "Input the INTERVAL, seconed")
|
2023-09-30 16:01:43 +00:00
|
|
|
IsVnstat = flag.Bool("vnstat", false, "Use vnstat for traffic statistics, linux only")
|
2023-09-18 08:08:16 +00:00
|
|
|
)
|
|
|
|
|
2023-10-31 16:02:52 +00:00
|
|
|
var version = "1.0.0"
|
|
|
|
|
2023-09-18 08:08:16 +00:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2023-09-30 16:01:43 +00:00
|
|
|
parsedURL, err := url.Parse(*Url)
|
2023-09-18 08:08:16 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Invalid URL:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if parsedURL.Scheme == "" {
|
|
|
|
log.Fatal("Invalid URL: Missing scheme")
|
|
|
|
}
|
|
|
|
|
2023-09-30 16:01:43 +00:00
|
|
|
if *WorkspaceId == "" {
|
2023-09-18 08:08:16 +00:00
|
|
|
log.Fatal("WORKSPACE_ID must not be blank!")
|
|
|
|
}
|
|
|
|
|
2023-09-30 16:01:43 +00:00
|
|
|
hostname, _ := os.Hostname()
|
|
|
|
var name string
|
|
|
|
if *Name != "" {
|
|
|
|
name = *Name
|
|
|
|
} else {
|
|
|
|
name = hostname
|
|
|
|
}
|
|
|
|
|
2023-10-01 16:10:11 +00:00
|
|
|
interval := *Interval
|
|
|
|
|
|
|
|
ticker := time.Tick(time.Duration(interval) * time.Second)
|
|
|
|
|
2023-10-31 16:02:52 +00:00
|
|
|
log.Println("Start reporting...")
|
|
|
|
log.Println("Mode:", *Mode)
|
|
|
|
log.Println("Version:", version)
|
|
|
|
|
2023-10-01 16:10:11 +00:00
|
|
|
for {
|
2024-05-11 06:38:55 +00:00
|
|
|
log.Println("Sending report data to:", parsedURL.String())
|
2023-10-31 16:02:52 +00:00
|
|
|
payload := ReportData{
|
2023-10-01 16:10:11 +00:00
|
|
|
WorkspaceId: *WorkspaceId,
|
|
|
|
Name: name,
|
|
|
|
Hostname: hostname,
|
2024-06-05 13:01:34 +00:00
|
|
|
Timeout: interval * 5,
|
2023-10-01 16:10:11 +00:00
|
|
|
Payload: utils.GetReportDataPaylod(interval, *IsVnstat),
|
2023-10-31 16:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if *Mode == "udp" {
|
|
|
|
sendUDPPack(*parsedURL, payload)
|
|
|
|
} else {
|
|
|
|
sendHTTPRequest(*parsedURL, payload)
|
|
|
|
}
|
2023-10-03 16:49:54 +00:00
|
|
|
|
|
|
|
<-ticker
|
2023-10-01 16:10:11 +00:00
|
|
|
}
|
2023-09-30 16:01:43 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 16:02:52 +00:00
|
|
|
/**
|
|
|
|
* Send UDP Pack to report server data
|
|
|
|
*/
|
|
|
|
func sendUDPPack(url url.URL, payload ReportData) {
|
2023-09-30 16:01:43 +00:00
|
|
|
// parse target url
|
|
|
|
addr, err := net.ResolveUDPAddr("udp", url.Hostname()+":"+url.Port())
|
|
|
|
if err != nil {
|
2023-10-01 16:10:11 +00:00
|
|
|
log.Println("Error resolving address:", err)
|
2023-09-30 16:01:43 +00:00
|
|
|
return
|
|
|
|
}
|
2023-09-18 08:08:16 +00:00
|
|
|
|
2023-09-30 16:01:43 +00:00
|
|
|
// create UDP connection
|
|
|
|
conn, err := net.DialUDP("udp", nil, addr)
|
|
|
|
if err != nil {
|
2023-10-01 16:10:11 +00:00
|
|
|
log.Println("Error creating connection:", err)
|
2023-09-30 16:01:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
// serialized message
|
2023-10-01 16:10:11 +00:00
|
|
|
jsonData, err := jsoniter.Marshal(payload)
|
|
|
|
log.Printf("[Report] %s\n", jsonData)
|
2023-09-30 16:01:43 +00:00
|
|
|
if err != nil {
|
2023-10-01 16:10:11 +00:00
|
|
|
log.Println("Error encoding JSON:", err)
|
2023-09-30 16:01:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send message
|
|
|
|
_, err = conn.Write(jsonData)
|
|
|
|
if err != nil {
|
2023-10-01 16:10:11 +00:00
|
|
|
log.Println("Error sending message:", err)
|
2023-09-30 16:01:43 +00:00
|
|
|
return
|
|
|
|
}
|
2023-09-18 08:08:16 +00:00
|
|
|
|
2023-10-01 16:10:11 +00:00
|
|
|
log.Println("Message sent successfully!")
|
2023-09-18 08:08:16 +00:00
|
|
|
}
|
2023-10-31 16:02:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send HTTP Request to report server data
|
|
|
|
*/
|
|
|
|
func sendHTTPRequest(_url url.URL, payload ReportData) {
|
|
|
|
jsonData, err := jsoniter.Marshal(payload)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error encoding JSON:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Printf("[Report] %s\n", jsonData)
|
|
|
|
|
|
|
|
reportUrl, err := url.JoinPath(_url.String(), "/serverStatus/report")
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Join url error:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", reportUrl, bytes.NewBuffer(jsonData))
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Create request error:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.Header.Set("x-tianji-report-version", version)
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2023-10-31 16:44:01 +00:00
|
|
|
log.Println("Send request error:", err)
|
2023-10-31 16:02:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// Read response
|
|
|
|
body := new(bytes.Buffer)
|
|
|
|
_, err = body.ReadFrom(resp.Body)
|
|
|
|
if err != nil {
|
2023-10-31 16:44:01 +00:00
|
|
|
log.Println("Read response error:", err)
|
2023-10-31 16:02:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Response:", body)
|
|
|
|
}
|