• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang metrics.Values函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/mackerelio/mackerel-agent/metrics.Values函数的典型用法代码示例。如果您正苦于以下问题:Golang Values函数的具体用法?Golang Values怎么用?Golang Values使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了Values函数的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: Generate

// Generate XXX
func (g *InterfaceGenerator) Generate() (metrics.Values, error) {
	prevValues, err := g.collectIntarfacesValues()
	if err != nil {
		return nil, err
	}

	time.Sleep(g.Interval)

	currValues, err := g.collectIntarfacesValues()
	if err != nil {
		return nil, err
	}

	ret := make(map[string]float64)
	for name, value := range prevValues {
		if !postInterfaceMetricsRegexp.MatchString(name) {
			continue
		}
		currValue, ok := currValues[name]
		if ok {
			ret[name+".delta"] = (currValue - value) / g.Interval.Seconds()
		}
	}

	return metrics.Values(ret), nil
}
开发者ID:lucasgalton,项目名称:sycub-agent,代码行数:27,代码来源:interface.go


示例2: Generate

// Generate XXX
func (g *FilesystemGenerator) Generate() (metrics.Values, error) {
	filesystems, err := util.CollectDfValues(dfColumnSpecs)
	if err != nil {
		return nil, err
	}

	ret := make(map[string]float64)
	for name, values := range filesystems {
		// https://github.com/docker/docker/blob/v1.5.0/daemon/graphdriver/devmapper/deviceset.go#L981
		if regexp.MustCompile(`^/dev/mapper/docker-`).FindStringSubmatch(name) != nil {
			continue
		}
		if matches := regexp.MustCompile(`^/dev/(.*)$`).FindStringSubmatch(name); matches != nil {
			device := regexp.MustCompile(`[^A-Za-z0-9_-]`).ReplaceAllString(matches[1], "_")
			for key, value := range values {
				intValue, valueTypeOk := value.(int64)
				if valueTypeOk {
					// kilo bytes -> bytes
					ret["filesystem."+device+"."+key] = float64(intValue) * 1024
				}
			}
		}
	}

	return metrics.Values(ret), nil
}
开发者ID:lucasgalton,项目名称:sycub-agent,代码行数:27,代码来源:filesystem.go


示例3: Generate

// Generate XXX
func (g *CPUUsageGenerator) Generate() (metrics.Values, error) {
	prevValues, prevTotal, _, err := g.collectProcStatValues()
	if err != nil {
		return nil, err
	}

	time.Sleep(g.Interval)

	currValues, currTotal, cpuCount, err := g.collectProcStatValues()
	if err != nil {
		return nil, err
	}

	ret := make(map[string]float64)
	for i, name := range cpuUsageMetricNames {
		// Values in /proc/stat differ in Linux kernel versions.
		// Not all metrics in cpuUsageMetricNames can be retrieved.
		// ref: `man 5 proc`
		if i >= len(currValues) || i >= len(prevValues) {
			break
		}

		// percentage of increased amount of CPU time
		ret[name+".percentage"] = (currValues[i] - prevValues[i]) * 100.0 * float64(cpuCount) / (currTotal - prevTotal)
	}

	return metrics.Values(ret), nil
}
开发者ID:lucasgalton,项目名称:sycub-agent,代码行数:29,代码来源:cpuusage.go


示例4: Generate

// Generate XXX
func (g *UptimeGenerator) Generate() (metrics.Values, error) {
	if g == nil {
		return nil, errors.New("UptimeGenerator is not initialized")
	}
	r, _, err := windows.GetTickCount.Call()
	if r == 0 {
		return nil, err
	}

	return metrics.Values(map[string]float64{"uptime": float64(r) / 1000}), nil
}
开发者ID:lucasgalton,项目名称:sycub-agent,代码行数:12,代码来源:uptime.go


示例5: collectIntarfacesValues

