Skip to content

Commit c81fb1c

Browse files
committedSep 7, 2024
feat: added sysinfo
1 parent 1c7b046 commit c81fb1c

File tree

8 files changed

+154
-4
lines changed

8 files changed

+154
-4
lines changed
 

‎client/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
)
1717

1818
func getBpfFilter(logType string) string {
19-
excludePort := "not port 5051 and not port 22 and not port 63262 and not port 62616"
19+
excludePort := "not port 5051 and not port 22 and not port 63262 and not port 62616 and not port 8888"
2020

2121
switch logType {
2222
case "http":

‎cmd/main.go

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/hanshal101/snapwall/database/migrate"
1212
"github.com/hanshal101/snapwall/database/psql"
1313
"github.com/hanshal101/snapwall/internal/router"
14+
"github.com/hanshal101/snapwall/internal/sysinfo"
1415
"github.com/joho/godotenv"
1516
)
1617

@@ -32,6 +33,9 @@ func main() {
3233
// go enforcer.ReconcileEnforcer(ctx, 5*time.Second)
3334
r := gin.Default()
3435
r.Use(cors.Default())
36+
37+
r.GET("/sysinfo", sysinfo.GetSystemInfo)
38+
3539
// POLICY Routes
3640
policy := r.Group("/policies")
3741
router.PolicyRoutes(policy)

‎go.mod

+8-3
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@ go 1.23.0
44

55
require (
66
github.com/ClickHouse/clickhouse-go/v2 v2.28.2
7+
github.com/coreos/go-iptables v0.8.0
8+
github.com/gin-contrib/cors v1.7.2
79
github.com/gin-gonic/gin v1.10.0
810
github.com/google/gopacket v1.1.19
11+
github.com/shirou/gopsutil v3.21.11+incompatible
912
google.golang.org/grpc v1.66.0
1013
gorm.io/gorm v1.25.11
1114
)
1215

1316
require (
1417
github.com/ClickHouse/ch-go v0.61.5 // indirect
1518
github.com/andybalholm/brotli v1.1.0 // indirect
16-
github.com/coreos/go-iptables v0.8.0 // indirect
17-
github.com/gin-contrib/cors v1.7.2 // indirect
1819
github.com/go-faster/city v1.0.1 // indirect
1920
github.com/go-faster/errors v0.7.1 // indirect
21+
github.com/go-ole/go-ole v1.2.6 // indirect
2022
github.com/google/uuid v1.6.0 // indirect
2123
github.com/jackc/pgpassfile v1.0.0 // indirect
2224
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
@@ -29,6 +31,9 @@ require (
2931
github.com/pkg/errors v0.9.1 // indirect
3032
github.com/segmentio/asm v1.2.0 // indirect
3133
github.com/shopspring/decimal v1.4.0 // indirect
34+
github.com/tklauser/go-sysconf v0.3.12 // indirect
35+
github.com/tklauser/numcpus v0.6.1 // indirect
36+
github.com/yusufpapurcu/wmi v1.2.3 // indirect
3237
go.opentelemetry.io/otel v1.26.0 // indirect
3338
go.opentelemetry.io/otel/trace v1.26.0 // indirect
3439
golang.org/x/sync v0.8.0 // indirect
@@ -60,7 +65,7 @@ require (
6065
github.com/ugorji/go/codec v1.2.12 // indirect
6166
golang.org/x/arch v0.8.0 // indirect
6267
golang.org/x/crypto v0.26.0 // indirect
63-
golang.org/x/net v0.28.0
68+
golang.org/x/net v0.28.0 // indirect
6469
golang.org/x/sys v0.23.0 // indirect
6570
golang.org/x/text v0.17.0 // indirect
6671
google.golang.org/protobuf v1.34.1

‎go.sum

+13
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ github.com/go-faster/city v1.0.1 h1:4WAxSZ3V2Ws4QRDrscLEDcibJY8uf41H6AhXDrNDcGw=
3030
github.com/go-faster/city v1.0.1/go.mod h1:jKcUJId49qdW3L1qKHH/3wPeUstCVpVSXTM6vO3VcTw=
3131
github.com/go-faster/errors v0.7.1 h1:MkJTnDoEdi9pDabt1dpWf7AA8/BaSYZqibYyhZ20AYg=
3232
github.com/go-faster/errors v0.7.1/go.mod h1:5ySTjWFiphBs07IKuiL69nxdfd5+fzh1u7FPGZP2quo=
33+
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
34+
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
3335
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
3436
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
3537
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
@@ -109,6 +111,8 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR
109111
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
110112
github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys=
111113
github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
114+
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
115+
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
112116
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
113117
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
114118
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@@ -125,6 +129,10 @@ github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXl
125129
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
126130
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
127131
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
132+
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
133+
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
134+
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
135+
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
128136
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
129137
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
130138
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
@@ -135,6 +143,8 @@ github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgk
135143
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
136144
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
137145
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
146+
github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFiw=
147+
github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
138148
go.mongodb.org/mongo-driver v1.11.4/go.mod h1:PTSz5yu21bkT/wXpkS7WR5f0ddqw5quethTUn9WM+2g=
139149
go.opentelemetry.io/otel v1.26.0 h1:LQwgL5s/1W7YiiRwxf03QGnWLb2HW4pLiAhaA5cZXBs=
140150
go.opentelemetry.io/otel v1.26.0/go.mod h1:UmLkJHUAidDval2EICqBMbnAd0/m2vmpf/dAM+fvFs4=
@@ -168,12 +178,15 @@ golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
168178
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
169179
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
170180
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
181+
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
171182
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
172183
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
173184
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
174185
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
175186
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
176187
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
188+
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
189+
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
177190
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
178191
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
179192
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=

‎internal/logs/logs.go

+47
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func GetLogs(c *gin.Context) {
8585
&logEntry.Destination,
8686
&logEntry.Port,
8787
&logEntry.Protocol,
88+
&logEntry.Severity,
8889
); err != nil {
8990
log.Fatalf("Error scanning row: %v", err)
9091
c.JSON(http.StatusBadGateway, gin.H{"error": "Error scanning row"})
@@ -131,6 +132,7 @@ func GetLogsByPort(c *gin.Context) {
131132
&logEntry.Destination,
132133
&logEntry.Port,
133134
&logEntry.Protocol,
135+
&logEntry.Severity,
134136
); err != nil {
135137
log.Printf("Error scanning row: %v", err)
136138
continue
@@ -177,6 +179,51 @@ func GetLogsByIP(c *gin.Context) {
177179
&logEntry.Destination,
178180
&logEntry.Port,
179181
&logEntry.Protocol,
182+
&logEntry.Severity,
183+
); err != nil {
184+
log.Printf("Error scanning row: %v", err)
185+
continue
186+
}
187+
188+
logs = append(logs, logEntry)
189+
}
190+
191+
if err := rows.Err(); err != nil {
192+
log.Printf("Error iterating over rows: %v", err)
193+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error retrieving logs"})
194+
return
195+
}
196+
197+
c.JSON(http.StatusOK, logs)
198+
}
199+
200+
func GetIntruderLogs(c *gin.Context) {
201+
query := `
202+
SELECT time, type, source, destination, port, protocol, severity
203+
FROM service_logs
204+
WHERE severity = ?
205+
`
206+
207+
rows, err := clickhouse.CHClient.Query(context.TODO(), query, "HIGH")
208+
if err != nil {
209+
log.Printf("Error executing query: %v", err)
210+
c.JSON(http.StatusBadRequest, gin.H{"error": "Error executing query"})
211+
return
212+
}
213+
defer rows.Close()
214+
215+
var logs []models.Log
216+
for rows.Next() {
217+
var logEntry models.Log
218+
219+
if err := rows.Scan(
220+
&logEntry.Time,
221+
&logEntry.Type,
222+
&logEntry.Source,
223+
&logEntry.Destination,
224+
&logEntry.Port,
225+
&logEntry.Protocol,
226+
&logEntry.Severity,
180227
); err != nil {
181228
log.Printf("Error scanning row: %v", err)
182229
continue

‎internal/router/router.go

+1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ func LogRoutes(r *gin.RouterGroup) {
2727
r.GET("", logs.GetLogs)
2828
r.GET("/port/:portNumber", logs.GetLogsByPort)
2929
r.GET("/:ioType/ip/:ipAddress", logs.GetLogsByIP)
30+
r.GET("/intruder", logs.GetIntruderLogs)
3031
}

‎internal/sysinfo/sysinfo.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package sysinfo
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gin-gonic/gin"
7+
"github.com/hanshal101/snapwall/models"
8+
"github.com/shirou/gopsutil/cpu"
9+
"github.com/shirou/gopsutil/disk"
10+
"github.com/shirou/gopsutil/host"
11+
"github.com/shirou/gopsutil/mem"
12+
)
13+
14+
func getSysInfo() (*models.SystemInfo, error) {
15+
cpuInfo, err := cpu.Info()
16+
if err != nil {
17+
return nil, err
18+
}
19+
20+
memInfo, err := mem.VirtualMemory()
21+
if err != nil {
22+
return nil, err
23+
}
24+
25+
partitions, err := disk.Partitions(true)
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
var diskInfo []disk.UsageStat
31+
for _, partition := range partitions {
32+
usage, err := disk.Usage(partition.Mountpoint)
33+
if err == nil {
34+
diskInfo = append(diskInfo, *usage)
35+
}
36+
}
37+
38+
hostInfo, err := host.Info()
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
uptime, err := host.Uptime()
44+
if err != nil {
45+
return nil, err
46+
}
47+
48+
return &models.SystemInfo{
49+
CPUInfo: cpuInfo,
50+
MemoryInfo: memInfo,
51+
DiskInfo: diskInfo,
52+
HostInfo: hostInfo,
53+
Uptime: uptime,
54+
}, nil
55+
}
56+
57+
func GetSystemInfo(c *gin.Context) {
58+
sysInfo, err := getSysInfo()
59+
if err != nil {
60+
c.JSON(http.StatusInternalServerError, gin.H{
61+
"error": "Failed to fetch system information",
62+
})
63+
return
64+
}
65+
66+
c.JSON(http.StatusOK, sysInfo)
67+
68+
}

‎models/config.go

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package models
33
import (
44
"time"
55

6+
"github.com/shirou/gopsutil/cpu"
7+
"github.com/shirou/gopsutil/disk"
8+
"github.com/shirou/gopsutil/host"
9+
"github.com/shirou/gopsutil/mem"
610
"gorm.io/gorm"
711
)
812

@@ -43,3 +47,11 @@ type Log struct {
4347
Protocol string `json:"protocol"`
4448
Severity string `json:"severity"`
4549
}
50+
51+
type SystemInfo struct {
52+
CPUInfo []cpu.InfoStat `json:"cpu_info"`
53+
MemoryInfo *mem.VirtualMemoryStat `json:"memory_info"`
54+
DiskInfo []disk.UsageStat `json:"disk_info"`
55+
HostInfo *host.InfoStat `json:"host_info"`
56+
Uptime uint64 `json:"uptime"`
57+
}

0 commit comments

Comments
 (0)
Please sign in to comment.