Skip to content

Commit

Permalink
feat: add initial sshNotifier
Browse files Browse the repository at this point in the history
  • Loading branch information
kreatoo committed Nov 16, 2024
1 parent beaea58 commit 788e644
Show file tree
Hide file tree
Showing 5 changed files with 342 additions and 10 deletions.
25 changes: 18 additions & 7 deletions common/alarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var AlarmSendCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
Init()
message, _ := cmd.Flags().GetString("message")
Alarm(message)
Alarm(message, "", "", false)
},
}

Expand Down Expand Up @@ -94,7 +94,7 @@ func AlarmCheckUp(service string, message string, noInterval bool) {
return
} else {
os.Remove(file_path)
Alarm(messageFinal)
Alarm(messageFinal, "", "", false)
}
}

Expand Down Expand Up @@ -166,7 +166,7 @@ func AlarmCheckDown(service string, message string, noInterval bool) {

err = os.WriteFile(filePath, jsonData, 0644)

Alarm(messageFinal)
Alarm(messageFinal, "", "", false)
}
return
}
Expand All @@ -185,7 +185,7 @@ func AlarmCheckDown(service string, message string, noInterval bool) {
LogError("Error writing to file: \n" + err.Error())
}

Alarm(messageFinal)
Alarm(messageFinal, "", "", false)
} else {
if j.Locked == false {
// currentDate - oldDate in minutes
Expand All @@ -203,7 +203,7 @@ func AlarmCheckDown(service string, message string, noInterval bool) {
LogError("Error writing to file: \n" + err.Error())
}

Alarm(messageFinal)
Alarm(messageFinal, "", "", false)
}
}
}
Expand Down Expand Up @@ -232,7 +232,7 @@ func AlarmCheckDown(service string, message string, noInterval bool) {


if Config.Alarm.Interval == 0 {
Alarm(messageFinal)
Alarm(messageFinal, "", "", false)
}
}
}
Expand All @@ -243,7 +243,7 @@ type ResponseData struct {
Code string `json:"code"`
}

func Alarm(m string) {
func Alarm(m string, customStream string, customTopic string, onlyFirstWebhook bool) {
if Config.Alarm.Enabled == false {
return
}
Expand All @@ -253,6 +253,13 @@ func Alarm(m string) {
body:= []byte(`{"text":"` + message + `"}`)

for _, webhook_url := range Config.Alarm.Webhook_urls {

if customStream != "" && customTopic != "" {
// Remove everything after &
webhook_url = strings.Split(webhook_url, "&")[0]
webhook_url = webhook_url + "&stream=" + customStream + "&topic=" + customTopic
}

r, err := http.NewRequest("POST", webhook_url, bytes.NewBuffer(body))
r.Header.Set("Content-Type", "application/json")

Expand Down Expand Up @@ -286,5 +293,9 @@ func Alarm(m string) {
}

defer res.Body.Close()

if onlyFirstWebhook == true {
break
}
}
}
13 changes: 13 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/monobilisim/monokit/osHealth"
"github.com/monobilisim/monokit/shutdownNotifier"
"github.com/monobilisim/monokit/pritunlHealth"
"github.com/monobilisim/monokit/sshNotifier"
"github.com/spf13/cobra"
"os"
)
Expand Down Expand Up @@ -49,6 +50,12 @@ func main() {
Run: pritunlHealth.Main,
}

var sshNotifierCmd = &cobra.Command{
Use: "sshNotifier",
Short: "SSH Notifier",
Run: sshNotifier.Main,
}

//// Common
RootCmd.AddCommand(redmineCmd)
RootCmd.AddCommand(common.AlarmCmd)
Expand Down Expand Up @@ -215,6 +222,12 @@ func main() {
/// Kubernetes Health
RootCmd.AddCommand(k8sHealthCmd)

/// SSH Notifier
RootCmd.AddCommand(sshNotifierCmd)

sshNotifierCmd.Flags().BoolP("login", "1", false, "Login")
sshNotifierCmd.Flags().BoolP("logout", "0", false, "Logout")

kubeconfig := os.Getenv("KUBECONFIG")

if kubeconfig == "" {
Expand Down
2 changes: 1 addition & 1 deletion mysqlHealth/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func CheckDB() {
for _, table := range tables {
message += table + "\n"
}
common.Alarm(message)
common.Alarm(message, "", "", false)
}
}

Expand Down
4 changes: 2 additions & 2 deletions shutdownNotifier/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ func Main(cmd *cobra.Command, args []string) {
poweroff, _ := cmd.Flags().GetBool("poweroff")

if poweron {
common.Alarm("[ " + common.Config.Identifier + " ] [:info: Info] Server is up...")
common.Alarm("[ " + common.Config.Identifier + " ] [:info: Info] Server is up...", "", "", false)
} else if poweroff {
common.Alarm("[ " + common.Config.Identifier + " ] [:warning: Warning] Server is shutting down...") } else {
common.Alarm("[ " + common.Config.Identifier + " ] [:warning: Warning] Server is shutting down...", "", "", false) } else {
fmt.Println("No action specified")
}

Expand Down
Loading

0 comments on commit 788e644

Please sign in to comment.