2015-09-02 22:34:15 +00:00
|
|
|
package monitor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-09-25 01:57:03 +00:00
|
|
|
"sort"
|
2015-09-02 22:34:15 +00:00
|
|
|
|
2016-02-10 17:26:18 +00:00
|
|
|
"github.com/influxdata/influxdb/influxql"
|
|
|
|
"github.com/influxdata/influxdb/models"
|
2016-02-13 22:56:15 +00:00
|
|
|
"github.com/influxdata/influxdb/monitor/diagnostics"
|
2015-09-02 22:34:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// StatementExecutor translates InfluxQL queries to Monitor methods.
|
|
|
|
type StatementExecutor struct {
|
|
|
|
Monitor interface {
|
2015-10-19 21:06:14 +00:00
|
|
|
Statistics(map[string]string) ([]*Statistic, error)
|
2016-02-13 22:56:15 +00:00
|
|
|
Diagnostics() (map[string]*diagnostics.Diagnostics, error)
|
2015-09-02 22:34:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecuteStatement executes monitor-related query statements.
|
|
|
|
func (s *StatementExecutor) ExecuteStatement(stmt influxql.Statement) *influxql.Result {
|
|
|
|
switch stmt := stmt.(type) {
|
|
|
|
case *influxql.ShowStatsStatement:
|
2015-09-22 23:28:24 +00:00
|
|
|
return s.executeShowStatistics(stmt.Module)
|
2015-09-02 22:34:15 +00:00
|
|
|
case *influxql.ShowDiagnosticsStatement:
|
2015-09-23 04:50:31 +00:00
|
|
|
return s.executeShowDiagnostics(stmt.Module)
|
2015-09-02 22:34:15 +00:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unsupported statement type: %T", stmt))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 23:28:24 +00:00
|
|
|
func (s *StatementExecutor) executeShowStatistics(module string) *influxql.Result {
|
2015-09-04 23:42:22 +00:00
|
|
|
stats, err := s.Monitor.Statistics(nil)
|
2015-09-03 02:38:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return &influxql.Result{Err: err}
|
|
|
|
}
|
2015-09-02 22:34:15 +00:00
|
|
|
|
2015-11-22 18:42:34 +00:00
|
|
|
var rows []*models.Row
|
2015-09-22 23:28:24 +00:00
|
|
|
for _, stat := range stats {
|
|
|
|
if module != "" && stat.Name != module {
|
|
|
|
continue
|
|
|
|
}
|
2015-09-16 21:32:50 +00:00
|
|
|
row := &models.Row{Name: stat.Name, Tags: stat.Tags}
|
2015-09-02 22:34:15 +00:00
|
|
|
|
|
|
|
values := make([]interface{}, 0, len(stat.Values))
|
|
|
|
for _, k := range stat.valueNames() {
|
|
|
|
row.Columns = append(row.Columns, k)
|
|
|
|
values = append(values, stat.Values[k])
|
|
|
|
}
|
|
|
|
row.Values = [][]interface{}{values}
|
2015-09-22 23:28:24 +00:00
|
|
|
rows = append(rows, row)
|
2015-09-02 22:34:15 +00:00
|
|
|
}
|
|
|
|
return &influxql.Result{Series: rows}
|
|
|
|
}
|
|
|
|
|
2015-09-23 04:50:31 +00:00
|
|
|
func (s *StatementExecutor) executeShowDiagnostics(module string) *influxql.Result {
|
2015-09-03 02:38:31 +00:00
|
|
|
diags, err := s.Monitor.Diagnostics()
|
|
|
|
if err != nil {
|
|
|
|
return &influxql.Result{Err: err}
|
|
|
|
}
|
2015-09-16 21:32:50 +00:00
|
|
|
rows := make([]*models.Row, 0, len(diags))
|
2015-09-03 02:38:31 +00:00
|
|
|
|
2015-09-25 01:57:03 +00:00
|
|
|
// Get a sorted list of diagnostics keys.
|
|
|
|
sortedKeys := make([]string, 0, len(diags))
|
2015-11-22 18:42:34 +00:00
|
|
|
for k := range diags {
|
2015-09-25 01:57:03 +00:00
|
|
|
sortedKeys = append(sortedKeys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(sortedKeys)
|
|
|
|
|
|
|
|
for _, k := range sortedKeys {
|
2015-09-23 04:50:31 +00:00
|
|
|
if module != "" && k != module {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-09-16 21:32:50 +00:00
|
|
|
row := &models.Row{Name: k}
|
2015-09-03 02:38:31 +00:00
|
|
|
|
2015-09-25 01:57:03 +00:00
|
|
|
row.Columns = diags[k].Columns
|
|
|
|
row.Values = diags[k].Rows
|
2015-09-03 02:38:31 +00:00
|
|
|
rows = append(rows, row)
|
|
|
|
}
|
|
|
|
return &influxql.Result{Series: rows}
|
2015-09-02 22:34:15 +00:00
|
|
|
}
|