func (g *InterfaceGenerator) collectIntarfacesValues() (metrics.Values, error) {
	out, err := exec.Command("netstat", "-bni").Output()
	if err != nil {
		interfaceLogger.Errorf("Failed to invoke netstat: %s", err)
		return nil, err
	}

	lineScanner := bufio.NewScanner(bytes.NewReader(out))

	results := make(map[string]float64)
	hasNonZeroValue := false
	for lineScanner.Scan() {
		line := lineScanner.Text()
		fields := strings.Fields(line)
		name := regexp.MustCompile(`[^A-Za-z0-9_-]`).ReplaceAllString(regexp.MustCompile(`\*`).ReplaceAllString(fields[0], ""), "_")
		if match, _ := regexp.MatchString(`^lo\d+$`, name); match {
			continue
		}
		if match, _ := regexp.MatchString(`^<Link#\d+>$`, fields[2]); match {
			rxIndex, txIndex := getFieldIndex(fields)
			rxKey := fmt.Sprintf("interface.%s.rxBytes", name)
			rxValue, rxErr := strconv.ParseFloat(fields[rxIndex], 64)
			if rxErr != nil {
				interfaceLogger.Warningf("Failed to parse host interfaces: %s", err)
				break
			}
			results[rxKey] = rxValue
			if rxValue != 0 {
				hasNonZeroValue = true
			}
			txKey := fmt.Sprintf("interface.%s.txBytes", name)
			txValue, txErr := strconv.ParseFloat(fields[txIndex], 64)
			if txErr != nil {
				interfaceLogger.Warningf("Failed to parse host interfaces: %s", err)
				break
			}
			results[txKey] = txValue
			if txValue != 0 {
				hasNonZeroValue = true
			}
		}
	}
	// results (eth0) sample
	/** [%!s(*metrics.Value=&{interface.eth0.rxBytes 6.6074069281e+10})
	     %!s(*metrics.Value=&{interface.eth0.txBytes 9.180531994e+09})
	    ]
	**/
	if hasNonZeroValue {
		return metrics.Values(results), nil
	}
	return nil, nil
}
开发者ID:lucasgalton,项目名称:sycub-agent,代码行数:52,代码来源:interface.go


示例6: Generate

// Generate generate metrics values
func (g *MemoryGenerator) Generate() (metrics.Values, error) {
	file, err := os.Open("/proc/meminfo")
	if err != nil {
		memoryLogger.Errorf("Failed (skip these metrics): %s", err)
		return nil, err
	}
	scanner := bufio.NewScanner(file)

	ret := make(map[string]float64)
	used := float64(0)
	usedCnt := 0
	for scanner.Scan() {
		line := scanner.Text()
		for k, regexp := range memItems {
			if matches := regexp.FindStringSubmatch(line); matches != nil {
				// ex.) MemTotal:        3916792 kB
				// matches[1] = 3916792, matches[2] = kB
				if matches[2] != "kB" {
					memoryLogger.Warningf("/proc/meminfo contains an invalid unit: %s", k)
					break
				}
				value, err := strconv.ParseFloat(matches[1], 64)
				if err != nil {
					memoryLogger.Warningf("Failed to parse memory metrics: %s", err)
					break
				}
				ret["memory."+k] = value * 1024
				if k == "free" || k == "buffers" || k == "cached" {
					used -= value
					usedCnt++
				}
				if k == "total" {
					used += value
					usedCnt++
				}
				break
			}
		}
	}
	if err := scanner.Err(); err != nil {
		memoryLogger.Errorf("Failed (skip these metrics): %s", err)
		return nil, err
	}
	if usedCnt == 4 { // 4 is free, buffers, cached and total
		ret["memory.used"] = used * 1024
	}

	return metrics.Values(ret), nil
}
开发者ID:lucasgalton,项目名称:sycub-agent,代码行数:50,代码来源:memory.go


示例7: Generate

// Generate XXX
func (g *Loadavg5Generator) Generate() (metrics.Values, error) {
	contentbytes, err := ioutil.ReadFile("/proc/loadavg")
	if err != nil {
		loadavg5Logger.Errorf("Failed (skip these metrics): %s", err)
		return nil, err
	}
	content := string(contentbytes)
	cols := strings.Split(content, " ")

	f, err := strconv.ParseFloat(cols[1], 64)
	if err != nil {
		loadavg5Logger.Errorf("Failed to parse loadavg5 metrics (skip these metrics): %s", err)
		return nil, err
	}

	return metrics.Values(map[string]float64{"loadavg5": f}), nil
}
开发者ID:lucasgalton,项目名称:sycub-agent,代码行数:18,代码来源:loadavg5.go


示例8: Generate

