示例#1
0
func NmonListMeasurement(c *cli.Context) {
	// parsing parameters
	params := ParseParameters(c)

	influxdb := influxdbclient.NewInfluxDB(params.Server, params.Port, params.Db, params.User, params.Password)
	influxdb.SetDebug(params.Debug)
	influxdb.Connect()

	filters := new(influxdbclient.Filters)

	if len(params.Host) > 0 {
		filters.Add("host", params.Host, "text")
	}

	measurements, _ := influxdb.ListMeasurement(filters)
	if measurements != nil {
		fmt.Printf("%s\n", measurements.Name)
		for _, value := range measurements.Datas {
			if len(params.Filter) == 0 {
				fmt.Printf("%s\n", value)
				continue
			}
			matched, _ := regexp.MatchString(params.Filter, value)
			if matched {
				fmt.Printf("%s\n", value)
			}
		}
	}
}
示例#2
0
func NmonStat(c *cli.Context) {
	// parsing parameters
	params := ParseStatsParameters(c)

	if len(params.Metric) == 0 {
		fmt.Printf("No metric specified ! Use -h option for help !\n")
		os.Exit(1)
	}

	influxdb := influxdbclient.NewInfluxDB(params.Server, params.Port, params.Db, params.User, params.Password)
	influxdb.SetDebug(params.Debug)
	influxdb.Connect()

	metric := params.Metric

	filters := new(influxdbclient.Filters)

	filters.Add("host", params.StatsHost, "text")

	if len(params.Filter) > 0 {
		filters.Add("name", params.Filter, "regexp")
	}
	fromUnix, _ := ConvertTimeStamp(params.From, params.TZ)
	fromTime := fromUnix.Format(querytimeformat)
	toUnix, _ := ConvertTimeStamp(params.To, params.TZ)
	toTime := toUnix.Format(querytimeformat)
	result, err := influxdb.ReadPoints("value", filters, "name", metric, fromTime, toTime, "")
	if err != nil {
		check(err)
	}

	//generate stats
	stats := influxdbclient.BuildStats(result)

	DisplayStats(&stats, params.Sort, params.Limit)
}
示例#3
0
func NmonImport(c *cli.Context) {

	if len(c.Args()) < 1 {
		fmt.Printf("file name needs to be provided\n")
		os.Exit(1)
	}
	// parsing parameters
	params := ParseParameters(c)

	nmon := InitNmon(params)

	influxdb := influxdbclient.NewInfluxDB(params.Server, params.Port, params.Db, params.User, params.Password)
	nmon.Debug = params.Debug
	influxdb.SetDebug(params.Debug)

	influxdb.Connect()

	if exist, _ := influxdb.ExistDB(params.Db); exist != true {
		_, err := influxdb.CreateDB(params.Db)
		check(err)
	}

	file, err := os.Open(params.Name)
	check(err)

	defer file.Close()
	reader := bufio.NewReader(file)
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanLines)

	var lines []string

	for scanner.Scan() {
		lines = append(lines, scanner.Text())
	}

	sort.Strings(lines)

	for _, line := range lines {

		if cpuallRegexp.MatchString(line) && !params.CpuAll {
			continue
		}

		if diskallRegexp.MatchString(line) && params.NoDisks {
			continue
		}

		if skipRegexp.MatchString(line) {
			continue
		}

		if statsRegexp.MatchString(line) {
			matched := statsRegexp.FindStringSubmatch(line)
			elems := strings.Split(line, ",")
			timeStr, err := nmon.GetTimeStamp(matched[1])
			check(err)
			name := elems[0]
			timestamp, err := ConvertTimeStamp(timeStr, nmon.Params.TZ)

			for i, value := range elems[2:] {
				if len(nmon.DataSeries[name].Columns) < i+1 {
					if nmon.Debug {
						fmt.Printf("Entry added position %d in serie %s since nmon start: skipped\n", i+1, name)
					}
					continue
				}
				tags := map[string]string{"host": nmon.Hostname, "name": nmon.DataSeries[name].Columns[i]}

				// try to convert string to integer
				converted, err := strconv.ParseFloat(value, 64)
				if err != nil {
					//if not working, skip to next value. We don't want text values in InfluxDB.
					continue
				}

				//send integer if it worked
				field := map[string]interface{}{"value": converted}

				measurement := ""
				if nfsRegexp.MatchString(name) {
					measurement = name
				} else {
					measurement = nameRegexp.ReplaceAllString(name, "")
				}

				influxdb.AddPoint(measurement, timestamp, field, tags)

				if influxdb.PointsCount() == 10000 {
					err = influxdb.WritePoints()
					check(err)
					influxdb.ClearPoints()
					fmt.Printf("#")
				}
			}
		}
	}
	// flushing remaining data
	influxdb.WritePoints()
	fmt.Printf("#\n")

	fmt.Printf("File %s imported !\n", params.Name)

}