Skip to content

Commit

Permalink
common: feat: add versionCheck and implement it on various Health com…
Browse files Browse the repository at this point in the history
…mands
  • Loading branch information
kreatoo committed Jan 30, 2025
1 parent cf5a9d4 commit 4703d0e
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 7 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@

## Tools

- versionCheck
- Checks the version of various services, including OPNsense, Zimbra and Proxmox.
- Creates a Redmine news entry if the version has been updated.
- Config: `/etc/mono/global.yaml`

- zimbraLdap
- Runs ldap.sh script.

Expand Down
57 changes: 57 additions & 0 deletions common/versionCheck/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package common

import (
"os"
"fmt"
"time"
"io/ioutil"
"github.com/spf13/cobra"
"github.com/monobilisim/monokit/common"
news "github.com/monobilisim/monokit/common/redmine/news"
)

func StoreVersion(service string, version string) {
common.WriteToFile(common.TmpDir + "/" + service + ".version", version)
}

func GatherVersion(service string) string {
// Check if the service has a file
if _, err := os.Stat(common.TmpDir + "/" + service + ".version"); os.IsNotExist(err) {
return ""
}

// Read the file
content, err := ioutil.ReadFile(common.TmpDir + "/" + service + ".version")
if err != nil {
return ""
}

return string(content)
}

func CreateNews(service string, oldVersion string, newVersion string) {
news.Create(common.Config.Identifier + " sunucusunun " + service + " sürümü güncellendi", common.Config.Identifier + " sunucusunda " + service + ", " + oldVersion + " sürümünden " + newVersion + " sürümüne yükseltildi.", true)
}


func VersionCheck(cmd *cobra.Command, args []string) {
version := "0.1.0"
common.ScriptName = "versionCheck"
common.TmpDir = "/var/cache/mono/" + common.ScriptName
common.Init()

fmt.Println("versionCheck - v" + version + " - " + time.Now().Format("2006-01-02 15:04:05"))

// Proxmox
ProxmoxVECheck()
ProxmoxMGCheck()
ProxmoxBSCheck()

// Zimbra
ZimbraCheck()

// OPNsense
OPNsenseCheck()
}


44 changes: 44 additions & 0 deletions common/versionCheck/opnsense.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package common

import (
"fmt"
"os/exec"
"strings"
)

func OPNsenseCheck() {
// Check if OPNsense is installed by checking the existence of command "opnsense-version"
_, err := exec.LookPath("opnsense-version")
if err != nil {
fmt.Println("OPNsense is not installed on this system.")
return
}

// Get the version of OPNsense
out, err := exec.Command("opnsense-version").Output()
if err != nil {
fmt.Println("Error getting OPNsense version.")
return
}

// Parse the version
// Eg. output
// OPNsense 21.1.8_1 (amd64)
version := strings.Split(string(out), " ")[1]
fmt.Println("OPNsense version:", version)

oldVersion := GatherVersion("opnsense")

if oldVersion != "" && oldVersion == version {
fmt.Println("OPNsense is up to date.")
return
} else if oldVersion != "" && oldVersion != version {
fmt.Println("OPNsense got updated.")
fmt.Println("Old version:", oldVersion)
fmt.Println("New version:", version)
CreateNews("OPNsense", oldVersion, version)
}


StoreVersion("opnsense", version)
}
115 changes: 115 additions & 0 deletions common/versionCheck/proxmox.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package common

import (
"fmt"
"os/exec"
"strings"
)

func ProxmoxVECheck() {
// Check if Proxmox VE is installed by checking the existence of command "pveversion"
_, err := exec.LookPath("pveversion")
if err != nil {
fmt.Println("Proxmox VE is not installed on this system.")
return
}

// Get the version of Proxmox VE
out, err := exec.Command("pveversion").Output()
if err != nil {
fmt.Println("Error getting Proxmox VE version.")
return
}

// Parse the version
// Eg. output
// pve-manager/6.4-13/1c2b3f0e (running kernel: 5.4.78-2-pve)
version := strings.Split(string(out), "/")[1]
fmt.Println("Proxmox VE version:", version)

oldVersion := GatherVersion("pve")

if oldVersion != "" && oldVersion == version {
fmt.Println("Proxmox VE is up to date.")
return
} else if oldVersion != "" && oldVersion != version {
fmt.Println("Proxmox VE has been updated.")
fmt.Println("Old version:", oldVersion)
fmt.Println("New version:", version)
CreateNews("Proxmox VE", oldVersion, version)
}

StoreVersion("pve", version)
}

func ProxmoxMGCheck() {
// Check if Proxmox Mail Gateway is installed by checking the existence of command "pmgversion"
_, err := exec.LookPath("pmgversion")
if err != nil {
fmt.Println("Proxmox Mail Gateway is not installed on this system.")
return
}

// Get the version of Proxmox Mail Gateway
out, err := exec.Command("pmgversion").Output()
if err != nil {
fmt.Println("Error getting Proxmox Mail Gateway version.")
return
}

// Parse the version
// Eg. output
// pmg/6.4-13/1c2b3f0e (running kernel: 5.4.78-2-pve)
version := strings.Split(string(out), "/")[1]
fmt.Println("Proxmox Mail Gateway version:", version)

oldVersion := GatherVersion("pmg")

if oldVersion != "" && oldVersion == version {
fmt.Println("Proxmox Mail Gateway is up to date.")
return
} else if oldVersion != "" && oldVersion != version {
fmt.Println("Proxmox Mail Gateway has been updated.")
fmt.Println("Old version:", oldVersion)
fmt.Println("New version:", version)
CreateNews("Proxmox Mail Gateway", oldVersion, version)
}

StoreVersion("pmg", version)
}

func ProxmoxBSCheck() {
// Check if Proxmox Backup Server is installed by checking the existence of command "proxmox-backup-manager"
_, err := exec.LookPath("proxmox-backup-manager")
if err != nil {
fmt.Println("Proxmox Backup Server is not installed on this system.")
return
}

// Get the version of Proxmox Backup Server
out, err := exec.Command("proxmox-backup-manager", "version").Output()
if err != nil {
fmt.Println("Error getting Proxmox Backup Server version.")
return
}

// Parse the version
// Eg. output
// proxmox-backup-server 3.3.2-1 running version: 3.3.2
version := strings.Split(string(out), " ")[1]
fmt.Println("Proxmox Backup Server version:", version)

oldVersion := GatherVersion("pbs")

if oldVersion != "" && oldVersion == version {
fmt.Println("Proxmox Backup Server is up to date.")
return
} else if oldVersion != "" && oldVersion != version {
fmt.Println("Proxmox Backup Server has been updated.")
fmt.Println("Old version:", oldVersion)
fmt.Println("New version:", version)
CreateNews("Proxmox Backup Server", oldVersion, version)
}

StoreVersion("pbs", version)
}
6 changes: 6 additions & 0 deletions common/versionCheck/zimbra-nonlinux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build !linux
package common

func ZimbraCheck() {
return
}
54 changes: 54 additions & 0 deletions common/versionCheck/zimbra.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//go:build linux
package common

import (
"os"
"fmt"
"strings"
"os/exec"
)

func ZimbraCheck() {
var zimbraPath string
var zimbraUser string

if _, err := os.Stat("/opt/zimbra"); !os.IsNotExist(err) {
zimbraPath = "/opt/zimbra"
zimbraUser = "zimbra"
}

if _, err := os.Stat("/opt/zextras"); !os.IsNotExist(err) {
zimbraPath = "/opt/zextras"
zimbraUser = "zextras"
}

// Get the version of Zimbra
cmd := exec.Command("/bin/su", zimbraUser, "-c", zimbraPath + "/bin/zmcontrol -v")
out, err := cmd.Output()
if err != nil {
fmt.Println("Error getting Zimbra version.")
return
}

// Parse the version
// Eg. output
// Release 8.8.15_GA_3869.UBUNTU18.64 UBUNTU18_64 FOSS edition.
version := strings.Split(string(out), " ")[1]
version = strings.Split(version, "_GA_")[0]

fmt.Println("Zimbra version:", version)

oldVersion := GatherVersion("zimbra")

if oldVersion != "" && oldVersion == version {
fmt.Println("Zimbra is up to date.")
return
} else if oldVersion != "" && oldVersion != version {
fmt.Println("Zimbra has been updated.")
fmt.Println("Old version:", oldVersion)
fmt.Println("New version:", version)
CreateNews("Zimbra", oldVersion, version)
}

StoreVersion("zimbra", version)
}
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/monobilisim/monokit/common"
issues "github.com/monobilisim/monokit/common/redmine/issues"
news "github.com/monobilisim/monokit/common/redmine/news"
verCheck "github.com/monobilisim/monokit/common/versionCheck"
"github.com/monobilisim/monokit/k8sHealth"
"github.com/monobilisim/monokit/osHealth"
"github.com/monobilisim/monokit/shutdownNotifier"
Expand Down Expand Up @@ -89,6 +90,12 @@ func main() {
Run: daemon.Main,
}

var versionCheckCmd = &cobra.Command{
Use: "versionCheck",
Short: "Version Check",
Run: verCheck.VersionCheck,
}

//// Common
RootCmd.AddCommand(redmineCmd)
RootCmd.AddCommand(common.AlarmCmd)
Expand Down Expand Up @@ -277,6 +284,9 @@ func main() {
/// SSH Notifier
RootCmd.AddCommand(sshNotifierCmd)

/// Version Check
RootCmd.AddCommand(versionCheckCmd)

/// WPPConnect
RootCmd.AddCommand(wppconnectHealthCmd)

Expand Down
4 changes: 4 additions & 0 deletions pmgHealth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/spf13/cobra"
"github.com/monobilisim/monokit/common"
mail "github.com/monobilisim/monokit/common/mail"
ver "github.com/monobilisim/monokit/common/versionCheck"
)


Expand Down Expand Up @@ -85,6 +86,9 @@ func Main(cmd *cobra.Command, args []string) {
common.ConfInit("mail", &MailHealthConfig)

fmt.Println("PMG Health Check REWRITE - v" + version + " - " + time.Now().Format("2006-01-02 15:04:05"))

common.SplitSection("Version Check")
ver.ProxmoxMGCheck()

common.SplitSection("PMG Services")
CheckPmgServices()
Expand Down
11 changes: 4 additions & 7 deletions zimbraHealth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/spf13/cobra"
"github.com/monobilisim/monokit/common"
mail "github.com/monobilisim/monokit/common/mail"
ver "github.com/monobilisim/monokit/common/versionCheck"
issues "github.com/monobilisim/monokit/common/redmine/issues"
)

Expand Down Expand Up @@ -45,13 +46,9 @@ func Main(cmd *cobra.Command, args []string) {
common.SplitSection("Zimbra Services:")
CheckZimbraServices()

common.SplitSection("Zimbra Version:")
zimbraVer, err := ExecZimbraCommand("zmcontrol -v", false, false)
if err != nil {
common.LogError("Error getting zimbra version: " + err.Error())
}
common.PrettyPrintStr("Zimbra Version", true, zimbraVer)

common.SplitSection("Zimbra Version Check:")
ver.ZimbraCheck()

if MailHealthConfig.Zimbra.Z_Url != "" {
common.SplitSection("Checking Z-Push:")
CheckZPush()
Expand Down

0 comments on commit 4703d0e

Please sign in to comment.