This repository has been archived by the owner on Feb 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
180 lines (147 loc) · 4.42 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// 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.
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"regexp"
"time"
"github.com/golang/protobuf/proto"
clientmodel "github.com/prometheus/client_golang/model"
"github.com/prometheus/log"
"gopkg.in/yaml.v2"
"github.com/prometheus/migrate/v0x13"
"github.com/prometheus/migrate/v0x14"
)
var outName = flag.String("out", "-", "Target for writing the output")
func usage() {
fmt.Fprintf(os.Stderr, "usage: %s [-out=<output_file>] [<config_file>]\n\n", os.Args[0])
flag.PrintDefaults()
os.Exit(2)
}
func main() {
flag.Usage = usage
flag.Parse()
var (
err error
in io.Reader = os.Stdin
out io.Writer = os.Stdout
)
if flag.NArg() > 0 {
filename := flag.Args()[0]
in, err = os.Open(filename)
if err != nil {
log.Fatalf("Error opening input file: %s", err)
}
log.Infof("Translating file %s", filename)
}
if *outName != "-" {
out, err = os.Create(*outName)
if err != nil {
log.Fatalf("Error creating output file: %s", err)
}
}
if err := translate(in, out); err != nil {
log.Fatal(err)
}
}
func translate(in io.Reader, out io.Writer) error {
b, err := ioutil.ReadAll(in)
if err != nil {
return err
}
var oldConf v0x13.Config
err = proto.UnmarshalText(string(b), &oldConf.PrometheusConfig)
if err != nil {
return fmt.Errorf("Error parsing old config file: %s", err)
}
var newGlobConf v0x14.GlobalConfig
newGlobConf.ScrapeInterval = v0x14.Duration(oldConf.ScrapeInterval())
// The global scrape timeout is new and will be set to 10 seconds. If the
// global scrape interval is shorter than 10s, set the timeout to the scrape interval.
if defTimeout := v0x14.Duration(10 * time.Second); newGlobConf.ScrapeInterval >= defTimeout {
newGlobConf.ScrapeTimeout = v0x14.Duration(10 * time.Second)
} else {
newGlobConf.ScrapeTimeout = newGlobConf.ScrapeInterval
}
newGlobConf.EvaluationInterval = v0x14.Duration(oldConf.EvaluationInterval())
var newConf v0x14.Config
newConf.GlobalConfig = &newGlobConf
if oldConf.Global != nil {
newConf.RuleFiles = oldConf.Global.GetRuleFile()
}
var scrapeConfs []*v0x14.ScrapeConfig
for _, oldJob := range oldConf.Jobs() {
scfg := &v0x14.ScrapeConfig{}
scfg.JobName = oldJob.GetName()
var firstScheme string
var firstPath string
for _, oldTG := range oldJob.TargetGroup {
newTG := &v0x14.TargetGroup{
Labels: clientmodel.LabelSet{},
}
for _, t := range oldTG.Target {
u, err := url.Parse(t)
if err != nil {
return err
}
if firstScheme == "" {
firstScheme = u.Scheme
} else if u.Scheme != firstScheme {
return fmt.Errorf("Multiple URL schemes in job not allowed.")
}
if firstPath == "" {
firstPath = u.Path
} else if u.Path != firstPath {
return fmt.Errorf("Multiple paths in job not allowed")
}
newTG.Targets = append(newTG.Targets, clientmodel.LabelSet{
clientmodel.AddressLabel: clientmodel.LabelValue(u.Host),
})
}
for _, lp := range oldTG.GetLabels().GetLabel() {
ln := clientmodel.LabelName(lp.GetName())
lv := clientmodel.LabelValue(lp.GetValue())
newTG.Labels[ln] = lv
}
scfg.TargetGroups = append(scfg.TargetGroups, newTG)
}
scfg.Scheme = firstScheme
if oldJob.SdName != nil {
dnscfg := &v0x14.DNSSDConfig{}
dnscfg.Names = []string{*oldJob.SdName}
dnscfg.RefreshInterval = v0x14.Duration(oldJob.SDRefreshInterval())
scfg.DNSSDConfigs = append(scfg.DNSSDConfigs, dnscfg)
}
scrapeConfs = append(scrapeConfs, scfg)
}
newConf.ScrapeConfigs = scrapeConfs
res, err := yaml.Marshal(newConf)
if err != nil {
return err
}
s := string(res)
// Surround hosts with spaces in output.
pat := regexp.MustCompile("- ([a-zA-Z-.]+:[0-9]+)\n")
s = pat.ReplaceAllString(s, "- '$1'\n")
pat = regexp.MustCompile("\n([a-z]|- j)")
s = pat.ReplaceAllString(s, "\n\n$1")
if _, err := out.Write([]byte(s)); err != nil {
return err
}
return nil
}