func printStats() { statTimer := time.NewTicker(30 * time.Second) diskUsageWarning := false for { <-statTimer.C var memstats runtime.MemStats runtime.ReadMemStats(&memstats) log.Printf("stats [started: %s]\n", humanize.RelTime(time.Time{}, stratuxClock.Time, "ago", "from now")) log.Printf(" - Disk bytes used = %s (%.1f %%), Disk bytes free = %s (%.1f %%)\n", humanize.Bytes(usage.Used()), 100*usage.Usage(), humanize.Bytes(usage.Free()), 100*(1-usage.Usage())) log.Printf(" - CPUTemp=%.02f deg C, MemStats.Alloc=%s, MemStats.Sys=%s, totalNetworkMessagesSent=%s\n", globalStatus.CPUTemp, humanize.Bytes(uint64(memstats.Alloc)), humanize.Bytes(uint64(memstats.Sys)), humanize.Comma(int64(totalNetworkMessagesSent))) log.Printf(" - UAT/min %s/%s [maxSS=%.02f%%], ES/min %s/%s, Total traffic targets tracked=%s", humanize.Comma(int64(globalStatus.UAT_messages_last_minute)), humanize.Comma(int64(globalStatus.UAT_messages_max)), float64(maxSignalStrength)/10.0, humanize.Comma(int64(globalStatus.ES_messages_last_minute)), humanize.Comma(int64(globalStatus.ES_messages_max)), humanize.Comma(int64(len(seenTraffic)))) log.Printf(" - Network data messages sent: %d total, %d nonqueueable. Network data bytes sent: %d total, %d nonqueueable.\n", globalStatus.NetworkDataMessagesSent, globalStatus.NetworkDataMessagesSentNonqueueable, globalStatus.NetworkDataBytesSent, globalStatus.NetworkDataBytesSentNonqueueable) if globalSettings.GPS_Enabled { log.Printf(" - Last GPS fix: %s, GPS solution type: %d using %d satellites (%d/%d seen/tracked), NACp: %d, est accuracy %.02f m\n", stratuxClock.HumanizeTime(mySituation.LastFixLocalTime), mySituation.Quality, mySituation.Satellites, mySituation.SatellitesSeen, mySituation.SatellitesTracked, mySituation.NACp, mySituation.Accuracy) log.Printf(" - GPS vertical velocity: %.02f ft/sec; GPS vertical accuracy: %v m\n", mySituation.GPSVertVel, mySituation.AccuracyVert) } // Check if we're using more than 95% of the free space. If so, throw a warning (only once). if !diskUsageWarning && usage.Usage() > 95.0 { err_p := fmt.Errorf("Disk bytes used = %s (%.1f %%), Disk bytes free = %s (%.1f %%)", humanize.Bytes(usage.Used()), 100*usage.Usage(), humanize.Bytes(usage.Free()), 100*(1-usage.Usage())) addSystemError(err_p) diskUsageWarning = true } logStatus() } }
func printStats() { statTimer := time.NewTicker(30 * time.Second) for { <-statTimer.C var memstats runtime.MemStats runtime.ReadMemStats(&memstats) log.Printf("stats [started: %s]\n", humanize.RelTime(time.Time{}, stratuxClock.Time, "ago", "from now")) log.Printf(" - CPUTemp=%.02f deg C, MemStats.Alloc=%s, MemStats.Sys=%s, totalNetworkMessagesSent=%s\n", globalStatus.CPUTemp, humanize.Bytes(uint64(memstats.Alloc)), humanize.Bytes(uint64(memstats.Sys)), humanize.Comma(int64(totalNetworkMessagesSent))) log.Printf(" - UAT/min %s/%s [maxSS=%.02f%%], ES/min %s/%s\n, Total traffic targets tracked=%s", humanize.Comma(int64(globalStatus.UAT_messages_last_minute)), humanize.Comma(int64(globalStatus.UAT_messages_max)), float64(maxSignalStrength)/10.0, humanize.Comma(int64(globalStatus.ES_messages_last_minute)), humanize.Comma(int64(globalStatus.ES_messages_max)), humanize.Comma(int64(len(seenTraffic)))) if globalSettings.GPS_Enabled { log.Printf(" - Last GPS fix: %s, GPS solution type: %d using %d satellites (%d/%d seen/tracked), NACp: %d, est accuracy %.02f m\n", stratuxClock.HumanizeTime(mySituation.LastFixLocalTime), mySituation.quality, mySituation.Satellites, mySituation.SatellitesSeen, mySituation.SatellitesTracked, mySituation.NACp, mySituation.Accuracy) log.Printf(" - GPS vertical velocity: %.02f ft/sec; GPS vertical accuracy: %v m\n", mySituation.GPSVertVel, mySituation.AccuracyVert) } } }
func (m *monotonic) HumanizeTime(t time.Time) string { return humanize.RelTime(m.Time, t, "ago", "from now") }
func proxyGo(host *config.Host, dryRun bool) error { stats := ConnectionStats{ CreatedAt: time.Now(), } connectHookArgs := ConnectHookArgs{ Host: host, Stats: &stats, } Logger.Debugf("Preparing host object") if err := hostPrepare(host); err != nil { return err } if dryRun { return fmt.Errorf("dry-run: Golang native TCP connection to '%s:%s'", host.HostName, host.Port) } // BeforeConnect hook Logger.Debugf("Calling BeforeConnect hooks") beforeConnectDrivers, err := host.Hooks.BeforeConnect.InvokeAll(connectHookArgs) if err != nil { Logger.Errorf("BeforeConnect hook failed: %v", err) } defer beforeConnectDrivers.Close() Logger.Debugf("Connecting to %s:%s", host.HostName, host.Port) conn, err := net.Dial("tcp", fmt.Sprintf("%s:%s", host.HostName, host.Port)) if err != nil { // OnConnectError hook connectHookArgs.Error = err Logger.Debugf("Calling OnConnectError hooks") onConnectErrorDrivers, err := host.Hooks.OnConnectError.InvokeAll(connectHookArgs) if err != nil { Logger.Errorf("OnConnectError hook failed: %v", err) } defer onConnectErrorDrivers.Close() return err } Logger.Debugf("Connected to %s:%s", host.HostName, host.Port) stats.ConnectedAt = time.Now() // OnConnect hook Logger.Debugf("Calling OnConnect hooks") onConnectDrivers, err := host.Hooks.OnConnect.InvokeAll(connectHookArgs) if err != nil { Logger.Errorf("OnConnect hook failed: %v", err) } defer onConnectDrivers.Close() // Ignore SIGHUP signal.Ignore(syscall.SIGHUP) waitGroup := sync.WaitGroup{} result := exportReadWrite{} ctx, cancel := context.WithCancel(context.Background()) ctx = context.WithValue(ctx, "sync", &waitGroup) waitGroup.Add(2) c1 := readAndWrite(ctx, conn, os.Stdout) c2 := readAndWrite(ctx, os.Stdin, conn) select { case result = <-c1: stats.WrittenBytes = result.written case result = <-c2: } if result.err != nil && result.err == io.EOF { result.err = nil } conn.Close() cancel() waitGroup.Wait() select { case res := <-c1: stats.WrittenBytes = res.written default: } stats.DisconnectedAt = time.Now() stats.ConnectionDuration = stats.DisconnectedAt.Sub(stats.ConnectedAt) averageSpeed := float64(stats.WrittenBytes) / stats.ConnectionDuration.Seconds() // round duraction stats.ConnectionDuration = ((stats.ConnectionDuration + time.Second/2) / time.Second) * time.Second stats.AverageSpeed = math.Ceil(averageSpeed*1000) / 1000 // human stats.WrittenBytesHuman = humanize.Bytes(stats.WrittenBytes) connectionDurationHuman := humanize.RelTime(stats.DisconnectedAt, stats.ConnectedAt, "", "") stats.ConnectionDurationHuman = strings.Replace(connectionDurationHuman, "now", "0 sec", -1) stats.AverageSpeedHuman = humanize.Bytes(uint64(stats.AverageSpeed)) + "/s" // OnDisconnect hook Logger.Debugf("Calling OnDisconnect hooks") onDisconnectDrivers, err := host.Hooks.OnDisconnect.InvokeAll(connectHookArgs) if err != nil { Logger.Errorf("OnDisconnect hook failed: %v", err) } defer onDisconnectDrivers.Close() Logger.Debugf("Byte written %v", stats.WrittenBytes) return result.err }