This commit is contained in:
UlricQin 2021-07-21 18:13:43 +08:00
commit 77c6e0dbff
13 changed files with 501 additions and 76 deletions

View File

@ -20,6 +20,7 @@ type DataSource interface {
PushEndpoint PushEndpoint
QueryData(inputs vos.DataQueryParam) []*vos.DataQueryResp // 查询一段时间 QueryData(inputs vos.DataQueryParam) []*vos.DataQueryResp // 查询一段时间
QueryDataInstant(ql string) []*vos.DataQueryInstanceResp // 查询一个时间点数据 等同于prometheus instant_query
QueryTagKeys(recv vos.CommonTagQueryParam) *vos.TagKeyQueryResp // 获取标签的names QueryTagKeys(recv vos.CommonTagQueryParam) *vos.TagKeyQueryResp // 获取标签的names
QueryTagValues(recv vos.CommonTagQueryParam) *vos.TagValueQueryResp // 根据一个label_name获取 values QueryTagValues(recv vos.CommonTagQueryParam) *vos.TagValueQueryResp // 根据一个label_name获取 values
QueryTagPairs(recv vos.CommonTagQueryParam) *vos.TagPairQueryResp // 根据匹配拿到所有 series 上面三个使用统一的结构体 QueryTagPairs(recv vos.CommonTagQueryParam) *vos.TagPairQueryResp // 根据匹配拿到所有 series 上面三个使用统一的结构体

View File

@ -24,7 +24,7 @@ const (
LABEL_IDENT = "ident" LABEL_IDENT = "ident"
LABEL_NAME = "__name__" LABEL_NAME = "__name__"
DEFAULT_QL = `{__name__=~".*a.*|.*e.*"}` DEFAULT_QL = `{__name__=~".*a.*|.*e.*"}`
DEFAULT_STEP = 14 DEFAULT_STEP = 15
) )
type commonQueryObj struct { type commonQueryObj struct {
@ -207,13 +207,15 @@ func (pd *PromeDataSource) QueryData(inputs vos.DataQueryParam) []*vos.DataQuery
startT := tsToUtcTs(inputs.Start) startT := tsToUtcTs(inputs.Start)
endT := tsToUtcTs(inputs.End) endT := tsToUtcTs(inputs.End)
// TODO 前端传入分辨率还是后端计算grafana和prometheus ui都是前端传入 resolution := time.Second * time.Duration(inputs.Step)
if inputs.Step == 0 {
// step==0 说明要自己算 grafana和prometheus ui都是前端传入
delta := (inputs.End - inputs.Start) / 3600 delta := (inputs.End - inputs.Start) / 3600
if delta <= 0 { if delta <= 0 {
delta = 1 delta = 1
} }
resolution := time.Second * time.Duration(delta*DEFAULT_STEP) resolution = time.Second * time.Duration(delta*DEFAULT_STEP)
}
q, err := pd.QueryEngine.NewRangeQuery(pd.Queryable, qlStrFinal, startT, endT, resolution) q, err := pd.QueryEngine.NewRangeQuery(pd.Queryable, qlStrFinal, startT, endT, resolution)
if err != nil { if err != nil {
logger.Errorf("[prome_query_error][QueryData_error_may_be_parse_ql_error][args:%+v][err:%+v]", input, err) logger.Errorf("[prome_query_error][QueryData_error_may_be_parse_ql_error][args:%+v][err:%+v]", input, err)
@ -253,7 +255,8 @@ func (pd *PromeDataSource) QueryData(inputs vos.DataQueryParam) []*vos.DataQuery
pNum := len(m.Points) pNum := len(m.Points)
for _, p := range m.Points { for _, p := range m.Points {
tmpP := &vos.Point{ tmpP := &vos.Point{
Timestamp: p.T, // 毫秒时间时间戳转 秒时间戳
Timestamp: p.T / 1e3,
Value: vos.JsonFloat(p.V), Value: vos.JsonFloat(p.V),
} }
oneResp.Values = append(oneResp.Values, tmpP) oneResp.Values = append(oneResp.Values, tmpP)
@ -266,7 +269,7 @@ func (pd *PromeDataSource) QueryData(inputs vos.DataQueryParam) []*vos.DataQuery
} }
tagStr = strings.TrimRight(tagStr, ",") tagStr = strings.TrimRight(tagStr, ",")
oneResp.Tags = tagStr oneResp.Tags = tagStr
oneResp.Resolution = delta * DEFAULT_STEP oneResp.Resolution = int64(resolution / time.Second)
oneResp.PNum = pNum oneResp.PNum = pNum
respD = append(respD, oneResp) respD = append(respD, oneResp)
@ -616,6 +619,36 @@ func (pd *PromeDataSource) QueryTagPairs(recv vos.CommonTagQueryParam) *vos.TagP
return respD return respD
} }
func (pd *PromeDataSource) QueryDataInstant(ql string) []*vos.DataQueryInstanceResp {
respD := make([]*vos.DataQueryInstanceResp, 0)
pv := pd.QueryVector(ql)
if pv == nil {
return respD
}
for _, s := range pv {
metricOne := make(map[string]interface{})
valueOne := make([]float64, 0)
for _, l := range s.Metric {
if l.Name == LABEL_NAME {
continue
}
metricOne[l.Name] = l.Value
}
// 毫秒时间时间戳转 秒时间戳
valueOne = append(valueOne, float64(s.Point.T)/1e3)
valueOne = append(valueOne, s.Point.V)
respD = append(respD, &vos.DataQueryInstanceResp{
Metric: metricOne,
Value: valueOne,
})
}
return respD
}
func (pd *PromeDataSource) QueryVector(ql string) promql.Vector { func (pd *PromeDataSource) QueryVector(ql string) promql.Vector {
t := time.Now() t := time.Now()
q, err := pd.QueryEngine.NewInstantQuery(pd.Queryable, ql, t) q, err := pd.QueryEngine.NewInstantQuery(pd.Queryable, ql, t)

306
etc/dashboard/jmx_exporter Normal file
View File

@ -0,0 +1,306 @@
[
{
"id": 0,
"name": "jmx_exporter",
"tags": "",
"configs": "{\"tags\":[{\"tagName\":\"java_app\",\"key\":\"java_app\",\"value\":\"*\",\"prefix\":false}]}",
"chart_groups": [
{
"id": 0,
"dashboard_id": 0,
"name": "jvm统计",
"weight": 1,
"charts": [
{
"id": 278,
"group_id": 75,
"configs": "{\"name\":\"jvm版本信息\",\"mode\":\"promethues\",\"prome_ql\":[\"avg(jvm_info{java_app=~\\\"$java_app\\\"}) without (runtime,vendor)\"],\"layout\":{\"h\":2,\"w\":12,\"x\":0,\"y\":0,\"i\":\"0\"}}",
"weight": 0
},
{
"id": 309,
"group_id": 75,
"configs": "{\"name\":\"java进程启动时间 单位:小时\",\"mode\":\"promethues\",\"prome_ql\":[\"(time() - process_start_time_seconds{java_app=~\\\"$java_app\\\"})/3600\"],\"layout\":{\"h\":2,\"w\":12,\"x\":12,\"y\":0,\"i\":\"1\"}}",
"weight": 0
}
]
},
{
"id": 0,
"dashboard_id": 0,
"name": "jvm内存使用",
"weight": 2,
"charts": [
{
"id": 279,
"group_id": 76,
"configs": "{\"name\":\"jvm内存使用 - nonheap 非堆区\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_memory_bytes_used{java_app=~\\\"$java_app\\\",area=\\\"nonheap\\\"}\"],\"layout\":{\"h\":2,\"w\":6,\"x\":0,\"y\":0,\"i\":\"0\"}}",
"weight": 0
},
{
"id": 280,
"group_id": 76,
"configs": "{\"name\":\"jvm内存使用 - heap堆区\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_memory_bytes_used{java_app=~\\\"$java_app\\\",area=\\\"heap\\\"}\"],\"layout\":{\"h\":2,\"w\":6,\"x\":6,\"y\":0,\"i\":\"1\"}}",
"weight": 0
},
{
"id": 281,
"group_id": 76,
"configs": "{\"name\":\"提交给 Java虚拟机使用的内存量 heap 堆区\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_memory_bytes_committed{java_app=~\\\"$java_app\\\",area=\\\"heap\\\"}\"],\"layout\":{\"h\":2,\"w\":6,\"x\":12,\"y\":0,\"i\":\"2\"}}",
"weight": 0
},
{
"id": 282,
"group_id": 76,
"configs": "{\"name\":\"提交给 Java虚拟机使用的内存量 nonheap 非堆区\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_memory_bytes_committed{java_app=~\\\"$java_app\\\",area=\\\"nonheap\\\"}\"],\"layout\":{\"h\":2,\"w\":6,\"x\":18,\"y\":0,\"i\":\"3\"}}",
"weight": 0
},
{
"id": 283,
"group_id": 76,
"configs": "{\"name\":\"jvm最大内存 \",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_memory_bytes_max{java_app=~\\\"$java_app\\\",area=\\\"heap\\\"}\"],\"layout\":{\"h\":2,\"w\":6,\"x\":0,\"y\":2,\"i\":\"4\"}}",
"weight": 0
},
{
"id": 285,
"group_id": 76,
"configs": "{\"name\":\"jvm 初始化内存\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_memory_bytes_init{java_app=~\\\"$java_app\\\",area=\\\"heap\\\"}\"],\"layout\":{\"h\":2,\"w\":6,\"x\":6,\"y\":2,\"i\":\"5\"}}",
"weight": 0
},
{
"id": 286,
"group_id": 76,
"configs": "{\"name\":\"jvm内存使用百分比% heap堆区 \",\"mode\":\"promethues\",\"prome_ql\":[\"100 * jvm_memory_bytes_used{java_app=~\\\"$java_app\\\",area=\\\"heap\\\"}/jvm_memory_bytes_max{java_app=~\\\"$java_app\\\",area=\\\"heap\\\"}\"],\"layout\":{\"h\":2,\"w\":6,\"x\":12,\"y\":2,\"i\":\"6\"}}",
"weight": 0
}
]
},
{
"id": 0,
"dashboard_id": 0,
"name": "jvm内存池",
"weight": 3,
"charts": [
{
"id": 287,
"group_id": 77,
"configs": "{\"name\":\"jvm内存池分pool展示\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_memory_pool_bytes_max{java_app=~\\\"$java_app\\\"}\"],\"layout\":{\"h\":2,\"w\":24,\"x\":0,\"y\":0,\"i\":\"0\"}}",
"weight": 0
},
{
"id": 316,
"group_id": 77,
"configs": "{\"name\":\" JVM 缓冲池使用缓存大小\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_buffer_pool_used_bytes{java_app=~\\\"$java_app\\\"}\"],\"layout\":{\"h\":2,\"w\":6,\"x\":0,\"y\":2,\"i\":\"1\"}}",
"weight": 0
},
{
"id": 317,
"group_id": 77,
"configs": "{\"name\":\"JVM 缓冲池的字节容量\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_buffer_pool_capacity_bytes{java_app=~\\\"$java_app\\\"}\"],\"layout\":{\"h\":2,\"w\":6,\"x\":6,\"y\":2,\"i\":\"2\"}}",
"weight": 0
},
{
"id": 318,
"group_id": 77,
"configs": "{\"name\":\"JVM 缓冲池使用的字节大小\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_buffer_pool_used_bytes{java_app=~\\\"$java_app\\\"}\"],\"layout\":{\"h\":2,\"w\":6,\"x\":12,\"y\":2,\"i\":\"3\"}}",
"weight": 0
}
]
},
{
"id": 0,
"dashboard_id": 0,
"name": "jvm gc情况",
"weight": 4,
"charts": [
{
"id": 288,
"group_id": 78,
"configs": "{\"name\":\"新生代gc耗时 1分钟\",\"mode\":\"promethues\",\"prome_ql\":[\"increase(jvm_gc_collection_seconds_sum{java_app=~\\\"$java_app\\\",gc=\\\"G1 Young Generation\\\" }[1m])\"],\"layout\":{\"h\":2,\"w\":8,\"x\":0,\"y\":0,\"i\":\"0\"}}",
"weight": 0
},
{
"id": 289,
"group_id": 78,
"configs": "{\"name\":\"老生代gc耗时 1分钟\",\"mode\":\"promethues\",\"prome_ql\":[\"increase(jvm_gc_collection_seconds_sum{java_app=~\\\"$java_app\\\",gc=\\\"G1 Old Generation\\\" }[1m])\"],\"layout\":{\"h\":2,\"w\":8,\"x\":8,\"y\":0,\"i\":\"1\"}}",
"weight": 0
},
{
"id": 290,
"group_id": 78,
"configs": "{\"name\":\"新生代gc次数 1分钟\",\"mode\":\"promethues\",\"prome_ql\":[\"increase(jvm_gc_collection_seconds_count{java_app=~\\\"$java_app\\\",gc=\\\"G1 Young Generation\\\" }[1m])\"],\"layout\":{\"h\":2,\"w\":8,\"x\":16,\"y\":0,\"i\":\"2\"}}",
"weight": 0
},
{
"id": 291,
"group_id": 78,
"configs": "{\"name\":\"老生代gc次数 1分钟\",\"mode\":\"promethues\",\"prome_ql\":[\"increase(jvm_gc_collection_seconds_count{java_app=~\\\"$java_app\\\",gc=\\\"G1 Old Generation\\\" }[1m])\"],\"layout\":{\"h\":2,\"w\":8,\"x\":0,\"y\":2,\"i\":\"3\"}}",
"weight": 0
},
{
"id": 292,
"group_id": 78,
"configs": "{\"name\":\"新生代平均gc耗时 秒\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_gc_collection_seconds_sum{java_app=~\\\"$java_app\\\",gc=\\\"G1 Young Generation\\\" }/jvm_gc_collection_seconds_count{java_app=~\\\"$java_app\\\",gc=\\\"G1 Young Generation\\\" }\"],\"layout\":{\"h\":2,\"w\":8,\"x\":8,\"y\":2,\"i\":\"4\"}}",
"weight": 0
},
{
"id": 293,
"group_id": 78,
"configs": "{\"name\":\"老生代平均gc耗时\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_gc_collection_seconds_sum{java_app=~\\\"$java_app\\\",gc=\\\"G1 Old Generation\\\"}/jvm_gc_collection_seconds_count{java_app=~\\\"$java_app\\\",gc=\\\"G1 Old Generation\\\" }\"],\"layout\":{\"h\":2,\"w\":8,\"x\":16,\"y\":2,\"i\":\"5\"}}",
"weight": 0
}
]
},
{
"id": 0,
"dashboard_id": 0,
"name": "jvm线程情况",
"weight": 5,
"charts": [
{
"id": 294,
"group_id": 79,
"configs": "{\"name\":\"当前线程数\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_threads_current{java_app=~\\\"$java_app\\\"}\"],\"layout\":{\"h\":2,\"w\":6,\"x\":0,\"y\":0,\"i\":\"0\"}}",
"weight": 0
},
{
"id": 295,
"group_id": 79,
"configs": "{\"name\":\"守护线程数\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_threads_daemon{java_app=~\\\"$java_app\\\"}\"],\"layout\":{\"h\":2,\"w\":6,\"x\":6,\"y\":0,\"i\":\"1\"}}",
"weight": 0
},
{
"id": 296,
"group_id": 79,
"configs": "{\"name\":\"死锁线程数\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_threads_deadlocked{java_app=~\\\"$java_app\\\"}\"],\"layout\":{\"h\":2,\"w\":6,\"x\":12,\"y\":0,\"i\":\"2\"}}",
"weight": 0
},
{
"id": 297,
"group_id": 79,
"configs": "{\"name\":\"活动线程峰值\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_threads_peak{java_app=~\\\"$java_app\\\"}\"],\"layout\":{\"h\":2,\"w\":6,\"x\":18,\"y\":0,\"i\":\"3\"}}",
"weight": 0
},
{
"id": 298,
"group_id": 79,
"configs": "{\"name\":\"自JVM启动后启动的线程总量包括daemon,non-daemon和终止了的\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_threads_started_total{java_app=~\\\"$java_app\\\"}\"],\"layout\":{\"h\":2,\"w\":6,\"x\":0,\"y\":2,\"i\":\"4\"}}",
"weight": 0
},
{
"id": 299,
"group_id": 79,
"configs": "{\"name\":\"当前TERMINATED线程个数\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_threads_state{java_app=~\\\"$java_app\\\",state=\\\"TERMINATED\\\"}\"],\"layout\":{\"h\":2,\"w\":6,\"x\":6,\"y\":4,\"i\":\"5\"}}",
"weight": 0
},
{
"id": 300,
"group_id": 79,
"configs": "{\"name\":\"当前RUNNABLE线程个数\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_threads_state{java_app=~\\\"$java_app\\\",state=\\\"RUNNABLE\\\"}\"],\"layout\":{\"h\":2,\"w\":6,\"x\":12,\"y\":2,\"i\":\"6\"}}",
"weight": 0
},
{
"id": 301,
"group_id": 79,
"configs": "{\"name\":\"当前NEW线程个数\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_threads_state{java_app=~\\\"$java_app\\\",state=\\\"NEW\\\"}\"],\"layout\":{\"h\":2,\"w\":6,\"x\":18,\"y\":2,\"i\":\"7\"}}",
"weight": 0
},
{
"id": 302,
"group_id": 79,
"configs": "{\"name\":\"当前TIMED_WAITING线程个数\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_threads_state{java_app=~\\\"$java_app\\\",state=\\\"TIMED_WAITING\\\"}\"],\"layout\":{\"h\":2,\"w\":6,\"x\":0,\"y\":4,\"i\":\"8\"}}",
"weight": 0
},
{
"id": 303,
"group_id": 79,
"configs": "{\"name\":\"当前BLOCKED线程个数\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_threads_state{java_app=~\\\"$java_app\\\",state=\\\"BLOCKED\\\"}\"],\"layout\":{\"h\":2,\"w\":6,\"x\":18,\"y\":4,\"i\":\"9\"}}",
"weight": 0
},
{
"id": 304,
"group_id": 79,
"configs": "{\"name\":\"当前WAITING线程个数\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_threads_state{java_app=~\\\"$java_app\\\",state=\\\"WAITING\\\"}\"],\"layout\":{\"h\":2,\"w\":6,\"x\":12,\"y\":4,\"i\":\"10\"}}",
"weight": 0
},
{
"id": 305,
"group_id": 79,
"configs": "{\"name\":\"当前线程状态汇总\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_threads_state{java_app=~\\\"$java_app\\\"}\"],\"layout\":{\"h\":2,\"w\":6,\"x\":6,\"y\":2,\"i\":\"11\"}}",
"weight": 0
}
]
},
{
"id": 0,
"dashboard_id": 0,
"name": "加载类情况",
"weight": 6,
"charts": [
{
"id": 306,
"group_id": 80,
"configs": "{\"name\":\"jvm 当前加载的类个数 \",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_classes_loaded{java_app=~\\\"$java_app\\\"}\"],\"layout\":{\"h\":2,\"w\":8,\"x\":0,\"y\":0,\"i\":\"0\"}}",
"weight": 0
},
{
"id": 307,
"group_id": 80,
"configs": "{\"name\":\"jvm启动以来加载的类总个数\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_classes_loaded_total{java_app=~\\\"$java_app\\\"}\"],\"layout\":{\"h\":2,\"w\":8,\"x\":8,\"y\":0,\"i\":\"1\"}}",
"weight": 0
},
{
"id": 308,
"group_id": 80,
"configs": "{\"name\":\"jvm启动以来卸载的类总个数\",\"mode\":\"promethues\",\"prome_ql\":[\"jvm_classes_unloaded_total{java_app=~\\\"$java_app\\\"}\"],\"layout\":{\"h\":2,\"w\":8,\"x\":16,\"y\":0,\"i\":\"2\"}}",
"weight": 0
}
]
},
{
"id": 0,
"dashboard_id": 0,
"name": "机器指标(配置了java.lang才有)",
"weight": 7,
"charts": [
{
"id": 311,
"group_id": 81,
"configs": "{\"name\":\"java进程打开fd数\",\"mode\":\"promethues\",\"prome_ql\":[\"os_open_file_descriptor_count{java_app=~\\\"$java_app\\\"}\"],\"layout\":{\"h\":2,\"w\":8,\"x\":0,\"y\":0,\"i\":\"0\"}}",
"weight": 0
},
{
"id": 312,
"group_id": 81,
"configs": "{\"name\":\"机器总内存\",\"mode\":\"promethues\",\"prome_ql\":[\"os_total_memory_size{java_app=~\\\"$java_app\\\"}\"],\"layout\":{\"h\":2,\"w\":8,\"x\":8,\"y\":0,\"i\":\"1\"}}",
"weight": 0
},
{
"id": 313,
"group_id": 81,
"configs": "{\"name\":\"机器可用内存数\",\"mode\":\"promethues\",\"prome_ql\":[\"os_free_memory_size{java_app=~\\\"$java_app\\\"}\"],\"layout\":{\"h\":2,\"w\":8,\"x\":16,\"y\":0,\"i\":\"2\"}}",
"weight": 0
},
{
"id": 314,
"group_id": 81,
"configs": "{\"name\":\"机器近期cpu使用率\",\"mode\":\"promethues\",\"link\":\"https://docs.oracle.com/javase/7/docs/jre/api/management/extension/com/sun/management/OperatingSystemMXBean.html#getSystemCpuLoad()\",\"prome_ql\":[\"100 * os_system_cpu_load{java_app=~\\\"$java_app\\\"}\"],\"layout\":{\"h\":2,\"w\":8,\"x\":0,\"y\":2,\"i\":\"3\"}}",
"weight": 0
},
{
"id": 315,
"group_id": 81,
"configs": "{\"name\":\"java进程cpu使用\",\"mode\":\"promethues\",\"link\":\"https://docs.oracle.com/javase/7/docs/jre/api/management/extension/com/sun/management/OperatingSystemMXBean.html#getProcessCpuLoad()\",\"prome_ql\":[\"os_process_cpu_load{java_app=~\\\"$java_app\\\"}\"],\"layout\":{\"h\":2,\"w\":8,\"x\":8,\"y\":2,\"i\":\"4\"}}",
"weight": 0
},
{
"id": 319,
"group_id": 81,
"configs": "{\"name\":\"jvm cpu百分比\",\"mode\":\"promethues\",\"prome_ql\":[\"100 *(os_process_cpu_load{java_app=~\\\"$java_app\\\"}/os_system_cpu_load{java_app=~\\\"$java_app\\\"})\"],\"layout\":{\"h\":2,\"w\":8,\"x\":16,\"y\":2,\"i\":\"5\"}}",
"weight": 0
}
]
}
]
}
]

View File

@ -2,6 +2,7 @@ package http
import ( import (
"net/http" "net/http"
"strings"
"github.com/didi/nightingale/v5/pkg/ierr" "github.com/didi/nightingale/v5/pkg/ierr"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -23,7 +24,17 @@ func admin() gin.HandlerFunc {
c.Set("username", username) c.Set("username", username)
user := loginUser(c) user := loginUser(c)
if user.Role != "Admin" {
roles := strings.Fields(user.RolesForDB)
found := false
for i := 0; i < len(roles); i++ {
if roles[i] == "Admin" {
found = true
break
}
}
if !found {
ierr.Bomb(http.StatusForbidden, "forbidden") ierr.Bomb(http.StatusForbidden, "forbidden")
} }

View File

@ -42,10 +42,12 @@ func configRoutes(r *gin.Engine) {
// 开源版本,为了支持图表分享功能,允许匿名查询数据 // 开源版本,为了支持图表分享功能,允许匿名查询数据
guest.POST("/query", GetData) guest.POST("/query", GetData)
guest.POST("/instant-query", GetDataInstant)
guest.POST("/tag-pairs", GetTagPairs) guest.POST("/tag-pairs", GetTagPairs)
guest.POST("/tag-keys", GetTagKeys) guest.POST("/tag-keys", GetTagKeys)
guest.POST("/tag-values", GetTagValues) guest.POST("/tag-values", GetTagValues)
guest.POST("/tag-metrics", GetMetrics) guest.POST("/tag-metrics", GetMetrics)
guest.GET("/check-promql", checkPromeQl)
} }
// for brower, expose location in nginx.conf // for brower, expose location in nginx.conf
@ -138,7 +140,6 @@ func configRoutes(r *gin.Engine) {
pages.DELETE("/alert-rule-group/:id", login(), alertRuleGroupDel) pages.DELETE("/alert-rule-group/:id", login(), alertRuleGroupDel)
pages.POST("/alert-rule-group/:id/favorites", login(), alertRuleGroupFavoriteAdd) pages.POST("/alert-rule-group/:id/favorites", login(), alertRuleGroupFavoriteAdd)
pages.DELETE("/alert-rule-group/:id/favorites", login(), alertRuleGroupFavoriteDel) pages.DELETE("/alert-rule-group/:id/favorites", login(), alertRuleGroupFavoriteDel)
pages.GET("/check-promql", checkPromeQl)
pages.POST("/alert-rules", login(), alertRuleAdd) pages.POST("/alert-rules", login(), alertRuleAdd)
pages.PUT("/alert-rules/status", login(), alertRuleStatusPut) pages.PUT("/alert-rules/status", login(), alertRuleStatusPut)
@ -169,6 +170,7 @@ func configRoutes(r *gin.Engine) {
pages.GET("/tpl/content", tplGet) pages.GET("/tpl/content", tplGet)
pages.GET("/status", Status) pages.GET("/status", Status)
} }
// for thirdparty, do not expose location in nginx.conf // for thirdparty, do not expose location in nginx.conf
@ -288,11 +290,12 @@ func configRoutes(r *gin.Engine) {
v1.GET("/status", Status) v1.GET("/status", Status)
v1.POST("/query", GetData) v1.POST("/query", GetData)
v1.GET("/check-promql", checkPromeQl) v1.POST("/instant-query", GetDataInstant)
v1.POST("/tag-keys", GetTagKeys) v1.POST("/tag-keys", GetTagKeys)
v1.POST("/tag-values", GetTagValues) v1.POST("/tag-values", GetTagValues)
v1.POST("/tag-metrics", GetMetrics) v1.POST("/tag-metrics", GetMetrics)
v1.POST("/tag-pairs", GetTagPairs) v1.POST("/tag-pairs", GetTagPairs)
v1.GET("/check-promql", checkPromeQl)
} }
push := r.Group("/v1/n9e/series").Use(gzip.Gzip(gzip.DefaultCompression)) push := r.Group("/v1/n9e/series").Use(gzip.Gzip(gzip.DefaultCompression))

View File

@ -129,9 +129,12 @@ func alertRuleGroupFavoriteDel(c *gin.Context) {
} }
func alertRuleWritePermCheck(alertRuleGroup *models.AlertRuleGroup, user *models.User) { func alertRuleWritePermCheck(alertRuleGroup *models.AlertRuleGroup, user *models.User) {
if user.Role == "Admin" { roles := strings.Fields(user.RolesForDB)
for i := 0; i < len(roles); i++ {
if roles[i] == "Admin" {
return return
} }
}
gids := IdsInt64(alertRuleGroup.UserGroupIds) gids := IdsInt64(alertRuleGroup.UserGroupIds)
if len(gids) == 0 { if len(gids) == 0 {

View File

@ -205,3 +205,17 @@ func GetData(c *gin.Context) {
resp := dataSource.QueryData(input) resp := dataSource.QueryData(input)
renderData(c, resp, nil) renderData(c, resp, nil)
} }
func GetDataInstant(c *gin.Context) {
dataSource, err := backend.GetDataSourceFor("")
if err != nil {
logger.Warningf("could not find datasource")
renderMessage(c, err)
return
}
var input vos.DataQueryInstantParam
dangerous(c.ShouldBindJSON(&input))
resp := dataSource.QueryDataInstant(input.PromeQl)
renderData(c, resp, nil)
}

View File

@ -2,6 +2,7 @@ package http
import ( import (
"encoding/json" "encoding/json"
"strings"
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -20,7 +21,14 @@ func userGets(c *gin.Context) {
list, err := models.UserGets(query, limit, offset(c, limit)) list, err := models.UserGets(query, limit, offset(c, limit))
dangerous(err) dangerous(err)
admin := loginUser(c).Role == "Admin" admin := false
roles := strings.Fields(loginUser(c).RolesForDB)
for i := 0; i < len(roles); i++ {
if roles[i] == "Admin" {
admin = true
break
}
}
renderData(c, gin.H{ renderData(c, gin.H{
"list": list, "list": list,
@ -36,7 +44,7 @@ type userAddForm struct {
Phone string `json:"phone"` Phone string `json:"phone"`
Email string `json:"email"` Email string `json:"email"`
Portrait string `json:"portrait"` Portrait string `json:"portrait"`
Role string `json:"role"` Roles []string `json:"roles"`
Contacts json.RawMessage `json:"contacts"` Contacts json.RawMessage `json:"contacts"`
} }
@ -50,6 +58,10 @@ func userAddPost(c *gin.Context) {
now := time.Now().Unix() now := time.Now().Unix()
username := loginUsername(c) username := loginUsername(c)
if len(f.Roles) == 0 {
bomb(200, "roles empty")
}
u := models.User{ u := models.User{
Username: f.Username, Username: f.Username,
Password: password, Password: password,
@ -57,7 +69,7 @@ func userAddPost(c *gin.Context) {
Phone: f.Phone, Phone: f.Phone,
Email: f.Email, Email: f.Email,
Portrait: f.Portrait, Portrait: f.Portrait,
Role: f.Role, RolesForDB: strings.Join(f.Roles, " "),
Contacts: f.Contacts, Contacts: f.Contacts,
CreateAt: now, CreateAt: now,
UpdateAt: now, UpdateAt: now,
@ -65,10 +77,6 @@ func userAddPost(c *gin.Context) {
UpdateBy: username, UpdateBy: username,
} }
if u.Role == "" {
u.Role = "Standard"
}
renderMessage(c, u.Add()) renderMessage(c, u.Add())
} }
@ -81,7 +89,7 @@ type userProfileForm struct {
Phone string `json:"phone"` Phone string `json:"phone"`
Email string `json:"email"` Email string `json:"email"`
Portrait string `json:"portrait"` Portrait string `json:"portrait"`
Role string `json:"role"` Roles []string `json:"roles"`
Status int `json:"status"` Status int `json:"status"`
Contacts json.RawMessage `json:"contacts"` Contacts json.RawMessage `json:"contacts"`
} }
@ -90,12 +98,16 @@ func userProfilePut(c *gin.Context) {
var f userProfileForm var f userProfileForm
bind(c, &f) bind(c, &f)
if len(f.Roles) == 0 {
bomb(200, "roles empty")
}
target := User(urlParamInt64(c, "id")) target := User(urlParamInt64(c, "id"))
target.Nickname = f.Nickname target.Nickname = f.Nickname
target.Phone = f.Phone target.Phone = f.Phone
target.Email = f.Email target.Email = f.Email
target.Portrait = f.Portrait target.Portrait = f.Portrait
target.Role = f.Role target.RolesForDB = strings.Join(f.Roles, " ")
target.Status = f.Status target.Status = f.Status
target.Contacts = f.Contacts target.Contacts = f.Contacts
target.UpdateAt = time.Now().Unix() target.UpdateAt = time.Now().Unix()
@ -107,7 +119,7 @@ func userProfilePut(c *gin.Context) {
"phone", "phone",
"email", "email",
"portrait", "portrait",
"role", "roles",
"status", "status",
"contacts", "contacts",
"update_at", "update_at",

View File

@ -44,7 +44,7 @@ func ResourceTotalByClasspathId(classpathIds []int64, query string) (int64, erro
} }
q := "%" + query + "%" q := "%" + query + "%"
num, err := DB.Where("ident in (select res_ident from classpath_resource where classpath_id in ("+str.IdsString(classpathIds)+")) and (ident like ? or alias like ?)", q, q).Count(new(Resource)) num, err := DB.Where("ident in (select res_ident from classpath_resource where classpath_id in ("+str.IdsString(classpathIds)+")) and (ident like ? or alias like ? or tags like ? or note like ?)", q, q, q, q).Count(new(Resource))
if err != nil { if err != nil {
logger.Errorf("mysql.error count resource in classpath(id=%v) query=%s fail: %v", classpathIds, query, err) logger.Errorf("mysql.error count resource in classpath(id=%v) query=%s fail: %v", classpathIds, query, err)
return 0, internalServerError return 0, internalServerError
@ -60,7 +60,7 @@ func ResourceGetsByClasspathId(classpathIds []int64, query string, limit, offset
q := "%" + query + "%" q := "%" + query + "%"
var objs []Resource var objs []Resource
err := DB.Where("ident in (select res_ident from classpath_resource where classpath_id in ("+str.IdsString(classpathIds)+")) and (ident like ? or alias like ?)", q, q).OrderBy("ident").Limit(limit, offset).Find(&objs) err := DB.Where("ident in (select res_ident from classpath_resource where classpath_id in ("+str.IdsString(classpathIds)+")) and (ident like ? or alias like ? or tags like ? or note like ?)", q, q, q, q).OrderBy("ident").Limit(limit, offset).Find(&objs)
if err != nil { if err != nil {
logger.Errorf("mysql.error query resource in classpath(id=%d) query=%s fail: %v", classpathIds, query, err) logger.Errorf("mysql.error query resource in classpath(id=%d) query=%s fail: %v", classpathIds, query, err)
return nil, internalServerError return nil, internalServerError

View File

@ -1,6 +1,9 @@
package models package models
import "github.com/toolkits/pkg/logger" import (
"github.com/toolkits/pkg/logger"
"xorm.io/builder"
)
type RoleOperation struct { type RoleOperation struct {
RoleName string RoleName string
@ -11,8 +14,11 @@ func (RoleOperation) TableName() string {
return "role_operation" return "role_operation"
} }
func RoleHasOperation(roleName, operation string) (bool, error) { func RoleHasOperation(roles []string, operation string) (bool, error) {
num, err := DB.Where("role_name=? and operation=?", roleName, operation).Count(new(RoleOperation)) cond := builder.NewCond()
cond = cond.And(builder.In("role_name", roles))
cond = cond.And(builder.Eq{"operation": operation})
num, err := DB.Where(cond).Count(new(RoleOperation))
if err != nil { if err != nil {
logger.Errorf("mysql.error query role_operation fail: %v", err) logger.Errorf("mysql.error query role_operation fail: %v", err)
return false, internalServerError return false, internalServerError

View File

@ -23,7 +23,8 @@ type User struct {
Email string `json:"email"` Email string `json:"email"`
Portrait string `json:"portrait"` Portrait string `json:"portrait"`
Status int `json:"status"` Status int `json:"status"`
Role string `json:"role"` RolesForDB string `json:"-" xorm:"'roles'"` // 这个字段写入数据库
RolesForFE []string `json:"roles" xorm:"-"` // 这个字段和前端交互
Contacts json.RawMessage `json:"contacts"` // 内容为 map[string]string 结构 Contacts json.RawMessage `json:"contacts"` // 内容为 map[string]string 结构
CreateAt int64 `json:"create_at"` CreateAt int64 `json:"create_at"`
CreateBy string `json:"create_by"` CreateBy string `json:"create_by"`
@ -114,7 +115,7 @@ func InitRoot() {
Password: pass, Password: pass,
Nickname: "超管", Nickname: "超管",
Portrait: "", Portrait: "",
Role: "Admin", RolesForDB: "Admin",
Contacts: []byte("{}"), Contacts: []byte("{}"),
CreateAt: now, CreateAt: now,
UpdateAt: now, UpdateAt: now,
@ -151,6 +152,8 @@ func UserGet(where string, args ...interface{}) (*User, error) {
return nil, nil return nil, nil
} }
obj.RolesForFE = strings.Fields(obj.RolesForDB)
return &obj, nil return &obj, nil
} }
@ -188,6 +191,10 @@ func UserGets(query string, limit, offset int) ([]User, error) {
return []User{}, nil return []User{}, nil
} }
for i := 0; i < len(users); i++ {
users[i].RolesForFE = strings.Fields(users[i].RolesForDB)
}
return users, nil return users, nil
} }
@ -204,6 +211,10 @@ func UserGetAll() ([]User, error) {
return []User{}, nil return []User{}, nil
} }
for i := 0; i < len(users); i++ {
users[i].RolesForFE = strings.Fields(users[i].RolesForDB)
}
return users, nil return users, nil
} }
@ -223,23 +234,31 @@ func UserGetsByIds(ids []int64) ([]User, error) {
return []User{}, nil return []User{}, nil
} }
for i := 0; i < len(users); i++ {
users[i].RolesForFE = strings.Fields(users[i].RolesForDB)
}
return users, nil return users, nil
} }
func UserGetsByIdsStr(ids []string) ([]User, error) { func UserGetsByIdsStr(ids []string) ([]User, error) {
var objs []User var users []User
err := DB.Where("id in (" + strings.Join(ids, ",") + ")").Find(&objs) err := DB.Where("id in (" + strings.Join(ids, ",") + ")").Find(&users)
if err != nil { if err != nil {
logger.Errorf("mysql.error: UserGetsByIds fail: %v", err) logger.Errorf("mysql.error: UserGetsByIds fail: %v", err)
return nil, internalServerError return nil, internalServerError
} }
if len(objs) == 0 { if len(users) == 0 {
return []User{}, nil return []User{}, nil
} }
return objs, nil for i := 0; i < len(users); i++ {
users[i].RolesForFE = strings.Fields(users[i].RolesForDB)
}
return users, nil
} }
func PassLogin(username, pass string) (*User, error) { func PassLogin(username, pass string) (*User, error) {
@ -312,7 +331,8 @@ func LdapLogin(username, pass string) (*User, error) {
user.Password = "******" user.Password = "******"
user.Portrait = "/img/linux.jpeg" user.Portrait = "/img/linux.jpeg"
user.Role = "Standard" user.RolesForDB = "Standard"
user.RolesForFE = []string{"Standard"}
user.Contacts = []byte("{}") user.Contacts = []byte("{}")
user.CreateAt = now user.CreateAt = now
user.UpdateAt = now user.UpdateAt = now
@ -474,9 +494,12 @@ func (u *User) MyUserGroups() ([]UserGroup, error) {
func (u *User) CanModifyUserGroup(ug *UserGroup) (bool, error) { func (u *User) CanModifyUserGroup(ug *UserGroup) (bool, error) {
// 我是管理员,自然可以 // 我是管理员,自然可以
if u.Role == "Admin" { roles := strings.Fields(u.RolesForDB)
for i := 0; i < len(roles); i++ {
if roles[i] == "Admin" {
return true, nil return true, nil
} }
}
// 我是创建者,自然可以 // 我是创建者,自然可以
if ug.CreateBy == u.Username { if ug.CreateBy == u.Username {
@ -493,11 +516,14 @@ func (u *User) CanModifyUserGroup(ug *UserGroup) (bool, error) {
} }
func (u *User) CanDo(op string) (bool, error) { func (u *User) CanDo(op string) (bool, error) {
if u.Role == "Admin" { roles := strings.Fields(u.RolesForDB)
for i := 0; i < len(roles); i++ {
if roles[i] == "Admin" {
return true, nil return true, nil
} }
}
return RoleHasOperation(u.Role, op) return RoleHasOperation(roles, op)
} }
// MustPerm return *User for link program // MustPerm return *User for link program

View File

@ -333,12 +333,12 @@ insert into metric_description(metric, description) values('system_cpu_num_cores
insert into metric_description(metric, description) values('system_cpu_steal', '等待处理其他虚拟核的时间占比(单位:%)'); insert into metric_description(metric, description) values('system_cpu_steal', '等待处理其他虚拟核的时间占比(单位:%)');
insert into metric_description(metric, description) values('system_cpu_system', '内核态CPU时间占比(单位:%)'); insert into metric_description(metric, description) values('system_cpu_system', '内核态CPU时间占比(单位:%)');
insert into metric_description(metric, description) values('system_cpu_user', '用户态CPU时间占比(单位:%)'); insert into metric_description(metric, description) values('system_cpu_user', '用户态CPU时间占比(单位:%)');
insert into metric_description(metric, description) values('system_disk_bytes_free', '磁盘某分区余量大小(单位:kb'); insert into metric_description(metric, description) values('system_disk_bytes_free', '磁盘某分区余量大小(单位:byte');
insert into metric_description(metric, description) values('system_disk_used_percent', '磁盘某分区用量占比(单位:%'); insert into metric_description(metric, description) values('system_disk_used_percent', '磁盘某分区用量占比(单位:%');
insert into metric_description(metric, description) values('system_disk_read_time', '设备读操作耗时(单位ms)'); insert into metric_description(metric, description) values('system_disk_read_time', '设备读操作耗时(单位ms)');
insert into metric_description(metric, description) values('system_disk_read_time_percent', '读取磁盘时间百分比(单位:%'); insert into metric_description(metric, description) values('system_disk_read_time_percent', '读取磁盘时间百分比(单位:%');
insert into metric_description(metric, description) values('system_disk_bytes_total', '磁盘某分区总量(单位:kb'); insert into metric_description(metric, description) values('system_disk_bytes_total', '磁盘某分区总量(单位:byte');
insert into metric_description(metric, description) values('system_disk_bytes_used', '磁盘某分区用量大小(单位:kb'); insert into metric_description(metric, description) values('system_disk_bytes_used', '磁盘某分区用量大小(单位:byte');
insert into metric_description(metric, description) values('system_disk_write_time', '设备写操作耗时(单位ms)'); insert into metric_description(metric, description) values('system_disk_write_time', '设备写操作耗时(单位ms)');
insert into metric_description(metric, description) values('system_disk_write_time_percent', ''); insert into metric_description(metric, description) values('system_disk_write_time_percent', '');
insert into metric_description(metric, description) values('system_files_allocated', '系统已分配文件句柄数'); insert into metric_description(metric, description) values('system_files_allocated', '系统已分配文件句柄数');

View File

@ -31,6 +31,11 @@ type DataQueryParam struct {
Limit int `json:"limit"` Limit int `json:"limit"`
Start int64 `json:"start"` Start int64 `json:"start"`
End int64 `json:"end"` End int64 `json:"end"`
Step int64 `json:"step"`
}
type DataQueryInstantParam struct {
PromeQl string `json:"prome_ql"`
} }
type DataQueryParamOne struct { type DataQueryParamOne struct {
@ -59,6 +64,11 @@ type DataQueryResp struct {
PNum int `json:"pNum"` PNum int `json:"pNum"`
} }
type DataQueryInstanceResp struct {
Metric map[string]interface{} `json:"metric"`
Value []float64 `json:"value"`
}
type DataQL struct { type DataQL struct {
Start int64 `json:"start"` Start int64 `json:"start"`
End int64 `json:"end"` End int64 `json:"end"`