| @@ -99,29 +99,8 @@ type ResultInfo struct { | |||||
| //elk输出的json结构end | //elk输出的json结构end | ||||
| //发送post请求到elk | |||||
| func SendReqToElk(jsonStr []byte) (content string, err error) { | |||||
| ElkBase64Init := setting.ElkUser + ":" + setting.ElkPassword | |||||
| ElkBase64 := base64.StdEncoding.EncodeToString([]byte(ElkBase64Init)) | |||||
| BasicElkBase64 := "Basic" + " " + ElkBase64 | |||||
| url := setting.ElkUrl | |||||
| req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr)) | |||||
| req.Header.Set("Content-Type", "application/json") | |||||
| req.Header.Set("kbn-version", "7.13.2") | |||||
| req.Header.Set("Authorization", BasicElkBase64) | |||||
| client := &http.Client{} | |||||
| resp, err := client.Do(req) | |||||
| if err != nil { | |||||
| panic(err) | |||||
| } | |||||
| defer resp.Body.Close() | |||||
| body, _ := ioutil.ReadAll(resp.Body) | |||||
| return string(body), err | |||||
| } | |||||
| //处理返回的elk数据,只保留totalView,即访问量;loaded是分片载入次数,用来判断返回的数据是否准确 | //处理返回的elk数据,只保留totalView,即访问量;loaded是分片载入次数,用来判断返回的数据是否准确 | ||||
| func GetResultFromElk(resultinfo ResultInfo, jsonStr []byte) (loaded int, totalView int, err error) { | |||||
| func GetResultFromElk(resultInfo ResultInfo, jsonStr []byte) (loaded int, totalView int, err error) { | |||||
| ElkBase64Init := setting.ElkUser + ":" + setting.ElkPassword | ElkBase64Init := setting.ElkUser + ":" + setting.ElkPassword | ||||
| ElkBase64 := base64.StdEncoding.EncodeToString([]byte(ElkBase64Init)) | ElkBase64 := base64.StdEncoding.EncodeToString([]byte(ElkBase64Init)) | ||||
| BasicElkBase64 := "Basic" + " " + ElkBase64 | BasicElkBase64 := "Basic" + " " + ElkBase64 | ||||
| @@ -139,11 +118,10 @@ func GetResultFromElk(resultinfo ResultInfo, jsonStr []byte) (loaded int, totalV | |||||
| defer resp.Body.Close() | defer resp.Body.Close() | ||||
| body, _ := ioutil.ReadAll(resp.Body) | body, _ := ioutil.ReadAll(resp.Body) | ||||
| var resultTest ResultInfo | |||||
| errs := json.Unmarshal([]byte(string(body)), &resultTest) | |||||
| errs := json.Unmarshal([]byte(string(body)), &resultInfo) | |||||
| fmt.Println(errs) | fmt.Println(errs) | ||||
| return resultTest.Result.Loaded, resultTest.Result.RawResponse.Hits.Total, err | |||||
| return resultInfo.Result.Loaded, resultInfo.Result.RawResponse.Hits.Total, err | |||||
| } | } | ||||
| //初始化传给elk的数据结构,给定用户名和项目名,查询的起止时间,返回初始化后的结构 | //初始化传给elk的数据结构,给定用户名和项目名,查询的起止时间,返回初始化后的结构 | ||||