mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Basic import of json dashboards is working, needs more work to handle updates, and continous watching, #22
This commit is contained in:
parent
1d6413bfae
commit
d95c5e6674
@ -11,6 +11,18 @@ router_logging = false
|
|||||||
static_root_path = public
|
static_root_path = public
|
||||||
enable_gzip = false
|
enable_gzip = false
|
||||||
|
|
||||||
|
[database]
|
||||||
|
; Either "mysql", "postgres" or "sqlite3", it's your choice
|
||||||
|
type = sqlite3
|
||||||
|
host = 127.0.0.1:3306
|
||||||
|
name = grafana
|
||||||
|
user = root
|
||||||
|
password =
|
||||||
|
; For "postgres" only, either "disable", "require" or "verify-full"
|
||||||
|
ssl_mode = disable
|
||||||
|
; For "sqlite3" only
|
||||||
|
path = data/grafana.db
|
||||||
|
|
||||||
[session]
|
[session]
|
||||||
; Either "memory", "file", default is "memory"
|
; Either "memory", "file", default is "memory"
|
||||||
provider = file
|
provider = file
|
||||||
@ -47,7 +59,7 @@ login_remember_days = 7
|
|||||||
cookie_username = grafana_user
|
cookie_username = grafana_user
|
||||||
cookie_remember_name = grafana_remember
|
cookie_remember_name = grafana_remember
|
||||||
; disable user signup / registration
|
; disable user signup / registration
|
||||||
disable_user_signup = false
|
; disable_user_signup = false, not implemented yet
|
||||||
|
|
||||||
[account.single]
|
[account.single]
|
||||||
; Enable this feature to auto assign new users to a single account, suitable for NON multi tenant setups
|
; Enable this feature to auto assign new users to a single account, suitable for NON multi tenant setups
|
||||||
@ -81,18 +93,6 @@ scopes = https://www.googleapis.com/auth/userinfo.profile https://www.googleapis
|
|||||||
auth_url = https://accounts.google.com/o/oauth2/auth
|
auth_url = https://accounts.google.com/o/oauth2/auth
|
||||||
token_url = https://accounts.google.com/o/oauth2/token
|
token_url = https://accounts.google.com/o/oauth2/token
|
||||||
|
|
||||||
[database]
|
|
||||||
; Either "mysql", "postgres" or "sqlite3", it's your choice
|
|
||||||
type = sqlite3
|
|
||||||
host = 127.0.0.1:3306
|
|
||||||
name = grafana
|
|
||||||
user = root
|
|
||||||
password =
|
|
||||||
; For "postgres" only, either "disable", "require" or "verify-full"
|
|
||||||
ssl_mode = disable
|
|
||||||
; For "sqlite3" only
|
|
||||||
path = data/grafana.db
|
|
||||||
|
|
||||||
[log]
|
[log]
|
||||||
root_path =
|
root_path =
|
||||||
; Either "console", "file", "conn", "smtp" or "database", default is "console"
|
; Either "console", "file", "conn", "smtp" or "database", default is "console"
|
||||||
|
5
main.go
5
main.go
@ -6,6 +6,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/torkelo/grafana-pro/pkg/cmd"
|
"github.com/torkelo/grafana-pro/pkg/cmd"
|
||||||
|
"github.com/torkelo/grafana-pro/pkg/log"
|
||||||
"github.com/torkelo/grafana-pro/pkg/setting"
|
"github.com/torkelo/grafana-pro/pkg/setting"
|
||||||
|
|
||||||
"github.com/codegangsta/cli"
|
"github.com/codegangsta/cli"
|
||||||
@ -30,7 +31,9 @@ func main() {
|
|||||||
app.Name = "Grafana Backend"
|
app.Name = "Grafana Backend"
|
||||||
app.Usage = "grafana web"
|
app.Usage = "grafana web"
|
||||||
app.Version = version
|
app.Version = version
|
||||||
app.Commands = []cli.Command{cmd.CmdWeb}
|
app.Commands = []cli.Command{cmd.CmdWeb, cmd.CmdImportJson}
|
||||||
app.Flags = append(app.Flags, []cli.Flag{}...)
|
app.Flags = append(app.Flags, []cli.Flag{}...)
|
||||||
app.Run(os.Args)
|
app.Run(os.Args)
|
||||||
|
|
||||||
|
log.Close()
|
||||||
}
|
}
|
||||||
|
120
pkg/cmd/import.go
Normal file
120
pkg/cmd/import.go
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/codegangsta/cli"
|
||||||
|
"github.com/torkelo/grafana-pro/pkg/bus"
|
||||||
|
"github.com/torkelo/grafana-pro/pkg/log"
|
||||||
|
m "github.com/torkelo/grafana-pro/pkg/models"
|
||||||
|
"github.com/torkelo/grafana-pro/pkg/services/sqlstore"
|
||||||
|
"github.com/torkelo/grafana-pro/pkg/setting"
|
||||||
|
)
|
||||||
|
|
||||||
|
var CmdImportJson = cli.Command{
|
||||||
|
Name: "import-json",
|
||||||
|
Usage: "grafana import",
|
||||||
|
Description: "Starts Grafana import process",
|
||||||
|
Action: runImport,
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "dir",
|
||||||
|
Usage: "path to folder containing json dashboards",
|
||||||
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "account",
|
||||||
|
Usage: "Account name to save dashboards under",
|
||||||
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "config",
|
||||||
|
Value: "grafana.ini",
|
||||||
|
Usage: "path to config file",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func runImport(c *cli.Context) {
|
||||||
|
dir := c.String("dir")
|
||||||
|
if len(dir) == 0 {
|
||||||
|
log.Error(3, "Missing command flag --dir")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := os.Stat(dir)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
log.Error(3, "Directory does not exist: %v", dir)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !file.IsDir() {
|
||||||
|
log.Error(3, "%v is not a directory", dir)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
accountName := c.String("account")
|
||||||
|
if len(accountName) == 0 {
|
||||||
|
log.Error(3, "Missing command flag --account")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
setting.NewConfigContext()
|
||||||
|
sqlstore.NewEngine()
|
||||||
|
sqlstore.EnsureAdminUser()
|
||||||
|
|
||||||
|
accountQuery := m.GetAccountByNameQuery{Name: accountName}
|
||||||
|
if err := bus.Dispatch(&accountQuery); err != nil {
|
||||||
|
log.Error(3, "Failed to find account", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
accountId := accountQuery.Result.Id
|
||||||
|
|
||||||
|
visitor := func(path string, f os.FileInfo, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if f.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(f.Name(), ".json") {
|
||||||
|
if err := importDashboard(path, accountId); err != nil {
|
||||||
|
log.Error(3, "Failed to import dashboard file: %v, err: %v", path, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := filepath.Walk(dir, visitor); err != nil {
|
||||||
|
log.Error(3, "failed to scan dir for json files: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func importDashboard(path string, accountId int64) error {
|
||||||
|
log.Info("Importing %v", path)
|
||||||
|
|
||||||
|
reader, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
dash := m.NewDashboard("temp")
|
||||||
|
jsonParser := json.NewDecoder(reader)
|
||||||
|
|
||||||
|
if err := jsonParser.Decode(&dash.Data); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := m.SaveDashboardCommand{
|
||||||
|
AccountId: accountId,
|
||||||
|
Dashboard: dash.Data,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := bus.Dispatch(&cmd); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@ -234,6 +234,4 @@ func readSessionConfig() {
|
|||||||
if SessionOptions.Provider == "file" {
|
if SessionOptions.Provider == "file" {
|
||||||
os.MkdirAll(path.Dir(SessionOptions.ProviderConfig), os.ModePerm)
|
os.MkdirAll(path.Dir(SessionOptions.ProviderConfig), os.ModePerm)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info("Session Service Enabled")
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user