-
Notifications
You must be signed in to change notification settings - Fork 4
/
net_query.go
55 lines (52 loc) · 1.18 KB
/
net_query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package psutilsql
import (
"github.com/noborus/trdsql"
"github.com/shirou/gopsutil/net"
)
// NetReader returns net.Connections result as trdsql.SliceReader.
func NetReader() (*trdsql.SliceReader, error) {
conns, err := net.Connections("all")
if err != nil {
return nil, err
}
type wrapConnection struct {
Fd uint32
Family uint32
Type uint32
LaddrIP string
LaddrPort uint32
RaddrIP string
RaddrPort uint32
status string
Uids []int32
Pid int32
}
data := make([]wrapConnection, len(conns))
for i, conn := range conns {
c := wrapConnection{}
c.Fd = conn.Fd
c.Family = conn.Family
c.Type = conn.Type
c.LaddrIP = conn.Laddr.IP
c.LaddrPort = conn.Laddr.Port
c.RaddrIP = conn.Raddr.IP
c.RaddrPort = conn.Raddr.Port
c.status = conn.Status
c.Uids = conn.Uids
c.Pid = conn.Pid
data[i] = c
}
return trdsql.NewSliceReader(psNet, data), nil
}
// NetQuery executes SQL on net.Connections.
func NetQuery(query string, w trdsql.Writer) error {
reader, err := NetReader()
if err != nil {
return err
}
defaultQuery := "SELECT * FROM " + psNet
if query == "" {
query = defaultQuery
}
return readerExec(reader, query, w)
}