• 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 文件地址

    码云地址

  • 相关阅读:
    <置顶>Eclipse和myeclipse常用快捷键-操作-设置
    Eclipse : Loading descriptor for ...错误解决
    ORA-00937: 不是单组分组函数
    An error has occurred,See error log for more details 错误解决办法
    [Error Code: 942, SQL State: 42000] ORA-00942: 表或视图不存在
    ORA-00001: 违反唯一约束条件
    eclipse 出现user operation is waiting
    [空格][空白][特殊]字符/文字
    powerdesigner16.5安装教程及破解步骤
    mybatis遇到日期类型数据时String到date的转化
  • 原文地址:https://www.cnblogs.com/gooooodmorning/p/13564887.html
Copyright © 2020-2023  润新知