2019-05-22 23:16:55 +02:00
package main
import (
"flag"
2020-10-06 13:54:39 +02:00
"fmt"
2019-05-22 23:16:55 +02:00
"net/http"
2020-05-16 10:59:30 +02:00
"os"
2019-05-22 23:16:55 +02:00
"time"
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert"
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmselect"
2020-12-14 12:08:22 +01:00
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmselect/promql"
2019-05-22 23:16:55 +02:00
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmstorage"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/buildinfo"
2020-02-10 12:26:18 +01:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/envflag"
2020-12-25 15:40:20 +01:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil"
2019-11-12 15:18:09 +01:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fs"
2019-05-22 23:16:55 +02:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/procutil"
2020-11-25 21:59:13 +01:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape"
2020-02-10 12:03:52 +01:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
2019-05-22 23:16:55 +02:00
)
2020-02-10 12:03:52 +01:00
var (
httpListenAddr = flag . String ( "httpListenAddr" , ":8428" , "TCP address to listen for http connections" )
2022-05-02 20:35:14 +02:00
minScrapeInterval = flag . Duration ( "dedup.minScrapeInterval" , 0 , "Leave only the last sample in every time series per each discrete interval " +
2021-12-15 12:18:04 +01:00
"equal to -dedup.minScrapeInterval > 0. See https://docs.victoriametrics.com/#deduplication and https://docs.victoriametrics.com/#downsampling" )
2020-11-25 21:59:13 +01:00
dryRun = flag . Bool ( "dryRun" , false , "Whether to check only -promscrape.config and then exit. " +
2022-02-08 14:37:38 +01:00
"Unknown config entries aren't allowed in -promscrape.config by default. This can be changed with -promscrape.config.strictParse=false command-line flag" )
2020-02-10 12:03:52 +01:00
)
2019-05-22 23:16:55 +02:00
func main ( ) {
2020-05-16 10:59:30 +02:00
// Write flags and help message to stdout, since it is easier to grep or pipe.
flag . CommandLine . SetOutput ( os . Stdout )
2020-12-25 15:40:20 +01:00
flag . Usage = usage
2020-02-10 12:26:18 +01:00
envflag . Parse ( )
2019-05-22 23:16:55 +02:00
buildinfo . Init ( )
logger . Init ( )
2020-11-25 21:59:13 +01:00
if promscrape . IsDryRun ( ) {
* dryRun = true
}
if * dryRun {
if err := promscrape . CheckConfig ( ) ; err != nil {
logger . Fatalf ( "error when checking -promscrape.config: %s" , err )
}
logger . Infof ( "-promscrape.config is ok; exitting with 0 status code" )
return
}
2019-12-19 13:32:08 +01:00
logger . Infof ( "starting VictoriaMetrics at %q..." , * httpListenAddr )
2019-05-22 23:16:55 +02:00
startTime := time . Now ( )
2021-12-14 19:49:08 +01:00
storage . SetDedupInterval ( * minScrapeInterval )
2020-12-14 12:08:22 +01:00
vmstorage . Init ( promql . ResetRollupResultCacheIfNeeded )
2019-05-22 23:16:55 +02:00
vmselect . Init ( )
vminsert . Init ( )
2020-01-25 18:19:23 +01:00
startSelfScraper ( )
2019-05-22 23:16:55 +02:00
go httpserver . Serve ( * httpListenAddr , requestHandler )
2020-01-22 17:27:44 +01:00
logger . Infof ( "started VictoriaMetrics in %.3f seconds" , time . Since ( startTime ) . Seconds ( ) )
2019-05-22 23:16:55 +02:00
sig := procutil . WaitForSigterm ( )
logger . Infof ( "received signal %s" , sig )
2020-01-25 18:19:23 +01:00
stopSelfScraper ( )
2019-05-22 23:16:55 +02:00
logger . Infof ( "gracefully shutting down webservice at %q" , * httpListenAddr )
startTime = time . Now ( )
if err := httpserver . Stop ( * httpListenAddr ) ; err != nil {
logger . Fatalf ( "cannot stop the webservice: %s" , err )
}
vminsert . Stop ( )
2020-01-22 17:27:44 +01:00
logger . Infof ( "successfully shut down the webservice in %.3f seconds" , time . Since ( startTime ) . Seconds ( ) )
2019-05-22 23:16:55 +02:00
vmstorage . Stop ( )
vmselect . Stop ( )
2019-11-12 15:18:09 +01:00
fs . MustStopDirRemover ( )
2020-01-22 17:27:44 +01:00
logger . Infof ( "the VictoriaMetrics has been stopped in %.3f seconds" , time . Since ( startTime ) . Seconds ( ) )
2019-05-22 23:16:55 +02:00
}
func requestHandler ( w http . ResponseWriter , r * http . Request ) bool {
2020-12-14 12:36:48 +01:00
if r . URL . Path == "/" {
2021-04-02 21:54:06 +02:00
if r . Method != "GET" {
return false
}
2022-03-21 09:13:28 +01:00
w . Header ( ) . Add ( "Content-Type" , "text/html; charset=utf-8" )
2021-04-30 08:19:08 +02:00
fmt . Fprintf ( w , "<h2>Single-node VictoriaMetrics</h2></br>" )
2021-04-20 19:16:17 +02:00
fmt . Fprintf ( w , "See docs at <a href='https://docs.victoriametrics.com/'>https://docs.victoriametrics.com/</a></br>" )
2021-04-30 08:19:08 +02:00
fmt . Fprintf ( w , "Useful endpoints:</br>" )
httpserver . WriteAPIHelp ( w , [ ] [ 2 ] string {
2021-12-02 12:51:49 +01:00
{ "vmui" , "Web UI" } ,
2022-06-06 23:57:05 +02:00
{ "targets" , "status for discovered active targets" } ,
{ "service-discovery" , "labels before and after relabeling for discovered targets" } ,
2021-12-02 12:51:49 +01:00
{ "api/v1/targets" , "advanced information about discovered targets in JSON format" } ,
{ "config" , "-promscrape.config contents" } ,
{ "metrics" , "available service metrics" } ,
{ "flags" , "command-line flags" } ,
{ "api/v1/status/tsdb" , "tsdb status page" } ,
{ "api/v1/status/top_queries" , "top queries" } ,
{ "api/v1/status/active_queries" , "active queries" } ,
2020-12-14 12:36:48 +01:00
} )
2020-10-06 13:54:39 +02:00
return true
}
2019-05-22 23:16:55 +02:00
if vminsert . RequestHandler ( w , r ) {
return true
}
if vmselect . RequestHandler ( w , r ) {
return true
}
if vmstorage . RequestHandler ( w , r ) {
return true
}
return false
}
2020-12-14 12:36:48 +01:00
2020-12-25 15:40:20 +01:00
func usage ( ) {
const s = `
victoria - metrics is a time series database and monitoring solution .
2021-04-20 19:16:17 +02:00
See the docs at https : //docs.victoriametrics.com/
2020-12-25 15:40:20 +01:00
`
flagutil . Usage ( s )
}