使用 Grafana和Prometheus 对程序实现监控

  • 首先搭建Grafana和Prometheus环境。 (这里为了方便使用docker 进行部署)
    • 启动node-exporter
      docker run -d -p 9100:9100 \
        -v "/proc:/host/proc:ro" \
        -v "/sys:/host/sys:ro" \
        -v "/:/rootfs:ro" \
        --net="host" \
        prom/node-exporter
      http://192.168.3.10:9100/metrics 查看服务是否起来

       

       

    • 启动Prometheus

      •  新建目录prometheus,编辑配置文件prometheus.yml
        mkdir /opt/prometheus
        cd /opt/prometheus/
        vim prometheus.yml
        global:
          scrape_interval:     60s
          evaluation_interval: 60s
         
        scrape_configs:
          - job_name: prometheus
            static_configs:
              - targets: ['localhost:9090']
                labels:
                  instance: prometheus
         
          - job_name: linux
            static_configs:
              - targets: ['192.168.3.10:9100']
                labels:
                  instance: localhost
      • 上面的targets 是你部署的node-exporter 节点  需要新增监控则修改yml文件进行添加即可
      • 使用docker 启动 Prometheus
        docker run  -d \
          -p 9090:9090 \
          -v /opt/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml  \
          prom/prometheus
      •   惯例查看是否起来http://192.168.3.10:9090/graph
    • 启动grafana
      #新建空文件夹grafana-storage,用来存储数据
      mkdir /opt/grafana-storage
      
      #设置权限 (一般设置读写权限即可)
      chmod 777 -R /opt/grafana-storage
      
      #启动grafana
      docker run -d \
        -p 3000:3000 \
        --name=grafana \
        -v /opt/grafana-storage:/var/lib/grafana \
        grafana/grafana
  • 具体配置grafana 参考 https://www.cnblogs.com/wx170119/p/12418835.html
  • 使用go(client_golang) 对站点内存进行监控
    •   首先编写监控代码
      package main
      
      import (
          "log"
          "net/http"
          "os"
          "time"
      
          "github.com/prometheus/client_golang/prometheus"
          "github.com/prometheus/client_golang/prometheus/promhttp"
          "github.com/shirou/gopsutil/mem"
      )
      
      func main() {
          //初始化日志服务
          logger := log.New(os.Stdout, "[Memory]", log.Lshortfile|log.Ldate|log.Ltime)
      
          //初始一个http handler
          http.Handle("/metrics", promhttp.Handler())
      
          //初始化一个容器
          diskPercent := prometheus.NewGaugeVec(prometheus.GaugeOpts{
              Name: "memeory_percent",
              Help: "memeory use percent",
          },
              []string{"percent"},
          )
          prometheus.MustRegister(diskPercent)
      
          // 启动web服务,监听8080端口
          go func() {
              logger.Println("ListenAndServe at:0.0.0.0:8080")
              err := http.ListenAndServe("192.168.3.87:8080", nil)
      
              if err != nil {
                  logger.Fatal("ListenAndServe: ", err)
              }
          }()
      
          //收集内存使用的百分比
          for {
              logger.Println("start collect memory used percent!")
              v, err := mem.VirtualMemory()
              if err != nil {
                  logger.Println("get memeory use percent error:%s", err)
              }
              usedPercent := v.UsedPercent
              logger.Println("get memeory use percent:", usedPercent)
              diskPercent.WithLabelValues("usedMemory").Set(usedPercent)
              time.Sleep(time.Second * 2)
          }
      }

      使用go build . 命令进行打包然后把应用程序跑起来。(这里的环境是3.87)

    • 进入到3.10 Prometheus部署环境  修改yml文件。添加名为 gotest 的 job
      global:
        scrape_interval:     60s
        evaluation_interval: 60s
      
      scrape_configs:
        - job_name: prometheus
          static_configs:
            - targets: ['localhost:9090']
              labels:
                instance: prometheus
      
        - job_name: linux
          static_configs:
            - targets: ['192.168.3.10:9100']
              labels:
                instance: localhost
      
        - job_name: gotest
          static_configs:
            - targets: ['192.168.3.87:8080']
              labels:
                instance: windows test

       

    • 重启Prometheus   使用docker restart 【name】
    • 访问http://192.168.3.10:9090/targets进行查看

       

       

    • 使用grafana配置刚才新增的监控到dashbord
    • http://192.168.3.10:3000/datasources 

       

       

    • 添加data source  数据源后save
    •  

       

    • 添加dashboard 

       

       

posted @ 2020-11-17 10:49  雨V幕  阅读(518)  评论(0编辑  收藏  举报