Skip to content

Commit

Permalink
pgsqlHealth: implement WAL-G timeline and integrity checks, fix getPa…
Browse files Browse the repository at this point in the history
…troniUrl alarm
  • Loading branch information
kreatoo committed Jan 8, 2025
1 parent f638848 commit 08b7a53
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 21 deletions.
56 changes: 37 additions & 19 deletions pgsqlHealth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
package pgsqlHealth

import (
"io"
"os"
"fmt"
"time"
"errors"
"os/exec"
"net/http"
"encoding/json"
"github.com/spf13/cobra"
Expand All @@ -18,17 +20,19 @@ var patroniApiUrl string

func Main(cmd *cobra.Command, args []string) {
version := "3.0.0"
common.ScriptName = "pgsqlHealth"
common.TmpDir = common.TmpDir + "pgsqlHealth"
common.Init()
common.ConfInit("db", &DbHealthConfig)

common.ScriptName = "pgsqlHealth"

// Check if user is postgres
if os.Getenv("USER") != "postgres" {
common.LogError("This script must be run as the postgres user")
return
}

common.TmpDir = os.Getenv("HOME") + "/.local/share/mono/" + "pgsqlHealth"
common.Init()
common.ConfInit("db", &DbHealthConfig)


//var isCluster bool

if _, err := os.Stat("/etc/patroni/patroni.yml"); !errors.Is(err, os.ErrNotExist) {
Expand Down Expand Up @@ -68,32 +72,46 @@ func Main(cmd *cobra.Command, args []string) {
DbHealthConfig.Postgres.Wal_g_verify_hour = "03:00"
}

//var role string
var role string

role = "undefined"
hour := time.Now().Format("15:04")

//role = "undefined"
lookPath, _ := exec.LookPath("wal-g")

// Check if patroni is installed
if _, err := os.Stat("/etc/patroni/patroni.yml"); !errors.Is(err, os.ErrNotExist) {
common.SplitSection("Cluster Status:")
clusterStatus()
// curl -s patroniApiUrl | jq -r .role
patroniRole, _ := http.Get(patroniApiUrl + "/patroni")
fmt.Println(patroniRole)

patroniRoleJson := json.NewDecoder(patroniRole.Body)
patroniRoleJson.Decode(&patroniRole)
fmt.Println(patroniRole)
patroniRole, err := http.Get("http://" + patroniApiUrl + "/patroni")
if err != nil {
common.LogError(fmt.Sprintf("Error getting patroni role: %v\n", err))
return
}

defer patroniRole.Body.Close()

//role = patroniRole["role"]
body, err := io.ReadAll(patroniRole.Body)
if err != nil {
common.LogError(fmt.Sprintf("Error reading patroni role body: %v\n", err))
return
}

var patroniRoleJson map[string]interface{}
err = json.Unmarshal(body, &patroniRoleJson)

//hour := time.Now().Format("15:04")
if err != nil {
common.LogError(fmt.Sprintf("Error decoding patroni role json: %v\n", err))
return
}

// Check if the command wal-g exists
role = patroniRoleJson["role"].(string)
}

//if (role == "master" || role == "undefined") && exec.LookPath("wal-g") != "" && hour == DbHealthConfig.Postgres.Wal_g_verify_hour {
//walgVerify()
//}
if (role == "master" || role == "undefined") && lookPath != "" && hour == DbHealthConfig.Postgres.Wal_g_verify_hour {
walgVerify()
}


if common.DpkgPackageExists("pmm2-client") {
Expand Down
45 changes: 43 additions & 2 deletions pgsqlHealth/pgsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"gopkg.in/yaml.v3"
_ "github.com/lib/pq"
"github.com/monobilisim/monokit/common"
//issues "github.com/monobilisim/monokit/common/redmine/issues"
issues "github.com/monobilisim/monokit/common/redmine/issues"
)

var Connection *sql.DB
Expand All @@ -40,6 +40,46 @@ type Member struct {
Lag *int64 `json:"lag,omitempty"`
}

func walgVerify() {
var integrityCheck string
var timelineCheck string
verifyOut, err := exec.Command("wal-g", "wal-verify", "integrity", "timeline").Output()
if err != nil {
common.LogError(fmt.Sprintf("Error executing command: %v\n", err))
return
}

for _, line := range strings.Split(string(verifyOut), "\n") {
if strings.Contains(line, "Integrity check status") {
integrityCheck = strings.Split(line, ": ")[1]
}
if strings.Contains(line, "Timeline check status") {
timelineCheck = strings.Split(line, ": ")[1]
}
}

if integrityCheck != "OK" {
common.PrettyPrintStr("WAL-G integrity check", false, "OK")
common.AlarmCheckDown("wal_g_integrity_check", "WAL-G integrity check failed, integrity check status: " + integrityCheck, false)
issues.CheckDown("wal_g_integrity_check", "WAL-G bütünlük kontrolü başarısız oldu", "Bütünlük durumu: " + integrityCheck, false, 0)
} else {
common.PrettyPrintStr("WAL-G integrity check", true, "OK")
common.AlarmCheckUp("wal_g_integrity_check", "WAL-G integrity check is now OK", false)
issues.CheckUp("wal_g_integrity_check", "WAL-G bütünlük kontrolü başarılı \n Bütünlük durumu: " + integrityCheck)
}

if timelineCheck != "OK" {
common.PrettyPrintStr("WAL-G timeline check", false, "OK")
common.AlarmCheckDown("wal_g_timeline_check", "WAL-G timeline check failed, timeline check status: " + timelineCheck, false)
issues.CheckDown("wal_g_timeline_check", "WAL-G timeline kontrolü başarısız oldu", "Timeline durumu: " + timelineCheck, false, 0)
} else {
common.PrettyPrintStr("WAL-G timeline check", true, "OK")
common.AlarmCheckUp("wal_g_timeline_check", "WAL-G timeline check is now OK", false)
issues.CheckUp("wal_g_timeline_check", "WAL-G timeline kontrolü başarılı \n Timeline durumu: " + timelineCheck)
}

}

func getPatroniUrl() (string, error) {
// Read the file
data, err := os.ReadFile("/etc/patroni/patroni.yml")
Expand Down Expand Up @@ -282,9 +322,10 @@ func clusterStatus() {

if common.SystemdUnitActive("patroni.service") {
common.PrettyPrintStr("Patroni Service", true, "accessible")
common.AlarmCheckDown("patroni_service", "Patroni service is now accessible", true)
common.AlarmCheckUp("patroni_service", "Patroni service is now accessible", false)
} else {
common.PrettyPrintStr("Patroni Service", false, "accessible")
common.AlarmCheckDown("patroni_service", "Patroni service is not accessible", false)
}

outputJSON := common.TmpDir + "/raw_output.json"
Expand Down

0 comments on commit 08b7a53

Please sign in to comment.