Skip to content

Commit

Permalink
feat: zimbraHealth: add TailWebhook
Browse files Browse the repository at this point in the history
  • Loading branch information
kreatoo committed Jan 18, 2025
1 parent 07715c7 commit c41c07a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
4 changes: 4 additions & 0 deletions common/mail/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ type Zimbra struct {
Restart bool
Queue_Limit int
Restart_Limit int
Webhook_tail struct {
Logfile string
Filter string
}
}

type Pmg struct {
Expand Down
9 changes: 9 additions & 0 deletions common/str.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package common

import (
"strings"
)

func IsEmptyOrWhitespaceStr(stringValue string) bool {
return len(strings.TrimSpace(stringValue)) == 0
}
3 changes: 3 additions & 0 deletions config/mail.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ zimbra:
restart: false
queue_limit: 50
restart_limit: 2
webhook_tail:
logfile: /opt/zimbra/log/cbpolicyd.log
filter: 'test'
49 changes: 49 additions & 0 deletions zimbraHealth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func Main(cmd *cobra.Command, args []string) {
common.SplitSection("Queued Messages:")
CheckQueuedMessages()

if !common.IsEmptyOrWhitespaceStr(MailHealthConfig.Zimbra.Webhook_tail.Logfile) && !common.IsEmptyOrWhitespaceStr(MailHealthConfig.Zimbra.Webhook_tail.Filter) {
TailWebhook(MailHealthConfig.Zimbra.Webhook_tail.Logfile, MailHealthConfig.Zimbra.Webhook_tail.Filter)
}

date := time.Now().Format("13:04")
if date == "01:00" {
common.SplitSection("SSL Expiration:")
Expand All @@ -69,6 +73,51 @@ func Main(cmd *cobra.Command, args []string) {
}
}


func escapeJSON(input string) string {
output := bytes.Buffer{}
for _, r := range input {
switch r {
case '"':
output.WriteString(`\"`)
case '\\':
output.WriteString(`\\`)
default:
output.WriteRune(r)
}
}
return output.String()
}

func TailWebhook(filePath string, pattern string) {
// Compile the regex pattern
regex, err := regexp.Compile(pattern)
if err != nil {
common.LogError("Invalid regex pattern: " + err.Error())
}

// Open the file
file, err := os.Open(filePath)
if err != nil {
common.LogError("Error opening file: " + err.Error())
}
defer file.Close()

// Read the file line by line
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if regex.MatchString(line) {
common.AlarmCheckDown("webhook_tail_" + escapeJSON(strings.ReplaceAll(line, " ", "_")), "Webhook tail matched: " + escapeJSON(line), false)
}
}

if err := scanner.Err(); err != nil {
common.LogError("Error reading file: " + err.Error())
}
}


func Zmfixperms() {
// Run zmfixperms
_, _ = ExecZimbraCommand("libexec/zmfixperms", true, true)
Expand Down

0 comments on commit c41c07a

Please sign in to comment.