You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
3.1 KiB
120 lines
3.1 KiB
package main
|
|
|
|
import (
|
|
"github.com/jnovack/flag"
|
|
"fmt"
|
|
log "github.com/sirupsen/logrus"
|
|
"net/http"
|
|
"github.com/denzs/wsjtx_dashboards/shared/wsjtx"
|
|
"github.com/hpcloud/tail"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
var station string
|
|
var pathin string
|
|
var metricpath string
|
|
var mysql_host string
|
|
var mysql_db string
|
|
var mysql_user string
|
|
var mysql_pass string
|
|
var mysql_table string
|
|
var port int
|
|
//var promcalls bool
|
|
var trace bool
|
|
var useProm bool
|
|
var useMysql bool
|
|
|
|
func usage() {
|
|
fmt.Printf("Usage of %s:\n", os.Args[0])
|
|
flag.PrintDefaults()
|
|
}
|
|
|
|
func init() {
|
|
flag.StringVar(&station, "station", "localstation", "your callsign or wsjtx instance identifier")
|
|
flag.StringVar(&pathin, "in", "/wsjtx/ALL.TXT", "path to WSJT-X ALL.TXT")
|
|
flag.StringVar(&mysql_host, "host", "db", "name/ip of mysql host")
|
|
flag.StringVar(&mysql_db, "db", "digimode_stats", "db name")
|
|
flag.StringVar(&mysql_user, "user", "wsjtx", "mysql username")
|
|
flag.StringVar(&mysql_pass, "pass", "secret", "mysql password")
|
|
flag.StringVar(&mysql_table, "table", "wsjtx_all_txt", "mysql table name")
|
|
flag.StringVar(&metricpath, "metricpath", "/metrics", "path for prometheus metric endpoint")
|
|
flag.IntVar(&port, "port", 2112, "port for prometheus metric endpoint")
|
|
flag.BoolVar(&useProm, "prometheus", false, "activate prometheus exporter")
|
|
flag.BoolVar(&useMysql, "mysql", false, "activate mysql exporter")
|
|
// flag.BoolVar(&promcalls, "promcalls", false, "activate prometheus callsign metrics")
|
|
flag.BoolVar(&trace, "trace", false, "log almost everything")
|
|
|
|
flag.Parse()
|
|
|
|
formatter := &log.TextFormatter{
|
|
FullTimestamp: true,
|
|
}
|
|
log.SetFormatter(formatter)
|
|
|
|
if trace {
|
|
log.SetLevel(log.TraceLevel)
|
|
log.Info("trace logging enabled")
|
|
} else {
|
|
log.Info("normal logging enabled")
|
|
}
|
|
|
|
if !useProm && !useMysql {
|
|
usage()
|
|
log.Fatal("you have to enable at least one exporter. see -mysql and -prometheus flags")
|
|
}
|
|
|
|
if useProm {
|
|
log.Info("prometheus exporter enabled..")
|
|
}
|
|
|
|
if useMysql {
|
|
log.Info("mysql exporter enabled..")
|
|
|
|
// wait for stupid mysql container to come up..
|
|
_, db_down := dbConn()
|
|
for db_down {
|
|
log.Info("waiting for db to come up..")
|
|
time.Sleep(2 * time.Second)
|
|
_, db_down = dbConn()
|
|
}
|
|
|
|
init_db()
|
|
}
|
|
}
|
|
|
|
func followFile(results chan wsjtx.Result) {
|
|
log.Printf("start following %s", pathin)
|
|
|
|
t, _:= tail.TailFile(pathin, tail.Config{Follow: true, Location: &tail.SeekInfo{Offset: 0, Whence: 2},})
|
|
for line := range t.Lines {
|
|
result, parsed := wsjtx.ScanLine(line.Text)
|
|
if parsed {
|
|
results <- result
|
|
} else {
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
|
|
func handleResults(results chan wsjtx.Result) {
|
|
for {
|
|
select {
|
|
case result := <- results :
|
|
handlePrometheus(result)
|
|
handleMysql(result)
|
|
}
|
|
}
|
|
}
|
|
|
|
func main(){
|
|
results := make(chan wsjtx.Result)
|
|
|
|
go followFile(results)
|
|
go handleResults(results)
|
|
|
|
log.Infof("listening on :%d%s",port, metricpath)
|
|
http.Handle(metricpath, promhttp.Handler())
|
|
http.ListenAndServe(fmt.Sprintf(":%d",port), nil)
|
|
}
|
|
|