Ejemplo n.º 1
0
func (c *processesCollector) Update(ch chan<- prometheus.Metric) error {
	processes, err := procfs.AllProcs()
	if err != nil {
		return fmt.Errorf("failed to get processes: %s", err)
	}

	for _, process := range processes {
		cmd, err := process.Comm()
		if err != nil {
			log.Debugf("Failed to get process command: %s", err)
			continue
		}

		stats, err := process.NewStat()
		if err != nil {
			log.Debugf("Failed to get process stats: %s", err)
			continue
		}

		// skip processes with empty stats
		if stats.ResidentMemory() == 0 || stats.CPUTime() == 0 {
			log.Debugf("Skipping process %s due to empty stats", cmd)
			continue
		}

		c.metrics[0].(*prometheus.GaugeVec).WithLabelValues(cmd).Set(float64(stats.ResidentMemory()))
		c.metrics[1].(*prometheus.GaugeVec).WithLabelValues(cmd).Set(stats.CPUTime())
	}

	for _, c := range c.metrics {
		c.Collect(ch)
	}

	return err
}
Ejemplo n.º 2
0
func (ps ProcStatus) procStatusMetrics() []metric.Metric {
	procs, err := procfs.AllProcs()
	if err != nil {
		ps.log.Warn("Error getting processes: ", err)
		return nil
	}

	ret := []metric.Metric{}

	for _, proc := range procs {
		cmd, err := proc.CmdLine()
		if err != nil {
			ps.log.Warn("Error getting command line: ", err)
			continue
		}

		if ps.matches(cmd, proc.Comm) {
			ret = append(ret, ps.getMetrics(proc, cmd)...)
		}
	}

	return ret
}
Ejemplo n.º 3
0
func (ps ProcStatus) procStatusMetrics() []metric.Metric {
	procs, err := procfs.AllProcs()
	if err != nil {
		ps.log.Warn("Error getting processes: ", err)
		return nil
	}

	ret := []metric.Metric{}

	for _, proc := range procs {
		cmd, err := proc.CmdLine()
		if err != nil {
			ps.log.Warn("Error getting command line: ", err)
			continue
		}

		if len(ps.processName) == 0 || len(cmd) > 0 && strings.Contains(cmd[0], ps.processName) {
			ret = append(ret, ps.getMetrics(proc, cmd)...)
		}
	}

	return ret
}