request.go 3.0 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
package cmd

import (
	"errors"
	"io/ioutil"
	"net/http"
	"net/url"
	"strconv"
	"strings"
	"time"

	node_common "github.com/eolinker/goku/goku-node/node-common"
)
// 新增报警信息
func AddAlertMessage(apiID int, apiName, requestURL, targetServer, targetURL, requestMethod, proxyMethod, headerList, queryParamList, formParamList, responseHeaderList string, alertPeriodType, alertCount, responseStatus int, isAlert string, strategyID string, strategyName, requestID string) (bool, string, error) {
	client := &http.Client{
		Timeout: time.Millisecond * 700,
	}
	var data url.Values = url.Values{}
	data.Add("requestID", requestID)
	data.Add("apiID", strconv.Itoa(apiID))
	data.Add("apiName", apiName)
	data.Add("requestURL", requestURL)
	data.Add("targetServer", targetServer)
	data.Add("targetURL", targetURL)
	data.Add("clusterName", node_common.ClusterName())
	data.Add("requestMethod", requestMethod)
	data.Add("proxyMethod", proxyMethod)
	data.Add("headerList", headerList)
	data.Add("queryParamList", queryParamList)
	data.Add("formParamList", formParamList)
	data.Add("responseHeaderList", responseHeaderList)
	data.Add("alertPeriodType", strconv.Itoa(alertPeriodType))
	data.Add("alertCount", strconv.Itoa(alertCount))
	data.Add("responseStatus", strconv.Itoa(responseStatus))
	data.Add("isAlert", isAlert)
	data.Add("strategyID", strategyID)
	data.Add("strategyName", strategyName)
	data.Add("nodePort", strconv.Itoa(node_common.ListenPort))
	request, err := http.NewRequest("POST", node_common.GetAdminUrl("/alert/msg/add"), strings.NewReader(data.Encode()))
	request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	if err != nil {

		return false, "[ERROR]Fail to create request!", err
	}
	response, err := client.Do(request)
	if err != nil {

		return false, "[ERROR]Fail to get response!", err
	}
	defer response.Body.Close()
	_, err = ioutil.ReadAll(response.Body)
	if err != nil {

		return false, "[ERROR]Fail to get body!", err
	}

	return true, "", nil
}

func SendRequestToAlertAddress(alertAddress, requestURL, targetServer, proxyURL, msg, apiName string, apiID int) (bool, string, error) {
	if alertAddress == "" {
		return false, "[ERROR] Illegal alertAddress!", errors.New("[ERROR] Illegal alertAddress!")
	} else {
		_, err := url.Parse(alertAddress)
		if err != nil {
			return false, err.Error(), err
		}
	}
	client := &http.Client{
		Timeout: time.Millisecond * 700,
	}
	now := time.Now().Format("2006-01-02 15:04:05")
	var data url.Values = url.Values{}
	data.Add("requestURL", requestURL)
	data.Add("targetServer", targetServer)
	data.Add("targetURL", proxyURL)
	data.Add("alertTime", now)
	data.Add("apiName", apiName)
	data.Add("apiID", strconv.Itoa(apiID))
	data.Add("msg", msg)
	request, err := http.NewRequest("POST", alertAddress, strings.NewReader(data.Encode()))
	request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	if err != nil {
		return false, "[ERROR]Fail to create request!", err
	}
	_, err = client.Do(request)
	if err != nil {
		return false, "[ERROR]Fail to get response!", err
	}
	return true, "", nil
}