2017-09-17 03:26:19 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2019-01-17 13:26:16 +01:00
|
|
|
"github.com/bdlm/log"
|
|
|
|
"github.com/spf13/cobra"
|
2017-09-17 03:26:19 +02:00
|
|
|
|
2018-01-03 15:41:40 +01:00
|
|
|
allDatabase "github.com/FreifunkBremen/yanic/database/all"
|
2017-09-17 03:26:19 +02:00
|
|
|
"github.com/FreifunkBremen/yanic/rrd"
|
|
|
|
"github.com/FreifunkBremen/yanic/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
// importCmd represents the import command
|
|
|
|
var importCmd = &cobra.Command{
|
2018-01-17 20:20:35 +01:00
|
|
|
Use: "import <file.rrd> <site> <domain>",
|
2017-09-17 03:26:19 +02:00
|
|
|
Short: "Imports global statistics from the given RRD files, requires InfluxDB",
|
2018-01-17 20:20:35 +01:00
|
|
|
Example: "yanic import --config /etc/yanic.toml olddata.rrd global global",
|
|
|
|
Args: cobra.ExactArgs(3),
|
2017-09-17 03:26:19 +02:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
path := args[0]
|
2017-11-21 15:12:06 +01:00
|
|
|
site := args[1]
|
2018-01-17 20:20:35 +01:00
|
|
|
domain := args[2]
|
2019-01-24 02:54:21 +01:00
|
|
|
config := &ServeConfig{}
|
|
|
|
if err := ReadConfigFile(configPath, config); err != nil {
|
|
|
|
log.Panicf("unable to load config file: %s", err)
|
|
|
|
}
|
2017-09-17 03:26:19 +02:00
|
|
|
|
2018-01-03 15:41:40 +01:00
|
|
|
err := allDatabase.Start(config.Database)
|
2017-09-17 03:26:19 +02:00
|
|
|
if err != nil {
|
2019-01-17 13:26:16 +01:00
|
|
|
log.Panicf("could not connect to database: %s", err)
|
2017-09-17 03:26:19 +02:00
|
|
|
}
|
2018-01-03 15:41:40 +01:00
|
|
|
defer allDatabase.Close()
|
2017-09-17 03:26:19 +02:00
|
|
|
|
2019-01-17 13:26:16 +01:00
|
|
|
log.Infof("importing RRD from %s", path)
|
2017-09-17 03:26:19 +02:00
|
|
|
|
|
|
|
for ds := range rrd.Read(path) {
|
2018-01-19 04:31:33 +01:00
|
|
|
allDatabase.Conn.InsertGlobals(
|
2017-09-17 03:26:19 +02:00
|
|
|
&runtime.GlobalStats{
|
|
|
|
Nodes: uint32(ds.Nodes),
|
|
|
|
Clients: uint32(ds.Clients),
|
|
|
|
},
|
|
|
|
ds.Time,
|
2017-11-21 15:12:06 +01:00
|
|
|
site,
|
2018-01-17 20:20:35 +01:00
|
|
|
domain,
|
2017-09-17 03:26:19 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RootCmd.AddCommand(importCmd)
|
|
|
|
importCmd.Flags().StringVarP(&configPath, "config", "c", "config.toml", "Path to configuration file")
|
|
|
|
}
|