Skip to content

Commit

Permalink
Merge pull request #37 from pesos/master
Browse files Browse the repository at this point in the history
Minor bug fixes, upgrade to version v1.0.1
  • Loading branch information
metonymic-smokey committed Aug 8, 2020
2 parents 8b00c40 + c360279 commit daf4742
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 78 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ Using go get:
go get -u github.com/pesos/grofer
```

As an executable:
```
curl -sSL https://github.com/pesos/grofer/releases/download/<version tag>/grofer --output grofer
chmod +x grofer
```
For system wide usage, install `grofer` to a location on `$PATH`, e.g. `/usr/local/bin`

Building from source:

```
Expand Down
10 changes: 4 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

overallGraph "github.com/pesos/grofer/src/display/general"
"github.com/pesos/grofer/src/general"
"github.com/pesos/grofer/src/utils"
)

const (
Expand All @@ -47,15 +48,12 @@ var rootCmd = &cobra.Command{

var wg sync.WaitGroup
endChannel := make(chan os.Signal, 1)
memChannel := make(chan []float64, 1)
cpuChannel := make(chan []float64, 1)
diskChannel := make(chan [][]string, 1)
netChannel := make(chan map[string][]float64, 1)
dataChannel := make(chan utils.DataStats, 1)

wg.Add(2)

go general.GlobalStats(endChannel, cpuChannel, memChannel, diskChannel, netChannel, overallRefreshRate, &wg)
go overallGraph.RenderCharts(endChannel, memChannel, cpuChannel, diskChannel, netChannel, overallRefreshRate, &wg)
go general.GlobalStats(endChannel, dataChannel, overallRefreshRate, &wg)
go overallGraph.RenderCharts(endChannel, dataChannel, overallRefreshRate, &wg)

wg.Wait()

Expand Down
1 change: 1 addition & 0 deletions src/display/general/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func (page *MainPage) InitGeneral(numCores int) {
ui.NewRow(0.34, page.DiskChart),
)

// Get Terminal Dimensions
w, h := ui.TerminalDimensions()
page.Grid.SetRect(w/2, 0, w, h)
}
112 changes: 57 additions & 55 deletions src/display/general/overallGraphs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"time"

ui "github.com/gizak/termui/v3"
"github.com/pesos/grofer/src/utils"
)

var isCPUSet = false
Expand All @@ -34,10 +35,7 @@ var run = true

