Example #1
0
// wanted format:
// Nov 25 23:05:18.250: INFO: densityN-X Pods: 12000 out of 12000 created, 1012 running,
// 23 pending, 10965 waiting, 0 inactive, 0 terminating, 0 unknown, 0 runningButNotReady
func parseDensity(r io.Reader) (results []densityResult) {
	densityFormat := "Pods: %d out of %d created, %d running, %d pending, %d waiting, %s"
	//10 seconds interval
	interval := 10
	seconds := interval

	br := bufio.NewReader(r)
	for {
		var r densityResult
		var garbageString string

		bytes, err := br.ReadBytes('\n')
		if err != nil {
			if err != io.EOF {
				panic(err)
			}
			break
		}

		line := strings.TrimSpace(string(bytes[:len(bytes)-1]))

		if !strings.HasSuffix(line, "runningButNotReady") {
			continue
		}

		pi := strings.Index(line, "Pods")
		if pi == -1 {
			fmt.Fprintln(os.Stderr, "Bad density format: cannot find Pods")
			os.Exit(1)
		}

		line = line[pi:]

		_, err = fmt.Sscanf(line, densityFormat, &r.created, &totalPods, &r.running,
			&r.pending, &r.waiting, &garbageString)

		if err != nil {
			fmt.Fprintln(os.Stderr, "Bad density format:", err)
			os.Exit(1)
		}

		r.seconds = seconds
		seconds += interval

		results = append(results, r)
	}
	return results
}