2015-09-26 08:36:40 -07:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-01-25 04:28:58 -08:00
|
|
|
// +build !notextfile
|
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-09-03 07:59:56 -07:00
|
|
|
"sort"
|
2015-01-25 04:28:58 -08:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2015-03-21 17:16:14 -07:00
|
|
|
"github.com/golang/protobuf/proto"
|
2015-09-17 05:05:56 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/common/expfmt"
|
2015-05-28 12:21:44 -07:00
|
|
|
"github.com/prometheus/log"
|
2015-03-21 17:16:14 -07:00
|
|
|
|
2015-09-17 05:05:56 -07:00
|
|
|
dto "github.com/prometheus/client_model/go"
|
2015-01-25 04:28:58 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-02-18 09:15:57 -08:00
|
|
|
textFileDirectory = flag.String("collector.textfile.directory", "", "Directory to read text files with metrics from.")
|
2015-01-25 04:28:58 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
type textFileCollector struct {
|
2015-09-03 07:59:56 -07:00
|
|
|
path string
|
2015-01-25 04:28:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Factories["textfile"] = NewTextFileCollector
|
|
|
|
}
|
|
|
|
|
2015-05-20 11:04:49 -07:00
|
|
|
// Takes a registers a
|
2015-01-25 04:28:58 -08:00
|
|
|
// SetMetricFamilyInjectionHook.
|
2015-05-20 11:04:49 -07:00
|
|
|
func NewTextFileCollector() (Collector, error) {
|
2015-09-03 07:59:56 -07:00
|
|
|
c := &textFileCollector{
|
|
|
|
path: *textFileDirectory,
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.path == "" {
|
2015-01-25 04:28:58 -08:00
|
|
|
// This collector is enabled by default, so do not fail if
|
|
|
|
// the flag is not passed.
|
2015-05-28 12:21:44 -07:00
|
|
|
log.Infof("No directory specified, see --textfile.directory")
|
2015-01-25 04:28:58 -08:00
|
|
|
} else {
|
2015-09-03 07:59:56 -07:00
|
|
|
prometheus.SetMetricFamilyInjectionHook(c.parseTextFiles)
|
2015-01-25 04:28:58 -08:00
|
|
|
}
|
|
|
|
|
2015-09-03 07:59:56 -07:00
|
|
|
return c, nil
|
2015-01-25 04:28:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// textFile collector works via SetMetricFamilyInjectionHook in parseTextFiles.
|
|
|
|
func (c *textFileCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-03 07:59:56 -07:00
|
|
|
func (c *textFileCollector) parseTextFiles() []*dto.MetricFamily {
|
2015-01-25 04:28:58 -08:00
|
|
|
error := 0.0
|
2015-10-11 08:05:40 -07:00
|
|
|
var metricFamilies []*dto.MetricFamily
|
2015-01-25 04:28:58 -08:00
|
|
|
mtimes := map[string]time.Time{}
|
|
|
|
|
|
|
|
// Iterate over files and accumulate their metrics.
|
2015-09-03 07:59:56 -07:00
|
|
|
files, err := ioutil.ReadDir(c.path)
|
|
|
|
if err != nil && c.path != "" {
|
|
|
|
log.Errorf("Error reading textfile collector directory %s: %s", c.path, err)
|
|
|
|
error = 1.0
|
|
|
|
}
|
2015-01-25 04:28:58 -08:00
|
|
|
for _, f := range files {
|
|
|
|
if !strings.HasSuffix(f.Name(), ".prom") {
|
|
|
|
continue
|
|
|
|
}
|
2015-09-03 07:59:56 -07:00
|
|
|
path := filepath.Join(c.path, f.Name())
|
2015-01-25 04:28:58 -08:00
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
2015-05-28 12:21:44 -07:00
|
|
|
log.Errorf("Error opening %s: %v", path, err)
|
2015-01-25 04:28:58 -08:00
|
|
|
error = 1.0
|
|
|
|
continue
|
|
|
|
}
|
2015-09-17 05:05:56 -07:00
|
|
|
var parser expfmt.TextParser
|
|
|
|
parsedFamilies, err := parser.TextToMetricFamilies(file)
|
2015-01-25 04:28:58 -08:00
|
|
|
if err != nil {
|
2015-05-28 12:21:44 -07:00
|
|
|
log.Errorf("Error parsing %s: %v", path, err)
|
2015-01-25 04:28:58 -08:00
|
|
|
error = 1.0
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Only set this once it has been parsed, so that
|
|
|
|
// a failure does not appear fresh.
|
|
|
|
mtimes[f.Name()] = f.ModTime()
|
|
|
|
for _, mf := range parsedFamilies {
|
|
|
|
if mf.Help == nil {
|
|
|
|
help := fmt.Sprintf("Metric read from %s", path)
|
|
|
|
mf.Help = &help
|
|
|
|
}
|
|
|
|
metricFamilies = append(metricFamilies, mf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Export the mtimes of the successful files.
|
|
|
|
if len(mtimes) > 0 {
|
|
|
|
mtimeMetricFamily := dto.MetricFamily{
|
|
|
|
Name: proto.String("node_textfile_mtime"),
|
|
|
|
Help: proto.String("Unixtime mtime of textfiles successfully read."),
|
|
|
|
Type: dto.MetricType_GAUGE.Enum(),
|
|
|
|
Metric: []*dto.Metric{},
|
|
|
|
}
|
2015-09-03 07:59:56 -07:00
|
|
|
|
|
|
|
// Sorting is needed for predictable output comparison in tests.
|
|
|
|
filenames := make([]string, 0, len(mtimes))
|
|
|
|
for filename := range mtimes {
|
|
|
|
filenames = append(filenames, filename)
|
|
|
|
}
|
|
|
|
sort.Strings(filenames)
|
|
|
|
|
|
|
|
for _, filename := range filenames {
|
2015-01-25 04:28:58 -08:00
|
|
|
mtimeMetricFamily.Metric = append(mtimeMetricFamily.Metric,
|
|
|
|
&dto.Metric{
|
|
|
|
Label: []*dto.LabelPair{
|
|
|
|
&dto.LabelPair{
|
|
|
|
Name: proto.String("file"),
|
2015-09-03 07:59:56 -07:00
|
|
|
Value: proto.String(filename),
|
2015-01-25 04:28:58 -08:00
|
|
|
},
|
|
|
|
},
|
2015-09-03 07:59:56 -07:00
|
|
|
Gauge: &dto.Gauge{Value: proto.Float64(float64(mtimes[filename].UnixNano()) / 1e9)},
|
2015-01-25 04:28:58 -08:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
metricFamilies = append(metricFamilies, &mtimeMetricFamily)
|
|
|
|
}
|
|
|
|
// Export if there were errors.
|
|
|
|
metricFamilies = append(metricFamilies, &dto.MetricFamily{
|
|
|
|
Name: proto.String("node_textfile_scrape_error"),
|
|
|
|
Help: proto.String("1 if there was an error opening or reading a file, 0 otherwise"),
|
|
|
|
Type: dto.MetricType_GAUGE.Enum(),
|
|
|
|
Metric: []*dto.Metric{
|
|
|
|
&dto.Metric{
|
|
|
|
Gauge: &dto.Gauge{Value: &error},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return metricFamilies
|
|
|
|
}
|