Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
eth-log-alerting
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Server Tools
eth-log-alerting
Commits
dc687c32
Commit
dc687c32
authored
7 years ago
by
Erick Hitter
Browse files
Options
Downloads
Patches
Plain Diff
Begin converting to config file, to support multiple logs
parent
8f3d63b7
No related branches found
No related tags found
1 merge request
!1
Convert to Golang
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
.gitignore
+1
-0
1 addition, 0 deletions
.gitignore
config-sample.json
+15
-0
15 additions, 0 deletions
config-sample.json
eth-log-alerting.go
+85
-52
85 additions, 52 deletions
eth-log-alerting.go
with
101 additions
and
52 deletions
.gitignore
0 → 100644
+
1
−
0
View file @
dc687c32
config.json
\ No newline at end of file
This diff is collapsed.
Click to expand it.
config-sample.json
0 → 100644
+
15
−
0
View file @
dc687c32
{
"debug-dest"
:
"os.Stdout"
,
"debug"
:
false
,
"logs"
:
[
{
"log_path"
:
""
,
"webhook_url"
:
""
,
"username"
:
""
,
"channel"
:
""
,
"color"
:
""
,
"icon_url"
:
""
,
"search"
:
""
}
]
}
This diff is collapsed.
Click to expand it.
eth-log-alerting.go
+
85
−
52
View file @
dc687c32
package
main
package
main
import
(
import
(
"encoding/json"
"flag"
"flag"
"fmt"
"fmt"
"log"
"log"
...
@@ -15,16 +16,15 @@ import (
...
@@ -15,16 +16,15 @@ import (
"github.com/hpcloud/tail"
"github.com/hpcloud/tail"
)
)
type
attachment
struct
{
type
config
struct
{
Fallback
string
`json:"fallback,omitempty"`
DebugDest
string
Pretext
string
`json:"pretext,omitempty"`
Debug
bool
Text
string
`json:"text"`
Logs
logConfigs
Color
string
`json:"color,omitempty"`
}
}
type
attachments
[]
attachment
type
logConfigs
[]
logConfig
var
(
type
logConfig
struct
{
logPath
string
logPath
string
webhookURL
string
webhookURL
string
username
string
username
string
...
@@ -32,8 +32,27 @@ var (
...
@@ -32,8 +32,27 @@ var (
color
string
color
string
iconURL
string
iconURL
string
searchRegex
string
searchRegex
string
}
type
attachment
struct
{
Fallback
string
`json:"fallback,omitempty"`
Pretext
string
`json:"pretext,omitempty"`
Text
string
`json:"text"`
Color
string
`json:"color,omitempty"`
}
mh
*
matterhook
.
Client
type
attachments
[]
attachment
var
(
logPath
string
// Deprecate
webhookURL
string
// Deprecate
username
string
// Deprecate
channel
string
// Deprecate
color
string
// Deprecate
iconURL
string
// Deprecate
searchRegex
string
// Deprecate
mh
*
matterhook
.
Client
// Deprecate
logger
*
log
.
Logger
logger
*
log
.
Logger
debugDest
string
debugDest
string
...
@@ -41,17 +60,31 @@ var (
...
@@ -41,17 +60,31 @@ var (
)
)
func
init
()
{
func
init
()
{
flag
.
StringVar
(
&
logPath
,
"log-path"
,
""
,
"Log to monitor"
)
var
configPath
string
flag
.
StringVar
(
&
webhookURL
,
"webhook"
,
""
,
"Webhook to forward log entries to"
)
// flag.StringVar(&logPath, "log-path", "", "Log to monitor")
flag
.
StringVar
(
&
username
,
"username"
,
"logbot"
,
"Username to post as"
)
// flag.StringVar(&webhookURL, "webhook", "", "Webhook to forward log entries to")
flag
.
StringVar
(
&
channel
,
"channel"
,
""
,
"Channel to post log entries to"
)
// flag.StringVar(&username, "username", "logbot", "Username to post as")
flag
.
StringVar
(
&
color
,
"color"
,
"default"
,
"Color for entry, either named or hex with `#`"
)
// flag.StringVar(&channel, "channel", "", "Channel to post log entries to")
flag
.
StringVar
(
&
iconURL
,
"icon-url"
,
""
,
"URL of icon to use for bot"
)
// flag.StringVar(&color, "color", "default", "Color for entry, either named or hex with `#`")
flag
.
StringVar
(
&
searchRegex
,
"search"
,
""
,
"Search term or regex to match"
)
// flag.StringVar(&iconURL, "icon-url", "", "URL of icon to use for bot")
flag
.
StringVar
(
&
debugDest
,
"debug-dest"
,
"os.Stdout"
,
"Destination for debug and other messages, omit to log to Stdout"
)
// flag.StringVar(&searchRegex, "search", "", "Search term or regex to match")
flag
.
BoolVar
(
&
debug
,
"debug"
,
false
,
"Include additional log data for debugging"
)
// flag.StringVar(&debugDest, "debug-dest", "os.Stdout", "Destination for debug and other messages, omit to log to Stdout")
// flag.BoolVar(&debug, "debug", false, "Include additional log data for debugging")
flag
.
StringVar
(
&
configPath
,
"config"
,
"./config.json"
,
"Path to configuration file"
)
flag
.
Parse
()
flag
.
Parse
()
validatePath
(
&
configPath
)
configFile
,
err
:=
os
.
Open
(
configPath
)
jsonDecoder
:=
json
.
NewDecoder
(
configFile
)
config
:=
config
{}
err
=
jsonDecoder
.
Decode
(
&
config
)
if
err
!=
nil
{
usage
()
}
fmt
.
Println
(
fmt
.
Sprintf
(
"%+v
\n
"
,
config
))
os
.
Exit
(
3
)
setUpLogger
()
setUpLogger
()
validatePath
(
&
logPath
)
validatePath
(
&
logPath
)
...
@@ -60,35 +93,35 @@ func init() {
...
@@ -60,35 +93,35 @@ func init() {
usage
()
usage
()
}
}
mh
=
matterhook
.
New
(
webhookURL
,
matterhook
.
Config
{
DisableServer
:
true
})
//
mh = matterhook.New(webhookURL, matterhook.Config{DisableServer: true})
}
}
func
main
()
{
func
main
()
{
logger
.
Printf
(
"Monitoring %s"
,
logPath
)
//
logger.Printf("Monitoring %s", logPath)
logger
.
Printf
(
"Forwarding entries to channel
\"
%s
\"
as user
\"
%s
\"
at %s"
,
channel
,
username
,
webhookURL
)
//
logger.Printf("Forwarding entries to channel \"%s\" as user \"%s\" at %s", channel, username, webhookURL)
sig
:=
make
(
chan
os
.
Signal
,
1
)
sig
:=
make
(
chan
os
.
Signal
,
1
)
signal
.
Notify
(
sig
,
syscall
.
SIGINT
,
syscall
.
SIGTERM
)
signal
.
Notify
(
sig
,
syscall
.
SIGINT
,
syscall
.
SIGTERM
)
t
,
err
:=
tail
.
TailFile
(
logPath
,
tail
.
Config
{
//
t, err := tail.TailFile(logPath, tail.Config{
Follow
:
true
,
//
Follow: true,
Location
:
&
tail
.
SeekInfo
{
Offset
:
0
,
Whence
:
2
},
//
Location: &tail.SeekInfo{Offset: 0, Whence: 2},
MustExist
:
true
,
//
MustExist: true,
ReOpen
:
true
,
//
ReOpen: true,
Logger
:
logger
,
//
Logger: logger,
})
//
})
if
err
!=
nil
{
//
if err != nil {
logger
.
Println
(
err
)
//
logger.Println(err)
t
.
Cleanup
()
//
t.Cleanup()
close
(
sig
)
//
close(sig)
os
.
Exit
(
3
)
//
os.Exit(3)
}
//
}
go
parseLinesAndSend
(
t
)
//
go parseLinesAndSend(t)
caughtSig
:=
<-
sig
caughtSig
:=
<-
sig
t
.
Stop
()
//
t.Stop()
t
.
Cleanup
()
//
t.Cleanup()
logger
.
Printf
(
"Stopping, got signal %s"
,
caughtSig
)
logger
.
Printf
(
"Stopping, got signal %s"
,
caughtSig
)
}
}
...
@@ -108,21 +141,21 @@ func parseLinesAndSend(t *tail.Tail) {
...
@@ -108,21 +141,21 @@ func parseLinesAndSend(t *tail.Tail) {
}
}
func
sendLine
(
line
*
tail
.
Line
)
{
func
sendLine
(
line
*
tail
.
Line
)
{
atts
:=
attachments
{
//
atts := attachments{
attachment
{
//
attachment{
Fallback
:
fmt
.
Sprintf
(
"New entry in %s"
,
logPath
),
//
Fallback: fmt.Sprintf("New entry in %s", logPath),
Pretext
:
fmt
.
Sprintf
(
"In `%s` at `%s`:"
,
logPath
,
line
.
Time
),
//
Pretext: fmt.Sprintf("In `%s` at `%s`:", logPath, line.Time),
Text
:
fmt
.
Sprintf
(
" %s"
,
line
.
Text
),
//
Text: fmt.Sprintf(" %s", line.Text),
Color
:
color
,
//
Color: color,
},
//
},
}
//
}
mh
.
Send
(
matterhook
.
OMessage
{
//
mh.Send(matterhook.OMessage{
Channel
:
channel
,
//
Channel: channel,
UserName
:
username
,
//
UserName: username,
Attachments
:
atts
,
//
Attachments: atts,
IconURL
:
iconURL
,
//
IconURL: iconURL,
})
//
})
}
}
func
setUpLogger
()
{
func
setUpLogger
()
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment