Exemplo n.º 1
0
// Connect to the database and spin, running the top command and outputting
// the results appropriately.
func (self *MongoTop) Run() error {

	// test the connection
	session, err := self.SessionProvider.GetSession()
	if err != nil {
		return err
	}
	session.Close()

	connUrl := self.Options.Host
	if self.Options.Port != "" {
		connUrl = connUrl + ":" + self.Options.Port
	}
	util.Printlnf("connected to: %v", connUrl)

	// the results used to be compared to each other
	var previousResults command.Command
	if self.OutputOptions.Locks {
		previousResults = &command.ServerStatus{}
	} else {
		previousResults = &command.Top{}
	}

	// populate the first run of the previous results
	err = self.SessionProvider.RunCommand("admin", previousResults)
	if err != nil {
		return fmt.Errorf("error running top command: %v", err)
	}

	for {

		// sleep
		time.Sleep(self.Sleeptime)

		var topResults command.Command
		if self.OutputOptions.Locks {
			topResults = &command.ServerStatus{}
		} else {
			topResults = &command.Top{}
		}

		// run the top command against the database
		err = self.SessionProvider.RunCommand("admin", topResults)
		if err != nil {
			return fmt.Errorf("error running top command: %v", err)
		}

		// diff the results
		diff, err := topResults.Diff(previousResults)
		if err != nil {
			return fmt.Errorf("error computing diff: %v", err)
		}

		// output the results
		if err := self.Outputter.Output(diff); err != nil {
			return fmt.Errorf("error outputting results: %v", err)
		}

		// update the previous results
		previousResults = topResults

		if self.Once {
			return nil
		}

	}

}
Exemplo n.º 2
0
// Print the tool version to stdout.  Returns whether or not the version flag
// is specified.
func (self *ToolOptions) PrintVersion() bool {
	if self.Version {
		util.Printlnf("%v version: %v", self.AppName, self.VersionStr)
	}
	return self.Version
}