<commit_before>// Copyright 2014 The Serviced Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package storage

import (
	"io/ioutil"
	"os"
	"path"
	"reflect"
	"sort"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/control-center/serviced/coordinator/client"
	"github.com/control-center/serviced/domain/host"
	"github.com/zenoss/glog"
)

const monitorSubDir string = "/monitor"

// Monitor monitors the exporting of a file system to clients.
type Monitor struct {
	driver StorageDriver

	monitoredHosts     map[string]bool
	monitoredHostsLock sync.RWMutex

	shouldRestart   bool
	monitorInterval time.Duration
	previousRestart time.Time

	conn               client.Connection
	storageClientsPath string
}

// NewMonitor returns a Monitor object to monitor the exported file system
func NewMonitor(driver StorageDriver, monitorInterval time.Duration) (*Monitor, error) {
	m := &Monitor{
		driver:          driver,
		monitoredHosts:  make(map[string]bool),
		shouldRestart:   getShouldRestartDFSOnFailure(),
		monitorInterval: monitorInterval,
	}

	return m, nil
}

// UpdateRemoteMonitorFile is used by remote clients to write a tiny file to the DFS volume at the given cycle
func UpdateRemoteMonitorFile(localPath string, writeInterval time.Duration, ipAddr string, shutdown <-chan interface{}) {
	remoteFile := path.Join(localPath, monitorSubDir, ipAddr)
	glog.Infof("updating DFS volume monitor file %s at write interval: %s", remoteFile, writeInterval)

	for {
		glog.V(2).Infof("writing DFS file %s", remoteFile)
		if err := ioutil.WriteFile(remoteFile, []byte(ipAddr), 0600); err != nil {
			glog.Warningf("unable to write DFS file %s: %s", remoteFile, err)
		}

		// wait for next cycle or shutdown
		select {
		case <-time.After(writeInterval):

		case <-shutdown:
			glog.Infof("no longer writing remote monitor status for DFS volume %s to %s", localPath, remoteFile)
			return
		}
	}
}

// SetMonitorStorageClients sets the monitored remote IPs that are active
func (m *Monitor) SetMonitorStorageClients(conn client.Connection, storageClientsPath string) {
	m.conn = conn
	m.storageClientsPath = storageClientsPath
	m.SetMonitorRemoteHosts(getActiveRemoteHosts(conn, storageClientsPath, m.monitorInterval)...)
}

// SetMonitorRemoteHosts sets the remote hosts to monitor<commit_msg>ensure monitor path in DFS remote agents<commit_after>// Copyright 2014 The Serviced Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package storage

import (
	"io/ioutil"
	"os"
	"path"
	"reflect"
	"sort"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/control-center/serviced/coordinator/client"
	"github.com/control-center/serviced/domain/host"
	"github.com/zenoss/glog"
)

const monitorSubDir string = "/monitor"

// Monitor monitors the exporting of a file system to clients.
type Monitor struct {
	driver StorageDriver

	monitoredHosts     map[string]bool
	monitoredHostsLock sync.RWMutex

	shouldRestart   bool
	monitorInterval time.Duration
	previousRestart time.Time

	conn               client.Connection
	storageClientsPath string
}

// NewMonitor returns a Monitor object to monitor the exported file system
func NewMonitor(driver StorageDriver, monitorInterval time.Duration) (*Monitor, error) {
	m := &Monitor{
		driver:          driver,
		monitoredHosts:  make(map[string]bool),
		shouldRestart:   getShouldRestartDFSOnFailure(),
		monitorInterval: monitorInterval,
	}

	return m, nil
}

// UpdateRemoteMonitorFile is used by remote clients to write a tiny file to the DFS volume at the given cycle
func UpdateRemoteMonitorFile(localPath string, writeInterval time.Duration, ipAddr string, shutdown <-chan interface{}) {
	monitorPath := path.Join(localPath, monitorSubDir)
	remoteFile := path.Join(localPath, monitorSubDir, ipAddr)
	glog.Infof("updating DFS volume monitor file %s at write interval: %s", remoteFile, writeInterval)

	for {
		glog.V(2).Infof("checking DFS monitor path %s", monitorPath)
		_, err := os.Stat(monitorPath)
		if err != nil {
			glog.V(2).Infof("unable to stat DFS monitor path: %s %s", monitorPath, err)
			if err := os.MkdirAll(monitorPath, 0755); err != nil {
				glog.Warningf("unable to create DFS volume monitor path %s: %s", monitorPath, err)
			} else {
				glog.Infof("created DFS volume monitor path %s", monitorPath)
			}
		}

		glog.V(2).Infof("writing DFS file %s", remoteFile)
		if err := ioutil.WriteFile(remoteFile, []byte(ipAddr), 0600); err != nil {
			glog.Warningf("unable to write DFS file %s: %s", remoteFile, err)
		}

		// wait for next cycle or shutdown
		select {
		case <-time.After(writeInterval):

		case <-shutdown:
			glog.Infof("no longer writing remote monitor status for DFS volume %s to %s", localPath, remoteFile)
			return
		}
	}
}

// SetMonitorStorageClients sets the monitored remote IPs that are active
func (m *Monitor) SetMonitorStorageClients(conn client.Connection, storageClientsPath string) {
	m.conn = conn
	m.storageClientsPath = storageClientsPath
	m.SetMonitorRemoteHosts(getActiveRemoteHosts(conn, storageClientsPath, m.monitorInterval)...)
}

// SetMonitorRemoteHosts sets the remote hosts to monitor
{'fraction_non_alphanumeric': 0.09613187530602252, 'fraction_numerical': 0.005222784396931615, 'mean_word_length': 4.224211423699915, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '337235', 'n_tokens_mistral': 1927, 'n_tokens_neox': 1815, 'n_words': 626}