2012-11-26 11:11:34 -08:00
|
|
|
// Copyright 2012 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.
|
|
|
|
|
2012-11-24 03:33:34 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-01-12 12:22:59 -08:00
|
|
|
"code.google.com/p/gorest"
|
2013-01-22 10:32:56 -08:00
|
|
|
"flag"
|
2013-01-04 08:55:58 -08:00
|
|
|
"github.com/matttproud/golang_instrumentation"
|
2013-01-12 12:22:59 -08:00
|
|
|
"github.com/matttproud/prometheus/api"
|
2013-01-07 14:24:26 -08:00
|
|
|
"github.com/matttproud/prometheus/config"
|
2012-12-25 04:50:36 -08:00
|
|
|
"github.com/matttproud/prometheus/retrieval"
|
2013-01-07 14:24:26 -08:00
|
|
|
"github.com/matttproud/prometheus/rules"
|
|
|
|
"github.com/matttproud/prometheus/rules/ast"
|
2012-11-26 10:56:51 -08:00
|
|
|
"github.com/matttproud/prometheus/storage/metric/leveldb"
|
2012-12-11 11:46:16 -08:00
|
|
|
"log"
|
2013-01-04 08:55:58 -08:00
|
|
|
"net/http"
|
2012-12-11 11:46:16 -08:00
|
|
|
"os"
|
2013-01-06 14:30:46 -08:00
|
|
|
"os/signal"
|
2012-11-24 03:33:34 -08:00
|
|
|
)
|
|
|
|
|
2013-01-22 10:32:56 -08:00
|
|
|
// Commandline flags.
|
|
|
|
var (
|
|
|
|
configFile = flag.String("configFile", "prometheus.conf", "Prometheus configuration file name.")
|
|
|
|
metricsStoragePath = flag.String("metricsStoragePath", "/tmp/metrics", "Base path for metrics storage.")
|
|
|
|
)
|
|
|
|
|
2012-11-24 03:33:34 -08:00
|
|
|
func main() {
|
2013-01-22 10:32:56 -08:00
|
|
|
flag.Parse()
|
|
|
|
conf, err := config.LoadFromFile(*configFile)
|
2013-01-07 14:24:26 -08:00
|
|
|
if err != nil {
|
2013-01-22 10:32:56 -08:00
|
|
|
log.Fatalf("Error loading configuration from %s: %v", configFile, err)
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
|
2013-01-22 10:32:56 -08:00
|
|
|
persistence, err := leveldb.NewLevelDBMetricPersistence(*metricsStoragePath)
|
2012-12-11 11:46:16 -08:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2013-01-06 14:30:46 -08:00
|
|
|
go func() {
|
|
|
|
notifier := make(chan os.Signal)
|
|
|
|
signal.Notify(notifier, os.Interrupt)
|
|
|
|
<-notifier
|
2013-01-07 14:24:26 -08:00
|
|
|
persistence.Close()
|
2013-01-06 14:30:46 -08:00
|
|
|
os.Exit(0)
|
2012-12-11 11:46:16 -08:00
|
|
|
}()
|
|
|
|
|
2013-01-07 14:24:26 -08:00
|
|
|
defer persistence.Close()
|
2013-01-06 14:30:46 -08:00
|
|
|
|
2013-01-07 14:24:26 -08:00
|
|
|
scrapeResults := make(chan retrieval.Result, 4096)
|
2013-01-04 05:41:47 -08:00
|
|
|
|
2013-01-07 14:24:26 -08:00
|
|
|
targetManager := retrieval.NewTargetManager(scrapeResults, 1)
|
|
|
|
targetManager.AddTargetsFromConfig(conf)
|
2012-12-25 04:50:36 -08:00
|
|
|
|
2013-01-07 14:24:26 -08:00
|
|
|
ruleResults := make(chan *rules.Result, 4096)
|
|
|
|
|
|
|
|
ast.SetPersistence(persistence)
|
|
|
|
ruleManager := rules.NewRuleManager(ruleResults, conf.Global.EvaluationInterval)
|
|
|
|
err = ruleManager.AddRulesFromConfig(conf)
|
|
|
|
if err != nil {
|
2013-01-22 10:32:56 -08:00
|
|
|
log.Fatalf("Error loading rule files: %v", err)
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2012-12-25 04:50:36 -08:00
|
|
|
|
2013-01-04 08:55:58 -08:00
|
|
|
go func() {
|
2013-01-12 12:22:59 -08:00
|
|
|
gorest.RegisterService(new(api.MetricsService))
|
2013-01-04 08:55:58 -08:00
|
|
|
exporter := registry.DefaultRegistry.YieldExporter()
|
2013-01-10 17:27:03 -08:00
|
|
|
|
2013-01-12 12:22:59 -08:00
|
|
|
http.Handle("/", gorest.Handle())
|
2013-01-04 08:55:58 -08:00
|
|
|
http.Handle("/metrics.json", exporter)
|
2013-01-12 12:22:59 -08:00
|
|
|
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
|
2013-01-04 08:55:58 -08:00
|
|
|
http.ListenAndServe(":9090", nil)
|
|
|
|
}()
|
|
|
|
|
2013-01-04 05:41:47 -08:00
|
|
|
for {
|
2013-01-07 14:24:26 -08:00
|
|
|
select {
|
|
|
|
case scrapeResult := <-scrapeResults:
|
2013-01-10 17:27:03 -08:00
|
|
|
//fmt.Printf("scrapeResult -> %s\n", scrapeResult)
|
2013-01-07 14:24:26 -08:00
|
|
|
for _, sample := range scrapeResult.Samples {
|
|
|
|
persistence.AppendSample(&sample)
|
|
|
|
}
|
|
|
|
case ruleResult := <-ruleResults:
|
2013-01-10 17:27:03 -08:00
|
|
|
//fmt.Printf("ruleResult -> %s\n", ruleResult)
|
2013-01-07 14:24:26 -08:00
|
|
|
for _, sample := range ruleResult.Samples {
|
|
|
|
persistence.AppendSample(sample)
|
|
|
|
}
|
2012-12-25 04:50:36 -08:00
|
|
|
}
|
2012-11-24 03:33:34 -08:00
|
|
|
}
|
|
|
|
}
|