yanic/cmd/import.go

50 lines
1.1 KiB
Go
Raw Permalink Normal View History

2017-09-17 01:26:19 +00:00
package cmd
import (
"log"
2018-01-03 14:41:40 +00:00
allDatabase "github.com/FreifunkBremen/yanic/database/all"
2017-09-17 01:26:19 +00:00
"github.com/FreifunkBremen/yanic/rrd"
"github.com/FreifunkBremen/yanic/runtime"
"github.com/spf13/cobra"
)
// importCmd represents the import command
var importCmd = &cobra.Command{
Use: "import <file.rrd> <site> <domain>",
2017-09-17 01:26:19 +00:00
Short: "Imports global statistics from the given RRD files, requires InfluxDB",
Example: "yanic import --config /etc/yanic.toml olddata.rrd global global",
Args: cobra.ExactArgs(3),
2017-09-17 01:26:19 +00:00
Run: func(cmd *cobra.Command, args []string) {
path := args[0]
site := args[1]
domain := args[2]
2017-09-17 01:26:19 +00:00
config := loadConfig()
2018-01-03 14:41:40 +00:00
err := allDatabase.Start(config.Database)
2017-09-17 01:26:19 +00:00
if err != nil {
panic(err)
}
2018-01-03 14:41:40 +00:00
defer allDatabase.Close()
2017-09-17 01:26:19 +00:00
log.Println("importing RRD from", path)
for ds := range rrd.Read(path) {
2018-01-19 03:31:33 +00:00
allDatabase.Conn.InsertGlobals(
2017-09-17 01:26:19 +00:00
&runtime.GlobalStats{
Nodes: uint32(ds.Nodes),
Clients: uint32(ds.Clients),
},
ds.Time,
site,
domain,
2017-09-17 01:26:19 +00:00
)
}
},
}
func init() {
RootCmd.AddCommand(importCmd)
importCmd.Flags().StringVarP(&configPath, "config", "c", "config.toml", "Path to configuration file")
}