Skip to content

Commit 1c4d92f

Browse files
committed
metrics: bridge between raw smb-data and exported-metrics
Define smbinfo: a bridge layer between raw smbstatus info and exported metric counters. This layer is also responsible for implementing any complex logic which requires transformation of raw data into more complex in-memory mappings. Signed-off-by: Shachar Sharon <[email protected]>
1 parent ba7999e commit 1c4d92f

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

internal/metrics/smbinfo.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package metrics
4+
5+
// SMBInfo provides a bridge layer between raw smbstatus info and exported
6+
// metric counters. It also implements the more complex logic which requires in
7+
// memory re-mapping of the low-level information (e.g., stats by machine/user).
8+
type SMBInfo struct {
9+
smbstat *SMBStatus
10+
}
11+
12+
func NewSMBInfo() *SMBInfo {
13+
return &SMBInfo{smbstat: NewSMBStatus()}
14+
}
15+
16+
func NewUpdatedSMBInfo() (*SMBInfo, error) {
17+
smbinfo := NewSMBInfo()
18+
err := smbinfo.Update()
19+
return smbinfo, err
20+
}
21+
22+
func (smbinfo *SMBInfo) Update() error {
23+
smbstat, err := RunSMBStatus()
24+
if err != nil {
25+
return err
26+
}
27+
smbinfo.smbstat = smbstat
28+
return nil
29+
}
30+
31+
func (smbinfo *SMBInfo) TotalSessions() int {
32+
return len(smbinfo.smbstat.Sessions)
33+
}
34+
35+
func (smbinfo *SMBInfo) TotalTreeCons() int {
36+
return len(smbinfo.smbstat.TCons)
37+
}
38+
39+
func (smbinfo *SMBInfo) TotalLockedFiles() int {
40+
return len(smbinfo.smbstat.LockedFiles)
41+
}
42+
43+
func (smbinfo *SMBInfo) TotalConnectedUsers() int {
44+
users := map[string]bool{}
45+
for _, session := range smbinfo.smbstat.Sessions {
46+
username := session.Username
47+
if len(username) > 0 {
48+
users[username] = true
49+
}
50+
}
51+
return len(users)
52+
}
53+
54+
func (smbinfo *SMBInfo) MapMachineToSessions() map[string][]*SMBStatusSession {
55+
ret := map[string][]*SMBStatusSession{}
56+
for _, session := range smbinfo.smbstat.Sessions {
57+
machineID := session.RemoteMachine
58+
sessionRef := &session
59+
ret[machineID] = append(ret[machineID], sessionRef)
60+
}
61+
return ret
62+
}
63+
64+
func (smbinfo *SMBInfo) MapServiceToTreeCons() map[string][]*SMBStatusTreeCon {
65+
ret := map[string][]*SMBStatusTreeCon{}
66+
for _, tcon := range smbinfo.smbstat.TCons {
67+
serviceID := tcon.Service
68+
tconRef := &tcon
69+
ret[serviceID] = append(ret[serviceID], tconRef)
70+
}
71+
return ret
72+
}
73+
74+
func (smbinfo *SMBInfo) MapMachineToTreeCons() map[string][]*SMBStatusTreeCon {
75+
ret := map[string][]*SMBStatusTreeCon{}
76+
for _, tcon := range smbinfo.smbstat.TCons {
77+
machineID := tcon.Machine
78+
tconRef := &tcon
79+
ret[machineID] = append(ret[machineID], tconRef)
80+
}
81+
return ret
82+
}
83+
84+
func (smbinfo *SMBInfo) MapServiceToMachines() map[string]map[string]int {
85+
ret := map[string]map[string]int{}
86+
for _, tcon := range smbinfo.smbstat.TCons {
87+
serviceID := tcon.Service
88+
machineID := tcon.Machine
89+
sub, found := ret[serviceID]
90+
if !found {
91+
ret[serviceID] = map[string]int{machineID: 1}
92+
} else {
93+
sub[machineID]++
94+
}
95+
}
96+
return ret
97+
}

0 commit comments

Comments
 (0)