zoukankan      html  css  js  c++  java
  • Promethues mysql_exporter 集中式监控

    Promethues mysql_exporter 集中式监控

    集中式优点

    1. 无须在每个mysql主机安装mysql_exporter
    2. 对于云数据库,无法安装mysql_exporter更适用
    3. 对于自动化监控更适合(只需API接口进行添加与删除即可完成整个生命周期)

    下载mysql_exporter

    mysql_exporter

    监控 mysql 主要是 dsn 变量

    dsn = fmt.Sprintf("%s:%s@tcp(%s:%d)/", "root", "123456", target, 3306)

    这里测试写死了 mysql 用与监控的账号与密码,可以用接口获取账户密码其他方式自由发挥

    修改mysql_exporter.go

    1. newHandler 函数修改
    func newHandler(metrics collector.Metrics, scrapers []collector.Scraper, logger log.Logger) http.HandlerFunc {
    	return func(w http.ResponseWriter, r *http.Request) {
    		filteredScrapers := scrapers
    		// -------------- 添加部分 ---------------- //
    		v:=r.URL.Query()
    		params := v["collect[]"]
    		target := v.Get("target")
    		dsn = fmt.Sprintf("%s:%s@tcp(%s:%d)/", "root", "123456", target, 3306)
    		level.Info(logger).Log("dsn",  dsn)
    		// -------------- 添加部分 ---------------- //
    		// Use request context for cancellation when connection gets closed.
    		ctx := r.Context()
    		// If a timeout is configured via the Prometheus header, add it to the context.
    		if v := r.Header.Get("X-Prometheus-Scrape-Timeout-Seconds"); v != "" {
    			timeoutSeconds, err := strconv.ParseFloat(v, 64)
    			if err != nil {
    				level.Error(logger).Log("msg", "Failed to parse timeout from Prometheus header", "err", err)
    			} else {
    				if *timeoutOffset >= timeoutSeconds {
    					// Ignore timeout offset if it doesn't leave time to scrape.
    					level.Error(logger).Log("msg", "Timeout offset should be lower than prometheus scrape timeout", "offset", *timeoutOffset, "prometheus_scrape_timeout", timeoutSeconds)
    				} else {
    					// Subtract timeout offset from timeout.
    					timeoutSeconds -= *timeoutOffset
    				}
    				// Create new timeout context with request context as parent.
    				var cancel context.CancelFunc
    				ctx, cancel = context.WithTimeout(ctx, time.Duration(timeoutSeconds*float64(time.Second)))
    				defer cancel()
    				// Overwrite request with timeout context.
    				r = r.WithContext(ctx)
    			}
    		}
    
    1. main 函数修改

    mark

    效果展示

    mark
    mark

    mysql_exporter.go 文件地址

    码云地址

  • 相关阅读:
    CSS对 网页进行布局
    myeclipse配置tomcat服务器
    JDBC链接数据库
    java集合框架
    java日期操作
    二叉树的最近公共祖先
    二叉树的深度
    飞地的数量
    岛屿数量
    岛屿的最大面积
  • 原文地址:https://www.cnblogs.com/gooooodmorning/p/13564887.html
Copyright © 2011-2022 走看看