2013-02-07 10:49:04 +00:00
|
|
|
// Copyright 2013 Prometheus Team
|
|
|
|
// 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.
|
|
|
|
|
2013-01-07 22:24:26 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-01-27 17:49:45 +00:00
|
|
|
"github.com/prometheus/prometheus/model"
|
2013-01-07 22:24:26 +00:00
|
|
|
"log"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Unfortunately, more global variables that are needed for parsing.
|
|
|
|
var tmpJobOptions = map[string]string{}
|
|
|
|
var tmpJobTargets = []Targets{}
|
|
|
|
var tmpTargetEndpoints = []string{}
|
|
|
|
var tmpTargetLabels = model.LabelSet{}
|
|
|
|
|
|
|
|
func configError(error string, v ...interface{}) {
|
|
|
|
message := fmt.Sprintf(error, v...)
|
|
|
|
log.Fatal(fmt.Sprintf("Line %v, char %v: %s", yyline, yypos, message))
|
|
|
|
}
|
|
|
|
|
|
|
|
func PushJobOption(option string, value string) {
|
|
|
|
tmpJobOptions[option] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
func PushJobTargets() {
|
|
|
|
targets := Targets{
|
|
|
|
Endpoints: tmpTargetEndpoints,
|
|
|
|
Labels: tmpTargetLabels,
|
|
|
|
}
|
|
|
|
tmpJobTargets = append(tmpJobTargets, targets)
|
|
|
|
tmpTargetLabels = model.LabelSet{}
|
|
|
|
tmpTargetEndpoints = []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func PushTargetEndpoints(endpoints []string) {
|
|
|
|
for _, endpoint := range endpoints {
|
|
|
|
tmpTargetEndpoints = append(tmpTargetEndpoints, endpoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func PushTargetLabels(labels model.LabelSet) {
|
|
|
|
for k, v := range labels {
|
|
|
|
tmpTargetLabels[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func PopJob() {
|
|
|
|
if err := parsedConfig.AddJob(tmpJobOptions, tmpJobTargets); err != nil {
|
|
|
|
configError(err.Error())
|
|
|
|
}
|
|
|
|
tmpJobOptions = map[string]string{}
|
|
|
|
tmpJobTargets = []Targets{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringToDuration(durationStr string) time.Duration {
|
2013-01-17 22:44:54 +00:00
|
|
|
durationRE := regexp.MustCompile("^([0-9]+)([ywdhms]+)$")
|
2013-01-07 22:24:26 +00:00
|
|
|
matches := durationRE.FindStringSubmatch(durationStr)
|
|
|
|
if len(matches) != 3 {
|
|
|
|
configError("Not a valid duration string: '%v'", durationStr)
|
|
|
|
}
|
|
|
|
value, _ := strconv.Atoi(matches[1])
|
|
|
|
unit := matches[2]
|
|
|
|
switch unit {
|
|
|
|
case "y":
|
|
|
|
value *= 60 * 60 * 24 * 365
|
2013-01-17 22:44:54 +00:00
|
|
|
case "w":
|
|
|
|
value *= 60 * 60 * 24 * 7
|
2013-01-07 22:24:26 +00:00
|
|
|
case "d":
|
|
|
|
value *= 60 * 60 * 24
|
|
|
|
case "h":
|
|
|
|
value *= 60 * 60
|
|
|
|
case "m":
|
|
|
|
value *= 60
|
|
|
|
}
|
|
|
|
return time.Duration(value) * time.Second
|
|
|
|
}
|