func GetDbConn() (conn *sql.DB, err error) { conn, err = sql.Open("mysql", g.Config().Index.Dsn) if err != nil { return nil, err } conn.SetMaxIdleConns(g.Config().Index.MaxIdle) err = conn.Ping() if err != nil { conn.Close() } return conn, err }
func configCommonRoutes() { http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("ok\n")) }) http.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(fmt.Sprintf("%s\n", g.VERSION))) }) http.HandleFunc("/workdir", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(fmt.Sprintf("%s\n", file.SelfDir()))) }) http.HandleFunc("/config", func(w http.ResponseWriter, r *http.Request) { RenderDataJson(w, g.Config()) }) http.HandleFunc("/config/reload", func(w http.ResponseWriter, r *http.Request) { if strings.HasPrefix(r.RemoteAddr, "127.0.0.1") { g.ParseConfig(g.ConfigFile) RenderDataJson(w, "ok") } else { RenderDataJson(w, "no privilege") } }) }
// 启动 索引全量更新 定时任务 func StartIndexUpdateAllTask() { for graphAddr, cronSpec := range g.Config().Index.Cluster { ga := graphAddr indexUpdateAllCron.AddFuncCC(cronSpec, func() { UpdateIndexOfOneGraph(ga, "cron") }, 1) } indexUpdateAllCron.Start() }
func Start() { if !g.Config().Collector.Enable { log.Println("collector.Start warning, not enable") return } // init url if g.Config().Collector.DestUrl != "" { destUrl = g.Config().Collector.DestUrl } if g.Config().Collector.SrcUrlFmt != "" { srcUrlFmt = g.Config().Collector.SrcUrlFmt } // start go startCollectorCron() log.Println("collector.Start, ok") }
// 初始化索引功能模块 func Start() { cfg := g.Config() if !cfg.Index.Enable { log.Println("index.Start warning, not enable") return } InitDB() if cfg.Index.AutoDelete { StartIndexDeleteTask() log.Println("index.Start warning, index cleaner enable") } StartIndexUpdateAllTask() log.Println("index.Start ok") }
// 手动触发全量更新 func UpdateAllIndex() { for graphAddr, _ := range g.Config().Index.Cluster { UpdateIndexOfOneGraph(graphAddr, "manual") } }
func _collect() { clientGet := nhttpclient.GetHttpClient("collector.get", 10*time.Second, 20*time.Second) tags := "type=statistics,pdl=falcon" for _, host := range g.Config().Collector.Cluster { ts := time.Now().Unix() jsonList := make([]*cmodel.JsonMetaData, 0) // get statistics by http-get hostInfo := strings.Split(host, ",") // "module,hostname:port,endpoint" if len(hostInfo) != 3 { continue } hostModule := hostInfo[0] hostNamePort := hostInfo[1] hostNamePortList := strings.Split(hostNamePort, ":") if len(hostNamePortList) != 2 { continue } hostName := hostInfo[2] hostPort := hostNamePortList[1] myTags := tags + ",module=" + hostModule + ",port=" + hostPort srcUrl := fmt.Sprintf(srcUrlFmt, hostNamePort) reqGet, _ := http.NewRequest("GET", srcUrl, nil) reqGet.Header.Set("Connection", "close") getResp, err := clientGet.Do(reqGet) if err != nil { log.Printf(hostNamePort+", get statistics error,", err) continue } defer getResp.Body.Close() body, err := ioutil.ReadAll(getResp.Body) if err != nil { log.Println(hostNamePort+", get statistics error,", err) continue } var data Dto err = json.Unmarshal(body, &data) if err != nil { log.Println(hostNamePort+", get statistics error,", err) continue } for _, item := range data.Data { if item["Name"] == nil { continue } itemName := item["Name"].(string) if item["Cnt"] != nil { var jmdCnt cmodel.JsonMetaData jmdCnt.Endpoint = hostName jmdCnt.Metric = itemName jmdCnt.Timestamp = ts jmdCnt.Step = 60 jmdCnt.Value = int64(item["Cnt"].(float64)) jmdCnt.CounterType = "GAUGE" jmdCnt.Tags = myTags jsonList = append(jsonList, &jmdCnt) } if item["Qps"] != nil { var jmdQps cmodel.JsonMetaData jmdQps.Endpoint = hostName jmdQps.Metric = itemName + ".Qps" jmdQps.Timestamp = ts jmdQps.Step = 60 jmdQps.Value = int64(item["Qps"].(float64)) jmdQps.CounterType = "GAUGE" jmdQps.Tags = myTags jsonList = append(jsonList, &jmdQps) } } // format result err = sendToTransfer(jsonList, destUrl) if err != nil { log.Println(hostNamePort, "send to transfer error,", err.Error()) } } // collector.alive _collectorAlive() }