Exemplo n.º 1
0
func Create(config config.Config) (Graphite, error) {
	g := Graphite{}

	hostname, port := splitHost(config.Graphite)

	conn, err := grp.NewGraphite(hostname, port)
	if err != nil {
		return g, err
	}

	g.Connection = conn
	err = g.Connection.Disconnect()
	if err != nil {
		return g, err
	}

	return g, nil
}
Exemplo n.º 2
0
func NewGraphite(method string, key string, args []byte) error {

	runtime.GOMAXPROCS(runtime.NumCPU())

	//parse config
	log.Debug("%s\n", *Configfile)
	cfg, err = ini.LoadFile(*Configfile)

	if err != nil {
		log.Fatalf("parse config "+*Configfile+" file error: %s", err)
	}

	logfile, ok := cfg.Get("system", "logfile")
	if !ok {
		log.Fatal("'logfile' missing from 'system' section")
	}

	logDir := logfile[0:strings.LastIndex(logfile, "/")]
	err = os.MkdirAll(logDir, 0777)
	if err != nil {
		log.Fatalf("MkdirAll error: %s", err)
	}

	//open log file
	logFile, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		log.Fatalf("%s, %s ", logfile, err)
	}

	defer func() {
		logFile.WriteString(fmt.Sprintf("closing %s", time.UnixDate))
		logFile.Close()
	}()

	logback := logging.NewLogBackend(logFile, "", 0)
	logformatted := logging.NewBackendFormatter(logback, logFormat)

	loglevel, ok := cfg.Get("system", "loglevel")
	if !ok {
		log.Fatal("'loglevel' missing from 'system' section")
	}

	loglevel = strings.ToUpper(loglevel)

	Gloglevel, err = logging.LogLevel(loglevel)
	if err != nil {
		Gloglevel = logging.DEBUG
	}

	logging.SetBackend(logformatted)

	//see what we have here
	log.Debug("BUILD: %s\n", Build)
	for name, section := range cfg {
		log.Debug("Section: %v\n", name)
		for k, v := range section {
			log.Debug("%v: %v\n", k, v)
		}
	}

	host, ok := cfg.Get("server", "host")
	if !ok {
		log.Fatal("'server' missing from 'host' section")
	}
	var port int
	port_str, ok := cfg.Get("server", "port")
	if !ok {
		log.Fatal("'server' missing from 'port' section")
	} else {
		v, err := strconv.Atoi(port_str)
		if err != nil {
			log.Warning("'server' missing from 'port' section")
		} else {
			port = v
		}
	}

	Graphite, err = graphite.NewGraphite(host, port)
	log.Debug("Loaded Graphite connection: %#v", Graphite)
	if method == "simplesend" {
		var meta map[string]interface{}
		json.Unmarshal(args, &meta)
		err = Graphite.SimpleSend(key, getArg(meta, "Value"))
		log.Debug("Loaded Graphite SimpleSend: %#v", err)
	} else if method == "sendmetric" {
		var meta map[string]interface{}
		json.Unmarshal(args, &meta)
		Metric.Name = key
		Metric.Value = getArg(meta, "Value")
		Metric.Timestamp = time.Now().Unix()
		err = Graphite.SendMetric(Metric)
		log.Debug("Loaded Graphite SendMetric: %#v", err)
	} else if method == "sendmetrics" {
		metas := make([]map[string]interface{}, 0)
		json.Unmarshal(args, &metas)
		log.Debug("metas: %#v", metas)
		for _, meta := range metas {
			Metric.Name = key
			Metric.Value = getArg(meta, "Value")
			Metric.Timestamp, _ = strconv.ParseInt(getArg(meta, "Date"), 10, 64)
			Metrics = append(Metrics, Metric)
		}
		log.Debug("Metrics: %#v", Metrics)
		err = Graphite.SendMetrics(Metrics)
		log.Debug("Loaded Graphite SendMetric: %#v", err)
	}
	Graphite.Disconnect()
	//	logging.SetLevel(Gloglevel, "")

	if err != nil {
		return err
	}
	return nil
}
Exemplo n.º 3
0
func sendMetrics(status []Status, config Configuration) {
	var Graphite, err = graphite.NewGraphite(config.MetricsHost, config.MetricsPort)
	if err != nil {
		log.Error("Can't connect to graphite collector: %v", err)
		return
	}

	for _, entry := range status {
		if entry.QCur != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".queued_requests"), entry.QCur)
		}
		if entry.SCur != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".current_sessions"), entry.SCur)
		}
		if entry.STot != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".total_connections"), entry.STot)
		}
		if entry.BytesIn != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".bytes_in"), entry.BytesIn)
		}
		if entry.BytesOut != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".bytes_out"), entry.BytesOut)
		}
		if entry.EReq != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".errors_request"), entry.EReq)
		}
		if entry.Econ != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".errors_connecting"), entry.Econ)
		}
		if entry.EResp != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".errors_response"), entry.EResp)
		}
		if entry.Chkfail != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".checks_failed"), entry.Chkfail)
		}
		if entry.ChkDown != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".switches_to_down"), entry.ChkDown)
		}
		if entry.Downtime != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".total_downtime"), entry.Downtime)
		}
		if entry.ChkDur != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".checks_duration"), entry.ChkDur)
		}
		if entry.HRSP1 != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".hrsp_1xx"), entry.HRSP1)
		}
		if entry.HRSP2 != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".hrsp_2xx"), entry.HRSP2)
		}
		if entry.HRSP3 != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".hrsp_3xx"), entry.HRSP3)
		}
		if entry.HRSP4 != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".hrsp_4xx"), entry.HRSP4)
		}
		if entry.HRSP5 != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".hrsp_5xx"), entry.HRSP5)
		}
		if entry.HRSPO != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".hrsp_other"), entry.HRSPO)
		}
		if entry.ReqTot != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".total_requests"), entry.ReqTot)
		}
		if entry.QTime != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".time_queue"), entry.QTime)
		}
		if entry.CTime != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".time_connect"), entry.CTime)
		}
		if entry.RTime != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".time_response"), entry.RTime)
		}
		if entry.TTime != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".time_total_session"), entry.TTime)
		}
		if entry.CurrReqRate != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".current_request_rate"), entry.CurrReqRate)
		}
		if entry.MaxReqRate != "" {
			Graphite.SimpleSend(fmt.Sprint(config.MetricsPrefix, ".haproxy.", entry.Type, ".", entry.Name, ".max_request_rate"), entry.MaxReqRate)
		}
	}
}