alert.go 2.7 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7
package admin

import (
	"net/http"
	"strconv"
	"strings"

黄孟柱 已提交
8 9 10
	"github.com/eolinker/goku-api-gateway/console/controller"
	alert_module "github.com/eolinker/goku-api-gateway/console/module/alert"
	"github.com/eolinker/goku-api-gateway/utils"
E
eoLinker API Management 已提交
11 12
)

Y
Your Name 已提交
13
// AddAlertMsg 新增告警信息
E
eoLinker API Management 已提交
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
func AddAlertMsg(httpResponse http.ResponseWriter, httpRequest *http.Request) {

	nodeIP := httpRequest.RemoteAddr
	requestID := httpRequest.PostFormValue("requestID")
	apiName := httpRequest.PostFormValue("apiName")
	requestURL := httpRequest.PostFormValue("requestURL")
	targetServer := httpRequest.PostFormValue("targetServer")
	targetURL := httpRequest.PostFormValue("targetURL")
	requestMethod := httpRequest.PostFormValue("requestMethod")
	proxyMethod := httpRequest.PostFormValue("proxyMethod")
	alertPeriodType := httpRequest.PostFormValue("alertPeriodType")
	alertCount := httpRequest.PostFormValue("alertCount")
	headerList := httpRequest.PostFormValue("headerList")
	queryParamList := httpRequest.PostFormValue("queryParamList")
	formParamList := httpRequest.PostFormValue("formParamList")
	responseHeaderList := httpRequest.PostFormValue("responseHeaderList")
	responseStatus := httpRequest.PostFormValue("responseStatus")
	isAlert := httpRequest.PostFormValue("isAlert")
	clusterName := httpRequest.PostFormValue("clusterName")
	nodePort := httpRequest.PostFormValue("nodePort")
	apiID := httpRequest.PostFormValue("apiID")
	strategyID := httpRequest.PostFormValue("strategyID")
	strategyName := httpRequest.PostFormValue("strategyName")

	ip := utils.InterceptIP(nodeIP, ":") + ":" + nodePort
	if realIP := strings.TrimSpace(httpRequest.Header.Get("X-Real-Ip")); realIP != "" {
		ip = realIP + ":" + nodePort
	}
	period, err := strconv.Atoi(alertPeriodType)
	if err != nil {
		controller.WriteError(httpResponse,
			"330002",
			"alert",
			"[ERROR]Illegal alertPeriodType!",
			err)
		return
	}
	count, err := strconv.Atoi(alertCount)
	if err != nil {
		controller.WriteError(httpResponse,
			"330003",
			"alert",
			"[ERROR]Illegal alertCount!",
			err)
		return
	}
	status, err := strconv.Atoi(responseStatus)
	if err != nil {
		controller.WriteError(httpResponse,
			"330004",
			"alert",
			"[ERROR]Illegal responseStatus!",
			err)
		return
	}
	alert := false
	if isAlert == "true" {
		alert = true
	}

	flag, result, err := alert_module.AddAlertMsg(apiID, apiName, requestURL, targetServer, targetURL, requestMethod, proxyMethod, headerList, queryParamList, formParamList, responseHeaderList, strategyID, strategyName, requestID, period, count, status, alert, ip, clusterName)
	if !flag {
		controller.WriteError(httpResponse,
			"330000",
			"alert",
			result,
			err)
		return
	}

	controller.WriteResultInfo(httpResponse, "alert", "", nil)
}