-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdiagnose.go
More file actions
198 lines (169 loc) · 5.27 KB
/
diagnose.go
File metadata and controls
198 lines (169 loc) · 5.27 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"encoding/binary"
"fmt"
"net"
"time"
"github.com/godbus/dbus/v5"
)
// RunDiagnostics runs all diagnostic checks and returns an exit code.
func RunDiagnostics(cfg Config) int {
fmt.Println("=== shm-et340 Diagnostic Report ===")
fmt.Println()
allPassed := true
// Check 1: Network interfaces
if !checkNetworkInterfaces() {
allPassed = false
}
// Check 2: Multicast reception
if !checkMulticastReception(cfg) {
allPassed = false
}
// Check 3: D-Bus connection
if !checkDBusConnection() {
allPassed = false
}
// Check 4: D-Bus name available
if !checkDBusNameAvailable(cfg.DBusName) {
allPassed = false
}
fmt.Println()
if allPassed {
fmt.Println("All checks passed.")
return 0
}
fmt.Println("Some checks failed. See details above.")
return 1
}
func checkNetworkInterfaces() bool {
ifaces, err := net.Interfaces()
if err != nil {
fmt.Printf("[FAIL] List network interfaces\n")
fmt.Printf(" Error: %v\n", err)
return false
}
found := false
for _, iface := range ifaces {
if iface.Flags&net.FlagLoopback != 0 {
continue
}
if iface.Flags&net.FlagUp == 0 {
continue
}
addrs, _ := iface.Addrs()
addrStrs := make([]string, 0, len(addrs))
for _, a := range addrs {
addrStrs = append(addrStrs, a.String())
}
supportsMulticast := iface.Flags&net.FlagMulticast != 0
status := "PASS"
if !supportsMulticast {
status = "WARN"
}
fmt.Printf("[%s] Interface %s supports multicast\n", status, iface.Name)
fmt.Printf(" addresses: %v, flags: %v\n", addrStrs, iface.Flags)
if supportsMulticast {
found = true
}
}
if !found {
fmt.Printf("[FAIL] No UP non-loopback interface supports multicast\n")
return false
}
return true
}
func checkMulticastReception(cfg Config) bool {
fmt.Printf("[....] Receive multicast packet within 10 seconds\n")
addr, err := net.ResolveUDPAddr("udp4", cfg.MulticastAddress)
if err != nil {
fmt.Printf("\r[FAIL] Receive multicast packet within 10 seconds\n")
fmt.Printf(" Could not resolve address %s: %v\n", cfg.MulticastAddress, err)
return false
}
var ifi *net.Interface
if cfg.Interface != "" {
ifi, err = net.InterfaceByName(cfg.Interface)
if err != nil {
fmt.Printf("\r[FAIL] Receive multicast packet within 10 seconds\n")
fmt.Printf(" Interface %s not found: %v\n", cfg.Interface, err)
return false
}
}
conn, err := net.ListenMulticastUDP("udp4", ifi, addr)
if err != nil {
fmt.Printf("\r[FAIL] Receive multicast packet within 10 seconds\n")
fmt.Printf(" Could not join multicast group: %v\n", err)
return false
}
defer conn.Close()
conn.SetReadDeadline(time.Now().Add(10 * time.Second))
buf := make([]byte, 2048)
n, _, err := conn.ReadFromUDP(buf)
if err != nil {
fmt.Printf("\r[FAIL] Receive multicast packet within 10 seconds\n")
fmt.Printf(" No packet received. This usually means:\n")
fmt.Printf(" 1. IGMP snooping on your switch is blocking multicast traffic\n")
fmt.Printf(" 2. The SMA meter is not on the same network/VLAN\n")
fmt.Printf(" 3. The SMA meter is powered off or misconfigured\n")
fmt.Printf(" Try: Disable IGMP snooping on your managed switch, or\n")
fmt.Printf(" enable the IGMP querier on your router.\n")
return false
}
fmt.Printf("\r[PASS] Receive multicast packet within 10 seconds\n")
fmt.Printf(" Packet size: %d bytes\n", n)
if n >= 24 {
serial := binary.BigEndian.Uint32(buf[20:24])
fmt.Printf(" SMA serial: %d\n", serial)
}
return true
}
func checkDBusConnection() bool {
conn, err := dbus.SystemBus()
if err != nil {
fmt.Printf("[FAIL] Connect to system D-Bus\n")
fmt.Printf(" Error: %v\n", err)
fmt.Printf(" Hint: Is this running on Venus OS? Is dbus-daemon running?\n")
return false
}
conn.Close()
fmt.Printf("[PASS] Connect to system D-Bus\n")
return true
}
func checkDBusNameAvailable(name string) bool {
conn, err := dbus.SystemBus()
if err != nil {
fmt.Printf("[FAIL] D-Bus name available (%s)\n", name)
fmt.Printf(" Could not connect to system bus: %v\n", err)
return false
}
defer conn.Close()
reply, err := conn.RequestName(name, dbus.NameFlagDoNotQueue)
if err != nil {
fmt.Printf("[FAIL] D-Bus name available (%s)\n", name)
fmt.Printf(" Error requesting name: %v\n", err)
return false
}
if reply != dbus.RequestNameReplyPrimaryOwner {
fmt.Printf("[FAIL] D-Bus name available (%s)\n", name)
fmt.Printf(" Name already taken — is another instance of shm-et340 running?\n")
return false
}
// Release the name so the actual app can claim it
conn.ReleaseName(name)
fmt.Printf("[PASS] D-Bus name available\n")
return true
}