Commit f36d5bae authored by Nico Baruna Putra's avatar Nico Baruna Putra

new way of checking log

parent f4798650
This diff is collapsed.
......@@ -3,6 +3,10 @@ module shell/tools/pm2
go 1.16
require (
github.com/hpcloud/tail v1.0.0 // indirect
golang.org/x/sys v0.0.0-20211020174200-9d6173849985 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/fsnotify.v1 v1.4.7 // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
)
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
golang.org/x/sys v0.0.0-20211020174200-9d6173849985 h1:LOlKVhfDyahgmqa97awczplwkjzNaELFg3zRIJ13RYo=
golang.org/x/sys v0.0.0-20211020174200-9d6173849985/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
......@@ -5,22 +5,21 @@ import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
"time"
"github.com/hpcloud/tail"
"gopkg.in/gomail.v2"
)
const CONFIG_SMTP_HOST = "Internalmail.banpuindo.co.id"
const CONFIG_SMTP_HOST = "internalmail.banpuindo.co.id"
const CONFIG_SMTP_PORT = 25
const CONFIG_SENDER_NAME = "Nico Baruna <nico.baruna@gmail.com>"
const CONFIG_SENDER_NAME = "Nico Baruna <nico_putra@banpuindo.co.id>"
const CONFIG_AUTH_EMAIL = "truckscale_app@banpuindo.co.id"
const CONFIG_AUTH_PASSWORD = ""
const ShellToUse = "bash"
const MYFILE = "/var/www/webapps/caraka/.pm2/logs/CHaT-API-error.log"
const KEYWORD = "SequelizeConnectionAcquireTimeoutError:"
const MYFILE = "CHaT-API-error1.log"
const KEYWORD = "ConnectionAcquireTimeoutError"
var statSize int64
......@@ -28,7 +27,7 @@ func sendMail(errorMsg string) {
fmt.Println("Try sending email to " + CONFIG_SENDER_NAME)
m := gomail.NewMessage()
m.SetHeader("From", "nico.baruna@gmail.com")
m.SetHeader("To", "nico.baruna@gmail.com", "nico_putra@banpuindo.co.id")
m.SetHeader("To", "nico.baruna@gmail.com", "nico_putra@banpuindo.co.id", "manandit@gmail.com")
//m.SetAddressHeader("Cc", "dan@example.com", "Dan")
m.SetHeader("Subject", "Error found in Chat BE UAT!")
m.SetBody("text/html", "Hello <b>Nico</b> ! we found and error "+errorMsg)
......@@ -54,13 +53,7 @@ func banner() {
func main() {
banner()
//sendMail()
c := time.Tick(10 * time.Second)
statSize = 0
for _ = range c {
fmt.Println("Start Scanning error log")
readFile(MYFILE)
}
readFile(MYFILE)
}
func Shellout(command string) (error, string, string) {
......@@ -85,25 +78,22 @@ func Native() {
}
func readFile(fname string) {
file, err := os.Open(fname)
if err != nil {
panic(err)
}
defer file.Close()
stat, err := os.Stat(fname)
buf := make([]byte, int64(stat.Size()))
if statSize < int64(stat.Size()) {
statSize = int64(stat.Size())
start := stat.Size() - statSize
_, err = file.ReadAt(buf, start)
if err == nil && strings.Contains(string(buf), KEYWORD) {
fmt.Println("error found at " + time.Now().String())
Native()
sendMail(string(buf))
t, err := tail.TailFile(MYFILE, tail.Config{Follow: true, Poll: true})
flag := 0
if err == nil {
for line := range t.Lines {
fmt.Println("==NEW line detected==")
fmt.Println(line.Text)
if strings.Contains(line.Text, KEYWORD) {
fmt.Println("ERROR FOUND")
Native()
sendMail(line.Text)
}
flag++
fmt.Println("end")
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment