Exemplo n.º 1
0
func main() {
	cfg := flag.String("c", "cfg.json", "configuration file")
	version := flag.Bool("v", false, "show version")
	help := flag.Bool("h", false, "help")
	flag.Parse()

	if *version {
		fmt.Println(g.VERSION)
		os.Exit(0)
	}

	if *help {
		flag.Usage()
		os.Exit(0)
	}

	g.ParseConfig(*cfg)
	db.Init()

	go http.Start()
	go cron.UpdateItems()

	// sdk configuration
	graph.GraphLastUrl = g.Config().Api.GraphLast
	sender.Debug = g.Config().Debug
	sender.PostPushUrl = g.Config().Api.Push
	portal.HostnamesUrl = g.Config().Api.Hostnames

	sender.StartSender()

	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
	go func() {
		<-sigs
		fmt.Println()
		os.Exit(0)
	}()

	select {}
}
Exemplo n.º 2
0
func ReadClusterMonitorItems() (M map[string]*g.Cluster, err error) {
	M = make(map[string]*g.Cluster)
	sql := "SELECT `id`, `grp_id`, `numerator`, `denominator`, `endpoint`, `metric`, `tags`, `ds_type`, `step`, `last_update` FROM `cluster`"

	cfg := g.Config()
	ids := cfg.Database.Ids
	if len(ids) != 2 {
		log.Fatalln("ids configuration error")
	}

	if ids[0] != -1 && ids[1] != -1 {
		sql = fmt.Sprintf("%s WHERE `id` >= %d and `id` <= %d", sql, ids[0], ids[1])
	} else {
		if ids[0] != -1 {
			sql = fmt.Sprintf("%s WHERE `id` >= %d", sql, ids[0])
		}

		if ids[1] != -1 {
			sql = fmt.Sprintf("%s WHERE `id` <= %d", sql, ids[1])
		}
	}

	if cfg.Debug {
		log.Println(sql)
	}

	rows, err := DB.Query(sql)
	if err != nil {
		log.Println("[E]", err)
		return M, err
	}

	defer rows.Close()
	for rows.Next() {
		var c g.Cluster
		err = rows.Scan(&c.Id, &c.GroupId, &c.Numerator, &c.Denominator, &c.Endpoint, &c.Metric, &c.Tags, &c.DsType, &c.Step, &c.LastUpdate)
		if err != nil {
			log.Println("[E]", err)
			continue
		}

		M[fmt.Sprintf("%d%v", c.Id, c.LastUpdate)] = &c
	}

	return M, err
}
Exemplo n.º 3
0
func WorkerRun(item *g.Cluster) {
	debug := g.Config().Debug

	numeratorStr := cleanParam(item.Numerator)
	denominatorStr := cleanParam(item.Denominator)

	if !expressionValid(numeratorStr) || !expressionValid(denominatorStr) {
		log.Println("[W] invalid numerator or denominator", item)
		return
	}

	needComputeNumerator := needCompute(numeratorStr)
	needComputeDenominator := needCompute(denominatorStr)

	if !needComputeNumerator && !needComputeDenominator {
		log.Println("[W] no need compute", item)
		return
	}

	numeratorOperands, numeratorOperators := parse(numeratorStr, needComputeNumerator)
	denominatorOperands, denominatorOperators := parse(denominatorStr, needComputeDenominator)

	if !operatorsValid(numeratorOperators) || !operatorsValid(denominatorOperators) {
		log.Println("[W] operators invalid", item)
		return
	}

	hostnames, err := portal.Hostnames(fmt.Sprintf("%d", item.GroupId))
	if err != nil || len(hostnames) == 0 {
		return
	}

	now := time.Now().Unix()

	valueMap, err := queryCounterLast(numeratorOperands, denominatorOperands, hostnames, now-int64(item.Step*2), now)
	if err != nil {
		log.Println("[E]", err, item)
		return
	}

	var numerator, denominator float64
	var validCount int

	for _, hostname := range hostnames {
		var numeratorVal, denominatorVal float64
		var (
			numeratorValid   = true
			denominatorValid = true
		)

		if needComputeNumerator {
			numeratorVal, numeratorValid = compute(numeratorOperands, numeratorOperators, hostname, valueMap)
			if !numeratorValid && debug {
				log.Printf("[W] [hostname:%s] [numerator:%s] invalid or not found", hostname, item.Numerator)
			}
		}

		if needComputeDenominator {
			denominatorVal, denominatorValid = compute(denominatorOperands, denominatorOperators, hostname, valueMap)
			if !denominatorValid && debug {
				log.Printf("[W] [hostname:%s] [denominator:%s] invalid or not found", hostname, item.Denominator)
			}
		}

		if numeratorValid && denominatorValid {
			numerator += numeratorVal
			denominator += denominatorVal
			validCount += 1
		}
	}

	if !needComputeNumerator {
		if numeratorStr == "$#" {
			numerator = float64(validCount)
		} else {
			numerator, err = strconv.ParseFloat(numeratorStr, 64)
			if err != nil {
				log.Printf("[E] strconv.ParseFloat(%s) fail %v", numeratorStr, item)
				return
			}
		}
	}

	if !needComputeDenominator {
		if denominatorStr == "$#" {
			denominator = float64(validCount)
		} else {
			denominator, err = strconv.ParseFloat(denominatorStr, 64)
			if err != nil {
				log.Printf("[E] strconv.ParseFloat(%s) fail %v", denominatorStr, item)
				return
			}
		}
	}

	if denominator == 0 {
		log.Println("[W] denominator == 0", item)
		return
	}

	sender.Push(item.Endpoint, item.Metric, item.Tags, numerator/denominator, item.DsType, int64(item.Step))
}