// Generate XXX
func (g *FilesystemGenerator) Generate() (metrics.Values, error) {
	filesystems, err := windows.CollectFilesystemValues()
	if err != nil {
		return nil, err
	}

	ret := make(map[string]float64)
	for name, values := range filesystems {
		if matches := regexp.MustCompile(`^(.*):`).FindStringSubmatch(name); matches != nil {
			device := regexp.MustCompile(`[^A-Za-z0-9_-]`).ReplaceAllString(matches[1], "_")

			ret["filesystem."+device+".size"] = values.KbSize * 1024
			ret["filesystem."+device+".used"] = values.KbUsed * 1024
		}
	}

	logger.Debugf("%q", ret)

	return metrics.Values(ret), nil
}
开发者ID:y-kuno,项目名称:mackerel-agent,代码行数:21,代码来源:filesystem.go


示例9: parseNetdev

func parseNetdev(out []byte) (metrics.Values, error) {
	lineScanner := bufio.NewScanner(bytes.NewReader(out))
	results := make(map[string]float64)
	for lineScanner.Scan() {
		line := lineScanner.Text()
		if kv := strings.SplitN(line, ":", 2); len(kv) == 2 {
			name := sanitizerReg.ReplaceAllString(strings.TrimSpace(kv[0]), "_")
			if name == "lo" {
				continue
			}

			cols := strings.Fields(kv[1])
			if len(cols) < len(interfaceMetrics) {
				continue
			}

			interfaceResult := make(map[string]float64)
			hasNonZeroValue := false
			for i, metricName := range interfaceMetrics {
				key := fmt.Sprintf("interface.%s.%s", name, metricName)
				value, err := strconv.ParseFloat(cols[i], 64)
				if err != nil {
					interfaceLogger.Warningf("Failed to parse host interfaces: %s", err)
					break
				}
				if value != 0 {
					hasNonZeroValue = true
				}
				interfaceResult[key] = value
			}
			if hasNonZeroValue {
				for k, v := range interfaceResult {
					results[k] = v
				}
			}
		}
	}

	return metrics.Values(results), nil
}
开发者ID:y-kuno,项目名称:mackerel-agent,代码行数:40,代码来源:interface.go


示例10: generateValues

func generateValues(generators []metrics.Generator) chan metrics.Values {
	processed := make(chan metrics.Values)
	finish := make(chan bool)
	result := make(chan metrics.Values)

	go func() {
		allValues := metrics.Values(make(map[string]float64))
		for {
			select {
			case values := <-processed:
				allValues.Merge(values)
			case <-finish:
				result <- allValues
				return
			}
		}
	}()

	go func() {
		var wg sync.WaitGroup
		for _, g := range generators {
			wg.Add(1)
			go func(g metrics.Generator) {
				defer wg.Done()

				values, err := g.Generate()
				if err != nil {
					logger.Errorf("Failed to generate value in %T (skip this metric): %s", g, err.Error())
					return
				}
				processed <- values
			}(g)
		}
		wg.Wait()
		finish <- true // processed all jobs
	}()

	return result
}
开发者ID:lucasgalton,项目名称:sycub-agent,代码行数:39,代码来源:generator.go


示例11: Generate

// Generate generate swap values
func (g *SwapGenerator) Generate() (metrics.Values, error) {
	outBytes, err := exec.Command("sysctl", "vm.swapusage").Output()
	if err != nil {
		swapLogger.Errorf("Failed (skip these metrics): %s", err)
		return nil, err
	}

	out := string(outBytes)
	matches := swapReg.FindStringSubmatch(out)
	if matches == nil || len(matches) != 4 {
		return nil, fmt.Errorf("faild to parse vm.swapusage result: [%q]", out)
	}
	t, _ := strconv.ParseFloat(matches[1], 64)
	// swap_used are calculated at server, so don't send it
	// u, _ := strconv.ParseFloat(matches[2], 64)
	f, _ := strconv.ParseFloat(matches[3], 64)

	const mb = 1024.0 * 1024.0
	return metrics.Values(map[string]float64{
		"memory.swap_total": t * mb,
		"memory.swap_free":  f * mb,
	}), nil
}
开发者ID:lucasgalton,项目名称:sycub-agent,代码行数:24,代码来源:swap.go



注:本文中的github.com/mackerelio/mackerel-agent/metrics.Values函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang logger.DieIf函数代码示例发布时间:2022-05-23
下一篇:
Golang logging.GetLogger函数代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap