Exemple #1
0
func fupdatePos(f *os.File, pos int64) {
	info, err := f.Stat()
	if err != nil {
		log.Errorf("Stat %s failed - %v", f.Name(), err)
		return
	}
	fname := filepath.Base(f.Name())
	if err := update(info.Sys().(*syscall.Stat_t).Ino, fname, pos); err != nil {
		log.Errorf("Update %s position(%d) failed - %v", f.Name(), pos, err)
	}
}
Exemple #2
0
func fgetPos(f *os.File) (string, int64, error) {
	info, err := f.Stat()
	if err != nil {
		log.Errorf("Stat %s failed - %v", f.Name(), err)
		return "", 0, err
	}
	fname, pos, err := get(info.Sys().(*syscall.Stat_t).Ino)
	if err != nil {
		if err != ErrNotFound {
			log.Errorf("Get %s position failed - %v", f.Name(), err)
		}
		return "", 0, err
	}
	return fname, pos, nil
}
Exemple #3
0
func Update(f *os.File) int64 {
	pos, err := f.Seek(0, os.SEEK_CUR)
	if err != nil {
		log.Errorf("Seek %s failed - %v", f.Name(), err)
		return -1
	}
	fupdatePos(f, pos)
	return pos
}
Exemple #4
0
func UpdateWithFix(f *os.File, fix int64) int64 {
	pos, err := f.Seek(0, os.SEEK_CUR)
	if err != nil {
		log.Errorf("Seek %s failed - %v", f.Name(), err)
		return -1
	}
	pos += fix
	fupdatePos(f, pos)
	return pos
}
Exemple #5
0
func (s *Syncer) sync() error {
	files, err := s.list()
	if err != nil {
		return err
	}

	for _, file := range files {
		file = filepath.Join(s.dir, file)
		if err := s.syncFile(file); err != nil {
			log.Errorf("Sync %s failed - %v", file, err)
		}
	}
	return nil
}
Exemple #6
0
// Start sync dir, it will loop until the Stop is called
func (s *Syncer) Start() {
	log.Infof("Start sync %v/%s", s.dir, s.matchPattern)
	for {
		if err := s.sync(); err != nil && !os.IsNotExist(err) {
			log.Errorf("Sync %s failed - %v", s.dir, err)
		}
		select {
		case <-s.endch:
			log.Infof("Stop sync %v/%s", s.dir, s.matchPattern)
			return
		default:
		}

		time.Sleep(time.Second * time.Duration(s.IntervalSecond))
	}
}
Exemple #7
0
func (s *influxSinker) Sink(counters map[string]uint64, dur time.Duration) {

	var (
		points = make([]influx.Point, 0, len(counters))
		now    = time.Now()
	)

	for m, n := range counters {
		var (
			measurement string
			tags        = map[string]string{"host": s.hostName}
		)
		if strings.HasPrefix(m, clientCounterPrefix) {
			measurement = "client_ops"
			tags["id"] = strings.TrimPrefix(m, clientCounterPrefix)
		} else {
			measurement = "server_ops"
			fields := strings.SplitN(m, "_", 2)
			if len(fields) != 2 {
				log.Errorf("influx sinker found unepxected metric: %s", m)
				continue
			}
			tags["disk"] = fields[0]
			tags["op"] = fields[1]
		}
		p := influx.Point{
			Measurement: measurement,
			Time:        now,
			Tags:        tags,
			Fields:      map[string]interface{}{"value": n},
		}
		points = append(points, p)
	}

	if _, err := s.influx.Write(influx.BatchPoints{
		Database: s.database,
		Points:   points,
	}); err != nil {
		log.Error("influx write failed -", err)
	}
}
Exemple #8
0
func handleRevConn(pURL *url.URL, lis net.Listener) {
	switch pURL.Scheme {
	case "tcp":
		for {
			rconn, err := lis.Accept()
			if err != nil {
				log.Errorf("accept error: %v", err)
				return
			}
			log.Info("dial local:", pURL)
			lconn, err := net.Dial("tcp", pURL.Host)
			if err != nil {
				// wsclient
				log.Println(err)
				rconn.Close()
				break
			}
			// start forward local proxy
			pc := &ProxyConn{
				lconn: lconn,
				rconn: rconn,
				stats: proxyStats,
			}
			go pc.start()
		}
	case "http", "https":
		remote := pURL
		rp := &httputil.ReverseProxy{
			Director: func(req *http.Request) {
				req.Host = remote.Host
				req.URL.Scheme = remote.Scheme
				req.URL.Host = remote.Host
			},
		}
		http.Serve(lis, rp)
	default:
		log.Println("Unknown protocol:", pURL.Scheme)
	}
}
Exemple #9
0
func serveRevConn(proto ProxyProtocol, pAddr string, lis net.Listener) error {
	switch proto {
	case TCP:
		for {
			rconn, err := lis.Accept()
			if err != nil {
				log.Errorf("accept error: %v", err)
				return err
			}
			log.Info("local dial tcp", pAddr)
			lconn, err := net.Dial("tcp", pAddr)
			if err != nil {
				log.Warn(err)
				rconn.Close()
				return err
			}
			// start forward local proxy
			pc := &proxyConn{
				lconn: lconn,
				rconn: rconn,
				stats: proxyStats,
			}
			go pc.start()
		}
	case HTTP:
		rp := &httputil.ReverseProxy{
			Director: func(req *http.Request) {
				req.Host = pAddr
				req.URL.Scheme = "http"
				req.URL.Host = pAddr
			},
		}
		return http.Serve(lis, rp)
	default:
		log.Println("Unknown protocol:", proto)
		return ErrUnknownProtocol
	}
}