node-exporter新增自定义模块
业务效果
源码修改
1.添加源码模块
go 1.16以后不再需要把源码包存储到gopath目录下,可以放到服务器的任何目录下即可
package collector import ( "github.com/go-kit/log" "github.com/prometheus/client_golang/prometheus" "os/exec" "strconv" "strings" "fmt" ) const ( errLogSubSystem = "errlog" ) func init() { registerCollector(errLogSubSystem,defaultEnabled,NewErrLogCollector) } type errLogCollector struct { logger log.Logger } func NewErrLogCollector(logger log.Logger) (Collector,error) { return &errLogCollector{logger},nil } func errLogGrep() string { errLogCmd := `grep -i error /var/log/messages | awk '{a[$5]++}END{for(i in a) print i,a[i]}'` cmd := exec.Command("sh","-c",errLogCmd) output,_ := cmd.CombinedOutput() return string(output) } func (c *errLogCollector) Update( ch chan<- prometheus.Metric) error { var metricType prometheus.ValueType metricType = prometheus.GaugeValue output := errLogGrep() for _, line := range strings.Split(output,"\n") { l := strings.Split(line,":") if len(l) != 2 { continue } name := strings.TrimSpace(l[0]) value := strings.TrimSpace(l[1]) v, _ := strconv.Atoi(value) name = strings.Replace(name,"-","_",-1) ch <- prometheus.MustNewConstMetric( prometheus.NewDesc( prometheus.BuildFQName(namespace,errLogSubSystem,name), fmt.Sprintf("/var/log/message err log %s.", name), nil, nil, ), metricType, float64(v), ) } return nil }
2.编译访问
metrics数据已经被prometheus采集到
把prometheus的metrics数据导入到grafana中展示
blackbox_exporter配置
1.默认情况下一个exporter只能采集一个对应的服务,比如一个mysql_exporter只能采集一个mysql服务实例.当一台主机上同时运行了N个mysql服务的时候就需要装N个mysql_exporter。
2.使用黑盒exporter就可以只安装一个黑盒exporter同时采集多个服务实例的metrics数据
本文来自博客园,作者:不懂123,转载请注明原文链接:https://www.cnblogs.com/yxh168/p/15538582.html