Example #1
0
func getStats() {
	file, err := ioutil.TempFile("", "sar")
	if err != nil {
		log.Print(err)
		return
	}

	defer func() {
		err = os.Remove(file.Name())
		if err != nil {
			log.Print(err)
		}
	}()

	err = exec.Command("sar", "1", "1", "-A", "-o", file.Name()).Run()
	if err != nil {
		log.Print(err)
		return
	}

	cmd := exec.Command("sadf", "1", "1", "--", "-A", file.Name())
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		log.Print(err)
		return
	}

	if err := cmd.Start(); err != nil {
		log.Print(err)
		return
	}

	csv := csv.NewReader(stdout)
	csv.Comma = '	'
	records, err := csv.ReadAll()
	if err != nil {
		log.Print(err)
		return
	}

	if err := cmd.Wait(); err != nil {
		log.Print(err)
		return
	}

	for _, record := range records {
		_ = sendRecord(record)
	}
}
Example #2
0
func importCsvResult(r io.Reader, acc plugins.Accumulator, host string) ([][]string, error) {
	csv := csv.NewReader(r)
	result, err := csv.ReadAll()

	for _, row := range result {

		for field, v := range row {
			tags := map[string]string{
				"host":  host,
				"proxy": row[HF_PXNAME],
				"sv":    row[HF_SVNAME],
			}
			switch field {
			case HF_QCUR:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("qcur", ival, tags)
				}
			case HF_QMAX:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("qmax", ival, tags)
				}
			case HF_SCUR:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("scur", ival, tags)
				}
			case HF_SMAX:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("smax", ival, tags)
				}
			case HF_BIN:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("bin", ival, tags)
				}
			case HF_BOUT:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("bout", ival, tags)
				}
			case HF_DREQ:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("dreq", ival, tags)
				}
			case HF_DRESP:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("dresp", ival, tags)
				}
			case HF_RATE:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("rate", ival, tags)
				}
			case HF_RATE_MAX:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("rate_max", ival, tags)
				}
			case HF_STOT:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("stot", ival, tags)
				}
			case HF_HRSP_1xx:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("http_response.1xx", ival, tags)
				}
			case HF_HRSP_2xx:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("http_response.2xx", ival, tags)
				}
			case HF_HRSP_3xx:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("http_response.3xx", ival, tags)
				}
			case HF_HRSP_4xx:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("http_response.4xx", ival, tags)
				}
			case HF_EREQ:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("ereq", ival, tags)
				}
			case HF_ERESP:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("eresp", ival, tags)
				}
			case HF_ECON:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("econ", ival, tags)
				}
			case HF_WRETR:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("wretr", ival, tags)
				}
			case HF_WREDIS:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("wredis", ival, tags)
				}
			case HF_REQ_RATE:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("req_rate", ival, tags)
				}
			case HF_REQ_RATE_MAX:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("req_rate_max", ival, tags)
				}
			case HF_REQ_TOT:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("req_tot", ival, tags)
				}
			case HF_THROTTLE:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("throttle", ival, tags)
				}
			case HF_LBTOT:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("lbtot", ival, tags)
				}

			}

		}
	}
	return result, err
}
Example #3
0
func importCsvResult(r io.Reader, acc telegraf.Accumulator, host string) error {
	csv := csv.NewReader(r)
	result, err := csv.ReadAll()
	now := time.Now()

	for _, row := range result {
		fields := make(map[string]interface{})
		tags := map[string]string{
			"server": host,
			"proxy":  row[HF_PXNAME],
			"sv":     row[HF_SVNAME],
		}
		for field, v := range row {
			switch field {
			case HF_QCUR:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["qcur"] = ival
				}
			case HF_QMAX:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["qmax"] = ival
				}
			case HF_SCUR:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["scur"] = ival
				}
			case HF_SMAX:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["smax"] = ival
				}
			case HF_STOT:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["stot"] = ival
				}
			case HF_BIN:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["bin"] = ival
				}
			case HF_BOUT:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["bout"] = ival
				}
			case HF_DREQ:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["dreq"] = ival
				}
			case HF_DRESP:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["dresp"] = ival
				}
			case HF_EREQ:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["ereq"] = ival
				}
			case HF_ECON:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["econ"] = ival
				}
			case HF_ERESP:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["eresp"] = ival
				}
			case HF_WRETR:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["wretr"] = ival
				}
			case HF_WREDIS:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["wredis"] = ival
				}
			case HF_ACT:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["active_servers"] = ival
				}
			case HF_BCK:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["backup_servers"] = ival
				}
			case HF_DOWNTIME:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["downtime"] = ival
				}
			case HF_THROTTLE:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["throttle"] = ival
				}
			case HF_LBTOT:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["lbtot"] = ival
				}
			case HF_RATE:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["rate"] = ival
				}
			case HF_RATE_MAX:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["rate_max"] = ival
				}
			case HF_CHECK_DURATION:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["check_duration"] = ival
				}
			case HF_HRSP_1xx:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["http_response.1xx"] = ival
				}
			case HF_HRSP_2xx:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["http_response.2xx"] = ival
				}
			case HF_HRSP_3xx:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["http_response.3xx"] = ival
				}
			case HF_HRSP_4xx:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["http_response.4xx"] = ival
				}
			case HF_HRSP_5xx:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["http_response.5xx"] = ival
				}
			case HF_REQ_RATE:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["req_rate"] = ival
				}
			case HF_REQ_RATE_MAX:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["req_rate_max"] = ival
				}
			case HF_REQ_TOT:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["req_tot"] = ival
				}
			case HF_CLI_ABRT:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["cli_abort"] = ival
				}
			case HF_SRV_ABRT:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["srv_abort"] = ival
				}
			case HF_QTIME:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["qtime"] = ival
				}
			case HF_CTIME:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["ctime"] = ival
				}
			case HF_RTIME:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["rtime"] = ival
				}
			case HF_TTIME:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					fields["ttime"] = ival
				}
			}
		}
		acc.AddFields("haproxy", fields, tags, now)
	}
	return err
}
Example #4
0
// ParseCSVResult - XXX
func ParseCSVResult(r io.Reader, host string, perf *PerformanceStruct) error {
	csv := csv.NewReader(r)
	result, err := csv.ReadAll()
	if err != nil {
		return fmt.Errorf("Unable to Parse HAProxy CSV: %s", err.Error())
	}

	gauges := make(map[string]interface{})
	counters := make(map[string]interface{})
	for _, row := range result {
		HostName := row[HF_SVNAME]
		ProxyName := row[HF_PXNAME]
		// ServerType := row[HF_TYPE]

		Key := fmt.Sprintf("%s.%s", ProxyName, HostName)

		for field, v := range row {
			switch field {
			case HF_QCUR:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					GaugeKey := fmt.Sprintf("queue.current.%s", Key)
					gauges[GaugeKey] = ival
				}
			case HF_QLIMIT:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					GaugeKey := fmt.Sprintf("queue.max.%s", Key)
					gauges[GaugeKey] = ival
				}
			case HF_SCUR:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					GaugeKey := fmt.Sprintf("session.current.%s", Key)
					gauges[GaugeKey] = ival
				}
			case HF_SMAX:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					GaugeKey := fmt.Sprintf("session.max.%s", Key)
					gauges[GaugeKey] = ival
				}

			case HF_REQ_RATE:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					GaugeKey := fmt.Sprintf("requests.rate.%s", Key)
					gauges[GaugeKey] = ival
				}

			case HF_REQ_RATE_MAX:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					GaugeKey := fmt.Sprintf("requests.rate_peak.%s", Key)
					gauges[GaugeKey] = ival
				}

			case HF_RTIME:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					GaugeKey := fmt.Sprintf("response.time.%s", Key)
					gauges[GaugeKey] = ival
				}

			case HF_TTIME:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					GaugeKey := fmt.Sprintf("session_time.%s", Key)
					gauges[GaugeKey] = ival
				}

			case HF_BIN:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					GaugeKey := fmt.Sprintf("bytes.in.%s", Key)
					gauges[GaugeKey] = ival
				}
			case HF_BOUT:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					GaugeKey := fmt.Sprintf("bytes.out.%s", Key)
					gauges[GaugeKey] = ival
				}

			case HF_HRSP_1xx:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					CounterKey := fmt.Sprintf("http_response.1xx.%s", Key)
					counters[CounterKey] = ival
				}
			case HF_HRSP_2xx:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					CounterKey := fmt.Sprintf("http_response.2xx.%s", Key)
					counters[CounterKey] = ival
				}
			case HF_HRSP_3xx:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					CounterKey := fmt.Sprintf("http_response.3xx.%s", Key)
					counters[CounterKey] = ival
				}
			case HF_HRSP_4xx:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					CounterKey := fmt.Sprintf("http_response.4xx.%s", Key)
					counters[CounterKey] = ival
				}
			case HF_HRSP_5xx:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					CounterKey := fmt.Sprintf("http_response.5xx.%s", Key)
					counters[CounterKey] = ival
				}

			case HF_ECON:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					CounterKey := fmt.Sprintf("error_connection_count.%s", Key)
					counters[CounterKey] = ival
				}

			case HF_DREQ:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					CounterKey := fmt.Sprintf("denied_request_count.%s", Key)
					counters[CounterKey] = ival
				}

			case HF_DRESP:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					CounterKey := fmt.Sprintf("denied_response_count.%s", Key)
					counters[CounterKey] = ival
				}

			case HF_EREQ:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					CounterKey := fmt.Sprintf("error_request_count.%s", Key)
					counters[CounterKey] = ival
				}

			case HF_ERESP:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					CounterKey := fmt.Sprintf("error_response_count.%s", Key)
					counters[CounterKey] = ival
				}

			case HF_WRETR:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					CounterKey := fmt.Sprintf("conn_retry_count.%s", Key)
					counters[CounterKey] = ival
				}

			case HF_WREDIS:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					CounterKey := fmt.Sprintf("redispatch_count.%s", Key)
					counters[CounterKey] = ival
				}

			case HF_DOWNTIME:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					CounterKey := fmt.Sprintf("downtime_seconds.%s", Key)
					counters[CounterKey] = ival
				}

			case HF_CHKFAIL:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					CounterKey := fmt.Sprintf("failed_check_count.%s", Key)
					counters[CounterKey] = ival
				}

			case HF_LBTOT:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					CounterKey := fmt.Sprintf("selection_count.%s", Key)
					counters[CounterKey] = ival
				}

			}
		}

	}
	perf.Gauges = gauges
	perf.Counters = counters

	return nil
}
Example #5
0
func importCsvResult(r io.Reader, acc plugins.Accumulator, host string) ([][]string, error) {
	csv := csv.NewReader(r)
	result, err := csv.ReadAll()

	for _, row := range result {

		for field, v := range row {
			tags := map[string]string{
				"server": host,
				"proxy":  row[HF_PXNAME],
				"sv":     row[HF_SVNAME],
			}
			switch field {
			case HF_QCUR:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("qcur", ival, tags)
				}
			case HF_QMAX:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("qmax", ival, tags)
				}
			case HF_SCUR:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("scur", ival, tags)
				}
			case HF_SMAX:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("smax", ival, tags)
				}
			case HF_STOT:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("stot", ival, tags)
				}
			case HF_BIN:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("bin", ival, tags)
				}
			case HF_BOUT:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("bout", ival, tags)
				}
			case HF_DREQ:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("dreq", ival, tags)
				}
			case HF_DRESP:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("dresp", ival, tags)
				}
			case HF_EREQ:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("ereq", ival, tags)
				}
			case HF_ECON:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("econ", ival, tags)
				}
			case HF_ERESP:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("eresp", ival, tags)
				}
			case HF_WRETR:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("wretr", ival, tags)
				}
			case HF_WREDIS:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("wredis", ival, tags)
				}
			case HF_ACT:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("active_servers", ival, tags)
				}
			case HF_BCK:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("backup_servers", ival, tags)
				}
			case HF_DOWNTIME:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("downtime", ival, tags)
				}
			case HF_THROTTLE:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("throttle", ival, tags)
				}
			case HF_LBTOT:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("lbtot", ival, tags)
				}
			case HF_RATE:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("rate", ival, tags)
				}
			case HF_RATE_MAX:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("rate_max", ival, tags)
				}
			case HF_CHECK_DURATION:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("check_duration", ival, tags)
				}
			case HF_HRSP_1xx:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("http_response.1xx", ival, tags)
				}
			case HF_HRSP_2xx:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("http_response.2xx", ival, tags)
				}
			case HF_HRSP_3xx:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("http_response.3xx", ival, tags)
				}
			case HF_HRSP_4xx:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("http_response.4xx", ival, tags)
				}
			case HF_HRSP_5xx:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("http_response.5xx", ival, tags)
				}
			case HF_REQ_RATE:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("req_rate", ival, tags)
				}
			case HF_REQ_RATE_MAX:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("req_rate_max", ival, tags)
				}
			case HF_REQ_TOT:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("req_tot", ival, tags)
				}
			case HF_CLI_ABRT:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("cli_abort", ival, tags)
				}
			case HF_SRV_ABRT:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("srv_abort", ival, tags)
				}
			case HF_QTIME:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("qtime", ival, tags)
				}
			case HF_CTIME:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("ctime", ival, tags)
				}
			case HF_RTIME:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("rtime", ival, tags)
				}
			case HF_TTIME:
				ival, err := strconv.ParseUint(v, 10, 64)
				if err == nil {
					acc.Add("ttime", ival, tags)
				}

			}

		}
	}
	return result, err
}