Home
Categories
WIKI
Topic
User
LANGUAGE:
中文
English
一个简易的带图形界面的网络监控器
社区开发
2160
views ·
3
replies ·
To
floor
Go
Bluek404
deepin
2015-04-28 02:34
Author
本帖最后由 Bluek404 于 2015-4-27 18:40 编辑
package main
import (
"io/ioutil"
"log"
"os"
"strconv"
"time"
"github.com/conformal/gotk3/gtk"
)
var net string
func readIntFromFile(path string) (int, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return 0, err
}
b = b[:len(b)-1] // delete "\n"
i, err := strconv.Atoi(string(b))
if err != nil {
return 0, err
}
return i, nil
}
func getNetIn() int {
i, err := readIntFromFile("/sys/class/net/" + net + "/statistics/rx_bytes")
if err != nil {
log.Fatal(err)
}
return i
}
func getNetOut() int {
i, err := readIntFromFile("/sys/class/net/" + net + "/statistics/tx_bytes")
if err != nil {
log.Fatal(err)
}
return i
}
func parseSize(src int) string {
if src == 0 {
return "0 B/s"
}
var unit string
for i := 0; ; i++ {
size := 1 << uint(i*10)
if src < size {
size = 1 << uint((i-1)*10)
src = src / size
switch i - 1 {
case 0:
unit = "B"
case 1:
unit = "KB"
case 2:
unit = "MB"
case 3:
unit = "GB"
}
break
}
}
return strconv.Itoa(src) + " " + unit + "/s"
}
func main() {
if len(os.Args) == 1 {
net = "wlan0"
} else {
net = os.Args[1]
}
gtk.Init(nil)
win, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
if err != nil {
log.Fatal("Unable to create window:", err)
}
win.Connect("destroy", func() {
gtk.MainQuit()
})
win.SetSkipTaskbarHint(true)
win.SetDecorated(false)
win.SetKeepAbove(true)
l, err := gtk.LabelNew("")
if err != nil {
log.Fatal("Unable to create label:", err)
}
l2, err := gtk.LabelNew("")
if err != nil {
log.Fatal("Unable to create label:", err)
}
box, err := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0)
if err != nil {
log.Fatal(err)
}
box.PackStart(l, false, true, 0)
box.PackStart(l2, false, true, 0)
win.Add(box)
win.ShowAll()
ticker := time.NewTicker(time.Second * 1)
go func() {
oldNetIn := getNetIn()
oldNetOut := getNetOut()
for _ = range ticker.C {
netIn := getNetIn()
netOut := getNetOut()
l.SetMarkup("
⇩
" + parseSize(netIn-oldNetIn))
l2.SetMarkup("
⇧
" + parseSize(netOut-oldNetOut))
oldNetIn = netIn
oldNetOut = netOut
}
}()
gtk.Main()
}
Copy the Code
按住Alt可拖动(当然具体按键还是得看窗口管理器的设置)默认是监控的wlan0
想要监控别的网卡的话在程序后面加上网卡名称作为参数就行
https://github.com/Bluek404/nsm
nsm64.7z
Reply
Like 0
Favorite
View the author
All Replies
ahyanglf
deepin
2015-04-29 04:49
#1
深度监控器吗?
Reply
Like 0
View the author
pengnannan
deepin
2015-08-25 17:50
#2
本帖最后由 pengnannan 于 2015-8-25 09:53 编辑
你这图行GTK完整吗
Reply
Like 0
View the author
pengnannan
deepin
2015-08-25 17:52
#3
我去这不是golang 吗 能加咱好友不我这在学呢
请留qq
Reply
Like 0
View the author
Please
sign
in first
New Thread
Popular Events
More
想要监控别的网卡的话在程序后面加上网卡名称作为参数就行
https://github.com/Bluek404/nsm
nsm64.7z