code: func (a *Agent) persistCheckState(check *checks.CheckTTL, status, output string) error {
  	// Create the persisted state
  	state := persistedCheckState{
  		CheckID: check.CheckID,
  		Status:  status,
  		Output:  output,
  		Expires: time.Now().Add(check.TTL).Unix(),
  	}
  
  	// Encode the state
  	buf, err := json.Marshal(state)
  	if err != nil {
  		return err
  	}
  
  	// Create the state dir if it doesn't exist
  	dir := filepath.Join(a.config.DataDir, checkStateDir)
  	if err := os.MkdirAll(dir, 0700); err != nil {
  		return fmt.Errorf("failed creating check state dir %q: %s", dir, err)
  	}
  
  	// Write the state to the file
  	file := filepath.Join(dir, checkIDHash(check.CheckID))
  
  	// Create temp file in same dir, to make more likely atomic
  	tempFile := file + ".tmp"
  
  	// persistCheckState is called frequently, so don't use writeFileAtomic to avoid calling fsync here
  	if err := ioutil.WriteFile(tempFile, buf, 0600); err != nil {
  		return fmt.Errorf("failed writing temp file %q: %s", tempFile, err)
  	}
  	if err := os.Rename(tempFile, file); err != nil {
  		return fmt.Errorf("failed to rename temp file from %q to %q: %s", tempFile, file, err)
  	}
  
  	return nil
  }
docs: // persistCheckState is used to record the check status into the data dir.
  // This allows the state to be restored on a later agent start. Currently
  // only useful for TTL based checks.