龙芯下适配prometheus

1、环境准备

      golang: yum install golang-1.18

      nodejs: yum install nodejs

      npm:  升级到最新版本 npm install -g npm@latest

2、go环境配置

     export GOPATH=/opt/gocode

     go env -w GOPROXY=http://goproxy.loongnix.cn:3000,direct

     go env -w GOSUMDB=off

3、源码准备

    promu源码:https://github.com/prometheus/promu   这个是官方提供的编译工具  先需要把它在龙芯电脑上编译好

    prometheus源码: https://github.com/prometheus/prometheus/releases/tag/v2.23.0

4、先编译promu

   解压后直接make build。

   可能遇到的问题:

go: downloading golang.org/x/tools v0.0.0-20190611222205-d73e1c7e250b
    go: downloading golang.org/x/sys v0.0.0-20210331175145-43e1dd70ce54
    verifying golang.org/x/sys@v0.0.0-20210331175145-43e1dd70ce54: checksum mismatch
        downloaded: h1:5HZZhvdNN0O5KldlyABQ6eFwlX3k1RRGy0D+JtnQ0zU=
        go.sum:     h1:rF3Ohx8DRyl8h2zw9qojyLHLhrJpEMgyPOImREEryf0=

解决办法:已经设置了 go env -w GOSUMDB=off,仍然出现以下校验错误,则删除 go.sum 文件,如果还报错missing....,执行go mod tidy

  5、编译prometheus

$ mkdir -p $GOPATH/src/github.com/prometheus
$ cd $GOPATH/src/github.com/prometheus

将下载的代码解压到此文件夹中,然后进入执行以下命令:
$ cd prometheus
$ go mod vendor

然后将上文编译的promu可执行文件,拷贝至prometheus项目根目录

批改Makefile.common,应用本地的promu工具

...
$(PROMU):
    #$(eval PROMU_TMP := $(shell mktemp -d))
    #curl -s -L $(PROMU_URL) | tar -xvzf - -C $(PROMU_TMP)
    #mkdir -p $(FIRST_GOPATH)/bin
    #cp $(PROMU_TMP)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM)/promu $(FIRST_GOPATH)/bin/promu
    #rm -r $(PROMU_TMP)
    cp ./promu $(FIRST_GOPATH)/bin/promu
.PHONY: proto
...

然后回到prometheus根目录, 执行make build

以上配置完成后,编译中可能报错一:

# github.com/prometheus/procfs
vendor/github.com/prometheus/procfs/cpuinfo.go:71:9: undefined: parseCPUInfo

解决办法:

cd vendor/github.com/prometheus/procfs/

新建cpuinfo_loong64.go  内容如下:
package procfs
var parseCPUInfo = parseCPUInfoLoong64

修改cpuinfo.go 增加方法:
func parseCPUInfoLoong64(info []byte) ([]CPUInfo, error) {
        scanner := bufio.NewScanner(bytes.NewReader(info))

        // find the first "processor" line
        firstLine := firstNonEmptyLine(scanner)
        if !strings.HasPrefix(firstLine, "system type") || !strings.Contains(firstLine, ":") {
                return nil, errors.New("invalid cpuinfo file: " + firstLine)
        }
        field := strings.SplitN(firstLine, ": ", 2)
        cpuinfo := []CPUInfo{}
        systemType := field[1]

        i := 0

        for scanner.Scan() {
                line := scanner.Text()
                if !strings.Contains(line, ":") {
                        continue
                }
                field := strings.SplitN(line, ": ", 2)
                switch strings.TrimSpace(field[0]) {
                case "processor":
                        v, err := strconv.ParseUint(field[1], 0, 32)
                        if err != nil {
                                return nil, err
                        }
                        i = int(v)
                        cpuinfo = append(cpuinfo, CPUInfo{}) // start of the next processor
                        cpuinfo[i].Processor = uint(v)
                        cpuinfo[i].VendorID = systemType
                case "cpu model":
                        cpuinfo[i].ModelName = field[1]
                case "BogoMIPS":
                        v, err := strconv.ParseFloat(field[1], 64)
                        if err != nil {
                                return nil, err
                        }
                        cpuinfo[i].BogoMips = v
                }
        }
        return cpuinfo, nil
}

6、运行

编译完成后,在根目录下先件prometheus.yaml文件,内容如下(本文件仅用于测试编译结果,生产环境需要根据实际情况配置):

global:
  scrape_interval:     15s 
  evaluation_interval: 15s
alerting:
  alertmanagers:
  - static_configs:
    - targets:
rule_files:
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

然后运行即可:

./prometheus

打开浏览器测试:

prometheus主界面  http://localhost:9090/
prometheus指标界面  http://localhost:9090/metrics
prometheus报警界面  http://localhost:9090/alerts

 

 

     

  

posted @ 2022-08-10 14:31  silent  阅读(293)  评论(6编辑  收藏  举报