Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit ed8fbf2

Browse files
committed
At startup check and install network-manager if needed
1 parent 9fbc24a commit ed8fbf2

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

handlers_stats.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ package main
2121
import (
2222
"encoding/json"
2323
"fmt"
24+
"os/exec"
25+
"strings"
2426

27+
apt "github.com/arduino/go-apt-client"
2528
"github.com/arduino/go-system-stats/disk"
2629
"github.com/arduino/go-system-stats/mem"
2730
"github.com/arduino/go-system-stats/network"
@@ -56,6 +59,28 @@ func (s *Status) EthEvent(client mqtt.Client, msg mqtt.Message) {
5659
net.AddWiredConnection(info)
5760
}
5861

62+
func checkAndInstallNetworkManager() {
63+
_, err := net.GetNetworkStats()
64+
if err == nil {
65+
return
66+
}
67+
if strings.Contains(err.Error(), "NetworkManager") {
68+
go func() {
69+
toInstall := &apt.Package{Name: "network-manager"}
70+
if out, err := apt.Install(toInstall); err != nil {
71+
fmt.Println("Failed to install network-manager:")
72+
fmt.Println(string(out))
73+
return
74+
}
75+
cmd := exec.Command("/etc/init.d/network-manager", "start")
76+
if out, err := cmd.CombinedOutput(); err != nil {
77+
fmt.Println("Failed to start network-manager:")
78+
fmt.Println(string(out))
79+
}
80+
}()
81+
}
82+
}
83+
5984
// StatsEvent sends statistics about resource used in the system (RAM, Disk, Network, etc...)
6085
func (s *Status) StatsEvent(client mqtt.Client, msg mqtt.Message) {
6186
// Gather all system data metrics

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ func main() {
123123
os.Exit(0)
124124
}
125125

126+
checkAndInstallNetworkManager()
127+
126128
err = s.Run()
127129
check(err, "RunService")
128130
}

0 commit comments

Comments
 (0)