summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apt.go73
-rw-r--r--apt_linux.go72
-rw-r--r--distro.go56
-rw-r--r--main.go14
-rw-r--r--send.go4
-rw-r--r--structs.go15
-rw-r--r--watchdog.go2
7 files changed, 10 insertions, 226 deletions
diff --git a/apt.go b/apt.go
deleted file mode 100644
index 5fa6385..0000000
--- a/apt.go
+++ /dev/null
@@ -1,73 +0,0 @@
-package main
-
-import (
- "fmt"
-
- "go.wit.com/lib/protobuf/zoopb"
- "go.wit.com/log"
-)
-
-// init the installed package list
-func initPackages() {
- // Get the list of installed packages for the detected distro
- newP, err := getPackageList(me.distro)
- if err != nil {
- fmt.Println("Error:", err)
- return
- }
-
- // Print the installed packages and their versions
- for pkg, version := range newP {
- new1 := new(zoopb.Package)
- new1.Name = pkg
- new1.Version = version
- if me.machine.Packages.Append(new1) {
- // log.Info("added", new1.Name, "ok")
- } else {
- log.Info("added", new1.Name, "failed")
- }
- }
-
- log.Info(me.hostname, "has distro", me.distro, "with", me.machine.Packages.Len(), "packages installed.")
-}
-
-func addNew(name string, version string) bool {
- new1 := new(zoopb.Package)
- new1.Name = name
- new1.Version = version
- return me.machine.Packages.Append(new1)
-}
-
-func updatePackages() string {
- // Get the list of installed packages for the detected distro
- newP, err := getPackageList(me.distro)
- if err != nil {
- fmt.Println("Error:", err)
- return fmt.Sprintln("getPackageList()", err)
- }
-
- var newCounter, changeCounter int
- // Print the installed packages and their versions
- for pkg, version := range newP {
- found := me.machine.Packages.FindByName(pkg)
- if found == nil {
- log.Info("adding new", pkg, version)
- addNew(pkg, version)
- newCounter += 1
- } else {
- found.Version = version
- if me.machine.Packages.Update(found) {
- changeCounter += 1
- }
- }
- }
-
- footer := fmt.Sprintf("%s has distro %s with %d packages installed", me.hostname, me.distro, me.machine.Packages.Len())
- if changeCounter != 0 {
- footer += fmt.Sprintf(" (%d changed)", changeCounter)
- }
- if newCounter != 0 {
- footer += fmt.Sprintf(" (%d new)", newCounter)
- }
- return footer
-}
diff --git a/apt_linux.go b/apt_linux.go
deleted file mode 100644
index 7e534f0..0000000
--- a/apt_linux.go
+++ /dev/null
@@ -1,72 +0,0 @@
-package main
-
-import (
- "bufio"
- "fmt"
- "os/exec"
- "strings"
-
- "go.wit.com/log"
-)
-
-// getPackageList returns the list of installed packages based on the distro
-func getPackageList(distro string) (map[string]string, error) {
- var cmd *exec.Cmd
-
- // Run the appropriate command based on the detected distribution
- switch distro {
- case "ubuntu", "debian":
- return dpkgQuery()
- case "fedora", "centos", "rhel":
- cmd = exec.Command("rpm", "-qa")
- case "arch", "manjaro":
- cmd = exec.Command("pacman", "-Q")
- default:
- return nil, fmt.Errorf("unsupported distribution: %s", distro)
- }
-
- // Capture the command's output
- output, err := cmd.CombinedOutput()
- if err != nil {
- return nil, fmt.Errorf("error running command: %v", err)
- }
-
- // todo: Split the output into lines and return
- lines := strings.Split(string(output), "\n")
- log.Info("output had", len(lines), "lines")
- return nil, nil
-}
-
-func dpkgQuery() (map[string]string, error) {
- // Run the dpkg-query command to list installed packages and versions
- cmd := exec.Command("dpkg-query", "-W", "-f=${Package} ${Version}\n")
- stdout, err := cmd.StdoutPipe()
- if err != nil {
- return nil, err
- }
-
- // Start the command execution
- if err := cmd.Start(); err != nil {
- return nil, err
- }
- defer cmd.Wait()
-
- // Create a map to store package names and versions
- installedPackages := make(map[string]string)
-
- // Use a scanner to read the output of the command line by line
- scanner := bufio.NewScanner(stdout)
- for scanner.Scan() {
- line := scanner.Text()
- // Split each line into package name and version
- parts := strings.SplitN(line, " ", 2)
- if len(parts) == 2 {
- packageName := parts[0]
- version := parts[1]
- installedPackages[packageName] = version
- }
- }
-
- // Return the map with package names and versions
- return installedPackages, scanner.Err()
-}
diff --git a/distro.go b/distro.go
deleted file mode 100644
index 0269ea1..0000000
--- a/distro.go
+++ /dev/null
@@ -1,56 +0,0 @@
-// Copyright 2024 WIT.COM Inc.
-
-package main
-
-import (
- "bufio"
- "fmt"
- "os"
- "runtime"
- "strings"
-)
-
-func initDistro() string {
- switch runtime.GOOS {
- case "windows":
- return "windows"
- case "macos":
- return "macos"
- case "linux":
- // Detect the Linux distribution
- distro := detectDistro()
- if distro == "" {
- fmt.Println("Unable to detect Linux distribution.")
- distro = "fixme"
- }
-
- fmt.Printf("Detected distribution: %s\n", distro)
- return distro
- default:
- return runtime.GOOS
- }
-}
-
-// detectDistro returns the Linux distribution name (if possible)
-func detectDistro() string {
- // Check if we're on Linux
-
- // Try to read /etc/os-release to determine the distro
- file, err := os.Open("/etc/os-release")
- if err != nil {
- return ""
- }
- defer file.Close()
-
- scanner := bufio.NewScanner(file)
- for scanner.Scan() {
- line := scanner.Text()
- if strings.HasPrefix(line, "ID=") {
- parts := strings.SplitN(line, "=", 2)
- if len(parts) == 2 {
- return strings.Trim(parts[1], `"`)
- }
- }
- }
- return ""
-}
diff --git a/main.go b/main.go
index 3b5bbba..0329ad5 100644
--- a/main.go
+++ b/main.go
@@ -8,7 +8,6 @@ import (
"time"
"go.wit.com/dev/alexflint/arg"
- "go.wit.com/lib/protobuf/zoopb"
"go.wit.com/log"
)
@@ -35,21 +34,10 @@ func main() {
me = new(stuff)
me.urlbase = "http://zookeeper.grid.wit.com:8080"
- me.hostname, _ = os.Hostname()
me.pollDelay = 3 * time.Second
me.failcountmax = 20 // die every minute if zookeeper can't be found
- // what OS?
- me.distro = initDistro()
-
- // init my machine protobuf
- me.machine = new(zoopb.Machine)
- me.machine.Packages = new(zoopb.Packages)
- me.machine.Hostname = me.hostname
-
- // init the installed package list
- // me.packages = new(zoopb.Packages)
- initPackages()
+ me.machine.ConfigLoad()
go NewWatchdog()
diff --git a/send.go b/send.go
index e82e42d..bd9c9e1 100644
--- a/send.go
+++ b/send.go
@@ -12,7 +12,7 @@ import (
func pingStatus() error {
var url string
- url = me.urlbase + "/status?hostname=" + me.hostname
+ url = me.urlbase + "/status?hostname=" + me.machine.Hostname
msg, err := me.machine.Packages.Marshal()
if err != nil {
log.Info("proto.Marshal() failed:", err)
@@ -68,7 +68,7 @@ func sendMachine(s string) error {
os.Exit(0)
} else {
log.Info(me.urlbase, "is maybe not working GOT:", line)
- log.Info(me.urlbase, "fail count", me.failcount, "from hostname", me.hostname)
+ log.Info(me.urlbase, "fail count", me.failcount, "from hostname", me.machine.Hostname)
}
}
return nil
diff --git a/structs.go b/structs.go
index 7cb8a8c..fe96980 100644
--- a/structs.go
+++ b/structs.go
@@ -10,13 +10,10 @@ var me *stuff
// this app's variables
type stuff struct {
- hostname string // my hostname to send to zookeeper
- urlbase string // the dns name for the zookeeper
- pollDelay time.Duration // how often to report our status
- dog *time.Ticker // the watchdog timer
- distro string // debian,redhat,gentoo,macos,wincrap
- machine *zoopb.Machine // my protobuf
- failcount int // how many times we've failed to contact the zookeeper
- failcountmax int // after this, exit and let systemd restart the daemon
- // packages *zoopb.Packages // installed packages and versions
+ urlbase string // the dns name for the zookeeper
+ pollDelay time.Duration // how often to report our status
+ dog *time.Ticker // the watchdog timer
+ machine zoopb.Machine // populated from protobuf based zoopb
+ failcount int // how many times we've failed to contact the zookeeper
+ failcountmax int // after this, exit and let systemd restart the daemon
}
diff --git a/watchdog.go b/watchdog.go
index 9db2471..e6587a8 100644
--- a/watchdog.go
+++ b/watchdog.go
@@ -31,7 +31,7 @@ func NewWatchdog() {
return
case _ = <-me.dog.C:
// log.Info("Watchdog() ticked", me.zookeeper, "Current time: ", t)
- s := updatePackages()
+ s := me.machine.UpdatePackages()
// pingStatus()
me.failcount += 1
sendMachine(s)