func main() {
	var policies []clcv2.SBSServerPolicy
	var day = time.Now()
	var date = flag.String("start", day.Format("2006-01-02"), "Day to query storage usage for")
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "usage: %s [options]  <Server-Policy-ID | Server-Name>\n", path.Base(os.Args[0]))
		flag.PrintDefaults()
	}

	flag.Parse()
	if flag.NArg() != 1 {
		flag.Usage()
		os.Exit(0)
	}

	day, err := time.Parse("2006-01-02", *date)
	if err != nil {
		exit.Error("invalid backup query date %s (expected format: YYYY-MM-DD)", *date)
	}

	client, err := clcv2.NewCLIClient()
	if err != nil {
		exit.Fatal(err.Error())
	}

	// First look up the corresponding Server Policy or Policies, since the query needs the Account Policy ID.
	if utils.LooksLikeServerName(flag.Arg(0)) {
		policies, err = client.SBSgetServerPolicyDetails(flag.Arg(0))
		if err != nil {
			exit.Fatalf("failed to list SBS policies for server %s: %s", flag.Arg(0), err)
		}
	} else {
		p, err := client.SBSgetServerPolicy(flag.Arg(0))
		if err != nil {
			exit.Fatalf("failed to list SBS Server Policy %s: %s", flag.Arg(0), err)
		}
		policies = []clcv2.SBSServerPolicy{*p}
	}

	table := tablewriter.NewWriter(os.Stdout)
	table.SetAutoFormatHeaders(false)
	table.SetAlignment(tablewriter.ALIGN_CENTRE)
	table.SetAutoWrapText(false)
	table.SetHeader([]string{"Server",
		fmt.Sprintf("Usage on %s", day.Format("Mon, Jan 2 2006")),
		"Server Policy ID", "Account Policy ID",
	})

	for _, p := range policies {
		usage, err := client.SBSgetServerStorageUsage(p.AccountPolicyID, p.ID, day)
		if err != nil {
			exit.Fatalf("failed to obtain server %s storage use on %s: %s", p.ServerID, day.Format("2006-01-02"), err)
		}
		table.Append([]string{p.ServerID, humanize.Bytes(usage), p.ID, p.AccountPolicyID})
	}
	table.Render()
}
Example #2
0
func main() {
	var startTime = time.Now().AddDate(0, 0, -maxDateRange)
	var endTime time.Time
	var start = flag.String("start", startTime.Format("2006-01-02"), "Start date of the backup (format YYYY-MM-DD)")
	var end = flag.String("end", "", "End date of the backup (format YYYY-MM-DD)")
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "usage: %s [options]  <server-policy-ID>\n", path.Base(os.Args[0]))
		flag.PrintDefaults()
	}

	flag.Parse()
	if flag.NArg() != 1 {
		flag.Usage()
		os.Exit(0)
	}

	// Date range sanity checks
	startTime, err := time.Parse("2006-01-02", *start)
	if err != nil {
		exit.Error("invalid backup start date %s (expected format: YYYY-MM-DD)", *start)
	}

	if *end == "" {
		endTime = startTime.AddDate(0, 0, maxDateRange)
		if endTime.After(time.Now()) {
			endTime = time.Now()
		}
	} else if endTime, err = time.Parse("2006-01-02", *end); err != nil {
		exit.Error("invalid backup end date %s (expected format: YYYY-MM-DD)", *end)
	}

	client, err := clcv2.NewCLIClient()
	if err != nil {
		exit.Fatal(err.Error())
	}

	// Look up the Account Policy ID associated with the Server Policy ID
	p, err := client.SBSgetServerPolicy(flag.Arg(0))
	if err != nil {
		exit.Fatalf("failed to look up Account Policy of %s: %s.", flag.Arg(0), err)
	}

	restorePoints, err := client.SBSgetRestorePointDetails(p.AccountPolicyID, flag.Arg(0), startTime, endTime)
	if err != nil {
		exit.Fatalf("failed to list SBS restore point details found for server policy %s: %s", flag.Arg(0), err)
	}

	if len(restorePoints) == 0 {
		fmt.Printf("No restore point details found found for server %s between %s and %s.\n", p.ServerID,
			startTime.Format("Mon, 2 Jan 2006"), endTime.Format("Mon, 2 Jan 2006"))
	} else {
		table := tablewriter.NewWriter(os.Stdout)
		table.SetAutoFormatHeaders(false)
		table.SetAlignment(tablewriter.ALIGN_RIGHT)
		table.SetAutoWrapText(false)
		table.SetHeader([]string{"Start", "Duration", "Status",
			"Transferred", "Failed", "Removed", "Unchanged",
			"Total #Files", "Expires",
		})

		for _, r := range restorePoints {
			var duration = r.BackupFinishedDate.Sub(r.BackupStartedDate)
			var runtime = duration.String()

			// If the backup did not finish (yet), the FinishedDate is set to a Unix Epoch date in the past.
			if duration < 0 {
				runtime = "unknown"
			}

			table.Append([]string{
				r.BackupStartedDate.Local().Format("Mon, 15:04 MST"), runtime,
				r.RestorePointCreationStatus,
				fmtTransfer(r.FilesTransferredToStorage, r.BytesTransferredToStorage, duration),
				fmtTransfer(r.FilesFailedTransferToStorage, r.BytesFailedToTransfer, 0),
				fmtTransfer(r.FilesRemovedFromDisk, r.BytesInStorageForItemsRemoved, 0),
				fmtTransfer(r.UnchangedFilesNotTransferred, r.UnchangedBytesInStorage, 0),
				fmt.Sprint(r.NumberOfProtectedFiles),
				r.RetentionExpiredDate.Local().Format("Jan 2/2006"),
			})
		}
		table.Render()
	}
}