// RenderCharts handles plotting graphs and charts for system stats in general.
func RenderCharts(endChannel chan os.Signal,
memChannel chan []float64,
cpuChannel chan []float64,
diskChannel chan [][]string,
netChannel chan map[string][]float64,
dataChannel chan utils.DataStats,
refreshRate int32,
wg *sync.WaitGroup) {

Expand Down Expand Up @@ -80,73 +78,71 @@ func RenderCharts(endChannel chan os.Signal,
pause()
}

case data := <-memChannel: // Update memory values
if run {
myPage.MemoryChart.Data = data
}

case data := <-diskChannel: // Update disk values
if run {
myPage.DiskChart.Rows = data
}

case data := <-netChannel: // Update network stats & render braille plots
case data := <-dataChannel:
if run {
switch data.FieldSet {

var curBytesRecv, curBytesSent float64
case "CPU": // Update CPU stats
for index, rate := range data.CpuStats {
myPage.CPUCharts[index].Title = " CPU " + strconv.Itoa(index) + " "
myPage.CPUCharts[index].Percent = int(rate)
}

for _, netInterface := range data {
curBytesRecv += netInterface[1]
curBytesSent += netInterface[0]
}
case "MEM": // Update Memory stats
myPage.MemoryChart.Data = data.MemStats

var recentBytesRecv, recentBytesSent float64
case "DISK": // Update Disk stats
myPage.DiskChart.Rows = data.DiskStats

if totalBytesRecv != 0 {
recentBytesRecv = curBytesRecv - totalBytesRecv
recentBytesSent = curBytesSent - totalBytesSent
case "NET": // Update Network stats
var curBytesRecv, curBytesSent float64

if int(recentBytesRecv) < 0 {
recentBytesRecv = 0
}
if int(recentBytesSent) < 0 {
recentBytesSent = 0
for _, netInterface := range data.NetStats {
curBytesRecv += netInterface[1]
curBytesSent += netInterface[0]
}

ipData = ipData[1:]
opData = opData[1:]
var recentBytesRecv, recentBytesSent float64

ipData = append(ipData, recentBytesRecv)
opData = append(opData, recentBytesSent)
}
if totalBytesRecv != 0 {
recentBytesRecv = curBytesRecv - totalBytesRecv
recentBytesSent = curBytesSent - totalBytesSent

totalBytesRecv = curBytesRecv
totalBytesSent = curBytesSent
if int(recentBytesRecv) < 0 {
recentBytesRecv = 0
}
if int(recentBytesSent) < 0 {
recentBytesSent = 0
}

titles := make([]string, 2)
ipData = ipData[1:]
opData = opData[1:]

for i := 0; i < 2; i++ {
if i == 0 {
titles[i] = fmt.Sprintf("[Total RX](fg:red): %5.1f %s\n", totalBytesRecv/1024, "mB")
} else {
titles[i] = fmt.Sprintf("\n[Total TX](fg:green): %5.1f %s", totalBytesSent/1024, "mB")
ipData = append(ipData, recentBytesRecv)
opData = append(opData, recentBytesSent)
}

}
totalBytesRecv = curBytesRecv
totalBytesSent = curBytesSent

myPage.NetPara.Text = titles[0] + titles[1]
titles := make([]string, 2)

temp := [][]float64{}
temp = append(temp, ipData)
temp = append(temp, opData)
myPage.NetworkChart.Data = temp
}
for i := 0; i < 2; i++ {
if i == 0 {
titles[i] = fmt.Sprintf("[Total RX](fg:red): %5.1f %s\n", totalBytesRecv/1024, "mB")
} else {
titles[i] = fmt.Sprintf("\n[Total TX](fg:green): %5.1f %s", totalBytesSent/1024, "mB")
}

}

myPage.NetPara.Text = titles[0] + titles[1]

temp := [][]float64{}
temp = append(temp, ipData)
temp = append(temp, opData)
myPage.NetworkChart.Data = temp

case cpu_data := <-cpuChannel: // Update Gauge map with newer values
if run {
for index, rate := range cpu_data {
myPage.CPUCharts[index].Title = " CPU " + strconv.Itoa(index) + " "
myPage.CPUCharts[index].Percent = int(rate)
}
}

Expand All @@ -155,9 +151,15 @@ func RenderCharts(endChannel chan os.Signal,
ui.Clear()

height := int(h / numCores)
heightOffset := h - (height * numCores) // There's some extra empty space left
heightOffset := h - (height * numCores)

// Adjust Grid dimensions
myPage.Grid.SetRect(w/2, 0, w, h-heightOffset)

// Adjust Memory Bar graph values
myPage.MemoryChart.BarGap = ((w / 2) - (4 * myPage.MemoryChart.BarWidth)) / 4

// Adjust CPU Gauge dimensions
if isCPUSet {
for i := 0; i < numCores; i++ {
myPage.CPUCharts[i].SetRect(0, i*height, w/2, (i+1)*height)
Expand Down
16 changes: 7 additions & 9 deletions src/general/generalStats.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ import (
"os"
"sync"
"time"

"github.com/pesos/grofer/src/utils"
)

// GlobalStats gets stats about the mem and the CPUs and prints it.
func GlobalStats(endChannel chan os.Signal,
cpuChannel chan []float64,
memChannel chan []float64,
diskChannel chan [][]string,
netChannel chan map[string][]float64,
refreshRate int32,
dataChannel chan utils.DataStats, refreshRate int32,
wg *sync.WaitGroup) {

for {
Expand All @@ -38,10 +36,10 @@ func GlobalStats(endChannel chan os.Signal,

default: // Get Memory and CPU rates per core periodically

go PrintCPURates(cpuChannel)
go PrintMemRates(memChannel)
go PrintDiskRates(diskChannel)
PrintNetRates(netChannel)
go PrintCPURates(dataChannel)
go PrintMemRates(dataChannel)
go PrintDiskRates(dataChannel)
PrintNetRates(dataChannel)
time.Sleep(time.Duration(refreshRate) * time.Millisecond)
}
}
Expand Down
39 changes: 31 additions & 8 deletions src/general/printStats.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"
"time"

"github.com/pesos/grofer/src/utils"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/mem"
Expand All @@ -34,26 +35,36 @@ func roundOff(num uint64) float64 {
}

// PrintCPURates print the cpu rates
func PrintCPURates(cpuChannel chan []float64) {
func PrintCPURates(cpuChannel chan utils.DataStats) {
cpuRates, err := cpu.Percent(time.Second, true)
if err != nil {
log.Fatal(err)
}
cpuChannel <- cpuRates
data := utils.DataStats{
CpuStats: cpuRates,
FieldSet: "CPU",
}
cpuChannel <- data
}

// PrintMemRates prints stats about the memory
func PrintMemRates(dataChannel chan []float64) {
func PrintMemRates(dataChannel chan utils.DataStats) {
memory, err := mem.VirtualMemory()
if err != nil {
log.Fatal(err)
}

data := []float64{roundOff(memory.Total), roundOff(memory.Available), roundOff(memory.Used), roundOff(memory.Free)}
memRates := []float64{roundOff(memory.Total), roundOff(memory.Available), roundOff(memory.Used), roundOff(memory.Free)}

data := utils.DataStats{
MemStats: memRates,
FieldSet: "MEM",
}

dataChannel <- data
}

func PrintDiskRates(dataChannel chan [][]string) {
func PrintDiskRates(dataChannel chan utils.DataStats) {

var partitions []disk.PartitionStat
var err error
Expand Down Expand Up @@ -83,10 +94,16 @@ func PrintDiskRates(dataChannel chan [][]string) {

}
}
dataChannel <- rows

data := utils.DataStats{
DiskStats: rows,
FieldSet: "DISK",
}

dataChannel <- data
}

func PrintNetRates(dataChannel chan map[string][]float64) {
func PrintNetRates(dataChannel chan utils.DataStats) {
netStats, err := net.IOCounters(false)
if err != nil {
log.Fatal(err)
Expand All @@ -96,5 +113,11 @@ func PrintNetRates(dataChannel chan map[string][]float64) {
nic := []float64{float64(IOStat.BytesSent) / (1024), float64(IOStat.BytesRecv) / (1024)}
IO[IOStat.Name] = nic
}
dataChannel <- IO

data := utils.DataStats{
NetStats: IO,
FieldSet: "NET",
}

dataChannel <- data
}
25 changes: 25 additions & 0 deletions src/utils/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Copyright © 2020 The PES Open Source Team pesos@pes.edu
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 utils

type DataStats struct {
CpuStats []float64
MemStats []float64
DiskStats [][]string
NetStats map[string][]float64
FieldSet string
}

0 comments on commit daf4742

Please sign in to comment.