代码拉取完成,页面将自动刷新
同步操作将从 Gitee 极速下载/mymon 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
/*
* Open-Falcon
*
* Copyright (c) 2014-2018 Xiaomi, Inc. All Rights Reserved.
*
* This product is licensed to you under the Apache License, Version 2.0 (the "License").
* You may not use this product except in compliance with the License.
*
* This product may include a number of subcomponents with separate copyright notices
* and license terms. Your use of these subcomponents is subject to the terms and
* conditions of the subcomponent's license, as noted in the LICENSE file.
*/
package main
import (
"bufio"
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"os"
"reflect"
"regexp"
"sort"
"strings"
"github.com/open-falcon/mymon/common"
)
// SendData Post the json of all result to falcon-agent
func SendData(conf *common.Config, data []*MetaData) ([]byte, error) {
data = filterIgnoreData(conf, data)
js, err := json.Marshal(data)
if err != nil {
return nil, err
}
Log.Info("Send to %s, size: %d", conf.Base.FalconClient, len(data))
for _, m := range data {
Log.Info("%v", m)
}
res, err := http.Post(conf.Base.FalconClient, "Content-Type: application/json", bytes.NewBuffer(js))
if err != nil {
return nil, err
}
defer func() { _ = res.Body.Close() }()
return ioutil.ReadAll(res.Body)
}
func parseLine(line string) map[string]string {
var parseRes map[string]string
// remove space and \n
line = strings.Replace(strings.TrimSpace(line), "\n", "", -1)
// match metric, tag and value
reMetricTagValue, _ := regexp.Compile("^([0-9A-Za-z_,]+)" + TagSplitChar + "?([0-9A-Za-z_,=]*)" + ValueSplitChar + "?([0-9A-Za-z_]*)$")
matchMetricTagValue := reMetricTagValue.FindSubmatch([]byte(line))
if len(matchMetricTagValue) > 0 {
parseRes = map[string]string{
"metric": string(matchMetricTagValue[1]),
"tag": string(matchMetricTagValue[2]),
"value": string(matchMetricTagValue[3]),
}
} else {
Log.Info("Error format of ignorefile: %s", line)
}
return parseRes
}
func filterIgnoreData(conf *common.Config, data []*MetaData) []*MetaData {
ignoreFile := conf.Base.IgnoreFile
if ignoreFile == "" {
return data
}
f, err := os.OpenFile(ignoreFile, os.O_RDONLY, 0644)
// ignorefile does not exists
if err != nil {
return data
}
inputReader := bufio.NewReader(f)
for {
// get a line
line, err := inputReader.ReadString('\n')
// over if EOF
if err != nil {
break
}
// jump head
if strings.Contains(line, "FalconIgnore") {
continue
}
// jump annotation
if strings.HasPrefix(line, "#") {
continue
}
metricTagValue := parseLine(line)
metric := metricTagValue["metric"]
// wrong format of ignorefile
if metric == "" {
continue
}
tag := metricTagValue["tag"]
value := metricTagValue["value"]
for i, eachData := range data {
if metric != eachData.Metric && metric != "*" {
continue
}
if tag != "" && !tagSame(tag, eachData.Tags) {
continue
}
if value != "" {
data[i].SetValue(value)
continue
}
eachData.SetName("_" + eachData.Metric)
}
}
return data
}
func tagSame(tag1, tag2 string) bool {
x, y := strings.Split(tag1, ","), strings.Split(tag2, ",")
sort.Strings(x)
sort.Strings(y)
return reflect.DeepEqual(x, y)
}
// Snapshot make a record of note, some metric should be noted before sending
func Snapshot(conf *common.Config, note string, fileNameDay string, fileNameOldDay string) error {
if conf.Base.SnapshotDay < 0 {
// Just remind but do not stop
Log.Info("snapshot_day setted error!")
}
f, err := os.OpenFile(fileNameDay, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(note)
if err != nil {
return err
}
e := os.Remove(fileNameOldDay)
if e != nil {
// Just remind but do not stop
Log.Info("Error remove %s, %s", fileNameOldDay, e.Error())
}
return err
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。